]> git.wh0rd.org - tt-rss.git/blame - classes/feedparser.php
schema: remove obsolete releases.rss feed
[tt-rss.git] / classes / feedparser.php
CommitLineData
cd07592c
AD
1<?php
2class FeedParser {
3 private $doc;
4 private $error;
54f23d38 5 private $libxml_errors = array();
cd07592c
AD
6 private $items;
7 private $link;
8 private $title;
9 private $type;
b9eee80e 10 private $xpath;
cd07592c
AD
11
12 const FEED_RDF = 0;
13 const FEED_RSS = 1;
14 const FEED_ATOM = 2;
15
4ad04ee2
AD
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
cd07592c
AD
26 function __construct($data) {
27 libxml_use_internal_errors(true);
28 libxml_clear_errors();
29 $this->doc = new DOMDocument();
30 $this->doc->loadXML($data);
d1f3fa97 31
f612dbe8
AD
32 mb_substitute_character("none");
33
d1f3fa97
AD
34 $error = libxml_get_last_error();
35
4f00f55c 36 // libxml compiled without iconv?
5fa36a64 37 if ($error && $error->code == 32) {
4ad04ee2 38 $data = $this->normalize_encoding($data);
4f00f55c 39
4ad04ee2
AD
40 if ($data) {
41 libxml_clear_errors();
f612dbe8 42
4ad04ee2
AD
43 $this->doc = new DOMDocument();
44 $this->doc->loadXML($data);
f612dbe8 45
4ad04ee2 46 $error = libxml_get_last_error();
5fa36a64 47 }
4f00f55c
AD
48 }
49
50 // some terrible invalid unicode entity?
5fa36a64 51 if ($error) {
4d49863f 52 foreach (libxml_get_errors() as $err) {
5fa36a64 53 if ($err->code == 9) {
4ad04ee2
AD
54 // if the source feed is not in utf8, next conversion will fail
55 $data = $this->normalize_encoding($data);
56
5fa36a64 57 // remove dangling bytes
58 $data = mb_convert_encoding($data, 'UTF-8', 'UTF-8');
4d49863f 59
5fa36a64 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 }
f612dbe8 73 }
d1f3fa97 74 }
4d49863f 75
4d49863f
AD
76 if ($error) {
77 foreach (libxml_get_errors() as $error) {
78 if ($error->level == LIBXML_ERR_FATAL) {
54f23d38 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);
d3305ff8 82 }
4d49863f 83 }
d3305ff8 84 }
cd07592c
AD
85 libxml_clear_errors();
86
87 $this->items = array();
88 }
89
90 function init() {
91 $root = $this->doc->firstChild;
04d2f9c8
AD
92 $xpath = new DOMXPath($this->doc);
93 $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
a3b9fd12 94 $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
4c00e15b 95 $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
852d4ac8 96 $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
d4992d6b
AD
97 $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
98 $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
8a95d630 99 $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
d71ac5d3 100 $xpath->registerNamespace('thread', 'http://purl.org/syndication/thread/1.0');
d4992d6b 101
b9eee80e 102 $this->xpath = $xpath;
04d2f9c8 103
a9000b03 104 $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)");
cd07592c 105
2c6f3c2e 106 if ($root && $root->length > 0) {
a9000b03
AD
107 $root = $root->item(0);
108
109 if ($root) {
110 switch (mb_strtolower($root->tagName)) {
111 case "rdf:rdf":
112 $this->type = $this::FEED_RDF;
113 break;
114 case "channel":
115 $this->type = $this::FEED_RSS;
116 break;
117 case "feed":
4834ad56 118 case "atom:feed":
a9000b03
AD
119 $this->type = $this::FEED_ATOM;
120 break;
121 default:
122 if( !isset($this->error) ){
123 $this->error = "Unknown/unsupported feed type";
124 }
125 return;
349c4229 126 }
cd07592c
AD
127 }
128
cd07592c
AD
129 switch ($this->type) {
130 case $this::FEED_ATOM:
cd07592c
AD
131
132 $title = $xpath->query("//atom:feed/atom:title")->item(0);
133
a3b9fd12
AD
134 if (!$title)
135 $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
136
137
cd07592c
AD
138 if ($title) {
139 $this->title = $title->nodeValue;
140 }
141
142 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
143
a3b9fd12
AD
144 if (!$link)
145 $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
146
147
cd07592c
AD
148 if ($link && $link->hasAttributes()) {
149 $this->link = $link->getAttribute("href");
150 }
151
152 $articles = $xpath->query("//atom:entry");
153
a3b9fd12
AD
154 if (!$articles || $articles->length == 0)
155 $articles = $xpath->query("//atom03:entry");
156
cd07592c 157 foreach ($articles as $article) {
4c00e15b 158 array_push($this->items, new FeedItem_Atom($article, $this->doc, $this->xpath));
cd07592c
AD
159 }
160
cd07592c
AD
161 break;
162 case $this::FEED_RSS:
04d2f9c8
AD
163 $title = $xpath->query("//channel/title")->item(0);
164
165 if ($title) {
166 $this->title = $title->nodeValue;
167 }
168
169 $link = $xpath->query("//channel/link")->item(0);
170
1874c8d6
AD
171 if ($link) {
172 if ($link->getAttribute("href"))
173 $this->link = $link->getAttribute("href");
174 else if ($link->nodeValue)
175 $this->link = $link->nodeValue;
04d2f9c8
AD
176 }
177
178 $articles = $xpath->query("//channel/item");
179
180 foreach ($articles as $article) {
4c00e15b 181 array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
04d2f9c8
AD
182 }
183
cd07592c 184 break;
852d4ac8
AD
185 case $this::FEED_RDF:
186 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
187
188 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
189
190 if ($title) {
191 $this->title = $title->nodeValue;
192 }
193
194 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
195
196 if ($link) {
197 $this->link = $link->nodeValue;
198 }
199
200 $articles = $xpath->query("//rssfake:item");
201
202 foreach ($articles as $article) {
203 array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
204 }
205
206 break;
207
cd07592c 208 }
31bd6f76
AD
209
210 if ($this->title) $this->title = trim($this->title);
211 if ($this->link) $this->link = trim($this->link);
212
852d4ac8 213 } else {
349c4229 214 if( !isset($this->error) ){
215 $this->error = "Unknown/unsupported feed type";
216 }
852d4ac8 217 return;
cd07592c
AD
218 }
219 }
220
221 function format_error($error) {
222 if ($error) {
223 return sprintf("LibXML error %s at line %d (column %d): %s",
224 $error->code, $error->line, $error->column,
225 $error->message);
226 } else {
227 return "";
228 }
229 }
230
231 function error() {
232 return $this->error;
233 }
234
54f23d38 235 function errors() {
236 return $this->libxml_errors;
237 }
238
cd07592c
AD
239 function get_link() {
240 return $this->link;
241 }
242
243 function get_title() {
244 return $this->title;
245 }
246
247 function get_items() {
248 return $this->items;
249 }
250
b9eee80e
AD
251 function get_links($rel) {
252 $rv = array();
253
254 switch ($this->type) {
255 case $this::FEED_ATOM:
256 $links = $this->xpath->query("//atom:feed/atom:link");
257
258 foreach ($links as $link) {
259 if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
31bd6f76 260 array_push($rv, trim($link->getAttribute('href')));
b9eee80e
AD
261 }
262 }
263 break;
264 case $this::FEED_RSS:
f17c3ee2
AD
265 $links = $this->xpath->query("//atom:link");
266
3c8060ac
AD
267 foreach ($links as $link) {
268 if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
31bd6f76 269 array_push($rv, trim($link->getAttribute('href')));
3c8060ac 270 }
b9eee80e
AD
271 }
272 break;
273 }
274
275 return $rv;
276 }
cd07592c 277} ?>