feedparser.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. class FeedParser {
  3. private $doc;
  4. private $error;
  5. private $libxml_errors = array();
  6. private $items;
  7. private $link;
  8. private $title;
  9. private $type;
  10. private $xpath;
  11. const FEED_RDF = 0;
  12. const FEED_RSS = 1;
  13. const FEED_ATOM = 2;
  14. function __construct($data) {
  15. libxml_use_internal_errors(true);
  16. libxml_clear_errors();
  17. $this->doc = new DOMDocument();
  18. $this->doc->loadXML($data);
  19. mb_substitute_character("none");
  20. $error = libxml_get_last_error();
  21. if ($error) {
  22. foreach (libxml_get_errors() as $error) {
  23. if ($error->level == LIBXML_ERR_FATAL) {
  24. if(!isset($this->error)) //currently only the first error is reported
  25. $this->error = $this->format_error($error);
  26. $this->libxml_errors [] = $this->format_error($error);
  27. }
  28. }
  29. }
  30. libxml_clear_errors();
  31. $this->items = array();
  32. }
  33. function init() {
  34. $root = $this->doc->firstChild;
  35. $xpath = new DOMXPath($this->doc);
  36. $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
  37. $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
  38. $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
  39. $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
  40. $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
  41. $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
  42. $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
  43. $xpath->registerNamespace('thread', 'http://purl.org/syndication/thread/1.0');
  44. $this->xpath = $xpath;
  45. $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)");
  46. if ($root && $root->length > 0) {
  47. $root = $root->item(0);
  48. if ($root) {
  49. switch (mb_strtolower($root->tagName)) {
  50. case "rdf:rdf":
  51. $this->type = $this::FEED_RDF;
  52. break;
  53. case "channel":
  54. $this->type = $this::FEED_RSS;
  55. break;
  56. case "feed":
  57. case "atom:feed":
  58. $this->type = $this::FEED_ATOM;
  59. break;
  60. default:
  61. if( !isset($this->error) ){
  62. $this->error = "Unknown/unsupported feed type";
  63. }
  64. return;
  65. }
  66. }
  67. switch ($this->type) {
  68. case $this::FEED_ATOM:
  69. $title = $xpath->query("//atom:feed/atom:title")->item(0);
  70. if (!$title)
  71. $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
  72. if ($title) {
  73. $this->title = $title->nodeValue;
  74. }
  75. $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
  76. if (!$link)
  77. $link = $xpath->query("//atom:feed/atom:link[@rel='alternate']")->item(0);
  78. if (!$link)
  79. $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
  80. if (!$link)
  81. $link = $xpath->query("//atom03:feed/atom03:link[@rel='alternate']")->item(0);
  82. if ($link && $link->hasAttributes()) {
  83. $this->link = $link->getAttribute("href");
  84. }
  85. $articles = $xpath->query("//atom:entry");
  86. if (!$articles || $articles->length == 0)
  87. $articles = $xpath->query("//atom03:entry");
  88. foreach ($articles as $article) {
  89. array_push($this->items, new FeedItem_Atom($article, $this->doc, $this->xpath));
  90. }
  91. break;
  92. case $this::FEED_RSS:
  93. $title = $xpath->query("//channel/title")->item(0);
  94. if ($title) {
  95. $this->title = $title->nodeValue;
  96. }
  97. $link = $xpath->query("//channel/link")->item(0);
  98. if ($link) {
  99. if ($link->getAttribute("href"))
  100. $this->link = $link->getAttribute("href");
  101. else if ($link->nodeValue)
  102. $this->link = $link->nodeValue;
  103. }
  104. $articles = $xpath->query("//channel/item");
  105. foreach ($articles as $article) {
  106. array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
  107. }
  108. break;
  109. case $this::FEED_RDF:
  110. $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
  111. $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
  112. if ($title) {
  113. $this->title = $title->nodeValue;
  114. }
  115. $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
  116. if ($link) {
  117. $this->link = $link->nodeValue;
  118. }
  119. $articles = $xpath->query("//rssfake:item");
  120. foreach ($articles as $article) {
  121. array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
  122. }
  123. break;
  124. }
  125. if ($this->title) $this->title = trim($this->title);
  126. if ($this->link) $this->link = trim($this->link);
  127. } else {
  128. if( !isset($this->error) ){
  129. $this->error = "Unknown/unsupported feed type";
  130. }
  131. return;
  132. }
  133. }
  134. function format_error($error) {
  135. if ($error) {
  136. return sprintf("LibXML error %s at line %d (column %d): %s",
  137. $error->code, $error->line, $error->column,
  138. $error->message);
  139. } else {
  140. return "";
  141. }
  142. }
  143. // libxml may have invalid unicode data in error messages
  144. function error() {
  145. return UConverter::transcode($this->error, 'UTF-8', 'UTF-8');
  146. }
  147. // WARNING: may return invalid unicode data
  148. function errors() {
  149. return $this->libxml_errors;
  150. }
  151. function get_link() {
  152. return clean($this->link);
  153. }
  154. function get_title() {
  155. return clean($this->title);
  156. }
  157. function get_items() {
  158. return $this->items;
  159. }
  160. function get_links($rel) {
  161. $rv = array();
  162. switch ($this->type) {
  163. case $this::FEED_ATOM:
  164. $links = $this->xpath->query("//atom:feed/atom:link");
  165. foreach ($links as $link) {
  166. if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
  167. array_push($rv, clean(trim($link->getAttribute('href'))));
  168. }
  169. }
  170. break;
  171. case $this::FEED_RSS:
  172. $links = $this->xpath->query("//atom:link");
  173. foreach ($links as $link) {
  174. if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
  175. array_push($rv, clean(trim($link->getAttribute('href'))));
  176. }
  177. }
  178. break;
  179. }
  180. return $rv;
  181. }
  182. }