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