]>
Commit | Line | Data |
---|---|---|
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 | mb_substitute_character("none"); | |
22 | ||
23 | $error = libxml_get_last_error(); | |
24 | ||
25 | // libxml compiled without iconv? | |
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]); | |
29 | ||
30 | $data = preg_replace('/^<\?xml[\t\n\r ].*?\?>/s', $matches[1] . "UTF-8" . $matches[3] , $data); | |
31 | ||
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 | } | |
40 | } | |
41 | } | |
42 | ||
43 | // some terrible invalid unicode entity? | |
44 | if ($error) { | |
45 | foreach(libxml_get_errors() as $err) { | |
46 | if ($err->code == 9) { | |
47 | // remove dangling bytes | |
48 | $data = mb_convert_encoding($data, 'UTF-8', 'UTF-8'); | |
49 | ||
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 | } | |
63 | } | |
64 | } | |
65 | ||
66 | $this->error = $this->format_error($error); | |
67 | libxml_clear_errors(); | |
68 | ||
69 | $this->items = array(); | |
70 | } | |
71 | ||
72 | function init() { | |
73 | $root = $this->doc->firstChild; | |
74 | $xpath = new DOMXPath($this->doc); | |
75 | $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom'); | |
76 | $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#'); | |
77 | $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/'); | |
78 | $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'); | |
79 | $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/'); | |
80 | $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/'); | |
81 | $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/'); | |
82 | ||
83 | $this->xpath = $xpath; | |
84 | ||
85 | $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)"); | |
86 | ||
87 | if ($root) { | |
88 | $root = $root->item(0); | |
89 | ||
90 | if ($root) { | |
91 | switch (mb_strtolower($root->tagName)) { | |
92 | case "rdf:rdf": | |
93 | $this->type = $this::FEED_RDF; | |
94 | break; | |
95 | case "channel": | |
96 | $this->type = $this::FEED_RSS; | |
97 | break; | |
98 | case "feed": | |
99 | $this->type = $this::FEED_ATOM; | |
100 | break; | |
101 | default: | |
102 | if( !isset($this->error) ){ | |
103 | $this->error = "Unknown/unsupported feed type"; | |
104 | } | |
105 | return; | |
106 | } | |
107 | } | |
108 | ||
109 | switch ($this->type) { | |
110 | case $this::FEED_ATOM: | |
111 | ||
112 | $title = $xpath->query("//atom:feed/atom:title")->item(0); | |
113 | ||
114 | if (!$title) | |
115 | $title = $xpath->query("//atom03:feed/atom03:title")->item(0); | |
116 | ||
117 | ||
118 | if ($title) { | |
119 | $this->title = $title->nodeValue; | |
120 | } | |
121 | ||
122 | $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0); | |
123 | ||
124 | if (!$link) | |
125 | $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0); | |
126 | ||
127 | ||
128 | if ($link && $link->hasAttributes()) { | |
129 | $this->link = $link->getAttribute("href"); | |
130 | } | |
131 | ||
132 | $articles = $xpath->query("//atom:entry"); | |
133 | ||
134 | if (!$articles || $articles->length == 0) | |
135 | $articles = $xpath->query("//atom03:entry"); | |
136 | ||
137 | foreach ($articles as $article) { | |
138 | array_push($this->items, new FeedItem_Atom($article, $this->doc, $this->xpath)); | |
139 | } | |
140 | ||
141 | break; | |
142 | case $this::FEED_RSS: | |
143 | $title = $xpath->query("//channel/title")->item(0); | |
144 | ||
145 | if ($title) { | |
146 | $this->title = $title->nodeValue; | |
147 | } | |
148 | ||
149 | $link = $xpath->query("//channel/link")->item(0); | |
150 | ||
151 | if ($link) { | |
152 | if ($link->getAttribute("href")) | |
153 | $this->link = $link->getAttribute("href"); | |
154 | else if ($link->nodeValue) | |
155 | $this->link = $link->nodeValue; | |
156 | } | |
157 | ||
158 | $articles = $xpath->query("//channel/item"); | |
159 | ||
160 | foreach ($articles as $article) { | |
161 | array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath)); | |
162 | } | |
163 | ||
164 | break; | |
165 | case $this::FEED_RDF: | |
166 | $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/'); | |
167 | ||
168 | $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0); | |
169 | ||
170 | if ($title) { | |
171 | $this->title = $title->nodeValue; | |
172 | } | |
173 | ||
174 | $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0); | |
175 | ||
176 | if ($link) { | |
177 | $this->link = $link->nodeValue; | |
178 | } | |
179 | ||
180 | $articles = $xpath->query("//rssfake:item"); | |
181 | ||
182 | foreach ($articles as $article) { | |
183 | array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath)); | |
184 | } | |
185 | ||
186 | break; | |
187 | ||
188 | } | |
189 | } else { | |
190 | if( !isset($this->error) ){ | |
191 | $this->error = "Unknown/unsupported feed type"; | |
192 | } | |
193 | return; | |
194 | } | |
195 | } | |
196 | ||
197 | function format_error($error) { | |
198 | if ($error) { | |
199 | return sprintf("LibXML error %s at line %d (column %d): %s", | |
200 | $error->code, $error->line, $error->column, | |
201 | $error->message); | |
202 | } else { | |
203 | return ""; | |
204 | } | |
205 | } | |
206 | ||
207 | function error() { | |
208 | return $this->error; | |
209 | } | |
210 | ||
211 | function get_link() { | |
212 | return $this->link; | |
213 | } | |
214 | ||
215 | function get_title() { | |
216 | return $this->title; | |
217 | } | |
218 | ||
219 | function get_items() { | |
220 | return $this->items; | |
221 | } | |
222 | ||
223 | function get_links($rel) { | |
224 | $rv = array(); | |
225 | ||
226 | switch ($this->type) { | |
227 | case $this::FEED_ATOM: | |
228 | $links = $this->xpath->query("//atom:feed/atom:link"); | |
229 | ||
230 | foreach ($links as $link) { | |
231 | if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) { | |
232 | array_push($rv, $link->getAttribute('href')); | |
233 | } | |
234 | } | |
235 | break; | |
236 | case $this::FEED_RSS: | |
237 | $links = $this->xpath->query("//atom:link"); | |
238 | ||
239 | foreach ($links as $link) { | |
240 | if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) { | |
241 | array_push($rv, $link->getAttribute('href')); | |
242 | } | |
243 | } | |
244 | break; | |
245 | } | |
246 | ||
247 | return $rv; | |
248 | } | |
249 | } ?> |