00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if ( !defined( 'MEDIAWIKI' ) ) {
00027
00028 require_once ( "ApiBase.php" );
00029 }
00030
00038 class ApiFeedWatchlist extends ApiBase {
00039
00040 public function __construct( $main, $action ) {
00041 parent :: __construct( $main, $action );
00042 }
00043
00047 public function getCustomPrinter() {
00048 return new ApiFormatFeedWrapper( $this->getMain() );
00049 }
00050
00055 public function execute() {
00056
00057 global $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgContLanguageCode;
00058
00059 try {
00060 $params = $this->extractRequestParams();
00061
00062
00063 $endTime = wfTimestamp( TS_MW, time() - intval( $params['hours'] * 60 * 60 ) );
00064
00065 $dbr = wfGetDB( DB_SLAVE );
00066
00067 $fauxReqArr = array (
00068 'action' => 'query',
00069 'meta' => 'siteinfo',
00070 'siprop' => 'general',
00071 'list' => 'watchlist',
00072 'wlprop' => 'title|user|comment|timestamp',
00073 'wldir' => 'older',
00074 'wlend' => $dbr->timestamp( $endTime ),
00075 'wllimit' => ( 50 > $wgFeedLimit ) ? $wgFeedLimit : 50
00076 );
00077
00078 if ( !is_null( $params['wlowner'] ) ) {
00079 $fauxReqArr['wlowner'] = $params['wlowner'];
00080 }
00081 if ( !is_null( $params['wltoken'] ) ) {
00082 $fauxReqArr['wltoken'] = $params['wltoken'];
00083 }
00084
00085
00086 if ( !is_null ( $params['allrev'] ) ) {
00087 $fauxReqArr['wlallrev'] = '';
00088 }
00089
00090
00091 $fauxReq = new FauxRequest ( $fauxReqArr );
00092
00093
00094 $module = new ApiMain( $fauxReq );
00095 $module->execute();
00096
00097
00098 $data = $module->getResultData();
00099
00100 $feedItems = array();
00101 foreach ( (array)$data['query']['watchlist'] as $info ) {
00102 $feedItems[] = $this->createFeedItem( $info );
00103 }
00104
00105 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgContLanguageCode . ']';
00106 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullUrl();
00107
00108 $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
00109
00110 ApiFormatFeedWrapper :: setResult( $this->getResult(), $feed, $feedItems );
00111
00112 } catch ( Exception $e ) {
00113
00114
00115 $this->getMain()->setCacheMaxAge( 0 );
00116
00117 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgContLanguageCode . ']';
00118 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullUrl();
00119
00120 $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss';
00121 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
00122
00123 if ( $e instanceof UsageException ) {
00124 $errorCode = $e->getCodeString();
00125 } else {
00126
00127 $errorCode = 'internal_api_error';
00128 }
00129
00130 $errorText = $e->getMessage();
00131 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, "", "", "" );
00132 ApiFormatFeedWrapper :: setResult( $this->getResult(), $feed, $feedItems );
00133 }
00134 }
00135
00136 private function createFeedItem( $info ) {
00137 $titleStr = $info['title'];
00138 $title = Title :: newFromText( $titleStr );
00139 $titleUrl = $title->getFullUrl();
00140 $comment = isset( $info['comment'] ) ? $info['comment'] : null;
00141 $timestamp = $info['timestamp'];
00142 $user = $info['user'];
00143
00144 $completeText = "$comment ($user)";
00145
00146 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
00147 }
00148
00149 public function getAllowedParams() {
00150 global $wgFeedClasses;
00151 $feedFormatNames = array_keys( $wgFeedClasses );
00152 return array (
00153 'feedformat' => array (
00154 ApiBase :: PARAM_DFLT => 'rss',
00155 ApiBase :: PARAM_TYPE => $feedFormatNames
00156 ),
00157 'hours' => array (
00158 ApiBase :: PARAM_DFLT => 24,
00159 ApiBase :: PARAM_TYPE => 'integer',
00160 ApiBase :: PARAM_MIN => 1,
00161 ApiBase :: PARAM_MAX => 72,
00162 ),
00163 'allrev' => null,
00164 'wlowner' => array (
00165 ApiBase :: PARAM_TYPE => 'user'
00166 ),
00167 'wltoken' => array (
00168 ApiBase :: PARAM_TYPE => 'string'
00169 )
00170 );
00171 }
00172
00173 public function getParamDescription() {
00174 return array (
00175 'feedformat' => 'The format of the feed',
00176 'hours' => 'List pages modified within this many hours from now',
00177 'allrev' => 'Include multiple revisions of the same page within given timeframe.',
00178 'wlowner' => "The user whose watchlist you want (must be accompanied by wltoken if it's not you)",
00179 'wltoken' => 'Security token that requested user set in their preferences'
00180 );
00181 }
00182
00183 public function getDescription() {
00184 return 'This module returns a watchlist feed';
00185 }
00186
00187 protected function getExamples() {
00188 return array (
00189 'api.php?action=feedwatchlist'
00190 );
00191 }
00192
00193 public function getVersion() {
00194 return __CLASS__ . ': $Id: ApiFeedWatchlist.php 69357 2010-07-14 22:39:23Z mah $';
00195 }
00196 }