]>
git.wh0rd.org - tt-rss.git/blob - classes/feedparser.php
f61c2a0031aba86c465abb0298ca2e1a23d94477
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/');
35 $this->xpath
= $xpath;
37 $root = $xpath->query("(//atom:feed|//channel|//rdf:rdf|//rdf:RDF)")->item(0);
40 switch (mb_strtolower($root->tagName
)) {
42 $this->type
= $this::FEED_RDF
;
45 $this->type
= $this::FEED_RSS
;
48 $this->type
= $this::FEED_ATOM
;
51 $this->error
= "Unknown/unsupported feed type";
55 switch ($this->type
) {
56 case $this::FEED_ATOM
:
58 $title = $xpath->query("//atom:feed/atom:title")->item(0);
61 $this->title
= $title->nodeValue
;
64 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
66 if ($link && $link->hasAttributes()) {
67 $this->link
= $link->getAttribute("href");
70 $articles = $xpath->query("//atom:entry");
72 foreach ($articles as $article) {
73 array_push($this->items
, new FeedItem_Atom($article, $this->doc
, $this->xpath
));
79 $title = $xpath->query("//channel/title")->item(0);
82 $this->title
= $title->nodeValue
;
85 $link = $xpath->query("//channel/link")->item(0);
87 if ($link && $link->hasAttributes()) {
88 $this->link
= $link->getAttribute("href");
91 $articles = $xpath->query("//channel/item");
93 foreach ($articles as $article) {
94 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
99 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
101 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
104 $this->title
= $title->nodeValue
;
107 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
110 $this->link
= $link->nodeValue
;
113 $articles = $xpath->query("//rssfake:item");
115 foreach ($articles as $article) {
116 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
123 $this->error
= "Unknown/unsupported feed type";
128 function format_error($error) {
130 return sprintf("LibXML error %s at line %d (column %d): %s",
131 $error->code
, $error->line
, $error->column
,
142 function get_link() {
146 function get_title() {
150 function get_items() {
154 function get_links($rel) {
157 switch ($this->type
) {
158 case $this::FEED_ATOM
:
159 $links = $this->xpath
->query("//atom:feed/atom:link");
161 foreach ($links as $link) {
162 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
163 array_push($rv, $link->getAttribute('href'));
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'));