]>
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 mb_substitute_character("none");
23 $error = libxml_get_last_error();
25 // libxml compiled without iconv?
26 if ($error && ($error->code
== 32 ||
$error->code
== 9)) {
27 if (preg_match('/^(<\?xml[\t\n\r ].*?encoding=["\'])(.+?)(["\'].*?\?>)/s', $data, $matches) === 1) {
30 $data = mb_convert_encoding($data, 'UTF-8', $enc);
32 $data = preg_replace('/^<\?xml[\t\n\r ].*?\?>/s', $matches[1] . "UTF-8" . $matches[3] , $data);
35 // apparently not all UTF-8 characters are valid for XML
36 $data = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $data);
39 libxml_clear_errors();
41 $this->doc
= new DOMDocument();
42 $this->doc
->loadXML($data);
44 $error = libxml_get_last_error();
49 // some terrible invalid unicode entity?
50 if ($error && $error->code
== 9) {
51 $data = mb_convert_encoding($data, 'UTF-8', 'UTF-8');
54 libxml_clear_errors();
56 $this->doc
= new DOMDocument();
57 $this->doc
->loadXML($data);
59 $error = libxml_get_last_error();
63 $this->error
= $this->format_error($error);
64 libxml_clear_errors();
66 $this->items
= array();
70 $root = $this->doc
->firstChild
;
71 $xpath = new DOMXPath($this->doc
);
72 $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
73 $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
74 $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
75 $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
76 $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
77 $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
78 $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
80 $this->xpath
= $xpath;
82 $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)");
85 $root = $root->item(0);
88 switch (mb_strtolower($root->tagName
)) {
90 $this->type
= $this::FEED_RDF
;
93 $this->type
= $this::FEED_RSS
;
96 $this->type
= $this::FEED_ATOM
;
99 if( !isset($this->error
) ){
100 $this->error
= "Unknown/unsupported feed type";
106 switch ($this->type
) {
107 case $this::FEED_ATOM
:
109 $title = $xpath->query("//atom:feed/atom:title")->item(0);
112 $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
116 $this->title
= $title->nodeValue
;
119 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
122 $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
125 if ($link && $link->hasAttributes()) {
126 $this->link
= $link->getAttribute("href");
129 $articles = $xpath->query("//atom:entry");
131 if (!$articles ||
$articles->length
== 0)
132 $articles = $xpath->query("//atom03:entry");
134 foreach ($articles as $article) {
135 array_push($this->items
, new FeedItem_Atom($article, $this->doc
, $this->xpath
));
139 case $this::FEED_RSS
:
140 $title = $xpath->query("//channel/title")->item(0);
143 $this->title
= $title->nodeValue
;
146 $link = $xpath->query("//channel/link")->item(0);
149 if ($link->getAttribute("href"))
150 $this->link
= $link->getAttribute("href");
151 else if ($link->nodeValue
)
152 $this->link
= $link->nodeValue
;
155 $articles = $xpath->query("//channel/item");
157 foreach ($articles as $article) {
158 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
162 case $this::FEED_RDF
:
163 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
165 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
168 $this->title
= $title->nodeValue
;
171 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
174 $this->link
= $link->nodeValue
;
177 $articles = $xpath->query("//rssfake:item");
179 foreach ($articles as $article) {
180 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
187 if( !isset($this->error
) ){
188 $this->error
= "Unknown/unsupported feed type";
194 function format_error($error) {
196 return sprintf("LibXML error %s at line %d (column %d): %s",
197 $error->code
, $error->line
, $error->column
,
208 function get_link() {
212 function get_title() {
216 function get_items() {
220 function get_links($rel) {
223 switch ($this->type
) {
224 case $this::FEED_ATOM
:
225 $links = $this->xpath
->query("//atom:feed/atom:link");
227 foreach ($links as $link) {
228 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
229 array_push($rv, $link->getAttribute('href'));
233 case $this::FEED_RSS
:
234 $links = $this->xpath
->query("//atom:link");
236 foreach ($links as $link) {
237 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
238 array_push($rv, $link->getAttribute('href'));