]>
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);
20 $this->error
= $this->format_error(libxml_get_last_error());
21 libxml_clear_errors();
23 $this->items
= array();
27 $root = $this->doc
->firstChild
;
28 $xpath = new DOMXPath($this->doc
);
29 $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
30 $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
31 $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
32 $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
33 $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
34 $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
35 $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
37 $this->xpath
= $xpath;
39 $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)")->item(0);
42 switch (mb_strtolower($root->tagName
)) {
44 $this->type
= $this::FEED_RDF
;
47 $this->type
= $this::FEED_RSS
;
50 $this->type
= $this::FEED_ATOM
;
53 if( !isset($this->error
) ){
54 $this->error
= "Unknown/unsupported feed type";
59 switch ($this->type
) {
60 case $this::FEED_ATOM
:
62 $title = $xpath->query("//atom:feed/atom:title")->item(0);
65 $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
69 $this->title
= $title->nodeValue
;
72 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
75 $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
78 if ($link && $link->hasAttributes()) {
79 $this->link
= $link->getAttribute("href");
82 $articles = $xpath->query("//atom:entry");
84 if (!$articles ||
$articles->length
== 0)
85 $articles = $xpath->query("//atom03:entry");
87 foreach ($articles as $article) {
88 array_push($this->items
, new FeedItem_Atom($article, $this->doc
, $this->xpath
));
94 $title = $xpath->query("//channel/title")->item(0);
97 $this->title
= $title->nodeValue
;
100 $link = $xpath->query("//channel/link")->item(0);
102 if ($link && $link->hasAttributes()) {
103 $this->link
= $link->getAttribute("href");
106 $articles = $xpath->query("//channel/item");
108 foreach ($articles as $article) {
109 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
113 case $this::FEED_RDF
:
114 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
116 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
119 $this->title
= $title->nodeValue
;
122 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
125 $this->link
= $link->nodeValue
;
128 $articles = $xpath->query("//rssfake:item");
130 foreach ($articles as $article) {
131 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
138 if( !isset($this->error
) ){
139 $this->error
= "Unknown/unsupported feed type";
145 function format_error($error) {
147 return sprintf("LibXML error %s at line %d (column %d): %s",
148 $error->code
, $error->line
, $error->column
,
159 function get_link() {
163 function get_title() {
167 function get_items() {
171 function get_links($rel) {
174 switch ($this->type
) {
175 case $this::FEED_ATOM
:
176 $links = $this->xpath
->query("//atom:feed/atom:link");
178 foreach ($links as $link) {
179 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
180 array_push($rv, $link->getAttribute('href'));
184 case $this::FEED_RSS
:
185 $links = $this->xpath
->query("//channel/link");
186 foreach ($links as $link) {
187 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
188 array_push($rv, $link->getAttribute('href'));