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