]>
git.wh0rd.org - tt-rss.git/blob - classes/feedparser.php
07de73a9d5927d338adc7d03754a044470ce60fb
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();
68 foreach(libxml_get_errors() as $error) {
69 if($error->level
== LIBXML_ERR_FATAL
) {
70 $this->error
= $this->format_error($error);
71 break; //break here because currently we only show one error
75 libxml_clear_errors();
77 $this->items
= array();
81 $root = $this->doc
->firstChild
;
82 $xpath = new DOMXPath($this->doc
);
83 $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
84 $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
85 $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
86 $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
87 $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
88 $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
89 $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
91 $this->xpath
= $xpath;
93 $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)");
96 $root = $root->item(0);
99 switch (mb_strtolower($root->tagName
)) {
101 $this->type
= $this::FEED_RDF
;
104 $this->type
= $this::FEED_RSS
;
107 $this->type
= $this::FEED_ATOM
;
110 if( !isset($this->error
) ){
111 $this->error
= "Unknown/unsupported feed type";
117 switch ($this->type
) {
118 case $this::FEED_ATOM
:
120 $title = $xpath->query("//atom:feed/atom:title")->item(0);
123 $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
127 $this->title
= $title->nodeValue
;
130 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
133 $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
136 if ($link && $link->hasAttributes()) {
137 $this->link
= $link->getAttribute("href");
140 $articles = $xpath->query("//atom:entry");
142 if (!$articles ||
$articles->length
== 0)
143 $articles = $xpath->query("//atom03:entry");
145 foreach ($articles as $article) {
146 array_push($this->items
, new FeedItem_Atom($article, $this->doc
, $this->xpath
));
150 case $this::FEED_RSS
:
151 $title = $xpath->query("//channel/title")->item(0);
154 $this->title
= $title->nodeValue
;
157 $link = $xpath->query("//channel/link")->item(0);
160 if ($link->getAttribute("href"))
161 $this->link
= $link->getAttribute("href");
162 else if ($link->nodeValue
)
163 $this->link
= $link->nodeValue
;
166 $articles = $xpath->query("//channel/item");
168 foreach ($articles as $article) {
169 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
173 case $this::FEED_RDF
:
174 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
176 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
179 $this->title
= $title->nodeValue
;
182 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
185 $this->link
= $link->nodeValue
;
188 $articles = $xpath->query("//rssfake:item");
190 foreach ($articles as $article) {
191 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
198 if( !isset($this->error
) ){
199 $this->error
= "Unknown/unsupported feed type";
205 function format_error($error) {
207 return sprintf("LibXML error %s at line %d (column %d): %s",
208 $error->code
, $error->line
, $error->column
,
219 function get_link() {
223 function get_title() {
227 function get_items() {
231 function get_links($rel) {
234 switch ($this->type
) {
235 case $this::FEED_ATOM
:
236 $links = $this->xpath
->query("//atom:feed/atom:link");
238 foreach ($links as $link) {
239 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
240 array_push($rv, $link->getAttribute('href'));
244 case $this::FEED_RSS
:
245 $links = $this->xpath
->query("//atom:link");
247 foreach ($links as $link) {
248 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
249 array_push($rv, $link->getAttribute('href'));