FeedExpander.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. require_once(__DIR__ . '/BridgeInterface.php');
  3. abstract class FeedExpander extends BridgeAbstract {
  4. private $name;
  5. private $uri;
  6. private $description;
  7. public function collectExpandableDatas($url, $maxItems = -1){
  8. if(empty($url)){
  9. $this->returnServerError('There is no $url for this RSS expander');
  10. }
  11. $this->debugMessage('Loading from ' . $url);
  12. /* Notice we do not use cache here on purpose:
  13. * we want a fresh view of the RSS stream each time
  14. */
  15. $content = $this->getContents($url)
  16. or $this->returnServerError('Could not request ' . $url);
  17. $rssContent = simplexml_load_string($content);
  18. $this->debugMessage('Detecting feed format/version');
  19. if(isset($rssContent->channel[0])){
  20. $this->debugMessage('Detected RSS format');
  21. if(isset($rssContent->item[0])){
  22. $this->debugMessage('Detected RSS 1.0 format');
  23. $this->collect_RSS_1_0_data($rssContent, $maxItems);
  24. } else {
  25. $this->debugMessage('Detected RSS 0.9x or 2.0 format');
  26. $this->collect_RSS_2_0_data($rssContent, $maxItems);
  27. }
  28. } elseif(isset($rssContent->entry[0])){
  29. $this->debugMessage('Detected ATOM format');
  30. $this->collect_ATOM_data($rssContent, $maxItems);
  31. } else {
  32. $this->debugMessage('Unknown feed format/version');
  33. $this->returnServerError('The feed format is unknown!');
  34. }
  35. }
  36. protected function collect_RSS_1_0_data($rssContent, $maxItems){
  37. $this->load_RSS_2_0_feed_data($rssContent->channel[0]);
  38. foreach($rssContent->item as $item){
  39. $this->debugMessage('parsing item ' . var_export($item, true));
  40. $this->items[] = $this->parseItem($item);
  41. if($maxItems !== -1 && count($this->items) >= $maxItems) break;
  42. }
  43. }
  44. protected function collect_RSS_2_0_data($rssContent, $maxItems){
  45. $rssContent = $rssContent->channel[0];
  46. $this->debugMessage('RSS content is ===========\n'
  47. . var_export($rssContent, true)
  48. . '===========');
  49. $this->load_RSS_2_0_feed_data($rssContent);
  50. foreach($rssContent->item as $item){
  51. $this->debugMessage('parsing item ' . var_export($item, true));
  52. $this->items[] = $this->parseItem($item);
  53. if($maxItems !== -1 && count($this->items) >= $maxItems) break;
  54. }
  55. }
  56. protected function collect_ATOM_data($content, $maxItems){
  57. $this->load_ATOM_feed_data($content);
  58. foreach($content->entry as $item){
  59. $this->debugMessage('parsing item ' . var_export($item, true));
  60. $this->items[] = $this->parseItem($item);
  61. if($maxItems !== -1 && count($this->items) >= $maxItems) break;
  62. }
  63. }
  64. protected function RSS_2_0_time_to_timestamp($item){
  65. return DateTime::createFromFormat('D, d M Y H:i:s e', $item->pubDate)->getTimestamp();
  66. }
  67. // TODO set title, link, description, language, and so on
  68. protected function load_RSS_2_0_feed_data($rssContent){
  69. $this->name = trim($rssContent->title);
  70. $this->uri = trim($rssContent->link);
  71. $this->description = trim($rssContent->description);
  72. }
  73. protected function load_ATOM_feed_data($content){
  74. $this->name = $content->title;
  75. // Find best link (only one, or first of 'alternate')
  76. if(!isset($content->link)){
  77. $this->uri = '';
  78. } elseif (count($content->link) === 1){
  79. $this->uri = $content->link[0]['href'];
  80. } else {
  81. $this->uri = '';
  82. foreach($content->link as $link){
  83. if(strtolower($link['rel']) === 'alternate'){
  84. $this->uri = $link['href'];
  85. break;
  86. }
  87. }
  88. }
  89. if(isset($content->subtitle))
  90. $this->description = $content->subtitle;
  91. }
  92. protected function parseATOMItem($feedItem){
  93. $item = array();
  94. if(isset($feedItem->id)) $item['uri'] = $feedItem->id;
  95. if(isset($feedItem->title)) $item['title'] = $feedItem->title;
  96. if(isset($feedItem->updated)) $item['timestamp'] = strtotime($feedItem->updated);
  97. if(isset($feedItem->author)) $item['author'] = $feedItem->author->name;
  98. if(isset($feedItem->content)) $item['content'] = $feedItem->content;
  99. return $item;
  100. }
  101. protected function parseRSS_0_9_1_Item($feedItem){
  102. $item = array();
  103. if(isset($feedItem->link)) $item['uri'] = $feedItem->link;
  104. if(isset($feedItem->title)) $item['title'] = $feedItem->title;
  105. // rss 0.91 doesn't support timestamps
  106. // rss 0.91 doesn't support authors
  107. if(isset($feedItem->description)) $item['content'] = $feedItem->description;
  108. return $item;
  109. }
  110. protected function parseRSS_1_0_Item($feedItem){
  111. // 1.0 adds optional elements around the 0.91 standard
  112. $item = $this->parseRSS_0_9_1_Item($feedItem);
  113. $namespaces = $feedItem->getNamespaces(true);
  114. if(isset($namespaces['dc'])){
  115. $dc = $feedItem->children($namespaces['dc']);
  116. if(isset($dc->date)) $item['timestamp'] = strtotime($dc->date);
  117. if(isset($dc->creator)) $item['author'] = $dc->creator;
  118. }
  119. return $item;
  120. }
  121. protected function parseRSS_2_0_Item($feedItem){
  122. // Primary data is compatible to 0.91 with some additional data
  123. $item = $this->parseRSS_0_9_1_Item($feedItem);
  124. $namespaces = $feedItem->getNamespaces(true);
  125. if(isset($namespaces['dc'])) $dc = $feedItem->children($namespaces['dc']);
  126. if(isset($feedItem->pubDate)){
  127. $item['timestamp'] = strtotime($feedItem->pubDate);
  128. } elseif(isset($dc->date)){
  129. $item['timestamp'] = strtotime($dc->date);
  130. }
  131. if(isset($feedItem->author)){
  132. $item['author'] = $feedItem->author;
  133. } elseif(isset($dc->creator)){
  134. $item['author'] = $dc->creator;
  135. }
  136. return $item;
  137. }
  138. /**
  139. * Method should return, from a source RSS item given by lastRSS, one of our Items objects
  140. * @param $item the input rss item
  141. * @return a RSS-Bridge Item, with (hopefully) the whole content)
  142. */
  143. abstract protected function parseItem($item);
  144. public function getURI(){
  145. return $this->uri;
  146. }
  147. public function getName(){
  148. return $this->name;
  149. }
  150. public function getDescription(){
  151. return $this->description;
  152. }
  153. }