RssExpander.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * A class providing facilities for RSS expansion. The goal here is to facilitate, as much as possible, writing bridges such as FreeNews, Gawker and other ones
  4. * @name RssExpander
  5. * @description Un bridge générique d'expansion automatique de contenu RSS ... pour tous ces sites qui ont un flux RSS mochement tonqué.
  6. * @update 15/03/2015
  7. * @use1(url="URL du flux dont vous souhaitez le contenu complet")
  8. */
  9. abstract class RssExpander extends HttpCachingBridgeAbstract{
  10. protected $name;
  11. private $uri;
  12. private $description;
  13. public function collectData(array $param){
  14. if (empty($param['url'])) {
  15. $this->returnError('There is no $param[\'url\'] for this RSS expander', 404);
  16. }
  17. // $this->message("Loading from ".$param['url']);
  18. // Notice WE DO NOT use cache here on purpose : we want a fresh view of the RSS stream each time
  19. $rssContent = simplexml_load_file($param['url']) or $this->returnError('Could not request '.$param['url'], 404);
  20. // $this->message("loaded RSS from ".$param['url']);
  21. // TODO insert RSS format detection
  22. // we suppose for now, we have some RSS 2.0
  23. $this->collect_RSS_2_0_data($rssContent);
  24. }
  25. private function collect_RSS_2_0_data($rssContent) {
  26. $rssContent = $rssContent->channel[0];
  27. // $this->message("RSS content is ===========\n".var_export($rssContent, true)."===========");
  28. $this->load_RSS_2_0_feed_data($rssContent);
  29. foreach($rssContent->item as $item) {
  30. // $this->message("parsing item ".var_export($item, true));
  31. $this->items[] = $this->parseRSSItem($item);
  32. }
  33. }
  34. protected function RSS_2_0_time_to_timestamp($item) {
  35. return DateTime::createFromFormat('D, d M Y H:i:s e', $item->pubDate)->getTimestamp();
  36. }
  37. // TODO set title, link, description, language, and so on
  38. protected function load_RSS_2_0_feed_data($rssContent) {
  39. $this->name = trim($rssContent->title);
  40. $this->uri = trim($rssContent->link);
  41. $this->description = trim($rssContent->description);
  42. }
  43. /**
  44. * Method should return, from a source RSS item given by lastRSS, one of our Items objects
  45. * @param $item the input rss item
  46. * @return a RSS-Bridge Item, with (hopefully) the whole content)
  47. */
  48. abstract protected function parseRSSItem($item);
  49. public function getName(){
  50. return $this->name;
  51. }
  52. public function getURI(){
  53. return $this->uri;
  54. }
  55. public function getDescription() {
  56. return $this->description;
  57. }
  58. }