]>
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 $this->xpath
= $xpath;
33 $root = $xpath->query("(//atom:feed|//channel)")->item(0);
36 switch ($root->tagName
) {
38 $this->type
= $this::FEED_RSS
;
41 $this->type
= $this::FEED_ATOM
;
44 $this->error
= "Unknown/unsupported feed type";
48 switch ($this->type
) {
49 case $this::FEED_ATOM
:
51 $title = $xpath->query("//atom:feed/atom:title")->item(0);
54 $this->title
= $title->nodeValue
;
57 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
59 if ($link && $link->hasAttributes()) {
60 $this->link
= $link->getAttribute("href");
63 $articles = $xpath->query("//atom:entry");
65 foreach ($articles as $article) {
66 array_push($this->items
, new FeedItem_Atom($article, $this->doc
, $this->xpath
));
72 $title = $xpath->query("//channel/title")->item(0);
75 $this->title
= $title->nodeValue
;
78 $link = $xpath->query("//channel/link")->item(0);
80 if ($link && $link->hasAttributes()) {
81 $this->link
= $link->getAttribute("href");
84 $articles = $xpath->query("//channel/item");
86 foreach ($articles as $article) {
87 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
95 function format_error($error) {
97 return sprintf("LibXML error %s at line %d (column %d): %s",
98 $error->code
, $error->line
, $error->column
,
109 function get_link() {
113 function get_title() {
117 function get_items() {
121 function get_links($rel) {
124 switch ($this->type
) {
125 case $this::FEED_ATOM
:
126 $links = $this->xpath
->query("//atom:feed/atom:link");
128 foreach ($links as $link) {
129 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
130 array_push($rv, $link->getAttribute('href'));
134 case $this::FEED_RSS
:
135 $links = $this->xpath
->query("//channel/link");
136 foreach ($links as $link) {
137 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
138 array_push($rv, $link->getAttribute('href'));