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