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