UniversalFeedCreator.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * UniversalFeedCreator lets you choose during runtime which
  4. * format to build.
  5. * For general usage of a feed class, see the FeedCreator class
  6. * below or the example above.
  7. *
  8. * @since 1.3
  9. * @author Kai Blankenhorn <kaib@bitfolge.de>
  10. */
  11. class UniversalFeedCreator extends FeedCreator
  12. {
  13. /** @var FeedCreator */
  14. protected $_feed;
  15. /**
  16. * @param string $format
  17. */
  18. protected function _setFormat($format)
  19. {
  20. switch (strtoupper($format)) {
  21. case "BASE":
  22. $this->format = $format;
  23. case "2.0":
  24. // fall through
  25. case "RSS2.0":
  26. $this->_feed = new RSSCreator20();
  27. break;
  28. case "GEOPHOTORSS":
  29. case "PHOTORSS":
  30. case "GEORSS":
  31. $this->format = $format;
  32. case "1.0":
  33. // fall through
  34. case "RSS1.0":
  35. $this->_feed = new RSSCreator10();
  36. break;
  37. case "0.91":
  38. // fall through
  39. case "RSS0.91":
  40. $this->_feed = new RSSCreator091();
  41. break;
  42. case "PIE0.1":
  43. $this->_feed = new PIECreator01();
  44. break;
  45. case "MBOX":
  46. $this->_feed = new MBOXCreator();
  47. break;
  48. case "OPML":
  49. $this->_feed = new OPMLCreator();
  50. break;
  51. case "TOOLBAR":
  52. $this->format = $format;
  53. case "ATOM":
  54. // fall through: always the latest ATOM version
  55. case "ATOM1.0":
  56. $this->_feed = new AtomCreator10();
  57. break;
  58. case "ATOM0.3":
  59. $this->_feed = new AtomCreator03();
  60. break;
  61. case "HTML":
  62. $this->_feed = new HTMLCreator();
  63. break;
  64. case "PHP":
  65. $this->_feed = new PHPCreator();
  66. break;
  67. case "GPX":
  68. $this->_feed = new GPXCreator();
  69. break;
  70. case "KML":
  71. $this->_feed = new KMLCreator();
  72. break;
  73. case "JS":
  74. // fall through
  75. case "JAVASCRIPT":
  76. $this->_feed = new JSCreator();
  77. break;
  78. default:
  79. $this->_feed = new RSSCreator091();
  80. break;
  81. }
  82. $vars = get_object_vars($this);
  83. foreach ($vars as $key => $value) {
  84. // prevent overwriting of properties "contentType", "encoding"; do not copy "_feed" itself
  85. if (!in_array($key, array("_feed", "contentType", "encoding"))) {
  86. $this->_feed->{$key} = $this->{$key};
  87. }
  88. }
  89. }
  90. /**
  91. * Creates a syndication feed based on the items previously added.
  92. *
  93. * @see FeedCreator::addItem()
  94. * @param string $format format the feed should comply to. Valid values are:
  95. * "PIE0.1", "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3", "HTML", "JS"
  96. * @return string the contents of the feed.
  97. */
  98. public function createFeed($format = "RSS0.91")
  99. {
  100. $this->_setFormat($format);
  101. return $this->_feed->createFeed();
  102. }
  103. /**
  104. * Saves this feed as a file on the local disk. After the file is saved, an HTTP redirect
  105. * header may be sent to redirect the use to the newly created file.
  106. *
  107. * @since 1.4
  108. * @param string $format format the feed should comply to. Valid values are:
  109. * "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM",
  110. * "ATOM0.3", "HTML", "JS"
  111. * @param string $filename optional the filename where a recent version of the feed is saved. If not
  112. * specified, the filename is $_SERVER["SCRIPT_NAME"] with the extension changed to
  113. * .xml (see _generateFilename()).
  114. * @param boolean $displayContents optional send the content of the file or not. If true, the file will be sent
  115. * in the body of the response.
  116. */
  117. public function saveFeed($format = "RSS0.91", $filename = "", $displayContents = true)
  118. {
  119. $this->_setFormat($format);
  120. $this->_feed->saveFeed($filename, $displayContents);
  121. }
  122. /**
  123. * Turns on caching and checks if there is a recent version of this feed in the cache.
  124. * If there is, an HTTP redirect header is sent.
  125. * To effectively use caching, you should create the FeedCreator object and call this method
  126. * before anything else, especially before you do the time consuming task to build the feed
  127. * (web fetching, for example).
  128. *
  129. * @param string $format format the feed should comply to. Valid values are:
  130. * "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3".
  131. * @param string $filename optional the filename where a recent version of the feed is saved. If not specified, the
  132. * filename is $_SERVER["SCRIPT_NAME"] with the extension changed to .xml (see
  133. * _generateFilename()).
  134. * @param int $timeout optional the timeout in seconds before a cached version is refreshed (defaults to 3600 =
  135. * 1 hour)
  136. */
  137. public function useCached($format = "RSS0.91", $filename = "", $timeout = 3600)
  138. {
  139. $this->_setFormat($format);
  140. $this->_feed->useCached($filename, $timeout);
  141. }
  142. }