MediaWiki
REL1_21
|
00001 <?php 00034 class ApiFeedWatchlist extends ApiBase { 00035 00041 public function getCustomPrinter() { 00042 return new ApiFormatFeedWrapper( $this->getMain() ); 00043 } 00044 00045 private $linkToDiffs = false; 00046 00051 public function execute() { 00052 global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode; 00053 00054 try { 00055 $params = $this->extractRequestParams(); 00056 00057 if( !$wgFeed ) { 00058 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' ); 00059 } 00060 00061 if( !isset( $wgFeedClasses[$params['feedformat']] ) ) { 00062 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' ); 00063 } 00064 00065 // limit to the number of hours going from now back 00066 $endTime = wfTimestamp( TS_MW, time() - intval( $params['hours'] * 60 * 60 ) ); 00067 00068 // Prepare parameters for nested request 00069 $fauxReqArr = array( 00070 'action' => 'query', 00071 'meta' => 'siteinfo', 00072 'siprop' => 'general', 00073 'list' => 'watchlist', 00074 'wlprop' => 'title|user|comment|timestamp', 00075 'wldir' => 'older', // reverse order - from newest to oldest 00076 'wlend' => $endTime, // stop at this time 00077 'wllimit' => ( 50 > $wgFeedLimit ) ? $wgFeedLimit : 50 00078 ); 00079 00080 if ( $params['wlowner'] !== null ) { 00081 $fauxReqArr['wlowner'] = $params['wlowner']; 00082 } 00083 if ( $params['wltoken'] !== null ) { 00084 $fauxReqArr['wltoken'] = $params['wltoken']; 00085 } 00086 if ( $params['wlexcludeuser'] !== null ) { 00087 $fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser']; 00088 } 00089 00090 // Support linking to diffs instead of article 00091 if ( $params['linktodiffs'] ) { 00092 $this->linkToDiffs = true; 00093 $fauxReqArr['wlprop'] .= '|ids'; 00094 } 00095 00096 // Check for 'allrev' parameter, and if found, show all revisions to each page on wl. 00097 if ( $params['allrev'] ) { 00098 $fauxReqArr['wlallrev'] = ''; 00099 } 00100 00101 // Create the request 00102 $fauxReq = new FauxRequest( $fauxReqArr ); 00103 00104 // Execute 00105 $module = new ApiMain( $fauxReq ); 00106 $module->execute(); 00107 00108 // Get data array 00109 $data = $module->getResultData(); 00110 00111 $feedItems = array(); 00112 foreach ( (array)$data['query']['watchlist'] as $info ) { 00113 $feedItems[] = $this->createFeedItem( $info ); 00114 } 00115 00116 $msg = wfMessage( 'watchlist' )->inContentLanguage()->text(); 00117 00118 $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']'; 00119 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL(); 00120 00121 $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( $msg ), $feedUrl ); 00122 00123 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems ); 00124 00125 } catch ( Exception $e ) { 00126 00127 // Error results should not be cached 00128 $this->getMain()->setCacheMaxAge( 0 ); 00129 00130 $feedTitle = $wgSitename . ' - Error - ' . wfMessage( 'watchlist' )->inContentLanguage()->text() . ' [' . $wgLanguageCode . ']'; 00131 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL(); 00132 00133 $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss'; 00134 $msg = wfMessage( 'watchlist' )->inContentLanguage()->escaped(); 00135 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, $msg, $feedUrl ); 00136 00137 if ( $e instanceof UsageException ) { 00138 $errorCode = $e->getCodeString(); 00139 } else { 00140 // Something is seriously wrong 00141 $errorCode = 'internal_api_error'; 00142 } 00143 00144 $errorText = $e->getMessage(); 00145 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' ); 00146 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems ); 00147 } 00148 } 00149 00154 private function createFeedItem( $info ) { 00155 $titleStr = $info['title']; 00156 $title = Title::newFromText( $titleStr ); 00157 if ( $this->linkToDiffs && isset( $info['revid'] ) ) { 00158 $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) ); 00159 } else { 00160 $titleUrl = $title->getFullURL(); 00161 } 00162 $comment = isset( $info['comment'] ) ? $info['comment'] : null; 00163 $timestamp = $info['timestamp']; 00164 $user = $info['user']; 00165 00166 $completeText = "$comment ($user)"; 00167 00168 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user ); 00169 } 00170 00171 public function getAllowedParams() { 00172 global $wgFeedClasses; 00173 $feedFormatNames = array_keys( $wgFeedClasses ); 00174 return array ( 00175 'feedformat' => array( 00176 ApiBase::PARAM_DFLT => 'rss', 00177 ApiBase::PARAM_TYPE => $feedFormatNames 00178 ), 00179 'hours' => array( 00180 ApiBase::PARAM_DFLT => 24, 00181 ApiBase::PARAM_TYPE => 'integer', 00182 ApiBase::PARAM_MIN => 1, 00183 ApiBase::PARAM_MAX => 72, 00184 ), 00185 'allrev' => false, 00186 'wlowner' => array( 00187 ApiBase::PARAM_TYPE => 'user' 00188 ), 00189 'wltoken' => array( 00190 ApiBase::PARAM_TYPE => 'string' 00191 ), 00192 'wlexcludeuser' => array( 00193 ApiBase::PARAM_TYPE => 'user' 00194 ), 00195 'linktodiffs' => false, 00196 ); 00197 } 00198 00199 public function getParamDescription() { 00200 return array( 00201 'feedformat' => 'The format of the feed', 00202 'hours' => 'List pages modified within this many hours from now', 00203 'allrev' => 'Include multiple revisions of the same page within given timeframe', 00204 'wlowner' => "The user whose watchlist you want (must be accompanied by {$this->getModulePrefix()}wltoken if it's not you)", 00205 'wltoken' => 'Security token that requested user set in their preferences', 00206 'wlexcludeuser' => 'A user whose edits should not be shown in the watchlist', 00207 'linktodiffs' => 'Link to change differences instead of article pages', 00208 ); 00209 } 00210 00211 public function getDescription() { 00212 return 'Returns a watchlist feed'; 00213 } 00214 00215 public function getPossibleErrors() { 00216 return array_merge( parent::getPossibleErrors(), array( 00217 array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ), 00218 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ), 00219 ) ); 00220 } 00221 00222 public function getExamples() { 00223 return array( 00224 'api.php?action=feedwatchlist', 00225 'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6' 00226 ); 00227 } 00228 00229 public function getHelpUrls() { 00230 return 'https://www.mediawiki.org/wiki/API:Watchlist_feed'; 00231 } 00232 }