]> git.wh0rd.org Git - tt-rss.git/blob - classes/feedparser.php
fix support of Atom 0.3
[tt-rss.git] / classes / feedparser.php
1 <?php
2 class FeedParser {
3         private $doc;
4         private $error;
5         private $items;
6         private $link;
7         private $title;
8         private $type;
9         private $xpath;
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;
28                 $xpath = new DOMXPath($this->doc);
29                 $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
30                 $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
31                 $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
32                 $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
33                 $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
34                 $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
35                 $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
36
37                 $this->xpath = $xpath;
38
39                 $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)")->item(0);
40
41                 if ($root) {
42                         switch (mb_strtolower($root->tagName)) {
43                         case "rdf:rdf":
44                                 $this->type = $this::FEED_RDF;
45                                 break;
46                         case "channel":
47                                 $this->type = $this::FEED_RSS;
48                                 break;
49                         case "feed":
50                                 $this->type = $this::FEED_ATOM;
51                                 break;
52                         default:
53                                 if( !isset($this->error) ){
54                                         $this->error = "Unknown/unsupported feed type";
55                                 }
56                                 return;
57                         }
58
59                         switch ($this->type) {
60                         case $this::FEED_ATOM:
61
62                                 $title = $xpath->query("//atom:feed/atom:title")->item(0);
63
64                                 if (!$title)
65                                         $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
66
67
68                                 if ($title) {
69                                         $this->title = $title->nodeValue;
70                                 }
71
72                                 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
73
74                                 if (!$link)
75                                         $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
76
77
78                                 if ($link && $link->hasAttributes()) {
79                                         $this->link = $link->getAttribute("href");
80                                 }
81
82                                 $articles = $xpath->query("//atom:entry");
83
84                                 if (!$articles || $articles->length == 0)
85                                         $articles = $xpath->query("//atom03:entry");
86
87                                 foreach ($articles as $article) {
88                                         array_push($this->items, new FeedItem_Atom($article, $this->doc, $this->xpath));
89                                 }
90
91                                 break;
92                         case $this::FEED_RSS:
93
94                                 $title = $xpath->query("//channel/title")->item(0);
95
96                                 if ($title) {
97                                         $this->title = $title->nodeValue;
98                                 }
99
100                                 $link = $xpath->query("//channel/link")->item(0);
101
102                                 if ($link && $link->hasAttributes()) {
103                                         $this->link = $link->getAttribute("href");
104                                 }
105
106                                 $articles = $xpath->query("//channel/item");
107
108                                 foreach ($articles as $article) {
109                                         array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
110                                 }
111
112                                 break;
113                         case $this::FEED_RDF:
114                                 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
115
116                                 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
117
118                                 if ($title) {
119                                         $this->title = $title->nodeValue;
120                                 }
121
122                                 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
123
124                                 if ($link) {
125                                         $this->link = $link->nodeValue;
126                                 }
127
128                                 $articles = $xpath->query("//rssfake:item");
129
130                                 foreach ($articles as $article) {
131                                         array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
132                                 }
133
134                                 break;
135
136                         }
137                 } else {
138                         if( !isset($this->error) ){
139                                 $this->error = "Unknown/unsupported feed type";
140                         }
141                         return;
142                 }
143         }
144
145         function format_error($error) {
146                 if ($error) {
147                         return sprintf("LibXML error %s at line %d (column %d): %s",
148                                 $error->code, $error->line, $error->column,
149                                 $error->message);
150                 } else {
151                         return "";
152                 }
153         }
154
155         function error() {
156                 return $this->error;
157         }
158
159         function get_link() {
160                 return $this->link;
161         }
162
163         function get_title() {
164                 return $this->title;
165         }
166
167         function get_items() {
168                 return $this->items;
169         }
170
171         function get_links($rel) {
172                 $rv = array();
173
174                 switch ($this->type) {
175                 case $this::FEED_ATOM:
176                         $links = $this->xpath->query("//atom:feed/atom:link");
177
178                         foreach ($links as $link) {
179                                 if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
180                                         array_push($rv, $link->getAttribute('href'));
181                                 }
182                         }
183                         break;
184                 case $this::FEED_RSS:
185                         $links = $this->xpath->query("//channel/link");
186                         foreach ($links as $link) {
187                                 if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
188                                         array_push($rv, $link->getAttribute('href'));
189                                 }
190                         }
191                         break;
192                 }
193
194                 return $rv;
195         }
196 } ?>