MediaWiki  master
ApiFeedRecentChanges.php
Go to the documentation of this file.
1 <?php
26 
33 
34  private $params;
35 
38 
44  public function __construct(
45  ApiMain $mainModule,
46  string $moduleName,
48  ) {
49  parent::__construct( $mainModule, $moduleName );
50  $this->specialPageFactory = $specialPageFactory;
51  }
52 
58  public function getCustomPrinter() {
59  return new ApiFormatFeedWrapper( $this->getMain() );
60  }
61 
66  public function execute() {
67  $config = $this->getConfig();
68 
69  $this->params = $this->extractRequestParams();
70 
71  if ( !$config->get( MainConfigNames::Feed ) ) {
72  $this->dieWithError( 'feed-unavailable' );
73  }
74 
75  $feedClasses = $config->get( MainConfigNames::FeedClasses );
76  if ( !isset( $feedClasses[$this->params['feedformat']] ) ) {
77  $this->dieWithError( 'feed-invalid' );
78  }
79 
80  $this->getMain()->setCacheMode( 'public' );
81  if ( !$this->getMain()->getParameter( 'smaxage' ) ) {
82  // T65249: This page gets hit a lot, cache at least 15 seconds.
83  $this->getMain()->setCacheMaxAge( 15 );
84  }
85 
86  $feedFormat = $this->params['feedformat'];
87  $specialPageName = $this->params['target'] !== null
88  ? 'Recentchangeslinked'
89  : 'Recentchanges';
90 
91  $formatter = $this->getFeedObject( $feedFormat, $specialPageName );
92 
93  // Parameters are passed via the request in the context… :(
94  $context = new DerivativeContext( $this );
95  $context->setRequest( new DerivativeRequest(
96  $this->getRequest(),
97  $this->params,
98  $this->getRequest()->wasPosted()
99  ) );
100 
101  // The row-getting functionality should be factored out of ChangesListSpecialPage too…
102  $rc = $this->specialPageFactory->getPage( $specialPageName );
103  if ( $rc === null ) {
104  throw new RuntimeException( __METHOD__ . ' not able to instance special page ' . $specialPageName );
105  }
106  '@phan-var ChangesListSpecialPage $rc';
107  $rc->setContext( $context );
108  $rows = $rc->getRows();
109 
110  $feedItems = $rows ? ChangesFeed::buildItems( $rows ) : [];
111 
112  ApiFormatFeedWrapper::setResult( $this->getResult(), $formatter, $feedItems );
113  }
114 
123  private function getFeedObject( $feedFormat, $specialPageName ) {
124  if ( $specialPageName === 'Recentchangeslinked' ) {
125  $title = Title::newFromText( $this->params['target'] );
126  if ( !$title ) {
127  $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['target'] ) ] );
128  }
129 
130  $feed = new ChangesFeed( $feedFormat );
131  $feedObj = $feed->getFeedObject(
132  $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() )
133  ->inContentLanguage()->text(),
134  $this->msg( 'recentchangeslinked-feed' )->inContentLanguage()->text(),
135  SpecialPage::getTitleFor( 'Recentchangeslinked' )->getFullURL()
136  );
137  } else {
138  $feed = new ChangesFeed( $feedFormat );
139  $feedObj = $feed->getFeedObject(
140  $this->msg( 'recentchanges' )->inContentLanguage()->text(),
141  $this->msg( 'recentchanges-feed-description' )->inContentLanguage()->text(),
142  SpecialPage::getTitleFor( 'Recentchanges' )->getFullURL()
143  );
144  }
145 
146  return $feedObj;
147  }
148 
149  public function getAllowedParams() {
150  $config = $this->getConfig();
151  $feedFormatNames = array_keys( $config->get( MainConfigNames::FeedClasses ) );
152 
153  $ret = [
154  'feedformat' => [
155  ParamValidator::PARAM_DEFAULT => 'rss',
156  ParamValidator::PARAM_TYPE => $feedFormatNames,
157  ],
158 
159  'namespace' => [
160  ParamValidator::PARAM_TYPE => 'namespace',
161  ],
162  'invert' => false,
163  'associated' => false,
164 
165  'days' => [
166  ParamValidator::PARAM_DEFAULT => 7,
167  IntegerDef::PARAM_MIN => 1,
168  ParamValidator::PARAM_TYPE => 'integer',
169  ],
170  'limit' => [
171  ParamValidator::PARAM_DEFAULT => 50,
172  IntegerDef::PARAM_MIN => 1,
173  IntegerDef::PARAM_MAX => $config->get( MainConfigNames::FeedLimit ),
174  ParamValidator::PARAM_TYPE => 'integer',
175  ],
176  'from' => [
177  ParamValidator::PARAM_TYPE => 'timestamp',
178  ],
179 
180  'hideminor' => false,
181  'hidebots' => false,
182  'hideanons' => false,
183  'hideliu' => false,
184  'hidepatrolled' => false,
185  'hidemyself' => false,
186  'hidecategorization' => false,
187 
188  'tagfilter' => [
189  ParamValidator::PARAM_TYPE => 'string',
190  ],
191 
192  'target' => [
193  ParamValidator::PARAM_TYPE => 'string',
194  ],
195  'showlinkedto' => false,
196  ];
197 
198  return $ret;
199  }
200 
201  protected function getExamplesMessages() {
202  return [
203  'action=feedrecentchanges'
204  => 'apihelp-feedrecentchanges-example-simple',
205  'action=feedrecentchanges&days=30'
206  => 'apihelp-feedrecentchanges-example-30days',
207  ];
208  }
209 }
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:56
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition: ApiBase.php:1458
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition: ApiBase.php:886
getMain()
Get the main module.
Definition: ApiBase.php:514
getResult()
Get the result object.
Definition: ApiBase.php:629
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:765
Recent changes feed.
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:52
Feed to Special:RecentChanges and Special:RecentChangesLinked.
Definition: ChangesFeed.php:33
static buildItems( $rows)
Generate the feed items given a row from the database.
Definition: ChangesFeed.php:76
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...
A class containing constants representing the names of configuration variables.
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,...
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:370
Service for formatting and validating API parameters.
Type definition for integer types.
Definition: IntegerDef.php:23