]>
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);
21 $error = libxml_get_last_error();
23 if ($error && $error->code
== 9) {
24 libxml_clear_errors();
26 // we might want to try guessing input encoding here too
27 $data = iconv("UTF-8", "UTF-8//IGNORE", $data);
29 $this->doc
= new DOMDocument();
30 $this->doc
->loadXML($data);
32 $error = libxml_get_last_error();
35 $this->error
= $this->format_error($error);
36 libxml_clear_errors();
38 $this->items
= array();
42 $root = $this->doc
->firstChild
;
43 $xpath = new DOMXPath($this->doc
);
44 $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
45 $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
46 $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
47 $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
48 $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
49 $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
50 $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
52 $this->xpath
= $xpath;
54 $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)")->item(0);
57 switch (mb_strtolower($root->tagName
)) {
59 $this->type
= $this::FEED_RDF
;
62 $this->type
= $this::FEED_RSS
;
65 $this->type
= $this::FEED_ATOM
;
68 if( !isset($this->error
) ){
69 $this->error
= "Unknown/unsupported feed type";
74 switch ($this->type
) {
75 case $this::FEED_ATOM
:
77 $title = $xpath->query("//atom:feed/atom:title")->item(0);
80 $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
84 $this->title
= $title->nodeValue
;
87 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
90 $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
93 if ($link && $link->hasAttributes()) {
94 $this->link
= $link->getAttribute("href");
97 $articles = $xpath->query("//atom:entry");
99 if (!$articles ||
$articles->length
== 0)
100 $articles = $xpath->query("//atom03:entry");
102 foreach ($articles as $article) {
103 array_push($this->items
, new FeedItem_Atom($article, $this->doc
, $this->xpath
));
107 case $this::FEED_RSS
:
108 $title = $xpath->query("//channel/title")->item(0);
111 $this->title
= $title->nodeValue
;
114 $link = $xpath->query("//channel/link")->item(0);
117 if ($link->getAttribute("href"))
118 $this->link
= $link->getAttribute("href");
119 else if ($link->nodeValue
)
120 $this->link
= $link->nodeValue
;
123 $articles = $xpath->query("//channel/item");
125 foreach ($articles as $article) {
126 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
130 case $this::FEED_RDF
:
131 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
133 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
136 $this->title
= $title->nodeValue
;
139 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
142 $this->link
= $link->nodeValue
;
145 $articles = $xpath->query("//rssfake:item");
147 foreach ($articles as $article) {
148 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
155 if( !isset($this->error
) ){
156 $this->error
= "Unknown/unsupported feed type";
162 function format_error($error) {
164 return sprintf("LibXML error %s at line %d (column %d): %s",
165 $error->code
, $error->line
, $error->column
,
176 function get_link() {
180 function get_title() {
184 function get_items() {
188 function get_links($rel) {
191 switch ($this->type
) {
192 case $this::FEED_ATOM
:
193 $links = $this->xpath
->query("//atom:feed/atom:link");
195 foreach ($links as $link) {
196 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
197 array_push($rv, $link->getAttribute('href'));
201 case $this::FEED_RSS
:
202 $links = $this->xpath
->query("//atom:link");
204 foreach ($links as $link) {
205 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
206 array_push($rv, $link->getAttribute('href'));