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 ( 'ApiQueryBase.php' );
00029 }
00030
00037 class ApiQueryWatchlist extends ApiQueryGeneratorBase {
00038
00039 public function __construct( $query, $moduleName ) {
00040 parent :: __construct( $query, $moduleName, 'wl' );
00041 }
00042
00043 public function execute() {
00044 $this->run();
00045 }
00046
00047 public function executeGenerator( $resultPageSet ) {
00048 $this->run( $resultPageSet );
00049 }
00050
00051 private $fld_ids = false, $fld_title = false, $fld_patrol = false, $fld_flags = false,
00052 $fld_timestamp = false, $fld_user = false, $fld_comment = false, $fld_parsedcomment = false, $fld_sizes = false,
00053 $fld_notificationtimestamp = false;
00054
00055 private function run( $resultPageSet = null ) {
00056 global $wgUser;
00057
00058 $this->selectNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
00059
00060 $params = $this->extractRequestParams();
00061
00062 if ( !is_null( $params['owner'] ) && !is_null( $params['token'] ) ) {
00063 $user = User::newFromName( $params['owner'], false );
00064 if ( !$user->getId() ) {
00065 $this->dieUsage( 'Specified user does not exist', 'bad_wlowner' );
00066 }
00067 $token = $user->getOption( 'watchlisttoken' );
00068 if ( $token == '' || $token != $params['token'] ) {
00069 $this->dieUsage( 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences', 'bad_wltoken' );
00070 }
00071 } elseif ( !$wgUser->isLoggedIn() ) {
00072 $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
00073 } else {
00074 $user = $wgUser;
00075 }
00076
00077 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
00078
00079 $prop = array_flip( $params['prop'] );
00080
00081 $this->fld_ids = isset( $prop['ids'] );
00082 $this->fld_title = isset( $prop['title'] );
00083 $this->fld_flags = isset( $prop['flags'] );
00084 $this->fld_user = isset( $prop['user'] );
00085 $this->fld_comment = isset( $prop['comment'] );
00086 $this->fld_parsedcomment = isset ( $prop['parsedcomment'] );
00087 $this->fld_timestamp = isset( $prop['timestamp'] );
00088 $this->fld_sizes = isset( $prop['sizes'] );
00089 $this->fld_patrol = isset( $prop['patrol'] );
00090 $this->fld_notificationtimestamp = isset( $prop['notificationtimestamp'] );
00091
00092 if ( $this->fld_patrol ) {
00093 if ( !$user->useRCPatrol() && !$user->useNPPatrol() )
00094 $this->dieUsage( 'patrol property is not available', 'patrol' );
00095 }
00096 }
00097
00098 $this->addFields( array (
00099 'rc_namespace',
00100 'rc_title',
00101 'rc_timestamp'
00102 ) );
00103
00104 if ( is_null( $resultPageSet ) ) {
00105 $this->addFields( array (
00106 'rc_cur_id',
00107 'rc_this_oldid'
00108 ) );
00109
00110 $this->addFieldsIf( 'rc_new', $this->fld_flags );
00111 $this->addFieldsIf( 'rc_minor', $this->fld_flags );
00112 $this->addFieldsIf( 'rc_bot', $this->fld_flags );
00113 $this->addFieldsIf( 'rc_user', $this->fld_user );
00114 $this->addFieldsIf( 'rc_user_text', $this->fld_user );
00115 $this->addFieldsIf( 'rc_comment', $this->fld_comment || $this->fld_parsedcomment );
00116 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrol );
00117 $this->addFieldsIf( 'rc_old_len', $this->fld_sizes );
00118 $this->addFieldsIf( 'rc_new_len', $this->fld_sizes );
00119 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp );
00120 } elseif ( $params['allrev'] ) {
00121 $this->addFields( 'rc_this_oldid' );
00122 } else {
00123 $this->addFields( 'rc_cur_id' );
00124 }
00125
00126 $this->addTables( array (
00127 'watchlist',
00128 'page',
00129 'recentchanges'
00130 ) );
00131
00132 $userId = $user->getId();
00133 $this->addWhere( array (
00134 'wl_namespace = rc_namespace',
00135 'wl_title = rc_title',
00136 'rc_cur_id = page_id',
00137 'wl_user' => $userId,
00138 'rc_deleted' => 0,
00139 ) );
00140
00141 $this->addWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
00142 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
00143 $this->addWhereIf( 'rc_this_oldid=page_latest', !$params['allrev'] );
00144
00145 if ( !is_null( $params['show'] ) ) {
00146 $show = array_flip( $params['show'] );
00147
00148
00149 if ( ( isset ( $show['minor'] ) && isset ( $show['!minor'] ) )
00150 || ( isset ( $show['bot'] ) && isset ( $show['!bot'] ) )
00151 || ( isset ( $show['anon'] ) && isset ( $show['!anon'] ) )
00152 || ( isset ( $show['patrolled'] ) && isset ( $show['!patrolled'] ) ) ) {
00153
00154 $this->dieUsageMsg( array( 'show' ) );
00155 }
00156
00157
00158 if ( ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() )
00159 $this->dieUsage( "You need the patrol right to request the patrolled flag", 'permissiondenied' );
00160
00161
00162 $this->addWhereIf( 'rc_minor = 0', isset ( $show['!minor'] ) );
00163 $this->addWhereIf( 'rc_minor != 0', isset ( $show['minor'] ) );
00164 $this->addWhereIf( 'rc_bot = 0', isset ( $show['!bot'] ) );
00165 $this->addWhereIf( 'rc_bot != 0', isset ( $show['bot'] ) );
00166 $this->addWhereIf( 'rc_user = 0', isset ( $show['anon'] ) );
00167 $this->addWhereIf( 'rc_user != 0', isset ( $show['!anon'] ) );
00168 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
00169 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
00170 }
00171
00172 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) )
00173 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
00174 if ( !is_null( $params['user'] ) )
00175 $this->addWhereFld( 'rc_user_text', $params['user'] );
00176 if ( !is_null( $params['excludeuser'] ) )
00177 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
00178
00179 $db = $this->getDB();
00180
00181
00182 $this->addWhereIf( "rc_timestamp > ''", !isset ( $params['start'] ) && !isset ( $params['end'] ) && $db->getType() == 'mysql' );
00183
00184 $this->addOption( 'LIMIT', $params['limit'] + 1 );
00185
00186 $ids = array ();
00187 $count = 0;
00188 $res = $this->select( __METHOD__ );
00189
00190 while ( $row = $db->fetchObject( $res ) ) {
00191 if ( ++ $count > $params['limit'] ) {
00192
00193 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
00194 break;
00195 }
00196
00197 if ( is_null( $resultPageSet ) ) {
00198 $vals = $this->extractRowInfo( $row );
00199 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
00200 if ( !$fit )
00201 {
00202 $this->setContinueEnumParameter( 'start',
00203 wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
00204 break;
00205 }
00206 } else {
00207 if ( $params['allrev'] ) {
00208 $ids[] = intval( $row->rc_this_oldid );
00209 } else {
00210 $ids[] = intval( $row->rc_cur_id );
00211 }
00212 }
00213 }
00214
00215 $db->freeResult( $res );
00216
00217 if ( is_null( $resultPageSet ) ) {
00218 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
00219 }
00220 elseif ( $params['allrev'] ) {
00221 $resultPageSet->populateFromRevisionIDs( $ids );
00222 } else {
00223 $resultPageSet->populateFromPageIDs( $ids );
00224 }
00225 }
00226
00227 private function extractRowInfo( $row ) {
00228
00229 $vals = array ();
00230
00231 if ( $this->fld_ids ) {
00232 $vals['pageid'] = intval( $row->rc_cur_id );
00233 $vals['revid'] = intval( $row->rc_this_oldid );
00234 }
00235
00236 $title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
00237
00238 if ( $this->fld_title )
00239 ApiQueryBase::addTitleInfo( $vals, $title );
00240
00241 if ( $this->fld_user ) {
00242 $vals['user'] = $row->rc_user_text;
00243 if ( !$row->rc_user )
00244 $vals['anon'] = '';
00245 }
00246
00247 if ( $this->fld_flags ) {
00248 if ( $row->rc_new )
00249 $vals['new'] = '';
00250 if ( $row->rc_minor )
00251 $vals['minor'] = '';
00252 if ( $row->rc_bot )
00253 $vals['bot'] = '';
00254 }
00255
00256 if ( $this->fld_patrol && isset( $row->rc_patrolled ) )
00257 $vals['patrolled'] = '';
00258
00259 if ( $this->fld_timestamp )
00260 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp );
00261
00262 if ( $this->fld_sizes ) {
00263 $vals['oldlen'] = intval( $row->rc_old_len );
00264 $vals['newlen'] = intval( $row->rc_new_len );
00265 }
00266
00267 if ( $this->fld_notificationtimestamp )
00268 $vals['notificationtimestamp'] = ( $row->wl_notificationtimestamp == null ) ? '' : wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
00269
00270 if ( $this->fld_comment && isset( $row->rc_comment ) )
00271 $vals['comment'] = $row->rc_comment;
00272
00273 if ( $this->fld_parsedcomment && isset( $row->rc_comment ) ) {
00274 global $wgUser;
00275 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rc_comment, $title );
00276 }
00277
00278 return $vals;
00279 }
00280
00281 public function getAllowedParams() {
00282 return array (
00283 'allrev' => false,
00284 'start' => array (
00285 ApiBase :: PARAM_TYPE => 'timestamp'
00286 ),
00287 'end' => array (
00288 ApiBase :: PARAM_TYPE => 'timestamp'
00289 ),
00290 'namespace' => array (
00291 ApiBase :: PARAM_ISMULTI => true,
00292 ApiBase :: PARAM_TYPE => 'namespace'
00293 ),
00294 'user' => array(
00295 ApiBase :: PARAM_TYPE => 'user',
00296 ),
00297 'excludeuser' => array(
00298 ApiBase :: PARAM_TYPE => 'user',
00299 ),
00300 'dir' => array (
00301 ApiBase :: PARAM_DFLT => 'older',
00302 ApiBase :: PARAM_TYPE => array (
00303 'newer',
00304 'older'
00305 )
00306 ),
00307 'limit' => array (
00308 ApiBase :: PARAM_DFLT => 10,
00309 ApiBase :: PARAM_TYPE => 'limit',
00310 ApiBase :: PARAM_MIN => 1,
00311 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00312 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00313 ),
00314 'prop' => array (
00315 APIBase :: PARAM_ISMULTI => true,
00316 APIBase :: PARAM_DFLT => 'ids|title|flags',
00317 APIBase :: PARAM_TYPE => array (
00318 'ids',
00319 'title',
00320 'flags',
00321 'user',
00322 'comment',
00323 'parsedcomment',
00324 'timestamp',
00325 'patrol',
00326 'sizes',
00327 'notificationtimestamp'
00328 )
00329 ),
00330 'show' => array (
00331 ApiBase :: PARAM_ISMULTI => true,
00332 ApiBase :: PARAM_TYPE => array (
00333 'minor',
00334 '!minor',
00335 'bot',
00336 '!bot',
00337 'anon',
00338 '!anon',
00339 'patrolled',
00340 '!patrolled',
00341 )
00342 ),
00343 'owner' => array (
00344 ApiBase :: PARAM_TYPE => 'user'
00345 ),
00346 'token' => array (
00347 ApiBase :: PARAM_TYPE => 'string'
00348 )
00349 );
00350 }
00351
00352 public function getParamDescription() {
00353 return array (
00354 'allrev' => 'Include multiple revisions of the same page within given timeframe.',
00355 'start' => 'The timestamp to start enumerating from.',
00356 'end' => 'The timestamp to end enumerating.',
00357 'namespace' => 'Filter changes to only the given namespace(s).',
00358 'user' => 'Only list changes by this user',
00359 'excludeuser' => 'Don\'t list changes by this user',
00360 'dir' => 'In which direction to enumerate pages.',
00361 'limit' => 'How many total results to return per request.',
00362 'prop' => 'Which additional items to get (non-generator mode only).',
00363 'show' => array (
00364 'Show only items that meet this criteria.',
00365 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
00366 ),
00367 'owner' => "The name of the user whose watchlist you'd like to access",
00368 'token' => "Give a security token (settable in preferences) to allow access to another user's watchlist"
00369 );
00370 }
00371
00372 public function getDescription() {
00373 return "Get all recent changes to pages in the logged in user's watchlist";
00374 }
00375
00376 public function getPossibleErrors() {
00377 return array_merge( parent::getPossibleErrors(), array(
00378 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
00379 array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ),
00380 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
00381 array( 'code' => 'patrol', 'info' => 'patrol property is not available' ),
00382 array( 'show' ),
00383 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
00384 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
00385 ) );
00386 }
00387
00388 protected function getExamples() {
00389 return array (
00390 'api.php?action=query&list=watchlist',
00391 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
00392 'api.php?action=query&list=watchlist&wlallrev&wlprop=ids|title|timestamp|user|comment',
00393 'api.php?action=query&generator=watchlist&prop=info',
00394 'api.php?action=query&generator=watchlist&gwlallrev&prop=revisions&rvprop=timestamp|user',
00395 'api.php?action=query&list=watchlist&wlowner=Bob_Smith&wltoken=d8d562e9725ea1512894cdab28e5ceebc7f20237'
00396 );
00397 }
00398
00399 public function getVersion() {
00400 return __CLASS__ . ': $Id: ApiQueryWatchlist.php 69932 2010-07-26 08:03:21Z tstarling $';
00401 }
00402 }