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->getUser();
63 $wlowner = $this->getWatchlistUser( $params );
65 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
66 $prop = array_flip( $params['prop'] );
68 $this->fld_ids
= isset( $prop['ids'] );
69 $this->fld_title
= isset( $prop['title'] );
70 $this->fld_flags
= isset( $prop['flags'] );
71 $this->fld_user
= isset( $prop['user'] );
72 $this->fld_userid
= isset( $prop['userid'] );
73 $this->fld_comment
= isset( $prop['comment'] );
74 $this->fld_parsedcomment
= isset( $prop['parsedcomment'] );
75 $this->fld_timestamp
= isset( $prop['timestamp'] );
76 $this->fld_sizes
= isset( $prop['sizes'] );
77 $this->fld_patrol
= isset( $prop['patrol'] );
78 $this->fld_notificationtimestamp
= isset( $prop['notificationtimestamp'] );
79 $this->fld_loginfo
= isset( $prop['loginfo'] );
81 if ( $this->fld_patrol
) {
82 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
83 $this->dieUsage( 'patrol property is not available', 'patrol' );
88 $this->addFields( array(
96 if ( is_null( $resultPageSet ) ) {
97 $this->addFields( array(
103 $this->addFieldsIf( array( 'rc_type', 'rc_minor', 'rc_bot' ), $this->fld_flags
);
104 $this->addFieldsIf( 'rc_user', $this->fld_user ||
$this->fld_userid
);
105 $this->addFieldsIf( 'rc_user_text', $this->fld_user
);
106 $this->addFieldsIf( 'rc_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
107 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrol
);
108 $this->addFieldsIf( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes
);
109 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp
);
111 array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ),
114 } elseif ( $params['allrev'] ) {
115 $this->addFields( 'rc_this_oldid' );
117 $this->addFields( 'rc_cur_id' );
120 $this->addTables( array(
125 $userId = $wlowner->getId();
126 $this->addJoinConds( array( 'watchlist' => array( 'INNER JOIN',
128 'wl_user' => $userId,
129 'wl_namespace=rc_namespace',
134 $db = $this->getDB();
136 $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'],
137 $params['start'], $params['end'] );
138 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
140 if ( !$params['allrev'] ) {
141 $this->addTables( 'page' );
142 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN', 'rc_cur_id=page_id' ) ) );
143 $this->addWhere( 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG
);
146 if ( !is_null( $params['show'] ) ) {
147 $show = array_flip( $params['show'] );
149 /* Check for conflicting parameters. */
150 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
151 ||
( isset( $show['bot'] ) && isset( $show['!bot'] ) )
152 ||
( isset( $show['anon'] ) && isset( $show['!anon'] ) )
153 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
155 $this->dieUsageMsg( 'show' );
158 // Check permissions.
159 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
160 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
162 'You need the patrol right to request the patrolled flag',
168 /* Add additional conditions to query depending upon parameters. */
169 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
170 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
171 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
172 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
173 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
174 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
175 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
176 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
179 if ( !is_null( $params['type'] ) ) {
180 $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) );
183 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
184 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
186 if ( !is_null( $params['user'] ) ) {
187 $this->addWhereFld( 'rc_user_text', $params['user'] );
189 if ( !is_null( $params['excludeuser'] ) ) {
190 $this->addWhere( 'rc_user_text != ' . $db->addQuotes( $params['excludeuser'] ) );
193 // This is an index optimization for mysql, as done in the Special:Watchlist page
196 !isset( $params['start'] ) && !isset( $params['end'] ) && $db->getType() == 'mysql'
199 // Paranoia: avoid brute force searches (bug 17342)
200 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
201 if ( !$user->isAllowed( 'deletedhistory' ) ) {
202 $bitmask = Revision
::DELETED_USER
;
203 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
204 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
209 $this->addWhere( $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask" );
213 // LogPage::DELETED_ACTION hides the affected page, too. So hide those
214 // entirely from the watchlist, or someone could guess the title.
215 if ( !$user->isAllowed( 'deletedhistory' ) ) {
216 $bitmask = LogPage
::DELETED_ACTION
;
217 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
218 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
223 $this->addWhere( $this->getDB()->makeList( array(
224 'rc_type != ' . RC_LOG
,
225 $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
229 $this->addOption( 'LIMIT', $params['limit'] +
1 );
233 $res = $this->select( __METHOD__
);
235 foreach ( $res as $row ) {
236 if ( ++
$count > $params['limit'] ) {
237 // We've reached the one extra which shows that there are
238 // additional pages to be had. Stop here...
239 $this->setContinueEnumParameter(
241 wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
)
246 if ( is_null( $resultPageSet ) ) {
247 $vals = $this->extractRowInfo( $row );
248 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
250 $this->setContinueEnumParameter( 'start',
251 wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
255 if ( $params['allrev'] ) {
256 $ids[] = intval( $row->rc_this_oldid
);
258 $ids[] = intval( $row->rc_cur_id
);
263 if ( is_null( $resultPageSet ) ) {
264 $this->getResult()->setIndexedTagName_internal(
265 array( 'query', $this->getModuleName() ),
268 } elseif ( $params['allrev'] ) {
269 $resultPageSet->populateFromRevisionIDs( $ids );
271 $resultPageSet->populateFromPageIDs( $ids );
275 private function extractRowInfo( $row ) {
276 /* Determine the title of the page that has been changed. */
277 $title = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
278 $user = $this->getUser();
280 /* Our output data. */
283 $type = intval( $row->rc_type
);
285 /* Determine what kind of change this was. */
288 $vals['type'] = 'edit';
291 $vals['type'] = 'new';
294 $vals['type'] = 'move';
297 $vals['type'] = 'log';
300 $vals['type'] = 'external';
302 case RC_MOVE_OVER_REDIRECT
:
303 $vals['type'] = 'move over redirect';
306 $vals['type'] = $type;
311 /* Create a new entry in the result for the title. */
312 if ( $this->fld_title ||
$this->fld_ids
) {
313 // These should already have been filtered out of the query, but just in case.
314 if ( $type === RC_LOG
&& ( $row->rc_deleted
& LogPage
::DELETED_ACTION
) ) {
315 $vals['actionhidden'] = '';
318 if ( $type !== RC_LOG ||
319 LogEventsList
::userCanBitfield( $row->rc_deleted
, LogPage
::DELETED_ACTION
, $user )
321 if ( $this->fld_title
) {
322 ApiQueryBase
::addTitleInfo( $vals, $title );
324 if ( $this->fld_ids
) {
325 $vals['pageid'] = intval( $row->rc_cur_id
);
326 $vals['revid'] = intval( $row->rc_this_oldid
);
327 $vals['old_revid'] = intval( $row->rc_last_oldid
);
332 /* Add user data and 'anon' flag, if user is anonymous. */
333 if ( $this->fld_user ||
$this->fld_userid
) {
334 if ( $row->rc_deleted
& Revision
::DELETED_USER
) {
335 $vals['userhidden'] = '';
338 if ( Revision
::userCanBitfield( $row->rc_deleted
, Revision
::DELETED_USER
, $user ) ) {
339 if ( $this->fld_userid
) {
340 $vals['userid'] = $row->rc_user
;
341 // for backwards compatibility
342 $vals['user'] = $row->rc_user
;
345 if ( $this->fld_user
) {
346 $vals['user'] = $row->rc_user_text
;
349 if ( !$row->rc_user
) {
355 /* Add flags, such as new, minor, bot. */
356 if ( $this->fld_flags
) {
357 if ( $row->rc_bot
) {
360 if ( $row->rc_type
== RC_NEW
) {
363 if ( $row->rc_minor
) {
368 /* Add sizes of each revision. (Only available on 1.10+) */
369 if ( $this->fld_sizes
) {
370 $vals['oldlen'] = intval( $row->rc_old_len
);
371 $vals['newlen'] = intval( $row->rc_new_len
);
374 /* Add the timestamp. */
375 if ( $this->fld_timestamp
) {
376 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
);
379 if ( $this->fld_notificationtimestamp
) {
380 $vals['notificationtimestamp'] = ( $row->wl_notificationtimestamp
== null )
382 : wfTimestamp( TS_ISO_8601
, $row->wl_notificationtimestamp
);
385 /* Add edit summary / log summary. */
386 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
387 if ( $row->rc_deleted
& Revision
::DELETED_COMMENT
) {
388 $vals['commenthidden'] = '';
391 if ( Revision
::userCanBitfield( $row->rc_deleted
, Revision
::DELETED_COMMENT
, $user ) ) {
392 if ( $this->fld_comment
&& isset( $row->rc_comment
) ) {
393 $vals['comment'] = $row->rc_comment
;
396 if ( $this->fld_parsedcomment
&& isset( $row->rc_comment
) ) {
397 $vals['parsedcomment'] = Linker
::formatComment( $row->rc_comment
, $title );
402 /* Add the patrolled flag */
403 if ( $this->fld_patrol
&& $row->rc_patrolled
== 1 ) {
404 $vals['patrolled'] = '';
407 if ( $this->fld_patrol
&& ChangesList
::isUnpatrolled( $row, $user ) ) {
408 $vals['unpatrolled'] = '';
411 if ( $this->fld_loginfo
&& $row->rc_type
== RC_LOG
) {
412 if ( $row->rc_deleted
& LogPage
::DELETED_ACTION
) {
413 $vals['actionhidden'] = '';
416 if ( LogEventsList
::userCanBitfield( $row->rc_deleted
, LogPage
::DELETED_ACTION
, $user ) ) {
417 $vals['logid'] = intval( $row->rc_logid
);
418 $vals['logtype'] = $row->rc_log_type
;
419 $vals['logaction'] = $row->rc_log_action
;
420 $logEntry = DatabaseLogEntry
::newFromRow( (array)$row );
421 ApiQueryLogEvents
::addLogParams(
424 $logEntry->getParameters(),
425 $logEntry->getType(),
426 $logEntry->getSubtype(),
427 $logEntry->getTimestamp()
432 if ( $anyHidden && ( $row->rc_deleted
& Revision
::DELETED_RESTRICTED
) ) {
433 $vals['suppressed'] = '';
439 /** Copied from ApiQueryRecentChanges.
441 * @param string $type
444 private function parseRCType( $type ) {
445 if ( is_array( $type ) ) {
447 foreach ( $type as $t ) {
448 $retval[] = $this->parseRCType( $t );
464 ApiBase
::dieDebug( __METHOD__
, "Unknown type '$type'" );
468 public function getAllowedParams() {
472 ApiBase
::PARAM_TYPE
=> 'timestamp'
475 ApiBase
::PARAM_TYPE
=> 'timestamp'
477 'namespace' => array(
478 ApiBase
::PARAM_ISMULTI
=> true,
479 ApiBase
::PARAM_TYPE
=> 'namespace'
482 ApiBase
::PARAM_TYPE
=> 'user',
484 'excludeuser' => array(
485 ApiBase
::PARAM_TYPE
=> 'user',
488 ApiBase
::PARAM_DFLT
=> 'older',
489 ApiBase
::PARAM_TYPE
=> array(
495 ApiBase
::PARAM_DFLT
=> 10,
496 ApiBase
::PARAM_TYPE
=> 'limit',
497 ApiBase
::PARAM_MIN
=> 1,
498 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
499 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
502 ApiBase
::PARAM_ISMULTI
=> true,
503 ApiBase
::PARAM_DFLT
=> 'ids|title|flags',
504 ApiBase
::PARAM_TYPE
=> array(
515 'notificationtimestamp',
520 ApiBase
::PARAM_ISMULTI
=> true,
521 ApiBase
::PARAM_TYPE
=> array(
533 ApiBase
::PARAM_ISMULTI
=> true,
534 ApiBase
::PARAM_TYPE
=> array(
542 ApiBase
::PARAM_TYPE
=> 'user'
545 ApiBase
::PARAM_TYPE
=> 'string'
550 public function getParamDescription() {
551 $p = $this->getModulePrefix();
554 'allrev' => 'Include multiple revisions of the same page within given timeframe',
555 'start' => 'The timestamp to start enumerating from',
556 'end' => 'The timestamp to end enumerating',
557 'namespace' => 'Filter changes to only the given namespace(s)',
558 'user' => 'Only list changes by this user',
559 'excludeuser' => 'Don\'t list changes by this user',
560 'dir' => $this->getDirectionDescription( $p ),
561 'limit' => 'How many total results to return per request',
563 'Which additional items to get (non-generator mode only).',
564 ' ids - Adds revision ids and page ids',
565 ' title - Adds title of the page',
566 ' flags - Adds flags for the edit',
567 ' user - Adds the user who made the edit',
568 ' userid - Adds user id of whom made the edit',
569 ' comment - Adds comment of the edit',
570 ' parsedcomment - Adds parsed comment of the edit',
571 ' timestamp - Adds timestamp of the edit',
572 ' patrol - Tags edits that are patrolled',
573 ' sizes - Adds the old and new lengths of the page',
574 ' notificationtimestamp - Adds timestamp of when the user was last notified about the edit',
575 ' loginfo - Adds log information where appropriate',
578 'Show only items that meet this criteria.',
579 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
582 'Which types of changes to show',
583 ' edit - Regular page edits',
584 ' external - External changes',
585 ' new - Page creations',
586 ' log - Log entries',
588 'owner' => 'The name of the user whose watchlist you\'d like to access',
589 'token' => 'Give a security token (settable in preferences) to ' .
590 'allow access to another user\'s watchlist',
594 public function getResultProperties() {
600 ApiBase
::PROP_TYPE
=> array(
610 'pageid' => 'integer',
611 'revid' => 'integer',
612 'old_revid' => 'integer'
623 'userid' => 'integer',
628 'minor' => 'boolean',
632 'patrolled' => 'boolean'
634 'timestamp' => array(
635 'timestamp' => 'timestamp'
638 'oldlen' => 'integer',
639 'newlen' => 'integer'
641 'notificationtimestamp' => array(
642 'notificationtimestamp' => array(
643 ApiBase
::PROP_TYPE
=> 'timestamp',
644 ApiBase
::PROP_NULLABLE
=> true
649 ApiBase
::PROP_TYPE
=> 'string',
650 ApiBase
::PROP_NULLABLE
=> true
653 'parsedcomment' => array(
654 'parsedcomment' => array(
655 ApiBase
::PROP_TYPE
=> 'string',
656 ApiBase
::PROP_NULLABLE
=> true
661 ApiBase
::PROP_TYPE
=> 'integer',
662 ApiBase
::PROP_NULLABLE
=> true
665 ApiBase
::PROP_TYPE
=> $wgLogTypes,
666 ApiBase
::PROP_NULLABLE
=> true
668 'logaction' => array(
669 ApiBase
::PROP_TYPE
=> 'string',
670 ApiBase
::PROP_NULLABLE
=> true
676 public function getDescription() {
677 return "Get all recent changes to pages in the logged in user's watchlist";
680 public function getPossibleErrors() {
681 return array_merge( parent
::getPossibleErrors(), array(
682 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
684 'code' => 'bad_wltoken',
685 'info' => 'Incorrect watchlist token provided -- ' .
686 'please set a correct token in Special:Preferences'
688 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
689 array( 'code' => 'patrol', 'info' => 'patrol property is not available' ),
692 'code' => 'permissiondenied',
693 'info' => 'You need the patrol right to request the patrolled flag'
695 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
699 public function getExamples() {
701 'api.php?action=query&list=watchlist',
702 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
703 'api.php?action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment',
704 'api.php?action=query&generator=watchlist&prop=info',
705 'api.php?action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user',
706 'api.php?action=query&list=watchlist&wlowner=Bob_Smith&wltoken=123ABC'
710 public function getHelpUrls() {
711 return 'https://www.mediawiki.org/wiki/API:Watchlist';