ApiFeedRecentChanges.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. * @since 1.23
  20. */
  21. /**
  22. * Recent changes feed.
  23. *
  24. * @ingroup API
  25. */
  26. class ApiFeedRecentChanges extends ApiBase {
  27. private $params;
  28. /**
  29. * This module uses a custom feed wrapper printer.
  30. *
  31. * @return ApiFormatFeedWrapper
  32. */
  33. public function getCustomPrinter() {
  34. return new ApiFormatFeedWrapper( $this->getMain() );
  35. }
  36. /**
  37. * Format the rows (generated by SpecialRecentchanges or SpecialRecentchangeslinked)
  38. * as an RSS/Atom feed.
  39. */
  40. public function execute() {
  41. $config = $this->getConfig();
  42. $this->params = $this->extractRequestParams();
  43. if ( !$config->get( 'Feed' ) ) {
  44. $this->dieWithError( 'feed-unavailable' );
  45. }
  46. $feedClasses = $config->get( 'FeedClasses' );
  47. if ( !isset( $feedClasses[$this->params['feedformat']] ) ) {
  48. $this->dieWithError( 'feed-invalid' );
  49. }
  50. $this->getMain()->setCacheMode( 'public' );
  51. if ( !$this->getMain()->getParameter( 'smaxage' ) ) {
  52. // T65249: This page gets hit a lot, cache at least 15 seconds.
  53. $this->getMain()->setCacheMaxAge( 15 );
  54. }
  55. $feedFormat = $this->params['feedformat'];
  56. $specialClass = $this->params['target'] !== null
  57. ? SpecialRecentChangesLinked::class
  58. : SpecialRecentChanges::class;
  59. $formatter = $this->getFeedObject( $feedFormat, $specialClass );
  60. // Parameters are passed via the request in the context… :(
  61. $context = new DerivativeContext( $this );
  62. $context->setRequest( new DerivativeRequest(
  63. $this->getRequest(),
  64. $this->params,
  65. $this->getRequest()->wasPosted()
  66. ) );
  67. // The row-getting functionality should be factored out of ChangesListSpecialPage too…
  68. $rc = new $specialClass();
  69. $rc->setContext( $context );
  70. $rows = $rc->getRows();
  71. $feedItems = $rows ? ChangesFeed::buildItems( $rows ) : [];
  72. ApiFormatFeedWrapper::setResult( $this->getResult(), $formatter, $feedItems );
  73. }
  74. /**
  75. * Return a ChannelFeed object.
  76. *
  77. * @param string $feedFormat Feed's format (either 'rss' or 'atom')
  78. * @param string $specialClass Relevant special page name (either 'SpecialRecentChanges' or
  79. * 'SpecialRecentChangesLinked')
  80. * @return ChannelFeed
  81. */
  82. public function getFeedObject( $feedFormat, $specialClass ) {
  83. if ( $specialClass === SpecialRecentChangesLinked::class ) {
  84. $title = Title::newFromText( $this->params['target'] );
  85. if ( !$title ) {
  86. $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['target'] ) ] );
  87. }
  88. $feed = new ChangesFeed( $feedFormat );
  89. $feedObj = $feed->getFeedObject(
  90. $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() )
  91. ->inContentLanguage()->text(),
  92. $this->msg( 'recentchangeslinked-feed' )->inContentLanguage()->text(),
  93. SpecialPage::getTitleFor( 'Recentchangeslinked' )->getFullURL()
  94. );
  95. } else {
  96. $feed = new ChangesFeed( $feedFormat );
  97. $feedObj = $feed->getFeedObject(
  98. $this->msg( 'recentchanges' )->inContentLanguage()->text(),
  99. $this->msg( 'recentchanges-feed-description' )->inContentLanguage()->text(),
  100. SpecialPage::getTitleFor( 'Recentchanges' )->getFullURL()
  101. );
  102. }
  103. return $feedObj;
  104. }
  105. public function getAllowedParams() {
  106. $config = $this->getConfig();
  107. $feedFormatNames = array_keys( $config->get( 'FeedClasses' ) );
  108. $ret = [
  109. 'feedformat' => [
  110. ApiBase::PARAM_DFLT => 'rss',
  111. ApiBase::PARAM_TYPE => $feedFormatNames,
  112. ],
  113. 'namespace' => [
  114. ApiBase::PARAM_TYPE => 'namespace',
  115. ],
  116. 'invert' => false,
  117. 'associated' => false,
  118. 'days' => [
  119. ApiBase::PARAM_DFLT => 7,
  120. ApiBase::PARAM_MIN => 1,
  121. ApiBase::PARAM_TYPE => 'integer',
  122. ],
  123. 'limit' => [
  124. ApiBase::PARAM_DFLT => 50,
  125. ApiBase::PARAM_MIN => 1,
  126. ApiBase::PARAM_MAX => $config->get( 'FeedLimit' ),
  127. ApiBase::PARAM_TYPE => 'integer',
  128. ],
  129. 'from' => [
  130. ApiBase::PARAM_TYPE => 'timestamp',
  131. ],
  132. 'hideminor' => false,
  133. 'hidebots' => false,
  134. 'hideanons' => false,
  135. 'hideliu' => false,
  136. 'hidepatrolled' => false,
  137. 'hidemyself' => false,
  138. 'hidecategorization' => false,
  139. 'tagfilter' => [
  140. ApiBase::PARAM_TYPE => 'string',
  141. ],
  142. 'target' => [
  143. ApiBase::PARAM_TYPE => 'string',
  144. ],
  145. 'showlinkedto' => false,
  146. ];
  147. return $ret;
  148. }
  149. protected function getExamplesMessages() {
  150. return [
  151. 'action=feedrecentchanges'
  152. => 'apihelp-feedrecentchanges-example-simple',
  153. 'action=feedrecentchanges&days=30'
  154. => 'apihelp-feedrecentchanges-example-30days',
  155. ];
  156. }
  157. }