]>
git.wh0rd.org - tt-rss.git/blob - classes/feedparser.php
5 private $libxml_errors = array();
16 function normalize_encoding($data) {
17 if (preg_match('/^(<\?xml[\t\n\r ].*?encoding[\t\n\r ]*=[\t\n\r ]*["\'])(.+?)(["\'].*?\?>)/s', $data, $matches) === 1) {
19 $encoding = strtolower($matches[2]);
21 if (in_array($encoding, array_map('strtolower', mb_list_encodings())))
22 $data = mb_convert_encoding($data, 'UTF-8', $encoding);
24 $data = preg_replace('/^<\?xml[\t\n\r ].*?\?>/s', $matches[1] . "UTF-8" . $matches[3] , $data);
30 function __construct($data) {
31 libxml_use_internal_errors(true);
32 libxml_clear_errors();
33 $this->doc
= new DOMDocument();
34 $this->doc
->loadXML($data);
36 mb_substitute_character("none");
38 $error = libxml_get_last_error();
40 // libxml compiled without iconv?
41 if ($error && $error->code
== 32) {
42 $data = $this->normalize_encoding($data);
45 libxml_clear_errors();
47 $this->doc
= new DOMDocument();
48 $this->doc
->loadXML($data);
50 $error = libxml_get_last_error();
54 // some terrible invalid unicode entity?
56 foreach (libxml_get_errors() as $err) {
57 if ($err->code
== 9) {
58 // if the source feed is not in utf8, next conversion will fail
59 $data = $this->normalize_encoding($data);
61 // remove dangling bytes
62 $data = mb_convert_encoding($data, 'UTF-8', 'UTF-8');
64 // apparently not all UTF-8 characters are valid for XML
65 $data = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $data);
68 libxml_clear_errors();
70 $this->doc
= new DOMDocument();
71 $this->doc
->loadXML($data);
73 $error = libxml_get_last_error();
81 foreach (libxml_get_errors() as $error) {
82 if ($error->level
== LIBXML_ERR_FATAL
) {
83 if(!isset($this->error
)) //currently only the first error is reported
84 $this->error
= $this->format_error($error);
85 $this->libxml_errors
[] = $this->format_error($error);
89 libxml_clear_errors();
91 $this->items
= array();
95 $root = $this->doc
->firstChild
;
96 $xpath = new DOMXPath($this->doc
);
97 $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
98 $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
99 $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
100 $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
101 $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
102 $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
103 $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
104 $xpath->registerNamespace('thread', 'http://purl.org/syndication/thread/1.0');
106 $this->xpath
= $xpath;
108 $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)");
110 if ($root && $root->length
> 0) {
111 $root = $root->item(0);
114 switch (mb_strtolower($root->tagName
)) {
116 $this->type
= $this::FEED_RDF
;
119 $this->type
= $this::FEED_RSS
;
123 $this->type
= $this::FEED_ATOM
;
126 if( !isset($this->error
) ){
127 $this->error
= "Unknown/unsupported feed type";
133 switch ($this->type
) {
134 case $this::FEED_ATOM
:
136 $title = $xpath->query("//atom:feed/atom:title")->item(0);
139 $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
143 $this->title
= $title->nodeValue
;
146 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
149 $link = $xpath->query("//atom:feed/atom:link[@rel='alternate']")->item(0);
152 $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
155 $link = $xpath->query("//atom03:feed/atom03:link[@rel='alternate']")->item(0);
157 if ($link && $link->hasAttributes()) {
158 $this->link
= $link->getAttribute("href");
161 $articles = $xpath->query("//atom:entry");
163 if (!$articles ||
$articles->length
== 0)
164 $articles = $xpath->query("//atom03:entry");
166 foreach ($articles as $article) {
167 array_push($this->items
, new FeedItem_Atom($article, $this->doc
, $this->xpath
));
171 case $this::FEED_RSS
:
172 $title = $xpath->query("//channel/title")->item(0);
175 $this->title
= $title->nodeValue
;
178 $link = $xpath->query("//channel/link")->item(0);
181 if ($link->getAttribute("href"))
182 $this->link
= $link->getAttribute("href");
183 else if ($link->nodeValue
)
184 $this->link
= $link->nodeValue
;
187 $articles = $xpath->query("//channel/item");
189 foreach ($articles as $article) {
190 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
194 case $this::FEED_RDF
:
195 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
197 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
200 $this->title
= $title->nodeValue
;
203 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
206 $this->link
= $link->nodeValue
;
209 $articles = $xpath->query("//rssfake:item");
211 foreach ($articles as $article) {
212 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
219 if ($this->title
) $this->title
= trim($this->title
);
220 if ($this->link
) $this->link
= trim($this->link
);
223 if( !isset($this->error
) ){
224 $this->error
= "Unknown/unsupported feed type";
230 function format_error($error) {
232 return sprintf("LibXML error %s at line %d (column %d): %s",
233 $error->code
, $error->line
, $error->column
,
245 return $this->libxml_errors
;
248 function get_link() {
252 function get_title() {
256 function get_items() {
260 function get_links($rel) {
263 switch ($this->type
) {
264 case $this::FEED_ATOM
:
265 $links = $this->xpath
->query("//atom:feed/atom:link");
267 foreach ($links as $link) {
268 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
269 array_push($rv, trim($link->getAttribute('href')));
273 case $this::FEED_RSS
:
274 $links = $this->xpath
->query("//atom:link");
276 foreach ($links as $link) {
277 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
278 array_push($rv, trim($link->getAttribute('href')));