]>
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 $this->xpath
= $xpath;
32 $root = $xpath->query("(//atom:feed|//channel)")->item(0);
35 switch ($root->tagName
) {
37 $this->type
= $this::FEED_RSS
;
40 $this->type
= $this::FEED_ATOM
;
43 $this->error
= "Unknown/unsupported feed type";
47 switch ($this->type
) {
48 case $this::FEED_ATOM
:
50 $title = $xpath->query("//atom:feed/atom:title")->item(0);
53 $this->title
= $title->nodeValue
;
56 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
58 if ($link && $link->hasAttributes()) {
59 $this->link
= $link->getAttribute("href");
62 $articles = $xpath->query("//atom:entry");
64 foreach ($articles as $article) {
65 array_push($this->items
, new FeedItem_Atom($article));
71 $title = $xpath->query("//channel/title")->item(0);
74 $this->title
= $title->nodeValue
;
77 $link = $xpath->query("//channel/link")->item(0);
79 if ($link && $link->hasAttributes()) {
80 $this->link
= $link->getAttribute("href");
83 $articles = $xpath->query("//channel/item");
85 foreach ($articles as $article) {
86 array_push($this->items
, new FeedItem_RSS($article));
94 function format_error($error) {
96 return sprintf("LibXML error %s at line %d (column %d): %s",
97 $error->code
, $error->line
, $error->column
,
108 function get_link() {
112 function get_title() {
116 function get_items() {
120 function get_links($rel) {
123 switch ($this->type
) {
124 case $this::FEED_ATOM
:
125 $links = $this->xpath
->query("//atom:feed/atom:link");
127 foreach ($links as $link) {
128 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
129 array_push($rv, $link->getAttribute('href'));
133 case $this::FEED_RSS
:
134 $links = $this->xpath
->query("//channel/link");
135 foreach ($links as $link) {
136 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
137 array_push($rv, $link->getAttribute('href'));