MediaWiki
REL1_23
|
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']] ( 00136 $feedTitle, 00137 htmlspecialchars( $msg ), 00138 $feedUrl 00139 ); 00140 00141 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems ); 00142 } catch ( Exception $e ) { 00143 // Error results should not be cached 00144 $this->getMain()->setCacheMaxAge( 0 ); 00145 00146 // @todo FIXME: Localise brackets 00147 $feedTitle = $wgSitename . ' - Error - ' . 00148 wfMessage( 'watchlist' )->inContentLanguage()->text() . 00149 ' [' . $wgLanguageCode . ']'; 00150 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL(); 00151 00152 $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss'; 00153 $msg = wfMessage( 'watchlist' )->inContentLanguage()->escaped(); 00154 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, $msg, $feedUrl ); 00155 00156 if ( $e instanceof UsageException ) { 00157 $errorCode = $e->getCodeString(); 00158 } else { 00159 // Something is seriously wrong 00160 $errorCode = 'internal_api_error'; 00161 } 00162 00163 $errorText = $e->getMessage(); 00164 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' ); 00165 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems ); 00166 } 00167 } 00168 00173 private function createFeedItem( $info ) { 00174 $titleStr = $info['title']; 00175 $title = Title::newFromText( $titleStr ); 00176 if ( $this->linkToDiffs && isset( $info['revid'] ) ) { 00177 $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) ); 00178 } else { 00179 $titleUrl = $title->getFullURL(); 00180 } 00181 $comment = isset( $info['comment'] ) ? $info['comment'] : null; 00182 00183 // Create an anchor to section. 00184 // The anchor won't work for sections that have dupes on page 00185 // as there's no way to strip that info from ApiWatchlist (apparently?). 00186 // RegExp in the line below is equal to Linker::formatAutocomments(). 00187 if ( $this->linkToSections && $comment !== null && 00188 preg_match( '!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment, $matches ) 00189 ) { 00190 global $wgParser; 00191 00192 $sectionTitle = $wgParser->stripSectionName( $matches[2] ); 00193 $sectionTitle = Sanitizer::normalizeSectionNameWhitespace( $sectionTitle ); 00194 $titleUrl .= Title::newFromText( '#' . $sectionTitle )->getFragmentForURL(); 00195 } 00196 00197 $timestamp = $info['timestamp']; 00198 $user = $info['user']; 00199 00200 $completeText = "$comment ($user)"; 00201 00202 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user ); 00203 } 00204 00205 private function getWatchlistModule() { 00206 if ( $this->watchlistModule === null ) { 00207 $this->watchlistModule = $this->getMain()->getModuleManager()->getModule( 'query' ) 00208 ->getModuleManager()->getModule( 'watchlist' ); 00209 } 00210 00211 return $this->watchlistModule; 00212 } 00213 00214 public function getAllowedParams( $flags = 0 ) { 00215 global $wgFeedClasses; 00216 $feedFormatNames = array_keys( $wgFeedClasses ); 00217 $ret = array( 00218 'feedformat' => array( 00219 ApiBase::PARAM_DFLT => 'rss', 00220 ApiBase::PARAM_TYPE => $feedFormatNames 00221 ), 00222 'hours' => array( 00223 ApiBase::PARAM_DFLT => 24, 00224 ApiBase::PARAM_TYPE => 'integer', 00225 ApiBase::PARAM_MIN => 1, 00226 ApiBase::PARAM_MAX => 72, 00227 ), 00228 'linktodiffs' => false, 00229 'linktosections' => false, 00230 ); 00231 if ( $flags ) { 00232 $wlparams = $this->getWatchlistModule()->getAllowedParams( $flags ); 00233 $ret['allrev'] = $wlparams['allrev']; 00234 $ret['wlowner'] = $wlparams['owner']; 00235 $ret['wltoken'] = $wlparams['token']; 00236 $ret['wlshow'] = $wlparams['show']; 00237 $ret['wltype'] = $wlparams['type']; 00238 $ret['wlexcludeuser'] = $wlparams['excludeuser']; 00239 } else { 00240 $ret['allrev'] = null; 00241 $ret['wlowner'] = null; 00242 $ret['wltoken'] = null; 00243 $ret['wlshow'] = null; 00244 $ret['wltype'] = null; 00245 $ret['wlexcludeuser'] = null; 00246 } 00247 00248 return $ret; 00249 } 00250 00251 public function getParamDescription() { 00252 $wldescr = $this->getWatchlistModule()->getParamDescription(); 00253 00254 return array( 00255 'feedformat' => 'The format of the feed', 00256 'hours' => 'List pages modified within this many hours from now', 00257 'linktodiffs' => 'Link to change differences instead of article pages', 00258 'linktosections' => 'Link directly to changed sections if possible', 00259 'allrev' => $wldescr['allrev'], 00260 'wlowner' => $wldescr['owner'], 00261 'wltoken' => $wldescr['token'], 00262 'wlshow' => $wldescr['show'], 00263 'wltype' => $wldescr['type'], 00264 'wlexcludeuser' => $wldescr['excludeuser'], 00265 ); 00266 } 00267 00268 public function getDescription() { 00269 return 'Returns a watchlist feed.'; 00270 } 00271 00272 public function getPossibleErrors() { 00273 return array_merge( parent::getPossibleErrors(), array( 00274 array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ), 00275 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ), 00276 ) ); 00277 } 00278 00279 public function getExamples() { 00280 return array( 00281 'api.php?action=feedwatchlist', 00282 'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6' 00283 ); 00284 } 00285 00286 public function getHelpUrls() { 00287 return 'https://www.mediawiki.org/wiki/API:Watchlist_feed'; 00288 } 00289 }