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