]>
git.wh0rd.org - tt-rss.git/blob - classes/feedparser.php
5 private $libxml_errors = array();
16 function __construct($data) {
17 libxml_use_internal_errors(true);
18 libxml_clear_errors();
19 $this->doc
= new DOMDocument();
20 $this->doc
->loadXML($data);
22 mb_substitute_character("none");
24 $error = libxml_get_last_error();
26 // libxml compiled without iconv?
27 if ($error && $error->code
== 32) {
28 if (preg_match('/^(<\?xml[\t\n\r ].*?encoding[\t\n\r ]*=[\t\n\r ]*["\'])(.+?)(["\'].*?\?>)/s', $data, $matches) === 1) {
29 $data = mb_convert_encoding($data, 'UTF-8', $matches[2]);
31 $data = preg_replace('/^<\?xml[\t\n\r ].*?\?>/s', $matches[1] . "UTF-8" . $matches[3] , $data);
34 libxml_clear_errors();
36 $this->doc
= new DOMDocument();
37 $this->doc
->loadXML($data);
39 $error = libxml_get_last_error();
44 // some terrible invalid unicode entity?
46 foreach (libxml_get_errors() as $err) {
47 if ($err->code
== 9) {
48 // remove dangling bytes
49 $data = mb_convert_encoding($data, 'UTF-8', 'UTF-8');
51 // apparently not all UTF-8 characters are valid for XML
52 $data = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $data);
55 libxml_clear_errors();
57 $this->doc
= new DOMDocument();
58 $this->doc
->loadXML($data);
60 $error = libxml_get_last_error();
68 foreach (libxml_get_errors() as $error) {
69 if ($error->level
== LIBXML_ERR_FATAL
) {
70 if(!isset($this->error
)) //currently only the first error is reported
71 $this->error
= $this->format_error($error);
72 $this->libxml_errors
[] = $this->format_error($error);
76 libxml_clear_errors();
78 $this->items
= array();
82 $root = $this->doc
->firstChild
;
83 $xpath = new DOMXPath($this->doc
);
84 $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
85 $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
86 $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
87 $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
88 $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
89 $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
90 $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
92 $this->xpath
= $xpath;
94 $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)");
97 $root = $root->item(0);
100 switch (mb_strtolower($root->tagName
)) {
102 $this->type
= $this::FEED_RDF
;
105 $this->type
= $this::FEED_RSS
;
108 $this->type
= $this::FEED_ATOM
;
111 if( !isset($this->error
) ){
112 $this->error
= "Unknown/unsupported feed type";
118 switch ($this->type
) {
119 case $this::FEED_ATOM
:
121 $title = $xpath->query("//atom:feed/atom:title")->item(0);
124 $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
128 $this->title
= $title->nodeValue
;
131 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
134 $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
137 if ($link && $link->hasAttributes()) {
138 $this->link
= $link->getAttribute("href");
141 $articles = $xpath->query("//atom:entry");
143 if (!$articles ||
$articles->length
== 0)
144 $articles = $xpath->query("//atom03:entry");
146 foreach ($articles as $article) {
147 array_push($this->items
, new FeedItem_Atom($article, $this->doc
, $this->xpath
));
151 case $this::FEED_RSS
:
152 $title = $xpath->query("//channel/title")->item(0);
155 $this->title
= $title->nodeValue
;
158 $link = $xpath->query("//channel/link")->item(0);
161 if ($link->getAttribute("href"))
162 $this->link
= $link->getAttribute("href");
163 else if ($link->nodeValue
)
164 $this->link
= $link->nodeValue
;
167 $articles = $xpath->query("//channel/item");
169 foreach ($articles as $article) {
170 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
174 case $this::FEED_RDF
:
175 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
177 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
180 $this->title
= $title->nodeValue
;
183 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
186 $this->link
= $link->nodeValue
;
189 $articles = $xpath->query("//rssfake:item");
191 foreach ($articles as $article) {
192 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
199 if( !isset($this->error
) ){
200 $this->error
= "Unknown/unsupported feed type";
206 function format_error($error) {
208 return sprintf("LibXML error %s at line %d (column %d): %s",
209 $error->code
, $error->line
, $error->column
,
221 return $this->libxml_errors
;
224 function get_link() {
228 function get_title() {
232 function get_items() {
236 function get_links($rel) {
239 switch ($this->type
) {
240 case $this::FEED_ATOM
:
241 $links = $this->xpath
->query("//atom:feed/atom:link");
243 foreach ($links as $link) {
244 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
245 array_push($rv, $link->getAttribute('href'));
249 case $this::FEED_RSS
:
250 $links = $this->xpath
->query("//atom:link");
252 foreach ($links as $link) {
253 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
254 array_push($rv, $link->getAttribute('href'));