]>
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 // libxml compiled without iconv?
24 if ($error && $error->code
== 32) {
25 if (preg_match('/^(<\?xml[\t\n\r ].*?encoding=["\'])(.+?)(["\'].*?\?>)/s', $data, $matches) === 1) {
26 libxml_clear_errors();
30 $data = iconv($enc, 'UTF-8//IGNORE', $data);
31 $data = preg_replace('/^<\?xml[\t\n\r ].*?\?>/s', $matches[1] . "UTF-8" . $matches[3] , $data);
33 $this->doc
= new DOMDocument();
34 $this->doc
->loadXML($data);
36 $error = libxml_get_last_error();
40 // some terrible invalid unicode entity?
41 if ($error && $error->code
== 9) {
42 libxml_clear_errors();
44 // we might want to try guessing input encoding here too
45 $data = iconv("UTF-8", "UTF-8//IGNORE", $data);
47 $this->doc
= new DOMDocument();
48 $this->doc
->loadXML($data);
50 $error = libxml_get_last_error();
53 $this->error
= $this->format_error($error);
54 libxml_clear_errors();
56 $this->items
= array();
60 $root = $this->doc
->firstChild
;
61 $xpath = new DOMXPath($this->doc
);
62 $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
63 $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
64 $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
65 $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
66 $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
67 $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
68 $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
70 $this->xpath
= $xpath;
72 $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)");
75 $root = $root->item(0);
78 switch (mb_strtolower($root->tagName
)) {
80 $this->type
= $this::FEED_RDF
;
83 $this->type
= $this::FEED_RSS
;
86 $this->type
= $this::FEED_ATOM
;
89 if( !isset($this->error
) ){
90 $this->error
= "Unknown/unsupported feed type";
96 switch ($this->type
) {
97 case $this::FEED_ATOM
:
99 $title = $xpath->query("//atom:feed/atom:title")->item(0);
102 $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
106 $this->title
= $title->nodeValue
;
109 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
112 $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
115 if ($link && $link->hasAttributes()) {
116 $this->link
= $link->getAttribute("href");
119 $articles = $xpath->query("//atom:entry");
121 if (!$articles ||
$articles->length
== 0)
122 $articles = $xpath->query("//atom03:entry");
124 foreach ($articles as $article) {
125 array_push($this->items
, new FeedItem_Atom($article, $this->doc
, $this->xpath
));
129 case $this::FEED_RSS
:
130 $title = $xpath->query("//channel/title")->item(0);
133 $this->title
= $title->nodeValue
;
136 $link = $xpath->query("//channel/link")->item(0);
139 if ($link->getAttribute("href"))
140 $this->link
= $link->getAttribute("href");
141 else if ($link->nodeValue
)
142 $this->link
= $link->nodeValue
;
145 $articles = $xpath->query("//channel/item");
147 foreach ($articles as $article) {
148 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
152 case $this::FEED_RDF
:
153 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
155 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
158 $this->title
= $title->nodeValue
;
161 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
164 $this->link
= $link->nodeValue
;
167 $articles = $xpath->query("//rssfake:item");
169 foreach ($articles as $article) {
170 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
177 if( !isset($this->error
) ){
178 $this->error
= "Unknown/unsupported feed type";
184 function format_error($error) {
186 return sprintf("LibXML error %s at line %d (column %d): %s",
187 $error->code
, $error->line
, $error->column
,
198 function get_link() {
202 function get_title() {
206 function get_items() {
210 function get_links($rel) {
213 switch ($this->type
) {
214 case $this::FEED_ATOM
:
215 $links = $this->xpath
->query("//atom:feed/atom:link");
217 foreach ($links as $link) {
218 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
219 array_push($rv, $link->getAttribute('href'));
223 case $this::FEED_RSS
:
224 $links = $this->xpath
->query("//atom:link");
226 foreach ($links as $link) {
227 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
228 array_push($rv, $link->getAttribute('href'));