ChannelFeed.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. * Class to support the outputting of syndication feeds in Atom and RSS format.
  25. *
  26. * @ingroup Feed
  27. */
  28. abstract class ChannelFeed extends FeedItem {
  29. /** @var TemplateParser */
  30. protected $templateParser;
  31. /**
  32. * @param string|Title $title Feed's title
  33. * @param string $description
  34. * @param string $url URL uniquely designating the feed.
  35. * @param string $date Feed's date
  36. * @param string $author Author's user name
  37. * @param string $comments
  38. */
  39. function __construct( $title, $description, $url, $date = '', $author = '', $comments = '' ) {
  40. parent::__construct( $title, $description, $url, $date, $author, $comments );
  41. $this->templateParser = new TemplateParser();
  42. }
  43. /**
  44. * Generate Header of the feed
  45. * @par Example:
  46. * @code
  47. * print "<feed>";
  48. * @endcode
  49. */
  50. abstract public function outHeader();
  51. /**
  52. * Generate an item
  53. * @par Example:
  54. * @code
  55. * print "<item>...</item>";
  56. * @endcode
  57. * @param FeedItem $item
  58. */
  59. abstract public function outItem( $item );
  60. /**
  61. * Generate Footer of the feed
  62. * @par Example:
  63. * @code
  64. * print "</feed>";
  65. * @endcode
  66. */
  67. abstract public function outFooter();
  68. /**
  69. * Setup and send HTTP headers. Don't send any content;
  70. * content might end up being cached and re-sent with
  71. * these same headers later.
  72. *
  73. * This should be called from the outHeader() method,
  74. * but can also be called separately.
  75. */
  76. public function httpHeaders() {
  77. global $wgOut, $wgVaryOnXFP;
  78. # We take over from $wgOut, excepting its cache header info
  79. $wgOut->disable();
  80. $mimetype = $this->contentType();
  81. header( "Content-type: $mimetype; charset=UTF-8" );
  82. // Set a sane filename
  83. $exts = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer()
  84. ->getExtensionsForType( $mimetype );
  85. $ext = $exts ? strtok( $exts, ' ' ) : 'xml';
  86. header( "Content-Disposition: inline; filename=\"feed.{$ext}\"" );
  87. if ( $wgVaryOnXFP ) {
  88. $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
  89. }
  90. $wgOut->sendCacheControl();
  91. }
  92. /**
  93. * Return an internet media type to be sent in the headers.
  94. *
  95. * @return string
  96. */
  97. private function contentType() {
  98. global $wgRequest;
  99. $ctype = $wgRequest->getVal( 'ctype', 'application/xml' );
  100. $allowedctypes = [
  101. 'application/xml',
  102. 'text/xml',
  103. 'application/rss+xml',
  104. 'application/atom+xml'
  105. ];
  106. return ( in_array( $ctype, $allowedctypes ) ? $ctype : 'application/xml' );
  107. }
  108. /**
  109. * Output the initial XML headers.
  110. */
  111. protected function outXmlHeader() {
  112. $this->httpHeaders();
  113. echo '<?xml version="1.0"?>' . "\n";
  114. }
  115. }