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