]> git.wh0rd.org - tt-rss.git/blob - classes/feedparser.php
Merge pull request #211 from PerryWerneck/master
[tt-rss.git] / classes / feedparser.php
1 <?php
2 class FeedParser {
3 private $doc;
4 private $error;
5 private $items;
6 private $link;
7 private $title;
8 private $type;
9 private $xpath;
10
11 const FEED_RDF = 0;
12 const FEED_RSS = 1;
13 const FEED_ATOM = 2;
14
15 function __construct($data) {
16 libxml_use_internal_errors(true);
17 libxml_clear_errors();
18 $this->doc = new DOMDocument();
19 $this->doc->loadXML($data);
20
21 $error = libxml_get_last_error();
22
23 if ($error && $error->code == 9) {
24 libxml_clear_errors();
25
26 // we might want to try guessing input encoding here too
27 $data = iconv("UTF-8", "UTF-8//IGNORE", $data);
28
29 $this->doc = new DOMDocument();
30 $this->doc->loadXML($data);
31
32 $error = libxml_get_last_error();
33 }
34
35 $this->error = $this->format_error($error);
36 libxml_clear_errors();
37
38 $this->items = array();
39 }
40
41 function init() {
42 $root = $this->doc->firstChild;
43 $xpath = new DOMXPath($this->doc);
44 $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
45 $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
46 $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
47 $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
48 $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
49 $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
50 $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
51
52 $this->xpath = $xpath;
53
54 $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)")->item(0);
55
56 if ($root) {
57 switch (mb_strtolower($root->tagName)) {
58 case "rdf:rdf":
59 $this->type = $this::FEED_RDF;
60 break;
61 case "channel":
62 $this->type = $this::FEED_RSS;
63 break;
64 case "feed":
65 $this->type = $this::FEED_ATOM;
66 break;
67 default:
68 if( !isset($this->error) ){
69 $this->error = "Unknown/unsupported feed type";
70 }
71 return;
72 }
73
74 switch ($this->type) {
75 case $this::FEED_ATOM:
76
77 $title = $xpath->query("//atom:feed/atom:title")->item(0);
78
79 if (!$title)
80 $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
81
82
83 if ($title) {
84 $this->title = $title->nodeValue;
85 }
86
87 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
88
89 if (!$link)
90 $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
91
92
93 if ($link && $link->hasAttributes()) {
94 $this->link = $link->getAttribute("href");
95 }
96
97 $articles = $xpath->query("//atom:entry");
98
99 if (!$articles || $articles->length == 0)
100 $articles = $xpath->query("//atom03:entry");
101
102 foreach ($articles as $article) {
103 array_push($this->items, new FeedItem_Atom($article, $this->doc, $this->xpath));
104 }
105
106 break;
107 case $this::FEED_RSS:
108 $title = $xpath->query("//channel/title")->item(0);
109
110 if ($title) {
111 $this->title = $title->nodeValue;
112 }
113
114 $link = $xpath->query("//channel/link")->item(0);
115
116 if ($link) {
117 if ($link->getAttribute("href"))
118 $this->link = $link->getAttribute("href");
119 else if ($link->nodeValue)
120 $this->link = $link->nodeValue;
121 }
122
123 $articles = $xpath->query("//channel/item");
124
125 foreach ($articles as $article) {
126 array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
127 }
128
129 break;
130 case $this::FEED_RDF:
131 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
132
133 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
134
135 if ($title) {
136 $this->title = $title->nodeValue;
137 }
138
139 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
140
141 if ($link) {
142 $this->link = $link->nodeValue;
143 }
144
145 $articles = $xpath->query("//rssfake:item");
146
147 foreach ($articles as $article) {
148 array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
149 }
150
151 break;
152
153 }
154 } else {
155 if( !isset($this->error) ){
156 $this->error = "Unknown/unsupported feed type";
157 }
158 return;
159 }
160 }
161
162 function format_error($error) {
163 if ($error) {
164 return sprintf("LibXML error %s at line %d (column %d): %s",
165 $error->code, $error->line, $error->column,
166 $error->message);
167 } else {
168 return "";
169 }
170 }
171
172 function error() {
173 return $this->error;
174 }
175
176 function get_link() {
177 return $this->link;
178 }
179
180 function get_title() {
181 return $this->title;
182 }
183
184 function get_items() {
185 return $this->items;
186 }
187
188 function get_links($rel) {
189 $rv = array();
190
191 switch ($this->type) {
192 case $this::FEED_ATOM:
193 $links = $this->xpath->query("//atom:feed/atom:link");
194
195 foreach ($links as $link) {
196 if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
197 array_push($rv, $link->getAttribute('href'));
198 }
199 }
200 break;
201 case $this::FEED_RSS:
202 $links = $this->xpath->query("//atom:link");
203
204 foreach ($links as $link) {
205 if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
206 array_push($rv, $link->getAttribute('href'));
207 }
208 }
209 break;
210 }
211
212 return $rv;
213 }
214 } ?>