MediaWiki  master
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' => [
163  ApiBase::PARAM_DFLT => 7,
164  ApiBase::PARAM_MIN => 1,
165  ApiBase::PARAM_TYPE => 'integer',
166  ],
167  'limit' => [
168  ApiBase::PARAM_DFLT => 50,
169  ApiBase::PARAM_MIN => 1,
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 }
DerivativeRequest
Similar to FauxRequest, but only fakes URL parameters and method (POST or GET) and use the base reque...
Definition: DerivativeRequest.php:36
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:49
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:39
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:72
ApiFeedRecentChanges\$params
$params
Definition: ApiFeedRecentChanges.php:31
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:383
ApiFeedRecentChanges\__construct
__construct(ApiMain $mainModule, string $moduleName, SpecialPageFactory $specialPageFactory)
Definition: ApiFeedRecentChanges.php:41
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1379
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:72
MediaWiki\SpecialPage\SpecialPageFactory
Factory for handling the special page list and generating SpecialPage objects.
Definition: SpecialPageFactory.php:63
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:571
ApiFeedRecentChanges\getCustomPrinter
getCustomPrinter()
This module uses a custom feed wrapper printer.
Definition: ApiFeedRecentChanges.php:55
SpecialPage\getTitleFor
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,...
Definition: SpecialPage.php:107
ContextSource\getRequest
getRequest()
Definition: ContextSource.php:81
ApiFeedRecentChanges\getFeedObject
getFeedObject( $feedFormat, $specialPageName)
Return a ChannelFeed object.
Definition: ApiFeedRecentChanges.php:120
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:55
ApiBase\PARAM_MIN
const PARAM_MIN
Definition: ApiBase.php:75
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:32
ApiFormatFeedWrapper
This printer is used to wrap an instance of the Feed class.
Definition: ApiFormatFeedWrapper.php:27
ApiBase\PARAM_MAX
const PARAM_MAX
Definition: ApiBase.php:73
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:707
$title
$title
Definition: testCompression.php:38
ChangesFeed
Feed to Special:RecentChanges and Special:RecentChangesLinked.
Definition: ChangesFeed.php:32
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:197
ApiFeedRecentChanges\$specialPageFactory
SpecialPageFactory $specialPageFactory
Definition: ApiFeedRecentChanges.php:34
ApiFeedRecentChanges\execute
execute()
Format the rows (generated by SpecialRecentchanges or SpecialRecentchangeslinked) as an RSS/Atom feed...
Definition: ApiFeedRecentChanges.php:63
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1459
ApiFeedRecentChanges\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiFeedRecentChanges.php:198
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:70
ApiBase\getParameter
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition: ApiBase.php:827
ApiFeedRecentChanges
Recent changes feed.
Definition: ApiFeedRecentChanges.php:29
ChangesFeed\buildItems
static buildItems( $rows)
Generate the feed items given a row from the database.
Definition: ChangesFeed.php:73
ApiBase\getMain
getMain()
Get the main module.
Definition: ApiBase.php:456
ApiFeedRecentChanges\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiFeedRecentChanges.php:146
ApiFormatFeedWrapper\setResult
static setResult( $result, $feed, $feedItems)
Call this method to initialize output data.
Definition: ApiFormatFeedWrapper.php:39