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,
48 $fld_flags = false, $fld_timestamp = false, $fld_user = false,
49 $fld_comment = false, $fld_parsedcomment = false, $fld_sizes = false,
50 $fld_notificationtimestamp = false, $fld_userid = false,
54 * @param $resultPageSet ApiPageSet
57 private function run( $resultPageSet = null ) {
58 $this->selectNamedDB( 'watchlist', DB_SLAVE
, 'watchlist' );
60 $params = $this->extractRequestParams();
62 $user = $this->getWatchlistUser( $params );
64 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
65 $prop = array_flip( $params['prop'] );
67 $this->fld_ids
= isset( $prop['ids'] );
68 $this->fld_title
= isset( $prop['title'] );
69 $this->fld_flags
= isset( $prop['flags'] );
70 $this->fld_user
= isset( $prop['user'] );
71 $this->fld_userid
= isset( $prop['userid'] );
72 $this->fld_comment
= isset( $prop['comment'] );
73 $this->fld_parsedcomment
= isset( $prop['parsedcomment'] );
74 $this->fld_timestamp
= isset( $prop['timestamp'] );
75 $this->fld_sizes
= isset( $prop['sizes'] );
76 $this->fld_patrol
= isset( $prop['patrol'] );
77 $this->fld_notificationtimestamp
= isset( $prop['notificationtimestamp'] );
78 $this->fld_loginfo
= isset( $prop['loginfo'] );
80 if ( $this->fld_patrol
) {
81 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
82 $this->dieUsage( 'patrol property is not available', 'patrol' );
87 $this->addFields( array(
94 if ( is_null( $resultPageSet ) ) {
95 $this->addFields( array(
101 $this->addFieldsIf( array( 'rc_type', 'rc_minor', 'rc_bot' ), $this->fld_flags
);
102 $this->addFieldsIf( 'rc_user', $this->fld_user ||
$this->fld_userid
);
103 $this->addFieldsIf( 'rc_user_text', $this->fld_user
);
104 $this->addFieldsIf( 'rc_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
105 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrol
);
106 $this->addFieldsIf( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes
);
107 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp
);
109 array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ),
112 } elseif ( $params['allrev'] ) {
113 $this->addFields( 'rc_this_oldid' );
115 $this->addFields( 'rc_cur_id' );
118 $this->addTables( array(
123 $userId = $user->getId();
124 $this->addJoinConds( array( 'watchlist' => array( 'INNER JOIN',
126 'wl_user' => $userId,
127 'wl_namespace=rc_namespace',
132 $this->addWhere( array(
136 $db = $this->getDB();
138 $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'],
139 $params['start'], $params['end'] );
140 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
142 if ( !$params['allrev'] ) {
143 $this->addTables( 'page' );
144 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN', 'rc_cur_id=page_id' ) ) );
145 $this->addWhere( 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG
);
148 if ( !is_null( $params['show'] ) ) {
149 $show = array_flip( $params['show'] );
151 /* Check for conflicting parameters. */
152 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
153 ||
( isset( $show['bot'] ) && isset( $show['!bot'] ) )
154 ||
( isset( $show['anon'] ) && isset( $show['!anon'] ) )
155 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
157 $this->dieUsageMsg( 'show' );
160 // Check permissions.
161 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
162 $user = $this->getUser();
163 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
165 'You need the patrol right to request the patrolled flag',
171 /* Add additional conditions to query depending upon parameters. */
172 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
173 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
174 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
175 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
176 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
177 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
178 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
179 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
182 if ( !is_null( $params['type'] ) ) {
183 $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) );
186 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
187 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
189 if ( !is_null( $params['user'] ) ) {
190 $this->addWhereFld( 'rc_user_text', $params['user'] );
192 if ( !is_null( $params['excludeuser'] ) ) {
193 $this->addWhere( 'rc_user_text != ' . $db->addQuotes( $params['excludeuser'] ) );
196 // This is an index optimization for mysql, as done in the Special:Watchlist page
199 !isset( $params['start'] ) && !isset( $params['end'] ) && $db->getType() == 'mysql'
202 $this->addOption( 'LIMIT', $params['limit'] +
1 );
206 $res = $this->select( __METHOD__
);
208 foreach ( $res as $row ) {
209 if ( ++
$count > $params['limit'] ) {
210 // We've reached the one extra which shows that there are
211 // additional pages to be had. Stop here...
212 $this->setContinueEnumParameter(
214 wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
)
219 if ( is_null( $resultPageSet ) ) {
220 $vals = $this->extractRowInfo( $row );
221 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
223 $this->setContinueEnumParameter( 'start',
224 wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
228 if ( $params['allrev'] ) {
229 $ids[] = intval( $row->rc_this_oldid
);
231 $ids[] = intval( $row->rc_cur_id
);
236 if ( is_null( $resultPageSet ) ) {
237 $this->getResult()->setIndexedTagName_internal(
238 array( 'query', $this->getModuleName() ),
241 } elseif ( $params['allrev'] ) {
242 $resultPageSet->populateFromRevisionIDs( $ids );
244 $resultPageSet->populateFromPageIDs( $ids );
248 private function extractRowInfo( $row ) {
251 $type = intval( $row->rc_type
);
253 /* Determine what kind of change this was. */
256 $vals['type'] = 'edit';
259 $vals['type'] = 'new';
262 $vals['type'] = 'move';
265 $vals['type'] = 'log';
268 $vals['type'] = 'external';
270 case RC_MOVE_OVER_REDIRECT
:
271 $vals['type'] = 'move over redirect';
274 $vals['type'] = $type;
277 if ( $this->fld_ids
) {
278 $vals['pageid'] = intval( $row->rc_cur_id
);
279 $vals['revid'] = intval( $row->rc_this_oldid
);
280 $vals['old_revid'] = intval( $row->rc_last_oldid
);
283 $title = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
285 if ( $this->fld_title
) {
286 ApiQueryBase
::addTitleInfo( $vals, $title );
289 if ( $this->fld_user ||
$this->fld_userid
) {
291 if ( $this->fld_userid
) {
292 $vals['userid'] = $row->rc_user
;
293 // for backwards compatibility
294 $vals['user'] = $row->rc_user
;
297 if ( $this->fld_user
) {
298 $vals['user'] = $row->rc_user_text
;
301 if ( !$row->rc_user
) {
306 if ( $this->fld_flags
) {
307 if ( $row->rc_type
== RC_NEW
) {
310 if ( $row->rc_minor
) {
313 if ( $row->rc_bot
) {
318 if ( $this->fld_patrol
&& isset( $row->rc_patrolled
) ) {
319 $vals['patrolled'] = '';
322 if ( $this->fld_timestamp
) {
323 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
);
326 if ( $this->fld_sizes
) {
327 $vals['oldlen'] = intval( $row->rc_old_len
);
328 $vals['newlen'] = intval( $row->rc_new_len
);
331 if ( $this->fld_notificationtimestamp
) {
332 $vals['notificationtimestamp'] = ( $row->wl_notificationtimestamp
== null )
334 : wfTimestamp( TS_ISO_8601
, $row->wl_notificationtimestamp
);
337 if ( $this->fld_comment
&& isset( $row->rc_comment
) ) {
338 $vals['comment'] = $row->rc_comment
;
341 if ( $this->fld_parsedcomment
&& isset( $row->rc_comment
) ) {
342 $vals['parsedcomment'] = Linker
::formatComment( $row->rc_comment
, $title );
345 if ( $this->fld_loginfo
&& $row->rc_type
== RC_LOG
) {
346 $vals['logid'] = intval( $row->rc_logid
);
347 $vals['logtype'] = $row->rc_log_type
;
348 $vals['logaction'] = $row->rc_log_action
;
349 $logEntry = DatabaseLogEntry
::newFromRow( (array)$row );
350 ApiQueryLogEvents
::addLogParams(
353 $logEntry->getParameters(),
354 $logEntry->getType(),
355 $logEntry->getSubtype(),
356 $logEntry->getTimestamp()
363 /** Copied from ApiQueryRecentChanges.
365 * @param string $type
368 private function parseRCType( $type ) {
369 if ( is_array( $type ) ) {
371 foreach ( $type as $t ) {
372 $retval[] = $this->parseRCType( $t );
388 ApiBase
::dieDebug( __METHOD__
, "Unknown type '$type'" );
392 public function getAllowedParams() {
396 ApiBase
::PARAM_TYPE
=> 'timestamp'
399 ApiBase
::PARAM_TYPE
=> 'timestamp'
401 'namespace' => array(
402 ApiBase
::PARAM_ISMULTI
=> true,
403 ApiBase
::PARAM_TYPE
=> 'namespace'
406 ApiBase
::PARAM_TYPE
=> 'user',
408 'excludeuser' => array(
409 ApiBase
::PARAM_TYPE
=> 'user',
412 ApiBase
::PARAM_DFLT
=> 'older',
413 ApiBase
::PARAM_TYPE
=> array(
419 ApiBase
::PARAM_DFLT
=> 10,
420 ApiBase
::PARAM_TYPE
=> 'limit',
421 ApiBase
::PARAM_MIN
=> 1,
422 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
423 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
426 ApiBase
::PARAM_ISMULTI
=> true,
427 ApiBase
::PARAM_DFLT
=> 'ids|title|flags',
428 ApiBase
::PARAM_TYPE
=> array(
439 'notificationtimestamp',
444 ApiBase
::PARAM_ISMULTI
=> true,
445 ApiBase
::PARAM_TYPE
=> array(
457 ApiBase
::PARAM_ISMULTI
=> true,
458 ApiBase
::PARAM_TYPE
=> array(
466 ApiBase
::PARAM_TYPE
=> 'user'
469 ApiBase
::PARAM_TYPE
=> 'string'
474 public function getParamDescription() {
475 $p = $this->getModulePrefix();
478 'allrev' => 'Include multiple revisions of the same page within given timeframe',
479 'start' => 'The timestamp to start enumerating from',
480 'end' => 'The timestamp to end enumerating',
481 'namespace' => 'Filter changes to only the given namespace(s)',
482 'user' => 'Only list changes by this user',
483 'excludeuser' => 'Don\'t list changes by this user',
484 'dir' => $this->getDirectionDescription( $p ),
485 'limit' => 'How many total results to return per request',
487 'Which additional items to get (non-generator mode only).',
488 ' ids - Adds revision ids and page ids',
489 ' title - Adds title of the page',
490 ' flags - Adds flags for the edit',
491 ' user - Adds the user who made the edit',
492 ' userid - Adds user id of whom made the edit',
493 ' comment - Adds comment of the edit',
494 ' parsedcomment - Adds parsed comment of the edit',
495 ' timestamp - Adds timestamp of the edit',
496 ' patrol - Tags edits that are patrolled',
497 ' sizes - Adds the old and new lengths of the page',
498 ' notificationtimestamp - Adds timestamp of when the user was last notified about the edit',
499 ' loginfo - Adds log information where appropriate',
502 'Show only items that meet this criteria.',
503 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
506 'Which types of changes to show',
507 ' edit - Regular page edits',
508 ' external - External changes',
509 ' new - Page creations',
510 ' log - Log entries',
512 'owner' => 'The name of the user whose watchlist you\'d like to access',
513 'token' => 'Give a security token (settable in preferences) to ' .
514 'allow access to another user\'s watchlist'
518 public function getResultProperties() {
524 ApiBase
::PROP_TYPE
=> array(
534 'pageid' => 'integer',
535 'revid' => 'integer',
536 'old_revid' => 'integer'
547 'userid' => 'integer',
552 'minor' => 'boolean',
556 'patrolled' => 'boolean'
558 'timestamp' => array(
559 'timestamp' => 'timestamp'
562 'oldlen' => 'integer',
563 'newlen' => 'integer'
565 'notificationtimestamp' => array(
566 'notificationtimestamp' => array(
567 ApiBase
::PROP_TYPE
=> 'timestamp',
568 ApiBase
::PROP_NULLABLE
=> true
573 ApiBase
::PROP_TYPE
=> 'string',
574 ApiBase
::PROP_NULLABLE
=> true
577 'parsedcomment' => array(
578 'parsedcomment' => array(
579 ApiBase
::PROP_TYPE
=> 'string',
580 ApiBase
::PROP_NULLABLE
=> true
585 ApiBase
::PROP_TYPE
=> 'integer',
586 ApiBase
::PROP_NULLABLE
=> true
589 ApiBase
::PROP_TYPE
=> $wgLogTypes,
590 ApiBase
::PROP_NULLABLE
=> true
592 'logaction' => array(
593 ApiBase
::PROP_TYPE
=> 'string',
594 ApiBase
::PROP_NULLABLE
=> true
600 public function getDescription() {
601 return "Get all recent changes to pages in the logged in user's watchlist";
604 public function getPossibleErrors() {
605 return array_merge( parent
::getPossibleErrors(), array(
606 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
608 'code' => 'bad_wltoken',
609 'info' => 'Incorrect watchlist token provided -- ' .
610 'please set a correct token in Special:Preferences'
612 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
613 array( 'code' => 'patrol', 'info' => 'patrol property is not available' ),
616 'code' => 'permissiondenied',
617 'info' => 'You need the patrol right to request the patrolled flag'
619 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
623 public function getExamples() {
625 'api.php?action=query&list=watchlist',
626 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
627 'api.php?action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment',
628 'api.php?action=query&generator=watchlist&prop=info',
629 'api.php?action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user',
630 'api.php?action=query&list=watchlist&wlowner=Bob_Smith&wltoken=123ABC'
634 public function getHelpUrls() {
635 return 'https://www.mediawiki.org/wiki/API:Watchlist';