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