MediaWiki  REL1_22
ApiFeedWatchlist.php
Go to the documentation of this file.
00001 <?php
00034 class ApiFeedWatchlist extends ApiBase {
00035 
00036     private $watchlistModule = null;
00037     private $linkToDiffs = false;
00038     private $linkToSections = false;
00039 
00045     public function getCustomPrinter() {
00046         return new ApiFormatFeedWrapper( $this->getMain() );
00047     }
00048 
00053     public function execute() {
00054         global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
00055 
00056         try {
00057             $params = $this->extractRequestParams();
00058 
00059             if ( !$wgFeed ) {
00060                 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
00061             }
00062 
00063             if ( !isset( $wgFeedClasses[$params['feedformat']] ) ) {
00064                 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
00065             }
00066 
00067             // limit to the number of hours going from now back
00068             $endTime = wfTimestamp( TS_MW, time() - intval( $params['hours'] * 60 * 60 ) );
00069 
00070             // Prepare parameters for nested request
00071             $fauxReqArr = array(
00072                 'action' => 'query',
00073                 'meta' => 'siteinfo',
00074                 'siprop' => 'general',
00075                 'list' => 'watchlist',
00076                 'wlprop' => 'title|user|comment|timestamp',
00077                 'wldir' => 'older', // reverse order - from newest to oldest
00078                 'wlend' => $endTime, // stop at this time
00079                 'wllimit' => min( 50, $wgFeedLimit )
00080             );
00081 
00082             if ( $params['wlowner'] !== null ) {
00083                 $fauxReqArr['wlowner'] = $params['wlowner'];
00084             }
00085             if ( $params['wltoken'] !== null ) {
00086                 $fauxReqArr['wltoken'] = $params['wltoken'];
00087             }
00088             if ( $params['wlexcludeuser'] !== null ) {
00089                 $fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser'];
00090             }
00091             if ( $params['wlshow'] !== null ) {
00092                 $fauxReqArr['wlshow'] = $params['wlshow'];
00093             }
00094             if ( $params['wltype'] !== null ) {
00095                 $fauxReqArr['wltype'] = $params['wltype'];
00096             }
00097 
00098             // Support linking to diffs instead of article
00099             if ( $params['linktodiffs'] ) {
00100                 $this->linkToDiffs = true;
00101                 $fauxReqArr['wlprop'] .= '|ids';
00102             }
00103 
00104             // Support linking directly to sections when possible
00105             // (possible only if section name is present in comment)
00106             if ( $params['linktosections'] ) {
00107                 $this->linkToSections = true;
00108             }
00109 
00110             // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
00111             if ( $params['allrev'] ) {
00112                 $fauxReqArr['wlallrev'] = '';
00113             }
00114 
00115             // Create the request
00116             $fauxReq = new FauxRequest( $fauxReqArr );
00117 
00118             // Execute
00119             $module = new ApiMain( $fauxReq );
00120             $module->execute();
00121 
00122             // Get data array
00123             $data = $module->getResultData();
00124 
00125             $feedItems = array();
00126             foreach ( (array)$data['query']['watchlist'] as $info ) {
00127                 $feedItems[] = $this->createFeedItem( $info );
00128             }
00129 
00130             $msg = wfMessage( 'watchlist' )->inContentLanguage()->text();
00131 
00132             $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
00133             $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
00134 
00135             $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( $msg ), $feedUrl );
00136 
00137             ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
00138 
00139         } catch ( Exception $e ) {
00140 
00141             // Error results should not be cached
00142             $this->getMain()->setCacheMaxAge( 0 );
00143 
00144             $feedTitle = $wgSitename . ' - Error - ' . wfMessage( 'watchlist' )->inContentLanguage()->text() . ' [' . $wgLanguageCode . ']';
00145             $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
00146 
00147             $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss';
00148             $msg = wfMessage( 'watchlist' )->inContentLanguage()->escaped();
00149             $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, $msg, $feedUrl );
00150 
00151             if ( $e instanceof UsageException ) {
00152                 $errorCode = $e->getCodeString();
00153             } else {
00154                 // Something is seriously wrong
00155                 $errorCode = 'internal_api_error';
00156             }
00157 
00158             $errorText = $e->getMessage();
00159             $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
00160             ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
00161         }
00162     }
00163 
00168     private function createFeedItem( $info ) {
00169         $titleStr = $info['title'];
00170         $title = Title::newFromText( $titleStr );
00171         if ( $this->linkToDiffs && isset( $info['revid'] ) ) {
00172             $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
00173         } else {
00174             $titleUrl = $title->getFullURL();
00175         }
00176         $comment = isset( $info['comment'] ) ? $info['comment'] : null;
00177 
00178         // Create an anchor to section.
00179         // The anchor won't work for sections that have dupes on page
00180         // as there's no way to strip that info from ApiWatchlist (apparently?).
00181         // RegExp in the line below is equal to Linker::formatAutocomments().
00182         if ( $this->linkToSections && $comment !== null && preg_match( '!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment, $matches ) ) {
00183             global $wgParser;
00184             $sectionTitle = $wgParser->stripSectionName( $matches[2] );
00185             $sectionTitle = Sanitizer::normalizeSectionNameWhitespace( $sectionTitle );
00186             $titleUrl .= Title::newFromText( '#' . $sectionTitle )->getFragmentForURL();
00187         }
00188 
00189         $timestamp = $info['timestamp'];
00190         $user = $info['user'];
00191 
00192         $completeText = "$comment ($user)";
00193 
00194         return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
00195     }
00196 
00197     private function getWatchlistModule() {
00198         if ( $this->watchlistModule === null ) {
00199             $this->watchlistModule = $this->getMain()->getModuleManager()->getModule( 'query' )
00200                 ->getModuleManager()->getModule( 'watchlist' );
00201         }
00202         return $this->watchlistModule;
00203     }
00204 
00205     public function getAllowedParams( $flags = 0 ) {
00206         global $wgFeedClasses;
00207         $feedFormatNames = array_keys( $wgFeedClasses );
00208         $ret = array(
00209             'feedformat' => array(
00210                 ApiBase::PARAM_DFLT => 'rss',
00211                 ApiBase::PARAM_TYPE => $feedFormatNames
00212             ),
00213             'hours' => array(
00214                 ApiBase::PARAM_DFLT => 24,
00215                 ApiBase::PARAM_TYPE => 'integer',
00216                 ApiBase::PARAM_MIN => 1,
00217                 ApiBase::PARAM_MAX => 72,
00218             ),
00219             'linktodiffs' => false,
00220             'linktosections' => false,
00221         );
00222         if ( $flags ) {
00223             $wlparams = $this->getWatchlistModule()->getAllowedParams( $flags );
00224             $ret['allrev'] = $wlparams['allrev'];
00225             $ret['wlowner'] = $wlparams['owner'];
00226             $ret['wltoken'] = $wlparams['token'];
00227             $ret['wlshow'] = $wlparams['show'];
00228             $ret['wltype'] = $wlparams['type'];
00229             $ret['wlexcludeuser'] = $wlparams['excludeuser'];
00230         } else {
00231             $ret['allrev'] = null;
00232             $ret['wlowner'] = null;
00233             $ret['wltoken'] = null;
00234             $ret['wlshow'] = null;
00235             $ret['wltype'] = null;
00236             $ret['wlexcludeuser'] = null;
00237         }
00238         return $ret;
00239     }
00240 
00241     public function getParamDescription() {
00242         $wldescr = $this->getWatchlistModule()->getParamDescription();
00243         return array(
00244             'feedformat' => 'The format of the feed',
00245             'hours' => 'List pages modified within this many hours from now',
00246             'linktodiffs' => 'Link to change differences instead of article pages',
00247             'linktosections' => 'Link directly to changed sections if possible',
00248             'allrev' => $wldescr['allrev'],
00249             'wlowner' => $wldescr['owner'],
00250             'wltoken' => $wldescr['token'],
00251             'wlshow' => $wldescr['show'],
00252             'wltype' => $wldescr['type'],
00253             'wlexcludeuser' => $wldescr['excludeuser'],
00254         );
00255     }
00256 
00257     public function getDescription() {
00258         return 'Returns a watchlist feed';
00259     }
00260 
00261     public function getPossibleErrors() {
00262         return array_merge( parent::getPossibleErrors(), array(
00263             array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
00264             array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
00265         ) );
00266     }
00267 
00268     public function getExamples() {
00269         return array(
00270             'api.php?action=feedwatchlist',
00271             'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6'
00272         );
00273     }
00274 
00275     public function getHelpUrls() {
00276         return 'https://www.mediawiki.org/wiki/API:Watchlist_feed';
00277     }
00278 }