]> git.wh0rd.org - tt-rss.git/blame - classes/feedparser.php
reverting
[tt-rss.git] / classes / feedparser.php
CommitLineData
cd07592c
AD
1<?php
2class 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);
d1f3fa97
AD
20
21 $error = libxml_get_last_error();
22
23 if ($error && $error->code == 9) {
24 libxml_clear_errors();
25
26 // we might want to try guessing input encoding here too
27 $data = iconv("UTF-8", "UTF-8//IGNORE", $data);
28
29 $this->doc = new DOMDocument();
30 $this->doc->loadXML($data);
31
32 $error = libxml_get_last_error();
33 }
34
35 $this->error = $this->format_error($error);
cd07592c
AD
36 libxml_clear_errors();
37
38 $this->items = array();
39 }
40
41 function init() {
42 $root = $this->doc->firstChild;
04d2f9c8
AD
43 $xpath = new DOMXPath($this->doc);
44 $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
a3b9fd12 45 $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
4c00e15b 46 $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
852d4ac8 47 $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
d4992d6b
AD
48 $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
49 $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
8a95d630 50 $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
d4992d6b 51
b9eee80e 52 $this->xpath = $xpath;
04d2f9c8 53
a9000b03 54 $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)");
cd07592c
AD
55
56 if ($root) {
a9000b03
AD
57 $root = $root->item(0);
58
59 if ($root) {
60 switch (mb_strtolower($root->tagName)) {
61 case "rdf:rdf":
62 $this->type = $this::FEED_RDF;
63 break;
64 case "channel":
65 $this->type = $this::FEED_RSS;
66 break;
67 case "feed":
68 $this->type = $this::FEED_ATOM;
69 break;
70 default:
71 if( !isset($this->error) ){
72 $this->error = "Unknown/unsupported feed type";
73 }
74 return;
349c4229 75 }
cd07592c
AD
76 }
77
cd07592c
AD
78 switch ($this->type) {
79 case $this::FEED_ATOM:
cd07592c
AD
80
81 $title = $xpath->query("//atom:feed/atom:title")->item(0);
82
a3b9fd12
AD
83 if (!$title)
84 $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
85
86
cd07592c
AD
87 if ($title) {
88 $this->title = $title->nodeValue;
89 }
90
91 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
92
a3b9fd12
AD
93 if (!$link)
94 $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
95
96
cd07592c
AD
97 if ($link && $link->hasAttributes()) {
98 $this->link = $link->getAttribute("href");
99 }
100
101 $articles = $xpath->query("//atom:entry");
102
a3b9fd12
AD
103 if (!$articles || $articles->length == 0)
104 $articles = $xpath->query("//atom03:entry");
105
cd07592c 106 foreach ($articles as $article) {
f150f85a 107 array_push($this->items, new FeedItem_Atom($article, $this->doc, $this->xpath));
cd07592c
AD
108 }
109
cd07592c
AD
110 break;
111 case $this::FEED_RSS:
04d2f9c8
AD
112 $title = $xpath->query("//channel/title")->item(0);
113
114 if ($title) {
115 $this->title = $title->nodeValue;
116 }
117
118 $link = $xpath->query("//channel/link")->item(0);
119
1874c8d6
AD
120 if ($link) {
121 if ($link->getAttribute("href"))
122 $this->link = $link->getAttribute("href");
123 else if ($link->nodeValue)
124 $this->link = $link->nodeValue;
04d2f9c8
AD
125 }
126
127 $articles = $xpath->query("//channel/item");
128
129 foreach ($articles as $article) {
4c00e15b 130 array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
04d2f9c8
AD
131 }
132
cd07592c 133 break;
852d4ac8
AD
134 case $this::FEED_RDF:
135 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
136
137 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
138
139 if ($title) {
140 $this->title = $title->nodeValue;
141 }
142
143 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
144
145 if ($link) {
146 $this->link = $link->nodeValue;
147 }
148
149 $articles = $xpath->query("//rssfake:item");
150
151 foreach ($articles as $article) {
152 array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
153 }
154
155 break;
156
cd07592c 157 }
852d4ac8 158 } else {
349c4229 159 if( !isset($this->error) ){
160 $this->error = "Unknown/unsupported feed type";
161 }
852d4ac8 162 return;
cd07592c
AD
163 }
164 }
165
166 function format_error($error) {
167 if ($error) {
168 return sprintf("LibXML error %s at line %d (column %d): %s",
169 $error->code, $error->line, $error->column,
170 $error->message);
171 } else {
172 return "";
173 }
174 }
175
176 function error() {
177 return $this->error;
178 }
179
180 function get_link() {
181 return $this->link;
182 }
183
184 function get_title() {
185 return $this->title;
186 }
187
188 function get_items() {
189 return $this->items;
190 }
191
b9eee80e
AD
192 function get_links($rel) {
193 $rv = array();
194
195 switch ($this->type) {
196 case $this::FEED_ATOM:
197 $links = $this->xpath->query("//atom:feed/atom:link");
198
199 foreach ($links as $link) {
200 if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
201 array_push($rv, $link->getAttribute('href'));
202 }
203 }
204 break;
205 case $this::FEED_RSS:
f17c3ee2
AD
206 $links = $this->xpath->query("//atom:link");
207
3c8060ac
AD
208 foreach ($links as $link) {
209 if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
210 array_push($rv, $link->getAttribute('href'));
211 }
b9eee80e
AD
212 }
213 break;
214 }
215
216 return $rv;
217 }
cd07592c 218} ?>