5 * Created on Sep 25, 2006
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * This query action allows clients to retrieve a list of recently modified pages
29 * that are part of the logged-in user's watchlist.
33 class ApiQueryWatchlist
extends ApiQueryGeneratorBase
{
35 public function __construct( $query, $moduleName ) {
36 parent
::__construct( $query, $moduleName, 'wl' );
39 public function execute() {
43 public function executeGenerator( $resultPageSet ) {
44 $this->run( $resultPageSet );
47 private $fld_ids = false, $fld_title = false, $fld_patrol = false, $fld_flags = false,
48 $fld_timestamp = false, $fld_user = false, $fld_comment = false, $fld_parsedcomment = false, $fld_sizes = false,
49 $fld_notificationtimestamp = false, $fld_userid = false, $fld_loginfo = false;
52 * @param $resultPageSet ApiPageSet
55 private function run( $resultPageSet = null ) {
56 $this->selectNamedDB( 'watchlist', DB_SLAVE
, 'watchlist' );
58 $params = $this->extractRequestParams();
60 $user = $this->getWatchlistUser( $params );
62 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
63 $prop = array_flip( $params['prop'] );
65 $this->fld_ids
= isset( $prop['ids'] );
66 $this->fld_title
= isset( $prop['title'] );
67 $this->fld_flags
= isset( $prop['flags'] );
68 $this->fld_user
= isset( $prop['user'] );
69 $this->fld_userid
= isset( $prop['userid'] );
70 $this->fld_comment
= isset( $prop['comment'] );
71 $this->fld_parsedcomment
= isset ( $prop['parsedcomment'] );
72 $this->fld_timestamp
= isset( $prop['timestamp'] );
73 $this->fld_sizes
= isset( $prop['sizes'] );
74 $this->fld_patrol
= isset( $prop['patrol'] );
75 $this->fld_notificationtimestamp
= isset( $prop['notificationtimestamp'] );
76 $this->fld_loginfo
= isset( $prop['loginfo'] );
78 if ( $this->fld_patrol
) {
79 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
80 $this->dieUsage( 'patrol property is not available', 'patrol' );
85 $this->addFields( array(
92 if ( is_null( $resultPageSet ) ) {
93 $this->addFields( array(
99 $this->addFieldsIf( array( 'rc_new', 'rc_minor', 'rc_bot' ), $this->fld_flags
);
100 $this->addFieldsIf( 'rc_user', $this->fld_user ||
$this->fld_userid
);
101 $this->addFieldsIf( 'rc_user_text', $this->fld_user
);
102 $this->addFieldsIf( 'rc_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
103 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrol
);
104 $this->addFieldsIf( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes
);
105 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp
);
106 $this->addFieldsIf( array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ), $this->fld_loginfo
);
107 } elseif ( $params['allrev'] ) {
108 $this->addFields( 'rc_this_oldid' );
110 $this->addFields( 'rc_cur_id' );
113 $this->addTables( array(
118 $userId = $user->getId();
119 $this->addJoinConds( array( 'watchlist' => array('INNER JOIN',
121 'wl_user' => $userId,
122 'wl_namespace=rc_namespace',
126 $this->addWhere( array(
130 $db = $this->getDB();
132 $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'],
133 $params['start'], $params['end'] );
134 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
136 if ( !$params['allrev'] ) {
137 $this->addTables( 'page' );
138 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN','rc_cur_id=page_id' ) ) );
139 $this->addWhere( 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG
);
142 if ( !is_null( $params['show'] ) ) {
143 $show = array_flip( $params['show'] );
145 /* Check for conflicting parameters. */
146 if ( ( isset ( $show['minor'] ) && isset ( $show['!minor'] ) )
147 ||
( isset ( $show['bot'] ) && isset ( $show['!bot'] ) )
148 ||
( isset ( $show['anon'] ) && isset ( $show['!anon'] ) )
149 ||
( isset ( $show['patrolled'] ) && isset ( $show['!patrolled'] ) )
152 $this->dieUsageMsg( 'show' );
155 // Check permissions.
156 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
157 $user = $this->getUser();
158 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
159 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
163 /* Add additional conditions to query depending upon parameters. */
164 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
165 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
166 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
167 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
168 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
169 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
170 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
171 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
174 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
175 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
177 if ( !is_null( $params['user'] ) ) {
178 $this->addWhereFld( 'rc_user_text', $params['user'] );
180 if ( !is_null( $params['excludeuser'] ) ) {
181 $this->addWhere( 'rc_user_text != ' . $db->addQuotes( $params['excludeuser'] ) );
184 // This is an index optimization for mysql, as done in the Special:Watchlist page
185 $this->addWhereIf( "rc_timestamp > ''", !isset( $params['start'] ) && !isset( $params['end'] ) && $db->getType() == 'mysql' );
187 $this->addOption( 'LIMIT', $params['limit'] +
1 );
191 $res = $this->select( __METHOD__
);
193 foreach ( $res as $row ) {
194 if ( ++
$count > $params['limit'] ) {
195 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
196 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
200 if ( is_null( $resultPageSet ) ) {
201 $vals = $this->extractRowInfo( $row );
202 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
204 $this->setContinueEnumParameter( 'start',
205 wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
209 if ( $params['allrev'] ) {
210 $ids[] = intval( $row->rc_this_oldid
);
212 $ids[] = intval( $row->rc_cur_id
);
217 if ( is_null( $resultPageSet ) ) {
218 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
219 } elseif ( $params['allrev'] ) {
220 $resultPageSet->populateFromRevisionIDs( $ids );
222 $resultPageSet->populateFromPageIDs( $ids );
226 private function extractRowInfo( $row ) {
229 if ( $this->fld_ids
) {
230 $vals['pageid'] = intval( $row->rc_cur_id
);
231 $vals['revid'] = intval( $row->rc_this_oldid
);
232 $vals['old_revid'] = intval( $row->rc_last_oldid
);
235 $title = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
237 if ( $this->fld_title
) {
238 ApiQueryBase
::addTitleInfo( $vals, $title );
241 if ( $this->fld_user ||
$this->fld_userid
) {
243 if ( $this->fld_user
) {
244 $vals['user'] = $row->rc_user_text
;
247 if ( $this->fld_userid
) {
248 $vals['user'] = $row->rc_user
;
251 if ( !$row->rc_user
) {
256 if ( $this->fld_flags
) {
257 if ( $row->rc_new
) {
260 if ( $row->rc_minor
) {
263 if ( $row->rc_bot
) {
268 if ( $this->fld_patrol
&& isset( $row->rc_patrolled
) ) {
269 $vals['patrolled'] = '';
272 if ( $this->fld_timestamp
) {
273 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
);
276 if ( $this->fld_sizes
) {
277 $vals['oldlen'] = intval( $row->rc_old_len
);
278 $vals['newlen'] = intval( $row->rc_new_len
);
281 if ( $this->fld_notificationtimestamp
) {
282 $vals['notificationtimestamp'] = ( $row->wl_notificationtimestamp
== null )
284 : wfTimestamp( TS_ISO_8601
, $row->wl_notificationtimestamp
);
287 if ( $this->fld_comment
&& isset( $row->rc_comment
) ) {
288 $vals['comment'] = $row->rc_comment
;
291 if ( $this->fld_parsedcomment
&& isset( $row->rc_comment
) ) {
292 $vals['parsedcomment'] = Linker
::formatComment( $row->rc_comment
, $title );
295 if ( $this->fld_loginfo
&& $row->rc_type
== RC_LOG
) {
296 $vals['logid'] = intval( $row->rc_logid
);
297 $vals['logtype'] = $row->rc_log_type
;
298 $vals['logaction'] = $row->rc_log_action
;
299 $logEntry = DatabaseLogEntry
::newFromRow( (array)$row );
300 ApiQueryLogEvents
::addLogParams(
303 $logEntry->getParameters(),
304 $logEntry->getType(),
305 $logEntry->getSubtype(),
306 $logEntry->getTimestamp()
313 public function getAllowedParams() {
317 ApiBase
::PARAM_TYPE
=> 'timestamp'
320 ApiBase
::PARAM_TYPE
=> 'timestamp'
322 'namespace' => array (
323 ApiBase
::PARAM_ISMULTI
=> true,
324 ApiBase
::PARAM_TYPE
=> 'namespace'
327 ApiBase
::PARAM_TYPE
=> 'user',
329 'excludeuser' => array(
330 ApiBase
::PARAM_TYPE
=> 'user',
333 ApiBase
::PARAM_DFLT
=> 'older',
334 ApiBase
::PARAM_TYPE
=> array(
340 ApiBase
::PARAM_DFLT
=> 10,
341 ApiBase
::PARAM_TYPE
=> 'limit',
342 ApiBase
::PARAM_MIN
=> 1,
343 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
344 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
347 ApiBase
::PARAM_ISMULTI
=> true,
348 ApiBase
::PARAM_DFLT
=> 'ids|title|flags',
349 ApiBase
::PARAM_TYPE
=> array(
360 'notificationtimestamp',
365 ApiBase
::PARAM_ISMULTI
=> true,
366 ApiBase
::PARAM_TYPE
=> array(
378 ApiBase
::PARAM_TYPE
=> 'user'
381 ApiBase
::PARAM_TYPE
=> 'string'
386 public function getParamDescription() {
387 $p = $this->getModulePrefix();
389 'allrev' => 'Include multiple revisions of the same page within given timeframe',
390 'start' => 'The timestamp to start enumerating from',
391 'end' => 'The timestamp to end enumerating',
392 'namespace' => 'Filter changes to only the given namespace(s)',
393 'user' => 'Only list changes by this user',
394 'excludeuser' => 'Don\'t list changes by this user',
395 'dir' => $this->getDirectionDescription( $p ),
396 'limit' => 'How many total results to return per request',
398 'Which additional items to get (non-generator mode only).',
399 ' ids - Adds revision ids and page ids',
400 ' title - Adds title of the page',
401 ' flags - Adds flags for the edit',
402 ' user - Adds the user who made the edit',
403 ' userid - Adds user id of whom made the edit',
404 ' comment - Adds comment of the edit',
405 ' parsedcomment - Adds parsed comment of the edit',
406 ' timestamp - Adds timestamp of the edit',
407 ' patrol - Tags edits that are patrolled',
408 ' sizes - Adds the old and new lengths of the page',
409 ' notificationtimestamp - Adds timestamp of when the user was last notified about the edit',
410 ' loginfo - Adds log information where appropriate',
413 'Show only items that meet this criteria.',
414 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
416 'owner' => 'The name of the user whose watchlist you\'d like to access',
417 'token' => 'Give a security token (settable in preferences) to allow access to another user\'s watchlist'
421 public function getResultProperties() {
425 'pageid' => 'integer',
426 'revid' => 'integer',
427 'old_revid' => 'integer'
438 'userid' => 'integer',
443 'minor' => 'boolean',
447 'patrolled' => 'boolean'
449 'timestamp' => array(
450 'timestamp' => 'timestamp'
453 'oldlen' => 'integer',
454 'newlen' => 'integer'
456 'notificationtimestamp' => array(
457 'notificationtimestamp' => array(
458 ApiBase
::PROP_TYPE
=> 'timestamp',
459 ApiBase
::PROP_NULLABLE
=> true
464 ApiBase
::PROP_TYPE
=> 'string',
465 ApiBase
::PROP_NULLABLE
=> true
468 'parsedcomment' => array(
469 'parsedcomment' => array(
470 ApiBase
::PROP_TYPE
=> 'string',
471 ApiBase
::PROP_NULLABLE
=> true
476 ApiBase
::PROP_TYPE
=> 'integer',
477 ApiBase
::PROP_NULLABLE
=> true
480 ApiBase
::PROP_TYPE
=> $wgLogTypes,
481 ApiBase
::PROP_NULLABLE
=> true
483 'logaction' => array(
484 ApiBase
::PROP_TYPE
=> 'string',
485 ApiBase
::PROP_NULLABLE
=> true
491 public function getDescription() {
492 return "Get all recent changes to pages in the logged in user's watchlist";
495 public function getPossibleErrors() {
496 return array_merge( parent
::getPossibleErrors(), array(
497 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
498 array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ),
499 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
500 array( 'code' => 'patrol', 'info' => 'patrol property is not available' ),
502 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
503 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
507 public function getExamples() {
509 'api.php?action=query&list=watchlist',
510 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
511 'api.php?action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment',
512 'api.php?action=query&generator=watchlist&prop=info',
513 'api.php?action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user',
514 'api.php?action=query&list=watchlist&wlowner=Bob_Smith&wltoken=d8d562e9725ea1512894cdab28e5ceebc7f20237'
518 public function getHelpUrls() {
519 return 'https://www.mediawiki.org/wiki/API:Watchlist';
522 public function getVersion() {
523 return __CLASS__
. ': $Id$';