]> git.wh0rd.org - tt-rss.git/blame - classes/feedparser.php
remove simplepie
[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);
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;
04d2f9c8
AD
28 $xpath = new DOMXPath($this->doc);
29 $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
b9eee80e 30 $this->xpath = $xpath;
04d2f9c8
AD
31
32 $root = $xpath->query("(//atom:feed|//channel)")->item(0);
cd07592c
AD
33
34 if ($root) {
35 switch ($root->tagName) {
04d2f9c8 36 case "channel":
cd07592c
AD
37 $this->type = $this::FEED_RSS;
38 break;
39 case "feed":
40 $this->type = $this::FEED_ATOM;
41 break;
42 default:
43 $this->error = "Unknown/unsupported feed type";
44 return;
45 }
46
cd07592c
AD
47 switch ($this->type) {
48 case $this::FEED_ATOM:
cd07592c
AD
49
50 $title = $xpath->query("//atom:feed/atom:title")->item(0);
51
52 if ($title) {
53 $this->title = $title->nodeValue;
54 }
55
56 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
57
58 if ($link && $link->hasAttributes()) {
59 $this->link = $link->getAttribute("href");
60 }
61
62 $articles = $xpath->query("//atom:entry");
63
64 foreach ($articles as $article) {
65 array_push($this->items, new FeedItem_Atom($article));
66 }
67
cd07592c
AD
68 break;
69 case $this::FEED_RSS:
04d2f9c8
AD
70
71 $title = $xpath->query("//channel/title")->item(0);
72
73 if ($title) {
74 $this->title = $title->nodeValue;
75 }
76
77 $link = $xpath->query("//channel/link")->item(0);
78
79 if ($link && $link->hasAttributes()) {
80 $this->link = $link->getAttribute("href");
81 }
82
83 $articles = $xpath->query("//channel/item");
84
85 foreach ($articles as $article) {
86 array_push($this->items, new FeedItem_RSS($article));
87 }
88
cd07592c
AD
89 break;
90 }
91 }
92 }
93
94 function format_error($error) {
95 if ($error) {
96 return sprintf("LibXML error %s at line %d (column %d): %s",
97 $error->code, $error->line, $error->column,
98 $error->message);
99 } else {
100 return "";
101 }
102 }
103
104 function error() {
105 return $this->error;
106 }
107
108 function get_link() {
109 return $this->link;
110 }
111
112 function get_title() {
113 return $this->title;
114 }
115
116 function get_items() {
117 return $this->items;
118 }
119
b9eee80e
AD
120 function get_links($rel) {
121 $rv = array();
122
123 switch ($this->type) {
124 case $this::FEED_ATOM:
125 $links = $this->xpath->query("//atom:feed/atom:link");
126
127 foreach ($links as $link) {
128 if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
129 array_push($rv, $link->getAttribute('href'));
130 }
131 }
132 break;
133 case $this::FEED_RSS:
134 $links = $this->xpath->query("//channel/link");
3c8060ac
AD
135 foreach ($links as $link) {
136 if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
137 array_push($rv, $link->getAttribute('href'));
138 }
b9eee80e
AD
139 }
140 break;
141 }
142
143 return $rv;
144 }
cd07592c 145} ?>