MediaWiki master
ApiFeedRecentChanges.php
Go to the documentation of this file.
1<?php
22namespace MediaWiki\Api;
23
24use ChangesFeed;
33use RuntimeException;
36
44
46 private $params;
47
48 private SpecialPageFactory $specialPageFactory;
49 private TempUserConfig $tempUserConfig;
50
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;
66 }
67
73 public function getCustomPrinter() {
74 return new ApiFormatFeedWrapper( $this->getMain() );
75 }
76
81 public function execute() {
82 $config = $this->getConfig();
83
84 $this->params = $this->extractRequestParams();
85
86 if ( !$config->get( MainConfigNames::Feed ) ) {
87 $this->dieWithError( 'feed-unavailable' );
88 }
89
90 $feedClasses = $config->get( MainConfigNames::FeedClasses );
91 if ( !isset( $feedClasses[$this->params['feedformat']] ) ) {
92 $this->dieWithError( 'feed-invalid' );
93 }
94
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 );
99 }
100
101 $feedFormat = $this->params['feedformat'];
102 $specialPageName = $this->params['target'] !== null
103 ? 'Recentchangeslinked'
104 : 'Recentchanges';
105
106 $formatter = $this->getFeedObject( $feedFormat, $specialPageName );
107
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 ) );
115
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 );
120 }
121 '@phan-var \MediaWiki\SpecialPage\ChangesListSpecialPage $rc';
122 $rc->setContext( $context );
123 $rows = $rc->getRows();
124
125 $feedItems = $rows ? ChangesFeed::buildItems( $rows ) : [];
126
127 ApiFormatFeedWrapper::setResult( $this->getResult(), $formatter, $feedItems );
128 }
129
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'] ) ] );
143 }
144
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()
151 );
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()
158 );
159 }
160
161 return $feedObj;
162 }
163
164 public function getAllowedParams() {
165 $config = $this->getConfig();
166 $feedFormatNames = array_keys( $config->get( MainConfigNames::FeedClasses ) );
167
168 return [
169 'feedformat' => [
170 ParamValidator::PARAM_DEFAULT => 'rss',
171 ParamValidator::PARAM_TYPE => $feedFormatNames,
172 ],
173
174 'namespace' => [
175 ParamValidator::PARAM_TYPE => 'namespace',
176 ],
177 // TODO: Rename this option to 'invertnamespaces'?
178 'invert' => false,
179 'associated' => false,
180
181 'days' => [
182 ParamValidator::PARAM_DEFAULT => 7,
183 IntegerDef::PARAM_MIN => 1,
184 ParamValidator::PARAM_TYPE => 'integer',
185 ],
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',
191 ],
192 'from' => [
193 ParamValidator::PARAM_TYPE => 'timestamp',
194 ],
195
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',
203 ],
204 'hideliu' => false,
205 'hidepatrolled' => false,
206 'hidemyself' => false,
207 'hidecategorization' => false,
208
209 'tagfilter' => [
210 ParamValidator::PARAM_TYPE => 'string',
211 ],
212 'inverttags' => false,
213
214 'target' => [
215 ParamValidator::PARAM_TYPE => 'string',
216 ],
217 'showlinkedto' => false,
218 ];
219 }
220
221 protected function getExamplesMessages() {
222 return [
223 'action=feedrecentchanges'
224 => 'apihelp-feedrecentchanges-example-simple',
225 'action=feedrecentchanges&days=30'
226 => 'apihelp-feedrecentchanges-example-30days',
227 ];
228 }
229
230 public function getHelpUrls() {
231 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Feedrecentchanges';
232 }
233}
234
236class_alias( ApiFeedRecentChanges::class, 'ApiFeedRecentChanges' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
array $params
The job parameters.
XML feed for Special:RecentChanges and Special:RecentChangesLinked.
static buildItems( $rows)
Generate the feed items given a row from the database.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:76
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1577
getMain()
Get the main module.
Definition ApiBase.php:589
getResult()
Get the result object.
Definition ApiBase.php:710
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:184
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition ApiBase.php:973
execute()
Format the rows (generated by SpecialRecentchanges or SpecialRecentchangeslinked) as an RSS/Atom feed...
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getExamplesMessages()
Returns usage examples for this module.
getCustomPrinter()
This module uses a custom feed wrapper printer.
getHelpUrls()
Return links to more detailed help pages about the module.
__construct(ApiMain $mainModule, string $moduleName, SpecialPageFactory $specialPageFactory, TempUserConfig $tempUserConfig)
This printer is used to wrap an instance of the Feed class.
static setResult( $result, $feed, $feedItems)
Call this method to initialize output data.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:78
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
An IContextSource implementation which will inherit context from another source but allow individual ...
Class to support the outputting of syndication feeds in Atom and RSS format.
A class containing constants representing the names of configuration variables.
const FeedClasses
Name constant for the FeedClasses setting, for use with Config::get()
const Feed
Name constant for the Feed setting, for use with Config::get()
const FeedLimit
Name constant for the FeedLimit setting, for use with Config::get()
Similar to MediaWiki\Request\FauxRequest, but only fakes URL parameters and method (POST or GET) and ...
Factory for handling the special page list and generating SpecialPage objects.
Parent class for all special pages.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
Represents a title within MediaWiki.
Definition Title.php:78
Service for formatting and validating API parameters.
Type definition for integer types.
Interface for temporary user creation config and name matching.