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