]>
git.wh0rd.org - tt-rss.git/blob - classes/feedparser.php
15 function __construct($data) {
16 libxml_use_internal_errors(true);
17 libxml_clear_errors();
18 $this->doc
= new DOMDocument();
19 $this->doc
->loadXML($data);
21 $error = libxml_get_last_error();
23 if ($error && $error->code
== 9) {
24 libxml_clear_errors();
26 // we might want to try guessing input encoding here too
27 $data = iconv("UTF-8", "UTF-8//IGNORE", $data);
29 $this->doc
= new DOMDocument();
30 $this->doc
->loadXML($data);
32 $error = libxml_get_last_error();
35 $this->error
= $this->format_error($error);
36 libxml_clear_errors();
38 $this->items
= array();
42 $root = $this->doc
->firstChild
;
43 $xpath = new DOMXPath($this->doc
);
44 $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
45 $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
46 $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
47 $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
48 $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
49 $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
50 $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
52 $this->xpath
= $xpath;
54 $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)");
57 $root = $root->item(0);
60 switch (mb_strtolower($root->tagName
)) {
62 $this->type
= $this::FEED_RDF
;
65 $this->type
= $this::FEED_RSS
;
68 $this->type
= $this::FEED_ATOM
;
71 if( !isset($this->error
) ){
72 $this->error
= "Unknown/unsupported feed type";
78 switch ($this->type
) {
79 case $this::FEED_ATOM
:
81 $title = $xpath->query("//atom:feed/atom:title")->item(0);
84 $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
88 $this->title
= $title->nodeValue
;
91 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
94 $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
97 if ($link && $link->hasAttributes()) {
98 $this->link
= $link->getAttribute("href");
101 $articles = $xpath->query("//atom:entry");
103 if (!$articles ||
$articles->length
== 0)
104 $articles = $xpath->query("//atom03:entry");
106 $feed = $this->xpath
->query("//atom:feed")->item(0);
107 $atts = $feed->attributes
;
108 foreach($atts as $attrib)
110 if($attrib->name
== "base"){
111 $base = $attrib->nodeValue
;
114 foreach ($articles as $article) {
115 array_push($this->items
, new FeedItem_Atom($article, $this->doc
, $this->xpath
, $base));
120 case $this::FEED_RSS
:
121 $title = $xpath->query("//channel/title")->item(0);
124 $this->title
= $title->nodeValue
;
127 $link = $xpath->query("//channel/link")->item(0);
130 if ($link->getAttribute("href"))
131 $this->link
= $link->getAttribute("href");
132 else if ($link->nodeValue
)
133 $this->link
= $link->nodeValue
;
136 $articles = $xpath->query("//channel/item");
138 foreach ($articles as $article) {
139 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
143 case $this::FEED_RDF
:
144 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
146 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
149 $this->title
= $title->nodeValue
;
152 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
155 $this->link
= $link->nodeValue
;
158 $articles = $xpath->query("//rssfake:item");
160 foreach ($articles as $article) {
161 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
168 if( !isset($this->error
) ){
169 $this->error
= "Unknown/unsupported feed type";
175 function format_error($error) {
177 return sprintf("LibXML error %s at line %d (column %d): %s",
178 $error->code
, $error->line
, $error->column
,
189 function get_link() {
193 function get_title() {
197 function get_items() {
201 function get_links($rel) {
204 switch ($this->type
) {
205 case $this::FEED_ATOM
:
206 $links = $this->xpath
->query("//atom:feed/atom:link");
208 foreach ($links as $link) {
209 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
210 array_push($rv, $link->getAttribute('href'));
214 case $this::FEED_RSS
:
215 $links = $this->xpath
->query("//atom:link");
217 foreach ($links as $link) {
218 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
219 array_push($rv, $link->getAttribute('href'));