]>
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 | 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) { |
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 | } | |
f612dbe8 | 63 | } |
d1f3fa97 AD |
64 | } |
65 | ||
66 | $this->error = $this->format_error($error); | |
cd07592c AD |
67 | libxml_clear_errors(); |
68 | ||
69 | $this->items = array(); | |
70 | } | |
71 | ||
72 | function init() { | |
73 | $root = $this->doc->firstChild; | |
04d2f9c8 AD |
74 | $xpath = new DOMXPath($this->doc); |
75 | $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom'); | |
a3b9fd12 | 76 | $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#'); |
4c00e15b | 77 | $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/'); |
852d4ac8 | 78 | $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'); |
d4992d6b AD |
79 | $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/'); |
80 | $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/'); | |
8a95d630 | 81 | $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/'); |
d4992d6b | 82 | |
b9eee80e | 83 | $this->xpath = $xpath; |
04d2f9c8 | 84 | |
a9000b03 | 85 | $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)"); |
cd07592c AD |
86 | |
87 | if ($root) { | |
a9000b03 AD |
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; | |
349c4229 | 106 | } |
cd07592c AD |
107 | } |
108 | ||
cd07592c AD |
109 | switch ($this->type) { |
110 | case $this::FEED_ATOM: | |
cd07592c AD |
111 | |
112 | $title = $xpath->query("//atom:feed/atom:title")->item(0); | |
113 | ||
a3b9fd12 AD |
114 | if (!$title) |
115 | $title = $xpath->query("//atom03:feed/atom03:title")->item(0); | |
116 | ||
117 | ||
cd07592c AD |
118 | if ($title) { |
119 | $this->title = $title->nodeValue; | |
120 | } | |
121 | ||
122 | $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0); | |
123 | ||
a3b9fd12 AD |
124 | if (!$link) |
125 | $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0); | |
126 | ||
127 | ||
cd07592c AD |
128 | if ($link && $link->hasAttributes()) { |
129 | $this->link = $link->getAttribute("href"); | |
130 | } | |
131 | ||
132 | $articles = $xpath->query("//atom:entry"); | |
133 | ||
a3b9fd12 AD |
134 | if (!$articles || $articles->length == 0) |
135 | $articles = $xpath->query("//atom03:entry"); | |
136 | ||
cd07592c | 137 | foreach ($articles as $article) { |
4c00e15b | 138 | array_push($this->items, new FeedItem_Atom($article, $this->doc, $this->xpath)); |
cd07592c AD |
139 | } |
140 | ||
cd07592c AD |
141 | break; |
142 | case $this::FEED_RSS: | |
04d2f9c8 AD |
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 | ||
1874c8d6 AD |
151 | if ($link) { |
152 | if ($link->getAttribute("href")) | |
153 | $this->link = $link->getAttribute("href"); | |
154 | else if ($link->nodeValue) | |
155 | $this->link = $link->nodeValue; | |
04d2f9c8 AD |
156 | } |
157 | ||
158 | $articles = $xpath->query("//channel/item"); | |
159 | ||
160 | foreach ($articles as $article) { | |
4c00e15b | 161 | array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath)); |
04d2f9c8 AD |
162 | } |
163 | ||
cd07592c | 164 | break; |
852d4ac8 AD |
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 | ||
cd07592c | 188 | } |
852d4ac8 | 189 | } else { |
349c4229 | 190 | if( !isset($this->error) ){ |
191 | $this->error = "Unknown/unsupported feed type"; | |
192 | } | |
852d4ac8 | 193 | return; |
cd07592c AD |
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 | ||
b9eee80e AD |
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: | |
f17c3ee2 AD |
237 | $links = $this->xpath->query("//atom:link"); |
238 | ||
3c8060ac AD |
239 | foreach ($links as $link) { |
240 | if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) { | |
241 | array_push($rv, $link->getAttribute('href')); | |
242 | } | |
b9eee80e AD |
243 | } |
244 | break; | |
245 | } | |
246 | ||
247 | return $rv; | |
248 | } | |
cd07592c | 249 | } ?> |