MediaWiki REL1_37
ApiFeedRecentChanges.php
Go to the documentation of this file.
1<?php
23
30
31 private $params;
32
35
41 public function __construct(
42 ApiMain $mainModule,
43 string $moduleName,
45 ) {
46 parent::__construct( $mainModule, $moduleName );
47 $this->specialPageFactory = $specialPageFactory;
48 }
49
55 public function getCustomPrinter() {
56 return new ApiFormatFeedWrapper( $this->getMain() );
57 }
58
63 public function execute() {
64 $config = $this->getConfig();
65
66 $this->params = $this->extractRequestParams();
67
68 if ( !$config->get( 'Feed' ) ) {
69 $this->dieWithError( 'feed-unavailable' );
70 }
71
72 $feedClasses = $config->get( 'FeedClasses' );
73 if ( !isset( $feedClasses[$this->params['feedformat']] ) ) {
74 $this->dieWithError( 'feed-invalid' );
75 }
76
77 $this->getMain()->setCacheMode( 'public' );
78 if ( !$this->getMain()->getParameter( 'smaxage' ) ) {
79 // T65249: This page gets hit a lot, cache at least 15 seconds.
80 $this->getMain()->setCacheMaxAge( 15 );
81 }
82
83 $feedFormat = $this->params['feedformat'];
84 $specialPageName = $this->params['target'] !== null
85 ? 'Recentchangeslinked'
86 : 'Recentchanges';
87
88 $formatter = $this->getFeedObject( $feedFormat, $specialPageName );
89
90 // Parameters are passed via the request in the context… :(
91 $context = new DerivativeContext( $this );
92 $context->setRequest( new DerivativeRequest(
93 $this->getRequest(),
94 $this->params,
95 $this->getRequest()->wasPosted()
96 ) );
97
98 // The row-getting functionality should be factored out of ChangesListSpecialPage too…
99 $rc = $this->specialPageFactory->getPage( $specialPageName );
100 if ( $rc === null ) {
101 throw new RuntimeException( __METHOD__ . ' not able to instance special page ' . $specialPageName );
102 }
103 '@phan-var ChangesListSpecialPage $rc';
104 $rc->setContext( $context );
105 $rows = $rc->getRows();
106
107 $feedItems = $rows ? ChangesFeed::buildItems( $rows ) : [];
108
109 ApiFormatFeedWrapper::setResult( $this->getResult(), $formatter, $feedItems );
110 }
111
120 private function getFeedObject( $feedFormat, $specialPageName ) {
121 if ( $specialPageName === 'Recentchangeslinked' ) {
122 $title = Title::newFromText( $this->params['target'] );
123 if ( !$title ) {
124 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['target'] ) ] );
125 }
126
127 $feed = new ChangesFeed( $feedFormat );
128 $feedObj = $feed->getFeedObject(
129 $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() )
130 ->inContentLanguage()->text(),
131 $this->msg( 'recentchangeslinked-feed' )->inContentLanguage()->text(),
132 SpecialPage::getTitleFor( 'Recentchangeslinked' )->getFullURL()
133 );
134 } else {
135 $feed = new ChangesFeed( $feedFormat );
136 $feedObj = $feed->getFeedObject(
137 $this->msg( 'recentchanges' )->inContentLanguage()->text(),
138 $this->msg( 'recentchanges-feed-description' )->inContentLanguage()->text(),
139 SpecialPage::getTitleFor( 'Recentchanges' )->getFullURL()
140 );
141 }
142
143 return $feedObj;
144 }
145
146 public function getAllowedParams() {
147 $config = $this->getConfig();
148 $feedFormatNames = array_keys( $config->get( 'FeedClasses' ) );
149
150 $ret = [
151 'feedformat' => [
152 ApiBase::PARAM_DFLT => 'rss',
153 ApiBase::PARAM_TYPE => $feedFormatNames,
154 ],
155
156 'namespace' => [
157 ApiBase::PARAM_TYPE => 'namespace',
158 ],
159 'invert' => false,
160 'associated' => false,
161
162 'days' => [
165 ApiBase::PARAM_TYPE => 'integer',
166 ],
167 'limit' => [
170 ApiBase::PARAM_MAX => $config->get( 'FeedLimit' ),
171 ApiBase::PARAM_TYPE => 'integer',
172 ],
173 'from' => [
174 ApiBase::PARAM_TYPE => 'timestamp',
175 ],
176
177 'hideminor' => false,
178 'hidebots' => false,
179 'hideanons' => false,
180 'hideliu' => false,
181 'hidepatrolled' => false,
182 'hidemyself' => false,
183 'hidecategorization' => false,
184
185 'tagfilter' => [
186 ApiBase::PARAM_TYPE => 'string',
187 ],
188
189 'target' => [
190 ApiBase::PARAM_TYPE => 'string',
191 ],
192 'showlinkedto' => false,
193 ];
194
195 return $ret;
196 }
197
198 protected function getExamplesMessages() {
199 return [
200 'action=feedrecentchanges'
201 => 'apihelp-feedrecentchanges-example-simple',
202 'action=feedrecentchanges&days=30'
203 => 'apihelp-feedrecentchanges-example-30days',
204 ];
205 }
206}
wfEscapeWikiText( $text)
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:55
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1436
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition ApiBase.php:884
const PARAM_MAX
Definition ApiBase.php:85
getMain()
Get the main module.
Definition ApiBase.php:513
const PARAM_TYPE
Definition ApiBase.php:81
const PARAM_DFLT
Definition ApiBase.php:73
const PARAM_MIN
Definition ApiBase.php:93
getResult()
Get the result object.
Definition ApiBase.php:628
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:764
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getFeedObject( $feedFormat, $specialPageName)
Return a ChannelFeed object.
__construct(ApiMain $mainModule, string $moduleName, SpecialPageFactory $specialPageFactory)
SpecialPageFactory $specialPageFactory
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.
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:49
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()
IContextSource $context
An IContextSource implementation which will inherit context from another source but allow individual ...
Similar to FauxRequest, but only fakes URL parameters and method (POST or GET) and use the base reque...
Factory for handling the special page list and generating SpecialPage objects.
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,...