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