AtomFeed.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 Atom feed.
  25. *
  26. * @ingroup Feed
  27. */
  28. class AtomFeed extends ChannelFeed {
  29. /**
  30. * Format a date given timestamp, if one is given.
  31. *
  32. * @param string|int|null $timestamp
  33. * @return string|null
  34. */
  35. function formatTime( $timestamp ) {
  36. if ( $timestamp ) {
  37. // need to use RFC 822 time format at least for rss2.0
  38. return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $timestamp ) );
  39. }
  40. }
  41. /**
  42. * Outputs a basic header for Atom 1.0 feeds.
  43. */
  44. function outHeader() {
  45. global $wgVersion;
  46. $this->outXmlHeader();
  47. // Manually escaping rather than letting Mustache do it because Mustache
  48. // uses htmlentities, which does not work with XML
  49. $templateParams = [
  50. 'language' => $this->xmlEncode( $this->getLanguage() ),
  51. 'feedID' => $this->getFeedId(),
  52. 'title' => $this->getTitle(),
  53. 'url' => $this->xmlEncode( wfExpandUrl( $this->getUrlUnescaped(), PROTO_CURRENT ) ),
  54. 'selfUrl' => $this->getSelfUrl(),
  55. 'timestamp' => $this->xmlEncode( $this->formatTime( wfTimestampNow() ) ),
  56. 'description' => $this->getDescription(),
  57. 'version' => $this->xmlEncode( $wgVersion ),
  58. ];
  59. print $this->templateParser->processTemplate( 'AtomHeader', $templateParams );
  60. }
  61. /**
  62. * Atom 1.0 requires a unique, opaque IRI as a unique identifier
  63. * for every feed we create. For now just use the URL, but who
  64. * can tell if that's right? If we put options on the feed, do we
  65. * have to change the id? Maybe? Maybe not.
  66. *
  67. * @return string
  68. */
  69. private function getFeedId() {
  70. return $this->getSelfUrl();
  71. }
  72. /**
  73. * Atom 1.0 requests a self-reference to the feed.
  74. * @return string
  75. */
  76. private function getSelfUrl() {
  77. global $wgRequest;
  78. return htmlspecialchars( $wgRequest->getFullRequestURL() );
  79. }
  80. /**
  81. * Output a given item.
  82. * @param FeedItem $item
  83. */
  84. function outItem( $item ) {
  85. global $wgMimeType;
  86. // Manually escaping rather than letting Mustache do it because Mustache
  87. // uses htmlentities, which does not work with XML
  88. $templateParams = [
  89. "uniqueID" => $item->getUniqueID(),
  90. "title" => $item->getTitle(),
  91. "mimeType" => $this->xmlEncode( $wgMimeType ),
  92. "url" => $this->xmlEncode( wfExpandUrl( $item->getUrlUnescaped(), PROTO_CURRENT ) ),
  93. "date" => $this->xmlEncode( $this->formatTime( $item->getDate() ) ),
  94. "description" => $item->getDescription(),
  95. "author" => $item->getAuthor()
  96. ];
  97. print $this->templateParser->processTemplate( 'AtomItem', $templateParams );
  98. }
  99. /**
  100. * Outputs the footer for Atom 1.0 feed (basically '\</feed\>').
  101. */
  102. function outFooter() {
  103. print "</feed>";
  104. }
  105. }