]> git.wh0rd.org - tt-rss.git/blob - classes/feedparser.php
report all libxml errors in updater debug output
[tt-rss.git] / classes / feedparser.php
1 <?php
2 class FeedParser {
3 private $doc;
4 private $error;
5 private $libxml_errors = array();
6 private $items;
7 private $link;
8 private $title;
9 private $type;
10 private $xpath;
11
12 const FEED_RDF = 0;
13 const FEED_RSS = 1;
14 const FEED_ATOM = 2;
15
16 function normalize_encoding($data) {
17 if (preg_match('/^(<\?xml[\t\n\r ].*?encoding[\t\n\r ]*=[\t\n\r ]*["\'])(.+?)(["\'].*?\?>)/s', $data, $matches) === 1) {
18 $data = mb_convert_encoding($data, 'UTF-8', $matches[2]);
19
20 $data = preg_replace('/^<\?xml[\t\n\r ].*?\?>/s', $matches[1] . "UTF-8" . $matches[3] , $data);
21 }
22
23 return $data;
24 }
25
26 function __construct($data) {
27 libxml_use_internal_errors(true);
28 libxml_clear_errors();
29 $this->doc = new DOMDocument();
30 $this->doc->loadXML($data);
31
32 mb_substitute_character("none");
33
34 $error = libxml_get_last_error();
35
36 // libxml compiled without iconv?
37 if ($error && $error->code == 32) {
38 $data = $this->normalize_encoding($data);
39
40 if ($data) {
41 libxml_clear_errors();
42
43 $this->doc = new DOMDocument();
44 $this->doc->loadXML($data);
45
46 $error = libxml_get_last_error();
47 }
48 }
49
50 // some terrible invalid unicode entity?
51 if ($error) {
52 foreach (libxml_get_errors() as $err) {
53 if ($err->code == 9) {
54 // if the source feed is not in utf8, next conversion will fail
55 $data = $this->normalize_encoding($data);
56
57 // remove dangling bytes
58 $data = mb_convert_encoding($data, 'UTF-8', 'UTF-8');
59
60 // apparently not all UTF-8 characters are valid for XML
61 $data = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $data);
62
63 if ($data) {
64 libxml_clear_errors();
65
66 $this->doc = new DOMDocument();
67 $this->doc->loadXML($data);
68
69 $error = libxml_get_last_error();
70 }
71 break;
72 }
73 }
74 }
75
76 if ($error) {
77 foreach (libxml_get_errors() as $error) {
78 if ($error->level == LIBXML_ERR_FATAL) {
79 if(!isset($this->error)) //currently only the first error is reported
80 $this->error = $this->format_error($error);
81 $this->libxml_errors [] = $this->format_error($error);
82 }
83 }
84 }
85 libxml_clear_errors();
86
87 $this->items = array();
88 }
89
90 function init() {
91 $root = $this->doc->firstChild;
92 $xpath = new DOMXPath($this->doc);
93 $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
94 $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
95 $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
96 $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
97 $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
98 $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
99 $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
100
101 $this->xpath = $xpath;
102
103 $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)");
104
105 if ($root && $root->length > 0) {
106 $root = $root->item(0);
107
108 if ($root) {
109 switch (mb_strtolower($root->tagName)) {
110 case "rdf:rdf":
111 $this->type = $this::FEED_RDF;
112 break;
113 case "channel":
114 $this->type = $this::FEED_RSS;
115 break;
116 case "feed":
117 $this->type = $this::FEED_ATOM;
118 break;
119 default:
120 if( !isset($this->error) ){
121 $this->error = "Unknown/unsupported feed type";
122 }
123 return;
124 }
125 }
126
127 switch ($this->type) {
128 case $this::FEED_ATOM:
129
130 $title = $xpath->query("//atom:feed/atom:title")->item(0);
131
132 if (!$title)
133 $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
134
135
136 if ($title) {
137 $this->title = $title->nodeValue;
138 }
139
140 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
141
142 if (!$link)
143 $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
144
145
146 if ($link && $link->hasAttributes()) {
147 $this->link = $link->getAttribute("href");
148 }
149
150 $articles = $xpath->query("//atom:entry");
151
152 if (!$articles || $articles->length == 0)
153 $articles = $xpath->query("//atom03:entry");
154
155 foreach ($articles as $article) {
156 array_push($this->items, new FeedItem_Atom($article, $this->doc, $this->xpath));
157 }
158
159 break;
160 case $this::FEED_RSS:
161 $title = $xpath->query("//channel/title")->item(0);
162
163 if ($title) {
164 $this->title = $title->nodeValue;
165 }
166
167 $link = $xpath->query("//channel/link")->item(0);
168
169 if ($link) {
170 if ($link->getAttribute("href"))
171 $this->link = $link->getAttribute("href");
172 else if ($link->nodeValue)
173 $this->link = $link->nodeValue;
174 }
175
176 $articles = $xpath->query("//channel/item");
177
178 foreach ($articles as $article) {
179 array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
180 }
181
182 break;
183 case $this::FEED_RDF:
184 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
185
186 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
187
188 if ($title) {
189 $this->title = $title->nodeValue;
190 }
191
192 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
193
194 if ($link) {
195 $this->link = $link->nodeValue;
196 }
197
198 $articles = $xpath->query("//rssfake:item");
199
200 foreach ($articles as $article) {
201 array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
202 }
203
204 break;
205
206 }
207 } else {
208 if( !isset($this->error) ){
209 $this->error = "Unknown/unsupported feed type";
210 }
211 return;
212 }
213 }
214
215 function format_error($error) {
216 if ($error) {
217 return sprintf("LibXML error %s at line %d (column %d): %s",
218 $error->code, $error->line, $error->column,
219 $error->message);
220 } else {
221 return "";
222 }
223 }
224
225 function error() {
226 return $this->error;
227 }
228
229 function errors() {
230 return $this->libxml_errors;
231 }
232
233 function get_link() {
234 return $this->link;
235 }
236
237 function get_title() {
238 return $this->title;
239 }
240
241 function get_items() {
242 return $this->items;
243 }
244
245 function get_links($rel) {
246 $rv = array();
247
248 switch ($this->type) {
249 case $this::FEED_ATOM:
250 $links = $this->xpath->query("//atom:feed/atom:link");
251
252 foreach ($links as $link) {
253 if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
254 array_push($rv, $link->getAttribute('href'));
255 }
256 }
257 break;
258 case $this::FEED_RSS:
259 $links = $this->xpath->query("//atom:link");
260
261 foreach ($links as $link) {
262 if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
263 array_push($rv, $link->getAttribute('href'));
264 }
265 }
266 break;
267 }
268
269 return $rv;
270 }
271 } ?>