MediaWiki master
ApiFeedRecentChanges.php
Go to the documentation of this file.
1<?php
32
39
40 private $params;
41
42 private SpecialPageFactory $specialPageFactory;
43 private TempUserConfig $tempUserConfig;
44
51 public function __construct(
52 ApiMain $mainModule,
53 string $moduleName,
54 SpecialPageFactory $specialPageFactory,
55 TempUserConfig $tempUserConfig
56 ) {
57 parent::__construct( $mainModule, $moduleName );
58 $this->specialPageFactory = $specialPageFactory;
59 $this->tempUserConfig = $tempUserConfig;
60 }
61
67 public function getCustomPrinter() {
68 return new ApiFormatFeedWrapper( $this->getMain() );
69 }
70
75 public function execute() {
76 $config = $this->getConfig();
77
78 $this->params = $this->extractRequestParams();
79
80 if ( !$config->get( MainConfigNames::Feed ) ) {
81 $this->dieWithError( 'feed-unavailable' );
82 }
83
84 $feedClasses = $config->get( MainConfigNames::FeedClasses );
85 if ( !isset( $feedClasses[$this->params['feedformat']] ) ) {
86 $this->dieWithError( 'feed-invalid' );
87 }
88
89 $this->getMain()->setCacheMode( 'public' );
90 if ( !$this->getMain()->getParameter( 'smaxage' ) ) {
91 // T65249: This page gets hit a lot, cache at least 15 seconds.
92 $this->getMain()->setCacheMaxAge( 15 );
93 }
94
95 $feedFormat = $this->params['feedformat'];
96 $specialPageName = $this->params['target'] !== null
97 ? 'Recentchangeslinked'
98 : 'Recentchanges';
99
100 $formatter = $this->getFeedObject( $feedFormat, $specialPageName );
101
102 // Parameters are passed via the request in the context… :(
103 $context = new DerivativeContext( $this );
104 $context->setRequest( new DerivativeRequest(
105 $this->getRequest(),
106 $this->params,
107 $this->getRequest()->wasPosted()
108 ) );
109
110 // The row-getting functionality should be factored out of ChangesListSpecialPage too…
111 $rc = $this->specialPageFactory->getPage( $specialPageName );
112 if ( $rc === null ) {
113 throw new RuntimeException( __METHOD__ . ' not able to instance special page ' . $specialPageName );
114 }
115 '@phan-var \MediaWiki\SpecialPage\ChangesListSpecialPage $rc';
116 $rc->setContext( $context );
117 $rows = $rc->getRows();
118
119 $feedItems = $rows ? ChangesFeed::buildItems( $rows ) : [];
120
121 ApiFormatFeedWrapper::setResult( $this->getResult(), $formatter, $feedItems );
122 }
123
132 private function getFeedObject( $feedFormat, $specialPageName ) {
133 if ( $specialPageName === 'Recentchangeslinked' ) {
134 $title = Title::newFromText( $this->params['target'] );
135 if ( !$title || $title->isExternal() ) {
136 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['target'] ) ] );
137 }
138
139 $feed = new ChangesFeed( $feedFormat );
140 $feedObj = $feed->getFeedObject(
141 $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() )
142 ->inContentLanguage()->text(),
143 $this->msg( 'recentchangeslinked-feed' )->inContentLanguage()->text(),
144 SpecialPage::getTitleFor( 'Recentchangeslinked' )->getFullURL()
145 );
146 } else {
147 $feed = new ChangesFeed( $feedFormat );
148 $feedObj = $feed->getFeedObject(
149 $this->msg( 'recentchanges' )->inContentLanguage()->text(),
150 $this->msg( 'recentchanges-feed-description' )->inContentLanguage()->text(),
151 SpecialPage::getTitleFor( 'Recentchanges' )->getFullURL()
152 );
153 }
154
155 return $feedObj;
156 }
157
158 public function getAllowedParams() {
159 $config = $this->getConfig();
160 $feedFormatNames = array_keys( $config->get( MainConfigNames::FeedClasses ) );
161
162 return [
163 'feedformat' => [
164 ParamValidator::PARAM_DEFAULT => 'rss',
165 ParamValidator::PARAM_TYPE => $feedFormatNames,
166 ],
167
168 'namespace' => [
169 ParamValidator::PARAM_TYPE => 'namespace',
170 ],
171 // TODO: Rename this option to 'invertnamespaces'?
172 'invert' => false,
173 'associated' => false,
174
175 'days' => [
176 ParamValidator::PARAM_DEFAULT => 7,
177 IntegerDef::PARAM_MIN => 1,
178 ParamValidator::PARAM_TYPE => 'integer',
179 ],
180 'limit' => [
181 ParamValidator::PARAM_DEFAULT => 50,
182 IntegerDef::PARAM_MIN => 1,
183 IntegerDef::PARAM_MAX => $config->get( MainConfigNames::FeedLimit ),
184 ParamValidator::PARAM_TYPE => 'integer',
185 ],
186 'from' => [
187 ParamValidator::PARAM_TYPE => 'timestamp',
188 ],
189
190 'hideminor' => false,
191 'hidebots' => false,
192 'hideanons' => [
193 ParamValidator::PARAM_DEFAULT => false,
194 ApiBase::PARAM_HELP_MSG => $this->tempUserConfig->isKnown() ?
195 'apihelp-feedrecentchanges-param-hideanons-temp' :
196 'apihelp-feedrecentchanges-param-hideanons',
197 ],
198 'hideliu' => false,
199 'hidepatrolled' => false,
200 'hidemyself' => false,
201 'hidecategorization' => false,
202
203 'tagfilter' => [
204 ParamValidator::PARAM_TYPE => 'string',
205 ],
206 'inverttags' => false,
207
208 'target' => [
209 ParamValidator::PARAM_TYPE => 'string',
210 ],
211 'showlinkedto' => false,
212 ];
213 }
214
215 protected function getExamplesMessages() {
216 return [
217 'action=feedrecentchanges'
218 => 'apihelp-feedrecentchanges-example-simple',
219 'action=feedrecentchanges&days=30'
220 => 'apihelp-feedrecentchanges-example-30days',
221 ];
222 }
223
224 public function getHelpUrls() {
225 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Feedrecentchanges';
226 }
227}
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:65
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1540
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition ApiBase.php:943
getMain()
Get the main module.
Definition ApiBase.php:560
getResult()
Get the result object.
Definition ApiBase.php:681
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:821
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:172
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getHelpUrls()
Return links to more detailed help pages about the module.
getExamplesMessages()
Returns usage examples for this module.
execute()
Format the rows (generated by SpecialRecentchanges or SpecialRecentchangeslinked) as an RSS/Atom feed...
getCustomPrinter()
This module uses a custom feed wrapper printer.
__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:67
Feed to Special:RecentChanges and Special:RecentChangesLinked.
static buildItems( $rows)
Generate the feed items given a row from the database.
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.
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.
Represents a title within MediaWiki.
Definition Title.php:79
Service for formatting and validating API parameters.
Type definition for integer types.
Interface for temporary user creation config and name matching.