]>
Commit | Line | Data |
---|---|---|
cd07592c AD |
1 | <?php |
2 | class 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 AD |
20 | |
21 | $error = libxml_get_last_error(); | |
22 | ||
4f00f55c AD |
23 | // libxml compiled without iconv? |
24 | if ($error && $error->code == 32) { | |
25 | if (preg_match('/^(<\\?xml .*?)encoding="(.+?)"(.*?\\?>)/', $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 .*?\\?>/', $matches[1] . $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? | |
d1f3fa97 AD |
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); | |
cd07592c AD |
54 | libxml_clear_errors(); |
55 | ||
56 | $this->items = array(); | |
57 | } | |
58 | ||
59 | function init() { | |
60 | $root = $this->doc->firstChild; | |
04d2f9c8 AD |
61 | $xpath = new DOMXPath($this->doc); |
62 | $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom'); | |
a3b9fd12 | 63 | $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#'); |
4c00e15b | 64 | $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/'); |
852d4ac8 | 65 | $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'); |
d4992d6b AD |
66 | $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/'); |
67 | $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/'); | |
8a95d630 | 68 | $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/'); |
d4992d6b | 69 | |
b9eee80e | 70 | $this->xpath = $xpath; |
04d2f9c8 | 71 | |
a9000b03 | 72 | $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)"); |
cd07592c AD |
73 | |
74 | if ($root) { | |
a9000b03 AD |
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; | |
349c4229 | 93 | } |
cd07592c AD |
94 | } |
95 | ||
cd07592c AD |
96 | switch ($this->type) { |
97 | case $this::FEED_ATOM: | |
cd07592c AD |
98 | |
99 | $title = $xpath->query("//atom:feed/atom:title")->item(0); | |
100 | ||
a3b9fd12 AD |
101 | if (!$title) |
102 | $title = $xpath->query("//atom03:feed/atom03:title")->item(0); | |
103 | ||
104 | ||
cd07592c AD |
105 | if ($title) { |
106 | $this->title = $title->nodeValue; | |
107 | } | |
108 | ||
109 | $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0); | |
110 | ||
a3b9fd12 AD |
111 | if (!$link) |
112 | $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0); | |
113 | ||
114 | ||
cd07592c AD |
115 | if ($link && $link->hasAttributes()) { |
116 | $this->link = $link->getAttribute("href"); | |
117 | } | |
118 | ||
119 | $articles = $xpath->query("//atom:entry"); | |
120 | ||
a3b9fd12 AD |
121 | if (!$articles || $articles->length == 0) |
122 | $articles = $xpath->query("//atom03:entry"); | |
123 | ||
cd07592c | 124 | foreach ($articles as $article) { |
4c00e15b | 125 | array_push($this->items, new FeedItem_Atom($article, $this->doc, $this->xpath)); |
cd07592c AD |
126 | } |
127 | ||
cd07592c AD |
128 | break; |
129 | case $this::FEED_RSS: | |
04d2f9c8 AD |
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 | ||
1874c8d6 AD |
138 | if ($link) { |
139 | if ($link->getAttribute("href")) | |
140 | $this->link = $link->getAttribute("href"); | |
141 | else if ($link->nodeValue) | |
142 | $this->link = $link->nodeValue; | |
04d2f9c8 AD |
143 | } |
144 | ||
145 | $articles = $xpath->query("//channel/item"); | |
146 | ||
147 | foreach ($articles as $article) { | |
4c00e15b | 148 | array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath)); |
04d2f9c8 AD |
149 | } |
150 | ||
cd07592c | 151 | break; |
852d4ac8 AD |
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 | ||
cd07592c | 175 | } |
852d4ac8 | 176 | } else { |
349c4229 | 177 | if( !isset($this->error) ){ |
178 | $this->error = "Unknown/unsupported feed type"; | |
179 | } | |
852d4ac8 | 180 | return; |
cd07592c AD |
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 | ||
b9eee80e AD |
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: | |
f17c3ee2 AD |
224 | $links = $this->xpath->query("//atom:link"); |
225 | ||
3c8060ac AD |
226 | foreach ($links as $link) { |
227 | if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) { | |
228 | array_push($rv, $link->getAttribute('href')); | |
229 | } | |
b9eee80e AD |
230 | } |
231 | break; | |
232 | } | |
233 | ||
234 | return $rv; | |
235 | } | |
cd07592c | 236 | } ?> |