]> git.wh0rd.org - tt-rss.git/blame - classes/feedparser.php
add HOOK_PREFS_TAB_SECTION to user manager toolbar
[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
a3b9fd12 54 $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)")->item(0);
cd07592c
AD
55
56 if ($root) {
852d4ac8
AD
57 switch (mb_strtolower($root->tagName)) {
58 case "rdf:rdf":
59 $this->type = $this::FEED_RDF;
60 break;
04d2f9c8 61 case "channel":
cd07592c
AD
62 $this->type = $this::FEED_RSS;
63 break;
64 case "feed":
65 $this->type = $this::FEED_ATOM;
66 break;
67 default:
349c4229 68 if( !isset($this->error) ){
69 $this->error = "Unknown/unsupported feed type";
70 }
cd07592c
AD
71 return;
72 }
73
cd07592c
AD
74 switch ($this->type) {
75 case $this::FEED_ATOM:
cd07592c
AD
76
77 $title = $xpath->query("//atom:feed/atom:title")->item(0);
78
a3b9fd12
AD
79 if (!$title)
80 $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
81
82
cd07592c
AD
83 if ($title) {
84 $this->title = $title->nodeValue;
85 }
86
87 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
88
a3b9fd12
AD
89 if (!$link)
90 $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
91
92
cd07592c
AD
93 if ($link && $link->hasAttributes()) {
94 $this->link = $link->getAttribute("href");
95 }
96
97 $articles = $xpath->query("//atom:entry");
98
a3b9fd12
AD
99 if (!$articles || $articles->length == 0)
100 $articles = $xpath->query("//atom03:entry");
101
cd07592c 102 foreach ($articles as $article) {
4c00e15b 103 array_push($this->items, new FeedItem_Atom($article, $this->doc, $this->xpath));
cd07592c
AD
104 }
105
cd07592c
AD
106 break;
107 case $this::FEED_RSS:
04d2f9c8
AD
108
109 $title = $xpath->query("//channel/title")->item(0);
110
111 if ($title) {
112 $this->title = $title->nodeValue;
113 }
114
115 $link = $xpath->query("//channel/link")->item(0);
116
117 if ($link && $link->hasAttributes()) {
118 $this->link = $link->getAttribute("href");
119 }
120
121 $articles = $xpath->query("//channel/item");
122
123 foreach ($articles as $article) {
4c00e15b 124 array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
04d2f9c8
AD
125 }
126
cd07592c 127 break;
852d4ac8
AD
128 case $this::FEED_RDF:
129 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
130
131 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
132
133 if ($title) {
134 $this->title = $title->nodeValue;
135 }
136
137 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
138
139 if ($link) {
140 $this->link = $link->nodeValue;
141 }
142
143 $articles = $xpath->query("//rssfake:item");
144
145 foreach ($articles as $article) {
146 array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
147 }
148
149 break;
150
cd07592c 151 }
852d4ac8 152 } else {
349c4229 153 if( !isset($this->error) ){
154 $this->error = "Unknown/unsupported feed type";
155 }
852d4ac8 156 return;
cd07592c
AD
157 }
158 }
159
160 function format_error($error) {
161 if ($error) {
162 return sprintf("LibXML error %s at line %d (column %d): %s",
163 $error->code, $error->line, $error->column,
164 $error->message);
165 } else {
166 return "";
167 }
168 }
169
170 function error() {
171 return $this->error;
172 }
173
174 function get_link() {
175 return $this->link;
176 }
177
178 function get_title() {
179 return $this->title;
180 }
181
182 function get_items() {
183 return $this->items;
184 }
185
b9eee80e
AD
186 function get_links($rel) {
187 $rv = array();
188
189 switch ($this->type) {
190 case $this::FEED_ATOM:
191 $links = $this->xpath->query("//atom:feed/atom:link");
192
193 foreach ($links as $link) {
194 if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
195 array_push($rv, $link->getAttribute('href'));
196 }
197 }
198 break;
199 case $this::FEED_RSS:
200 $links = $this->xpath->query("//channel/link");
3c8060ac
AD
201 foreach ($links as $link) {
202 if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
203 array_push($rv, $link->getAttribute('href'));
204 }
b9eee80e
AD
205 }
206 break;
207 }
208
209 return $rv;
210 }
cd07592c 211} ?>