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