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