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