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