]>
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; | |
27 | ||
28 | if ($root) { | |
29 | switch ($root->tagName) { | |
30 | case "rss": | |
31 | $this->type = $this::FEED_RSS; | |
32 | break; | |
33 | case "feed": | |
34 | $this->type = $this::FEED_ATOM; | |
35 | break; | |
36 | default: | |
37 | $this->error = "Unknown/unsupported feed type"; | |
38 | return; | |
39 | } | |
40 | ||
41 | $xpath = new DOMXPath($this->doc); | |
42 | ||
43 | switch ($this->type) { | |
44 | case $this::FEED_ATOM: | |
45 | $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom'); | |
46 | ||
47 | $title = $xpath->query("//atom:feed/atom:title")->item(0); | |
48 | ||
49 | if ($title) { | |
50 | $this->title = $title->nodeValue; | |
51 | } | |
52 | ||
53 | $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0); | |
54 | ||
55 | if ($link && $link->hasAttributes()) { | |
56 | $this->link = $link->getAttribute("href"); | |
57 | } | |
58 | ||
59 | $articles = $xpath->query("//atom:entry"); | |
60 | ||
61 | foreach ($articles as $article) { | |
62 | array_push($this->items, new FeedItem_Atom($article)); | |
63 | } | |
64 | ||
65 | break; | |
66 | case $this::FEED_RDF: | |
67 | ||
68 | break; | |
69 | case $this::FEED_RSS: | |
70 | break; | |
71 | } | |
72 | } | |
73 | } | |
74 | ||
75 | function format_error($error) { | |
76 | if ($error) { | |
77 | return sprintf("LibXML error %s at line %d (column %d): %s", | |
78 | $error->code, $error->line, $error->column, | |
79 | $error->message); | |
80 | } else { | |
81 | return ""; | |
82 | } | |
83 | } | |
84 | ||
85 | function error() { | |
86 | return $this->error; | |
87 | } | |
88 | ||
89 | function get_link() { | |
90 | return $this->link; | |
91 | } | |
92 | ||
93 | function get_title() { | |
94 | return $this->title; | |
95 | } | |
96 | ||
97 | function get_items() { | |
98 | return $this->items; | |
99 | } | |
100 | ||
101 | } ?> |