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