]>
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 $this->error
= "Unknown/unsupported feed type";
56 switch ($this->type
) {
57 case $this::FEED_ATOM
:
59 $title = $xpath->query("//atom:feed/atom:title")->item(0);
62 $this->title
= $title->nodeValue
;
65 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
67 if ($link && $link->hasAttributes()) {
68 $this->link
= $link->getAttribute("href");
71 $articles = $xpath->query("//atom:entry");
73 foreach ($articles as $article) {
74 array_push($this->items
, new FeedItem_Atom($article, $this->doc
, $this->xpath
));
80 $title = $xpath->query("//channel/title")->item(0);
83 $this->title
= $title->nodeValue
;
86 $link = $xpath->query("//channel/link")->item(0);
88 if ($link && $link->hasAttributes()) {
89 $this->link
= $link->getAttribute("href");
92 $articles = $xpath->query("//channel/item");
94 foreach ($articles as $article) {
95 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
100 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
102 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
105 $this->title
= $title->nodeValue
;
108 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
111 $this->link
= $link->nodeValue
;
114 $articles = $xpath->query("//rssfake:item");
116 foreach ($articles as $article) {
117 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
124 $this->error
= "Unknown/unsupported feed type";
129 function format_error($error) {
131 return sprintf("LibXML error %s at line %d (column %d): %s",
132 $error->code
, $error->line
, $error->column
,
143 function get_link() {
147 function get_title() {
151 function get_items() {
155 function get_links($rel) {
158 switch ($this->type
) {
159 case $this::FEED_ATOM
:
160 $links = $this->xpath
->query("//atom:feed/atom:link");
162 foreach ($links as $link) {
163 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
164 array_push($rv, $link->getAttribute('href'));
168 case $this::FEED_RSS
:
169 $links = $this->xpath
->query("//channel/link");
170 foreach ($links as $link) {
171 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
172 array_push($rv, $link->getAttribute('href'));