MediaWiki  1.23.13
ApiFeedWatchlist.php
Go to the documentation of this file.
1 <?php
34 class ApiFeedWatchlist extends ApiBase {
35 
36  private $watchlistModule = null;
37  private $linkToDiffs = false;
38  private $linkToSections = false;
39 
45  public function getCustomPrinter() {
46  return new ApiFormatFeedWrapper( $this->getMain() );
47  }
48 
53  public function execute() {
54  global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
55 
56  try {
57  $params = $this->extractRequestParams();
58 
59  if ( !$wgFeed ) {
60  $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
61  }
62 
63  if ( !isset( $wgFeedClasses[$params['feedformat']] ) ) {
64  $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
65  }
66 
67  // limit to the number of hours going from now back
68  $endTime = wfTimestamp( TS_MW, time() - intval( $params['hours'] * 60 * 60 ) );
69 
70  // Prepare parameters for nested request
71  $fauxReqArr = array(
72  'action' => 'query',
73  'meta' => 'siteinfo',
74  'siprop' => 'general',
75  'list' => 'watchlist',
76  'wlprop' => 'title|user|comment|timestamp',
77  'wldir' => 'older', // reverse order - from newest to oldest
78  'wlend' => $endTime, // stop at this time
79  'wllimit' => min( 50, $wgFeedLimit )
80  );
81 
82  if ( $params['wlowner'] !== null ) {
83  $fauxReqArr['wlowner'] = $params['wlowner'];
84  }
85  if ( $params['wltoken'] !== null ) {
86  $fauxReqArr['wltoken'] = $params['wltoken'];
87  }
88  if ( $params['wlexcludeuser'] !== null ) {
89  $fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser'];
90  }
91  if ( $params['wlshow'] !== null ) {
92  $fauxReqArr['wlshow'] = $params['wlshow'];
93  }
94  if ( $params['wltype'] !== null ) {
95  $fauxReqArr['wltype'] = $params['wltype'];
96  }
97 
98  // Support linking to diffs instead of article
99  if ( $params['linktodiffs'] ) {
100  $this->linkToDiffs = true;
101  $fauxReqArr['wlprop'] .= '|ids';
102  }
103 
104  // Support linking directly to sections when possible
105  // (possible only if section name is present in comment)
106  if ( $params['linktosections'] ) {
107  $this->linkToSections = true;
108  }
109 
110  // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
111  if ( $params['allrev'] ) {
112  $fauxReqArr['wlallrev'] = '';
113  }
114 
115  // Create the request
116  $fauxReq = new FauxRequest( $fauxReqArr );
117 
118  // Execute
119  $module = new ApiMain( $fauxReq );
120  $module->execute();
121 
122  // Get data array
123  $data = $module->getResultData();
124 
125  $feedItems = array();
126  foreach ( (array)$data['query']['watchlist'] as $info ) {
127  $feedItems[] = $this->createFeedItem( $info );
128  }
129 
130  $msg = wfMessage( 'watchlist' )->inContentLanguage()->text();
131 
132  $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
133  $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
134 
135  $feed = new $wgFeedClasses[$params['feedformat']] (
136  $feedTitle,
137  htmlspecialchars( $msg ),
138  $feedUrl
139  );
140 
141  ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
142  } catch ( Exception $e ) {
143  // Error results should not be cached
144  $this->getMain()->setCacheMaxAge( 0 );
145 
146  // @todo FIXME: Localise brackets
147  $feedTitle = $wgSitename . ' - Error - ' .
148  wfMessage( 'watchlist' )->inContentLanguage()->text() .
149  ' [' . $wgLanguageCode . ']';
150  $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
151 
152  $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss';
153  $msg = wfMessage( 'watchlist' )->inContentLanguage()->escaped();
154  $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, $msg, $feedUrl );
155 
156  if ( $e instanceof UsageException ) {
157  $errorCode = $e->getCodeString();
158  } else {
159  // Something is seriously wrong
160  $errorCode = 'internal_api_error';
161  }
162 
163  $errorText = $e->getMessage();
164  $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
165  ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
166  }
167  }
168 
173  private function createFeedItem( $info ) {
174  $titleStr = $info['title'];
175  $title = Title::newFromText( $titleStr );
176  if ( $this->linkToDiffs && isset( $info['revid'] ) ) {
177  $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
178  } else {
179  $titleUrl = $title->getFullURL();
180  }
181  $comment = isset( $info['comment'] ) ? $info['comment'] : null;
182 
183  // Create an anchor to section.
184  // The anchor won't work for sections that have dupes on page
185  // as there's no way to strip that info from ApiWatchlist (apparently?).
186  // RegExp in the line below is equal to Linker::formatAutocomments().
187  if ( $this->linkToSections && $comment !== null &&
188  preg_match( '!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment, $matches )
189  ) {
191 
192  $sectionTitle = $wgParser->stripSectionName( $matches[2] );
193  $sectionTitle = Sanitizer::normalizeSectionNameWhitespace( $sectionTitle );
194  $titleUrl .= Title::newFromText( '#' . $sectionTitle )->getFragmentForURL();
195  }
196 
197  $timestamp = $info['timestamp'];
198  $user = $info['user'];
199 
200  $completeText = "$comment ($user)";
201 
202  return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
203  }
204 
205  private function getWatchlistModule() {
206  if ( $this->watchlistModule === null ) {
207  $this->watchlistModule = $this->getMain()->getModuleManager()->getModule( 'query' )
208  ->getModuleManager()->getModule( 'watchlist' );
209  }
210 
211  return $this->watchlistModule;
212  }
213 
214  public function getAllowedParams( $flags = 0 ) {
215  global $wgFeedClasses;
216  $feedFormatNames = array_keys( $wgFeedClasses );
217  $ret = array(
218  'feedformat' => array(
219  ApiBase::PARAM_DFLT => 'rss',
220  ApiBase::PARAM_TYPE => $feedFormatNames
221  ),
222  'hours' => array(
223  ApiBase::PARAM_DFLT => 24,
224  ApiBase::PARAM_TYPE => 'integer',
225  ApiBase::PARAM_MIN => 1,
226  ApiBase::PARAM_MAX => 72,
227  ),
228  'linktodiffs' => false,
229  'linktosections' => false,
230  );
231  if ( $flags ) {
232  $wlparams = $this->getWatchlistModule()->getAllowedParams( $flags );
233  $ret['allrev'] = $wlparams['allrev'];
234  $ret['wlowner'] = $wlparams['owner'];
235  $ret['wltoken'] = $wlparams['token'];
236  $ret['wlshow'] = $wlparams['show'];
237  $ret['wltype'] = $wlparams['type'];
238  $ret['wlexcludeuser'] = $wlparams['excludeuser'];
239  } else {
240  $ret['allrev'] = null;
241  $ret['wlowner'] = null;
242  $ret['wltoken'] = null;
243  $ret['wlshow'] = null;
244  $ret['wltype'] = null;
245  $ret['wlexcludeuser'] = null;
246  }
247 
248  return $ret;
249  }
250 
251  public function getParamDescription() {
252  $wldescr = $this->getWatchlistModule()->getParamDescription();
253 
254  return array(
255  'feedformat' => 'The format of the feed',
256  'hours' => 'List pages modified within this many hours from now',
257  'linktodiffs' => 'Link to change differences instead of article pages',
258  'linktosections' => 'Link directly to changed sections if possible',
259  'allrev' => $wldescr['allrev'],
260  'wlowner' => $wldescr['owner'],
261  'wltoken' => $wldescr['token'],
262  'wlshow' => $wldescr['show'],
263  'wltype' => $wldescr['type'],
264  'wlexcludeuser' => $wldescr['excludeuser'],
265  );
266  }
267 
268  public function getDescription() {
269  return 'Returns a watchlist feed.';
270  }
271 
272  public function getPossibleErrors() {
273  return array_merge( parent::getPossibleErrors(), array(
274  array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
275  array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
276  ) );
277  }
278 
279  public function getExamples() {
280  return array(
281  'api.php?action=feedwatchlist',
282  'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6'
283  );
284  }
285 
286  public function getHelpUrls() {
287  return 'https://www.mediawiki.org/wiki/API:Watchlist_feed';
288  }
289 }
ApiFeedWatchlist\getAllowedParams
getAllowedParams( $flags=0)
Definition: ApiFeedWatchlist.php:214
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:41
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: WebRequest.php:1275
ApiFeedWatchlist
This action allows users to get their watchlist items in RSS/Atom formats.
Definition: ApiFeedWatchlist.php:34
FeedItem
A base class for basic support for outputting syndication feeds in RSS and other formats.
Definition: Feed.php:38
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:189
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ApiFeedWatchlist\getDescription
getDescription()
Returns the description string for this module.
Definition: ApiFeedWatchlist.php:268
ApiFeedWatchlist\getCustomPrinter
getCustomPrinter()
This module uses a custom feed wrapper printer.
Definition: ApiFeedWatchlist.php:45
ApiFeedWatchlist\getExamples
getExamples()
Returns usage examples for this module.
Definition: ApiFeedWatchlist.php:279
$timestamp
if( $limit) $timestamp
Definition: importImages.php:104
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2530
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:50
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:205
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1530
Sanitizer\normalizeSectionNameWhitespace
static normalizeSectionNameWhitespace( $section)
Normalizes whitespace in a section name, such as might be returned by Parser::stripSectionName(),...
Definition: Sanitizer.php:1297
$params
$params
Definition: styleTest.css.php:40
SpecialPage\getTitleFor
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name.
Definition: SpecialPage.php:74
$flags
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2118
ApiFeedWatchlist\getHelpUrls
getHelpUrls()
Definition: ApiFeedWatchlist.php:286
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
ApiFeedWatchlist\$linkToSections
$linkToSections
Definition: ApiFeedWatchlist.php:38
ApiBase\PARAM_MIN
const PARAM_MIN
Definition: ApiBase.php:56
ApiFeedWatchlist\getPossibleErrors
getPossibleErrors()
Returns a list of all possible errors returned by the module.
Definition: ApiFeedWatchlist.php:272
ApiFeedWatchlist\$watchlistModule
$watchlistModule
Definition: ApiFeedWatchlist.php:36
ApiFormatFeedWrapper
This printer is used to wrap an instance of the Feed class.
Definition: ApiFormatBase.php:348
UsageException
This exception will be thrown when dieUsage is called to stop module execution.
Definition: ApiMain.php:1406
ApiFeedWatchlist\execute
execute()
Make a nested call to the API to request watchlist items in the last $hours.
Definition: ApiFeedWatchlist.php:53
wfMessage
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form externallinks including delete and has completed for all link tables default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
ApiBase\PARAM_MAX
const PARAM_MAX
Definition: ApiBase.php:52
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
$comment
$comment
Definition: importImages.php:107
ApiFeedWatchlist\createFeedItem
createFeedItem( $info)
Definition: ApiFeedWatchlist.php:173
TS_MW
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: GlobalFunctions.php:2478
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:687
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
$matches
if(!defined( 'MEDIAWIKI')) if(!isset( $wgVersion)) $matches
Definition: NoLocalSettings.php:33
ApiBase\dieUsage
dieUsage( $description, $errorCode, $httpRespCode=0, $extradata=null)
Throw a UsageException, which will (if uncaught) call the main module's error handler and die with an...
Definition: ApiBase.php:1363
ApiFeedWatchlist\getParamDescription
getParamDescription()
Returns an array of parameter descriptions.
Definition: ApiFeedWatchlist.php:251
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:237
ApiFeedWatchlist\$linkToDiffs
$linkToDiffs
Definition: ApiFeedWatchlist.php:37
$wgParser
$wgParser
Definition: Setup.php:587
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:46
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ApiBase\getMain
getMain()
Get the main module.
Definition: ApiBase.php:188
ApiFeedWatchlist\getWatchlistModule
getWatchlistModule()
Definition: ApiFeedWatchlist.php:205
ApiFormatFeedWrapper\setResult
static setResult( $result, $feed, $feedItems)
Call this method to initialize output data.
Definition: ApiFormatBase.php:360
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:1632