]>
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('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/');
36 $this->xpath
= $xpath;
38 $root = $xpath->query("(//atom:feed|//channel|//rdf:rdf|//rdf:RDF)")->item(0);
41 switch (mb_strtolower($root->tagName
)) {
43 $this->type
= $this::FEED_RDF
;
46 $this->type
= $this::FEED_RSS
;
49 $this->type
= $this::FEED_ATOM
;
52 if( !isset($this->error
) ){
53 $this->error
= "Unknown/unsupported feed type";
58 switch ($this->type
) {
59 case $this::FEED_ATOM
:
61 $title = $xpath->query("//atom:feed/atom:title")->item(0);
64 $this->title
= $title->nodeValue
;
67 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
69 if ($link && $link->hasAttributes()) {
70 $this->link
= $link->getAttribute("href");
73 $articles = $xpath->query("//atom:entry");
75 foreach ($articles as $article) {
76 array_push($this->items
, new FeedItem_Atom($article, $this->doc
, $this->xpath
));
82 $title = $xpath->query("//channel/title")->item(0);
85 $this->title
= $title->nodeValue
;
88 $link = $xpath->query("//channel/link")->item(0);
90 if ($link && $link->hasAttributes()) {
91 $this->link
= $link->getAttribute("href");
94 $articles = $xpath->query("//channel/item");
96 foreach ($articles as $article) {
97 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
101 case $this::FEED_RDF
:
102 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
104 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
107 $this->title
= $title->nodeValue
;
110 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
113 $this->link
= $link->nodeValue
;
116 $articles = $xpath->query("//rssfake:item");
118 foreach ($articles as $article) {
119 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
126 if( !isset($this->error
) ){
127 $this->error
= "Unknown/unsupported feed type";
133 function format_error($error) {
135 return sprintf("LibXML error %s at line %d (column %d): %s",
136 $error->code
, $error->line
, $error->column
,
147 function get_link() {
151 function get_title() {
155 function get_items() {
159 function get_links($rel) {
162 switch ($this->type
) {
163 case $this::FEED_ATOM
:
164 $links = $this->xpath
->query("//atom:feed/atom:link");
166 foreach ($links as $link) {
167 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
168 array_push($rv, $link->getAttribute('href'));
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'));