Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiFeedRecentChanges.php
blobbf5fae7e9ec3ab778d328aa9a2a29eee28792ebd
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.
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.
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
18 * @file
19 * @since 1.23
22 namespace MediaWiki\Api;
24 use ChangesFeed;
25 use MediaWiki\Context\DerivativeContext;
26 use MediaWiki\Feed\ChannelFeed;
27 use MediaWiki\MainConfigNames;
28 use MediaWiki\Request\DerivativeRequest;
29 use MediaWiki\SpecialPage\SpecialPage;
30 use MediaWiki\SpecialPage\SpecialPageFactory;
31 use MediaWiki\Title\Title;
32 use MediaWiki\User\TempUser\TempUserConfig;
33 use RuntimeException;
34 use Wikimedia\ParamValidator\ParamValidator;
35 use Wikimedia\ParamValidator\TypeDef\IntegerDef;
37 /**
38 * Recent changes feed.
40 * @ingroup RecentChanges
41 * @ingroup API
43 class ApiFeedRecentChanges extends ApiBase {
45 /** @var array */
46 private $params;
48 private SpecialPageFactory $specialPageFactory;
49 private TempUserConfig $tempUserConfig;
51 /**
52 * @param ApiMain $mainModule
53 * @param string $moduleName
54 * @param SpecialPageFactory $specialPageFactory
55 * @param TempUserConfig $tempUserConfig
57 public function __construct(
58 ApiMain $mainModule,
59 string $moduleName,
60 SpecialPageFactory $specialPageFactory,
61 TempUserConfig $tempUserConfig
62 ) {
63 parent::__construct( $mainModule, $moduleName );
64 $this->specialPageFactory = $specialPageFactory;
65 $this->tempUserConfig = $tempUserConfig;
68 /**
69 * This module uses a custom feed wrapper printer.
71 * @return ApiFormatFeedWrapper
73 public function getCustomPrinter() {
74 return new ApiFormatFeedWrapper( $this->getMain() );
77 /**
78 * Format the rows (generated by SpecialRecentchanges or SpecialRecentchangeslinked)
79 * as an RSS/Atom feed.
81 public function execute() {
82 $config = $this->getConfig();
84 $this->params = $this->extractRequestParams();
86 if ( !$config->get( MainConfigNames::Feed ) ) {
87 $this->dieWithError( 'feed-unavailable' );
90 $feedClasses = $config->get( MainConfigNames::FeedClasses );
91 if ( !isset( $feedClasses[$this->params['feedformat']] ) ) {
92 $this->dieWithError( 'feed-invalid' );
95 $this->getMain()->setCacheMode( 'public' );
96 if ( !$this->getMain()->getParameter( 'smaxage' ) ) {
97 // T65249: This page gets hit a lot, cache at least 15 seconds.
98 $this->getMain()->setCacheMaxAge( 15 );
101 $feedFormat = $this->params['feedformat'];
102 $specialPageName = $this->params['target'] !== null
103 ? 'Recentchangeslinked'
104 : 'Recentchanges';
106 $formatter = $this->getFeedObject( $feedFormat, $specialPageName );
108 // Parameters are passed via the request in the context… :(
109 $context = new DerivativeContext( $this );
110 $context->setRequest( new DerivativeRequest(
111 $this->getRequest(),
112 $this->params,
113 $this->getRequest()->wasPosted()
114 ) );
116 // The row-getting functionality should be factored out of ChangesListSpecialPage too…
117 $rc = $this->specialPageFactory->getPage( $specialPageName );
118 if ( $rc === null ) {
119 throw new RuntimeException( __METHOD__ . ' not able to instance special page ' . $specialPageName );
121 '@phan-var \MediaWiki\SpecialPage\ChangesListSpecialPage $rc';
122 $rc->setContext( $context );
123 $rows = $rc->getRows();
125 $feedItems = $rows ? ChangesFeed::buildItems( $rows ) : [];
127 ApiFormatFeedWrapper::setResult( $this->getResult(), $formatter, $feedItems );
131 * Return a MediaWiki\Feed\ChannelFeed object.
133 * @param string $feedFormat Feed's format (either 'rss' or 'atom')
134 * @param string $specialPageName Relevant special page name (either 'Recentchanges' or
135 * 'Recentchangeslinked')
136 * @return ChannelFeed
138 private function getFeedObject( $feedFormat, $specialPageName ) {
139 if ( $specialPageName === 'Recentchangeslinked' ) {
140 $title = Title::newFromText( $this->params['target'] );
141 if ( !$title || $title->isExternal() ) {
142 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['target'] ) ] );
145 $feed = new ChangesFeed( $feedFormat );
146 $feedObj = $feed->getFeedObject(
147 $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() )
148 ->inContentLanguage()->text(),
149 $this->msg( 'recentchangeslinked-feed' )->inContentLanguage()->text(),
150 SpecialPage::getTitleFor( 'Recentchangeslinked' )->getFullURL()
152 } else {
153 $feed = new ChangesFeed( $feedFormat );
154 $feedObj = $feed->getFeedObject(
155 $this->msg( 'recentchanges' )->inContentLanguage()->text(),
156 $this->msg( 'recentchanges-feed-description' )->inContentLanguage()->text(),
157 SpecialPage::getTitleFor( 'Recentchanges' )->getFullURL()
161 return $feedObj;
164 public function getAllowedParams() {
165 $config = $this->getConfig();
166 $feedFormatNames = array_keys( $config->get( MainConfigNames::FeedClasses ) );
168 return [
169 'feedformat' => [
170 ParamValidator::PARAM_DEFAULT => 'rss',
171 ParamValidator::PARAM_TYPE => $feedFormatNames,
174 'namespace' => [
175 ParamValidator::PARAM_TYPE => 'namespace',
177 // TODO: Rename this option to 'invertnamespaces'?
178 'invert' => false,
179 'associated' => false,
181 'days' => [
182 ParamValidator::PARAM_DEFAULT => 7,
183 IntegerDef::PARAM_MIN => 1,
184 ParamValidator::PARAM_TYPE => 'integer',
186 'limit' => [
187 ParamValidator::PARAM_DEFAULT => 50,
188 IntegerDef::PARAM_MIN => 1,
189 IntegerDef::PARAM_MAX => $config->get( MainConfigNames::FeedLimit ),
190 ParamValidator::PARAM_TYPE => 'integer',
192 'from' => [
193 ParamValidator::PARAM_TYPE => 'timestamp',
196 'hideminor' => false,
197 'hidebots' => false,
198 'hideanons' => [
199 ParamValidator::PARAM_DEFAULT => false,
200 ApiBase::PARAM_HELP_MSG => $this->tempUserConfig->isKnown() ?
201 'apihelp-feedrecentchanges-param-hideanons-temp' :
202 'apihelp-feedrecentchanges-param-hideanons',
204 'hideliu' => false,
205 'hidepatrolled' => false,
206 'hidemyself' => false,
207 'hidecategorization' => false,
209 'tagfilter' => [
210 ParamValidator::PARAM_TYPE => 'string',
212 'inverttags' => false,
214 'target' => [
215 ParamValidator::PARAM_TYPE => 'string',
217 'showlinkedto' => false,
221 protected function getExamplesMessages() {
222 return [
223 'action=feedrecentchanges'
224 => 'apihelp-feedrecentchanges-example-simple',
225 'action=feedrecentchanges&days=30'
226 => 'apihelp-feedrecentchanges-example-30days',
230 public function getHelpUrls() {
231 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Feedrecentchanges';
235 /** @deprecated class alias since 1.43 */
236 class_alias( ApiFeedRecentChanges::class, 'ApiFeedRecentChanges' );