]>
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) {
27 if (preg_match('/^(<\?xml[\t\n\r ].*?encoding[\t\n\r ]*=[\t\n\r ]*["\'])(.+?)(["\'].*?\?>)/s', $data, $matches) === 1) {
28 $data = mb_convert_encoding($data, 'UTF-8', $matches[2]);
30 $data = preg_replace('/^<\?xml[\t\n\r ].*?\?>/s', $matches[1] . "UTF-8" . $matches[3] , $data);
33 libxml_clear_errors();
35 $this->doc
= new DOMDocument();
36 $this->doc
->loadXML($data);
38 $error = libxml_get_last_error();
43 // some terrible invalid unicode entity?
45 foreach(libxml_get_errors() as $err) {
46 if ($err->code
== 9) {
47 // remove dangling bytes
48 $data = mb_convert_encoding($data, 'UTF-8', 'UTF-8');
50 // apparently not all UTF-8 characters are valid for XML
51 $data = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $data);
54 libxml_clear_errors();
56 $this->doc
= new DOMDocument();
57 $this->doc
->loadXML($data);
59 $error = libxml_get_last_error();
66 $this->error
= $this->format_error($error);
67 libxml_clear_errors();
69 $this->items
= array();
73 $root = $this->doc
->firstChild
;
74 $xpath = new DOMXPath($this->doc
);
75 $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
76 $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
77 $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
78 $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
79 $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
80 $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
81 $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
83 $this->xpath
= $xpath;
85 $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)");
88 $root = $root->item(0);
91 switch (mb_strtolower($root->tagName
)) {
93 $this->type
= $this::FEED_RDF
;
96 $this->type
= $this::FEED_RSS
;
99 $this->type
= $this::FEED_ATOM
;
102 if( !isset($this->error
) ){
103 $this->error
= "Unknown/unsupported feed type";
109 switch ($this->type
) {
110 case $this::FEED_ATOM
:
112 $title = $xpath->query("//atom:feed/atom:title")->item(0);
115 $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
119 $this->title
= $title->nodeValue
;
122 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
125 $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
128 if ($link && $link->hasAttributes()) {
129 $this->link
= $link->getAttribute("href");
132 $articles = $xpath->query("//atom:entry");
134 if (!$articles ||
$articles->length
== 0)
135 $articles = $xpath->query("//atom03:entry");
137 foreach ($articles as $article) {
138 array_push($this->items
, new FeedItem_Atom($article, $this->doc
, $this->xpath
));
142 case $this::FEED_RSS
:
143 $title = $xpath->query("//channel/title")->item(0);
146 $this->title
= $title->nodeValue
;
149 $link = $xpath->query("//channel/link")->item(0);
152 if ($link->getAttribute("href"))
153 $this->link
= $link->getAttribute("href");
154 else if ($link->nodeValue
)
155 $this->link
= $link->nodeValue
;
158 $articles = $xpath->query("//channel/item");
160 foreach ($articles as $article) {
161 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
165 case $this::FEED_RDF
:
166 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
168 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
171 $this->title
= $title->nodeValue
;
174 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
177 $this->link
= $link->nodeValue
;
180 $articles = $xpath->query("//rssfake:item");
182 foreach ($articles as $article) {
183 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
190 if( !isset($this->error
) ){
191 $this->error
= "Unknown/unsupported feed type";
197 function format_error($error) {
199 return sprintf("LibXML error %s at line %d (column %d): %s",
200 $error->code
, $error->line
, $error->column
,
211 function get_link() {
215 function get_title() {
219 function get_items() {
223 function get_links($rel) {
226 switch ($this->type
) {
227 case $this::FEED_ATOM
:
228 $links = $this->xpath
->query("//atom:feed/atom:link");
230 foreach ($links as $link) {
231 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
232 array_push($rv, $link->getAttribute('href'));
236 case $this::FEED_RSS
:
237 $links = $this->xpath
->query("//atom:link");
239 foreach ($links as $link) {
240 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
241 array_push($rv, $link->getAttribute('href'));