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