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