]>
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
:
109 $title = $xpath->query("//channel/title")->item(0);
112 $this->title
= $title->nodeValue
;
115 $link = $xpath->query("//channel/link")->item(0);
118 if ($link->getAttribute("href"))
119 $this->link
= $link->getAttribute("href");
120 else if ($link->nodeValue
)
121 $this->link
= $link->nodeValue
;
124 $articles = $xpath->query("//channel/item");
126 foreach ($articles as $article) {
127 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
131 case $this::FEED_RDF
:
132 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
134 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
137 $this->title
= $title->nodeValue
;
140 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
143 $this->link
= $link->nodeValue
;
146 $articles = $xpath->query("//rssfake:item");
148 foreach ($articles as $article) {
149 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
156 if( !isset($this->error
) ){
157 $this->error
= "Unknown/unsupported feed type";
163 function format_error($error) {
165 return sprintf("LibXML error %s at line %d (column %d): %s",
166 $error->code
, $error->line
, $error->column
,
177 function get_link() {
181 function get_title() {
185 function get_items() {
189 function get_links($rel) {
192 switch ($this->type
) {
193 case $this::FEED_ATOM
:
194 $links = $this->xpath
->query("//atom:feed/atom:link");
196 foreach ($links as $link) {
197 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
198 array_push($rv, $link->getAttribute('href'));
202 case $this::FEED_RSS
:
203 $links = $this->xpath
->query("//channel/link");
204 foreach ($links as $link) {
205 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
206 array_push($rv, $link->getAttribute('href'));