MediaWiki  master
ApiFeedRecentChanges.php
Go to the documentation of this file.
1 <?php
29 
36 
37  private $params;
38 
40  private $specialPageFactory;
41 
47  public function __construct(
48  ApiMain $mainModule,
49  string $moduleName,
50  SpecialPageFactory $specialPageFactory
51  ) {
52  parent::__construct( $mainModule, $moduleName );
53  $this->specialPageFactory = $specialPageFactory;
54  }
55 
61  public function getCustomPrinter() {
62  return new ApiFormatFeedWrapper( $this->getMain() );
63  }
64 
69  public function execute() {
70  $config = $this->getConfig();
71 
72  $this->params = $this->extractRequestParams();
73 
74  if ( !$config->get( MainConfigNames::Feed ) ) {
75  $this->dieWithError( 'feed-unavailable' );
76  }
77 
78  $feedClasses = $config->get( MainConfigNames::FeedClasses );
79  if ( !isset( $feedClasses[$this->params['feedformat']] ) ) {
80  $this->dieWithError( 'feed-invalid' );
81  }
82 
83  $this->getMain()->setCacheMode( 'public' );
84  if ( !$this->getMain()->getParameter( 'smaxage' ) ) {
85  // T65249: This page gets hit a lot, cache at least 15 seconds.
86  $this->getMain()->setCacheMaxAge( 15 );
87  }
88 
89  $feedFormat = $this->params['feedformat'];
90  $specialPageName = $this->params['target'] !== null
91  ? 'Recentchangeslinked'
92  : 'Recentchanges';
93 
94  $formatter = $this->getFeedObject( $feedFormat, $specialPageName );
95 
96  // Parameters are passed via the request in the context… :(
97  $context = new DerivativeContext( $this );
98  $context->setRequest( new DerivativeRequest(
99  $this->getRequest(),
100  $this->params,
101  $this->getRequest()->wasPosted()
102  ) );
103 
104  // The row-getting functionality should be factored out of ChangesListSpecialPage too…
105  $rc = $this->specialPageFactory->getPage( $specialPageName );
106  if ( $rc === null ) {
107  throw new RuntimeException( __METHOD__ . ' not able to instance special page ' . $specialPageName );
108  }
109  '@phan-var ChangesListSpecialPage $rc';
110  $rc->setContext( $context );
111  $rows = $rc->getRows();
112 
113  $feedItems = $rows ? ChangesFeed::buildItems( $rows ) : [];
114 
115  ApiFormatFeedWrapper::setResult( $this->getResult(), $formatter, $feedItems );
116  }
117 
126  private function getFeedObject( $feedFormat, $specialPageName ) {
127  if ( $specialPageName === 'Recentchangeslinked' ) {
128  $title = Title::newFromText( $this->params['target'] );
129  if ( !$title || $title->isExternal() ) {
130  $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['target'] ) ] );
131  }
132 
133  $feed = new ChangesFeed( $feedFormat );
134  $feedObj = $feed->getFeedObject(
135  $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() )
136  ->inContentLanguage()->text(),
137  $this->msg( 'recentchangeslinked-feed' )->inContentLanguage()->text(),
138  SpecialPage::getTitleFor( 'Recentchangeslinked' )->getFullURL()
139  );
140  } else {
141  $feed = new ChangesFeed( $feedFormat );
142  $feedObj = $feed->getFeedObject(
143  $this->msg( 'recentchanges' )->inContentLanguage()->text(),
144  $this->msg( 'recentchanges-feed-description' )->inContentLanguage()->text(),
145  SpecialPage::getTitleFor( 'Recentchanges' )->getFullURL()
146  );
147  }
148 
149  return $feedObj;
150  }
151 
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' => false,
187  'hideliu' => false,
188  'hidepatrolled' => false,
189  'hidemyself' => false,
190  'hidecategorization' => false,
191 
192  'tagfilter' => [
193  ParamValidator::PARAM_TYPE => 'string',
194  ],
195  'inverttags' => false,
196 
197  'target' => [
198  ParamValidator::PARAM_TYPE => 'string',
199  ],
200  'showlinkedto' => false,
201  ];
202  }
203 
204  protected function getExamplesMessages() {
205  return [
206  'action=feedrecentchanges'
207  => 'apihelp-feedrecentchanges-example-simple',
208  'action=feedrecentchanges&days=30'
209  => 'apihelp-feedrecentchanges-example-30days',
210  ];
211  }
212 }
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:59
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition: ApiBase.php:1460
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition: ApiBase.php:894
getMain()
Get the main module.
Definition: ApiBase.php:522
getResult()
Get the result object.
Definition: ApiBase.php:637
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:773
Recent changes feed.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
__construct(ApiMain $mainModule, string $moduleName, 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:59
Feed to Special:RecentChanges and Special:RecentChangesLinked.
Definition: ChangesFeed.php:37
static buildItems( $rows)
Generate the feed items given a row from the database.
Definition: ChangesFeed.php:80
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.
Definition: ChannelFeed.php:38
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.
Represents a title within MediaWiki.
Definition: Title.php:82
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,...
Service for formatting and validating API parameters.
Type definition for integer types.
Definition: IntegerDef.php:23