RSSFeed.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright © 2004 Brion Vibber <brion@pobox.com>
  4. * https://www.mediawiki.org/
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. * http://www.gnu.org/copyleft/gpl.html
  20. *
  21. * @file
  22. */
  23. /**
  24. * Generate an RSS feed.
  25. *
  26. * @ingroup Feed
  27. */
  28. class RSSFeed extends ChannelFeed {
  29. /**
  30. * Format a date given a timestamp. If a timestamp is not given, nothing is returned
  31. *
  32. * @param int|null $ts Timestamp
  33. * @return string|null Date string
  34. */
  35. function formatTime( $ts ) {
  36. if ( $ts ) {
  37. return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
  38. }
  39. }
  40. /**
  41. * Output an RSS 2.0 header
  42. */
  43. function outHeader() {
  44. global $wgVersion;
  45. $this->outXmlHeader();
  46. // Manually escaping rather than letting Mustache do it because Mustache
  47. // uses htmlentities, which does not work with XML
  48. $templateParams = [
  49. 'title' => $this->getTitle(),
  50. 'url' => $this->xmlEncode( wfExpandUrl( $this->getUrlUnescaped(), PROTO_CURRENT ) ),
  51. 'description' => $this->getDescription(),
  52. 'language' => $this->xmlEncode( $this->getLanguage() ),
  53. 'version' => $this->xmlEncode( $wgVersion ),
  54. 'timestamp' => $this->xmlEncode( $this->formatTime( wfTimestampNow() ) )
  55. ];
  56. print $this->templateParser->processTemplate( 'RSSHeader', $templateParams );
  57. }
  58. /**
  59. * Output an RSS 2.0 item
  60. * @param FeedItem $item Item to be output
  61. */
  62. function outItem( $item ) {
  63. // Manually escaping rather than letting Mustache do it because Mustache
  64. // uses htmlentities, which does not work with XML
  65. $templateParams = [
  66. "title" => $item->getTitle(),
  67. "url" => $this->xmlEncode( wfExpandUrl( $item->getUrlUnescaped(), PROTO_CURRENT ) ),
  68. "permalink" => $item->rssIsPermalink,
  69. "uniqueID" => $item->getUniqueID(),
  70. "description" => $item->getDescription(),
  71. "date" => $this->xmlEncode( $this->formatTime( $item->getDate() ) ),
  72. "author" => $item->getAuthor()
  73. ];
  74. $comments = $item->getCommentsUnescaped();
  75. if ( $comments ) {
  76. $commentsEscaped = $this->xmlEncode( wfExpandUrl( $comments, PROTO_CURRENT ) );
  77. $templateParams["comments"] = $commentsEscaped;
  78. }
  79. print $this->templateParser->processTemplate( 'RSSItem', $templateParams );
  80. }
  81. /**
  82. * Output an RSS 2.0 footer
  83. */
  84. function outFooter() {
  85. print "</channel></rss>";
  86. }
  87. }