]>
git.wh0rd.org - tt-rss.git/blob - classes/feedparser.php
efef3bca41a0914c01b5f1c85e3588840ee95d2b
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) {
18 $data = mb_convert_encoding($data, 'UTF-8', $matches[2]);
20 $data = preg_replace('/^<\?xml[\t\n\r ].*?\?>/s', $matches[1] . "UTF-8" . $matches[3] , $data);
26 function __construct($data) {
27 libxml_use_internal_errors(true);
28 libxml_clear_errors();
29 $this->doc
= new DOMDocument();
30 $this->doc
->preserveWhiteSpace
= false;
31 $this->doc
->loadXML($data);
33 mb_substitute_character("none");
35 $error = libxml_get_last_error();
37 // libxml compiled without iconv?
38 if ($error && $error->code
== 32) {
39 $data = $this->normalize_encoding($data);
42 libxml_clear_errors();
44 $this->doc
= new DOMDocument();
45 $this->doc
->preserveWhiteSpace
= false;
46 $this->doc
->loadXML($data);
48 $error = libxml_get_last_error();
52 // some terrible invalid unicode entity?
54 foreach (libxml_get_errors() as $err) {
55 if ($err->code
== 9) {
56 // if the source feed is not in utf8, next conversion will fail
57 $data = $this->normalize_encoding($data);
59 // remove dangling bytes
60 $data = mb_convert_encoding($data, 'UTF-8', 'UTF-8');
62 // apparently not all UTF-8 characters are valid for XML
63 $data = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $data);
66 libxml_clear_errors();
68 $this->doc
= new DOMDocument();
69 $this->doc
->preserveWhiteSpace
= false;
70 $this->doc
->loadXML($data);
72 $error = libxml_get_last_error();
80 foreach (libxml_get_errors() as $error) {
81 if ($error->level
== LIBXML_ERR_FATAL
) {
82 if(!isset($this->error
)) //currently only the first error is reported
83 $this->error
= $this->format_error($error);
84 $this->libxml_errors
[] = $this->format_error($error);
88 libxml_clear_errors();
90 $this->items
= array();
94 $root = $this->doc
->firstChild
;
95 $xpath = new DOMXPath($this->doc
);
96 $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
97 $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
98 $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
99 $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
100 $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
101 $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
102 $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
103 $xpath->registerNamespace('thread', 'http://purl.org/syndication/thread/1.0');
105 $this->xpath
= $xpath;
107 $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)");
109 if ($root && $root->length
> 0) {
110 $root = $root->item(0);
113 switch (mb_strtolower($root->tagName
)) {
115 $this->type
= $this::FEED_RDF
;
118 $this->type
= $this::FEED_RSS
;
121 $this->type
= $this::FEED_ATOM
;
124 if( !isset($this->error
) ){
125 $this->error
= "Unknown/unsupported feed type";
131 switch ($this->type
) {
132 case $this::FEED_ATOM
:
134 $title = $xpath->query("//atom:feed/atom:title")->item(0);
137 $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
141 $this->title
= $title->nodeValue
;
144 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
147 $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
150 if ($link && $link->hasAttributes()) {
151 $this->link
= $link->getAttribute("href");
154 $articles = $xpath->query("//atom:entry");
156 if (!$articles ||
$articles->length
== 0)
157 $articles = $xpath->query("//atom03:entry");
159 foreach ($articles as $article) {
160 array_push($this->items
, new FeedItem_Atom($article, $this->doc
, $this->xpath
));
164 case $this::FEED_RSS
:
165 $title = $xpath->query("//channel/title")->item(0);
168 $this->title
= $title->nodeValue
;
171 $link = $xpath->query("//channel/link")->item(0);
174 if ($link->getAttribute("href"))
175 $this->link
= $link->getAttribute("href");
176 else if ($link->nodeValue
)
177 $this->link
= $link->nodeValue
;
180 $articles = $xpath->query("//channel/item");
182 foreach ($articles as $article) {
183 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
187 case $this::FEED_RDF
:
188 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
190 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
193 $this->title
= $title->nodeValue
;
196 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
199 $this->link
= $link->nodeValue
;
202 $articles = $xpath->query("//rssfake:item");
204 foreach ($articles as $article) {
205 array_push($this->items
, new FeedItem_RSS($article, $this->doc
, $this->xpath
));
212 if( !isset($this->error
) ){
213 $this->error
= "Unknown/unsupported feed type";
219 function format_error($error) {
221 return sprintf("LibXML error %s at line %d (column %d): %s",
222 $error->code
, $error->line
, $error->column
,
234 return $this->libxml_errors
;
237 function get_link() {
241 function get_title() {
245 function get_items() {
249 function get_links($rel) {
252 switch ($this->type
) {
253 case $this::FEED_ATOM
:
254 $links = $this->xpath
->query("//atom:feed/atom:link");
256 foreach ($links as $link) {
257 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
258 array_push($rv, $link->getAttribute('href'));
262 case $this::FEED_RSS
:
263 $links = $this->xpath
->query("//atom:link");
265 foreach ($links as $link) {
266 if (!$rel ||
$link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
267 array_push($rv, $link->getAttribute('href'));