]>
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; | |
9 | ||
10 | const FEED_RDF = 0; | |
11 | const FEED_RSS = 1; | |
12 | const FEED_ATOM = 2; | |
13 | ||
14 | function __construct($data) { | |
15 | libxml_use_internal_errors(true); | |
16 | libxml_clear_errors(); | |
17 | $this->doc = new DOMDocument(); | |
18 | $this->doc->loadXML($data); | |
19 | $this->error = $this->format_error(libxml_get_last_error()); | |
20 | libxml_clear_errors(); | |
21 | ||
22 | $this->items = array(); | |
23 | } | |
24 | ||
25 | function init() { | |
26 | $root = $this->doc->firstChild; | |
04d2f9c8 AD |
27 | $xpath = new DOMXPath($this->doc); |
28 | $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom'); | |
29 | ||
30 | $root = $xpath->query("(//atom:feed|//channel)")->item(0); | |
cd07592c AD |
31 | |
32 | if ($root) { | |
33 | switch ($root->tagName) { | |
04d2f9c8 | 34 | case "channel": |
cd07592c AD |
35 | $this->type = $this::FEED_RSS; |
36 | break; | |
37 | case "feed": | |
38 | $this->type = $this::FEED_ATOM; | |
39 | break; | |
40 | default: | |
41 | $this->error = "Unknown/unsupported feed type"; | |
42 | return; | |
43 | } | |
44 | ||
cd07592c AD |
45 | switch ($this->type) { |
46 | case $this::FEED_ATOM: | |
cd07592c AD |
47 | |
48 | $title = $xpath->query("//atom:feed/atom:title")->item(0); | |
49 | ||
50 | if ($title) { | |
51 | $this->title = $title->nodeValue; | |
52 | } | |
53 | ||
54 | $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0); | |
55 | ||
56 | if ($link && $link->hasAttributes()) { | |
57 | $this->link = $link->getAttribute("href"); | |
58 | } | |
59 | ||
60 | $articles = $xpath->query("//atom:entry"); | |
61 | ||
62 | foreach ($articles as $article) { | |
63 | array_push($this->items, new FeedItem_Atom($article)); | |
64 | } | |
65 | ||
cd07592c AD |
66 | break; |
67 | case $this::FEED_RSS: | |
04d2f9c8 AD |
68 | |
69 | $title = $xpath->query("//channel/title")->item(0); | |
70 | ||
71 | if ($title) { | |
72 | $this->title = $title->nodeValue; | |
73 | } | |
74 | ||
75 | $link = $xpath->query("//channel/link")->item(0); | |
76 | ||
77 | if ($link && $link->hasAttributes()) { | |
78 | $this->link = $link->getAttribute("href"); | |
79 | } | |
80 | ||
81 | $articles = $xpath->query("//channel/item"); | |
82 | ||
83 | foreach ($articles as $article) { | |
84 | array_push($this->items, new FeedItem_RSS($article)); | |
85 | } | |
86 | ||
cd07592c AD |
87 | break; |
88 | } | |
89 | } | |
90 | } | |
91 | ||
92 | function format_error($error) { | |
93 | if ($error) { | |
94 | return sprintf("LibXML error %s at line %d (column %d): %s", | |
95 | $error->code, $error->line, $error->column, | |
96 | $error->message); | |
97 | } else { | |
98 | return ""; | |
99 | } | |
100 | } | |
101 | ||
102 | function error() { | |
103 | return $this->error; | |
104 | } | |
105 | ||
106 | function get_link() { | |
107 | return $this->link; | |
108 | } | |
109 | ||
110 | function get_title() { | |
111 | return $this->title; | |
112 | } | |
113 | ||
114 | function get_items() { | |
115 | return $this->items; | |
116 | } | |
117 | ||
118 | } ?> |