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( ApiQuery
$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 ApiPageSet $resultPageSet
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' );
97 if ( is_null( $resultPageSet ) ) {
104 $this->addFieldsIf( [ 'rc_type', 'rc_minor', 'rc_bot' ], $this->fld_flags
);
105 $this->addFieldsIf( 'rc_user', $this->fld_user ||
$this->fld_userid
);
106 $this->addFieldsIf( 'rc_user_text', $this->fld_user
);
107 $this->addFieldsIf( 'rc_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
108 $this->addFieldsIf( [ 'rc_patrolled', 'rc_log_type' ], $this->fld_patrol
);
109 $this->addFieldsIf( [ 'rc_old_len', 'rc_new_len' ], $this->fld_sizes
);
110 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp
);
112 [ 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ],
115 } elseif ( $params['allrev'] ) {
116 $this->addFields( 'rc_this_oldid' );
118 $this->addFields( 'rc_cur_id' );
126 $userId = $wlowner->getId();
127 $this->addJoinConds( [ 'watchlist' => [ 'INNER JOIN',
129 'wl_user' => $userId,
130 'wl_namespace=rc_namespace',
135 $db = $this->getDB();
137 $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'],
138 $params['start'], $params['end'] );
139 // Include in ORDER BY for uniqueness
140 $this->addWhereRange( 'rc_id', $params['dir'], null, null );
142 if ( !is_null( $params['continue'] ) ) {
143 $cont = explode( '|', $params['continue'] );
144 $this->dieContinueUsageIf( count( $cont ) != 2 );
145 $op = ( $params['dir'] === 'newer' ?
'>' : '<' );
146 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
147 $continueId = (int)$cont[1];
148 $this->dieContinueUsageIf( $continueId != $cont[1] );
149 $this->addWhere( "rc_timestamp $op $continueTimestamp OR " .
150 "(rc_timestamp = $continueTimestamp AND " .
151 "rc_id $op= $continueId)"
155 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
157 if ( !$params['allrev'] ) {
158 $this->addTables( 'page' );
159 $this->addJoinConds( [ 'page' => [ 'LEFT JOIN', 'rc_cur_id=page_id' ] ] );
160 $this->addWhere( 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG
);
163 if ( !is_null( $params['show'] ) ) {
164 $show = array_flip( $params['show'] );
166 /* Check for conflicting parameters. */
167 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
168 ||
( isset( $show['bot'] ) && isset( $show['!bot'] ) )
169 ||
( isset( $show['anon'] ) && isset( $show['!anon'] ) )
170 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
171 ||
( isset( $show['unread'] ) && isset( $show['!unread'] ) )
173 $this->dieUsageMsg( 'show' );
176 // Check permissions.
177 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
178 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
180 'You need the patrol right to request the patrolled flag',
186 /* Add additional conditions to query depending upon parameters. */
187 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
188 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
189 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
190 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
191 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
192 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
193 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
194 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
195 $this->addWhereIf( 'rc_timestamp >= wl_notificationtimestamp', isset( $show['unread'] ) );
197 'wl_notificationtimestamp IS NULL OR rc_timestamp < wl_notificationtimestamp',
198 isset( $show['!unread'] )
202 if ( !is_null( $params['type'] ) ) {
204 $this->addWhereFld( 'rc_type', RecentChange
::parseToRCType( $params['type'] ) );
205 } catch ( Exception
$e ) {
206 ApiBase
::dieDebug( __METHOD__
, $e->getMessage() );
210 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
211 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
213 if ( !is_null( $params['user'] ) ) {
214 $this->addWhereFld( 'rc_user_text', $params['user'] );
216 if ( !is_null( $params['excludeuser'] ) ) {
217 $this->addWhere( 'rc_user_text != ' . $db->addQuotes( $params['excludeuser'] ) );
220 // This is an index optimization for mysql, as done in the Special:Watchlist page
223 !isset( $params['start'] ) && !isset( $params['end'] ) && $db->getType() == 'mysql'
226 // Paranoia: avoid brute force searches (bug 17342)
227 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
228 if ( !$user->isAllowed( 'deletedhistory' ) ) {
229 $bitmask = Revision
::DELETED_USER
;
230 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
231 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
236 $this->addWhere( $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask" );
240 // LogPage::DELETED_ACTION hides the affected page, too. So hide those
241 // entirely from the watchlist, or someone could guess the title.
242 if ( !$user->isAllowed( 'deletedhistory' ) ) {
243 $bitmask = LogPage
::DELETED_ACTION
;
244 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
245 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
250 $this->addWhere( $this->getDB()->makeList( [
251 'rc_type != ' . RC_LOG
,
252 $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
256 $this->addOption( 'LIMIT', $params['limit'] +
1 );
260 $res = $this->select( __METHOD__
);
262 foreach ( $res as $row ) {
263 if ( ++
$count > $params['limit'] ) {
264 // We've reached the one extra which shows that there are
265 // additional pages to be had. Stop here...
266 $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
270 if ( is_null( $resultPageSet ) ) {
271 $vals = $this->extractRowInfo( $row );
272 $fit = $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals );
274 $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
278 if ( $params['allrev'] ) {
279 $ids[] = intval( $row->rc_this_oldid
);
281 $ids[] = intval( $row->rc_cur_id
);
286 if ( is_null( $resultPageSet ) ) {
287 $this->getResult()->addIndexedTagName(
288 [ 'query', $this->getModuleName() ],
291 } elseif ( $params['allrev'] ) {
292 $resultPageSet->populateFromRevisionIDs( $ids );
294 $resultPageSet->populateFromPageIDs( $ids );
298 private function extractRowInfo( $row ) {
299 /* Determine the title of the page that has been changed. */
300 $title = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
301 $user = $this->getUser();
303 /* Our output data. */
305 $type = intval( $row->rc_type
);
306 $vals['type'] = RecentChange
::parseFromRCType( $type );
309 /* Create a new entry in the result for the title. */
310 if ( $this->fld_title ||
$this->fld_ids
) {
311 // These should already have been filtered out of the query, but just in case.
312 if ( $type === RC_LOG
&& ( $row->rc_deleted
& LogPage
::DELETED_ACTION
) ) {
313 $vals['actionhidden'] = true;
316 if ( $type !== RC_LOG ||
317 LogEventsList
::userCanBitfield( $row->rc_deleted
, LogPage
::DELETED_ACTION
, $user )
319 if ( $this->fld_title
) {
320 ApiQueryBase
::addTitleInfo( $vals, $title );
322 if ( $this->fld_ids
) {
323 $vals['pageid'] = intval( $row->rc_cur_id
);
324 $vals['revid'] = intval( $row->rc_this_oldid
);
325 $vals['old_revid'] = intval( $row->rc_last_oldid
);
330 /* Add user data and 'anon' flag, if user is anonymous. */
331 if ( $this->fld_user ||
$this->fld_userid
) {
332 if ( $row->rc_deleted
& Revision
::DELETED_USER
) {
333 $vals['userhidden'] = true;
336 if ( Revision
::userCanBitfield( $row->rc_deleted
, Revision
::DELETED_USER
, $user ) ) {
337 if ( $this->fld_userid
) {
338 $vals['userid'] = (int)$row->rc_user
;
339 // for backwards compatibility
340 $vals['user'] = (int)$row->rc_user
;
343 if ( $this->fld_user
) {
344 $vals['user'] = $row->rc_user_text
;
347 if ( !$row->rc_user
) {
348 $vals['anon'] = true;
353 /* Add flags, such as new, minor, bot. */
354 if ( $this->fld_flags
) {
355 $vals['bot'] = (bool)$row->rc_bot
;
356 $vals['new'] = $row->rc_type
== RC_NEW
;
357 $vals['minor'] = (bool)$row->rc_minor
;
360 /* Add sizes of each revision. (Only available on 1.10+) */
361 if ( $this->fld_sizes
) {
362 $vals['oldlen'] = intval( $row->rc_old_len
);
363 $vals['newlen'] = intval( $row->rc_new_len
);
366 /* Add the timestamp. */
367 if ( $this->fld_timestamp
) {
368 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
);
371 if ( $this->fld_notificationtimestamp
) {
372 $vals['notificationtimestamp'] = ( $row->wl_notificationtimestamp
== null )
374 : wfTimestamp( TS_ISO_8601
, $row->wl_notificationtimestamp
);
377 /* Add edit summary / log summary. */
378 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
379 if ( $row->rc_deleted
& Revision
::DELETED_COMMENT
) {
380 $vals['commenthidden'] = true;
383 if ( Revision
::userCanBitfield( $row->rc_deleted
, Revision
::DELETED_COMMENT
, $user ) ) {
384 if ( $this->fld_comment
&& isset( $row->rc_comment
) ) {
385 $vals['comment'] = $row->rc_comment
;
388 if ( $this->fld_parsedcomment
&& isset( $row->rc_comment
) ) {
389 $vals['parsedcomment'] = Linker
::formatComment( $row->rc_comment
, $title );
394 /* Add the patrolled flag */
395 if ( $this->fld_patrol
) {
396 $vals['patrolled'] = $row->rc_patrolled
== 1;
397 $vals['unpatrolled'] = ChangesList
::isUnpatrolled( $row, $user );
400 if ( $this->fld_loginfo
&& $row->rc_type
== RC_LOG
) {
401 if ( $row->rc_deleted
& LogPage
::DELETED_ACTION
) {
402 $vals['actionhidden'] = true;
405 if ( LogEventsList
::userCanBitfield( $row->rc_deleted
, LogPage
::DELETED_ACTION
, $user ) ) {
406 $vals['logid'] = intval( $row->rc_logid
);
407 $vals['logtype'] = $row->rc_log_type
;
408 $vals['logaction'] = $row->rc_log_action
;
409 $vals['logparams'] = LogFormatter
::newFromRow( $row )->formatParametersForApi();
413 if ( $anyHidden && ( $row->rc_deleted
& Revision
::DELETED_RESTRICTED
) ) {
414 $vals['suppressed'] = true;
420 public function getAllowedParams() {
424 ApiBase
::PARAM_TYPE
=> 'timestamp'
427 ApiBase
::PARAM_TYPE
=> 'timestamp'
430 ApiBase
::PARAM_ISMULTI
=> true,
431 ApiBase
::PARAM_TYPE
=> 'namespace'
434 ApiBase
::PARAM_TYPE
=> 'user',
437 ApiBase
::PARAM_TYPE
=> 'user',
440 ApiBase
::PARAM_DFLT
=> 'older',
441 ApiBase
::PARAM_TYPE
=> [
445 ApiHelp
::PARAM_HELP_MSG
=> 'api-help-param-direction',
448 ApiBase
::PARAM_DFLT
=> 10,
449 ApiBase
::PARAM_TYPE
=> 'limit',
450 ApiBase
::PARAM_MIN
=> 1,
451 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
452 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
455 ApiBase
::PARAM_ISMULTI
=> true,
456 ApiBase
::PARAM_DFLT
=> 'ids|title|flags',
457 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
458 ApiBase
::PARAM_TYPE
=> [
469 'notificationtimestamp',
474 ApiBase
::PARAM_ISMULTI
=> true,
475 ApiBase
::PARAM_TYPE
=> [
489 ApiBase
::PARAM_DFLT
=> 'edit|new|log|categorize',
490 ApiBase
::PARAM_ISMULTI
=> true,
491 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
492 ApiBase
::PARAM_TYPE
=> RecentChange
::getChangeTypes()
495 ApiBase
::PARAM_TYPE
=> 'user'
498 ApiBase
::PARAM_TYPE
=> 'string'
501 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
506 protected function getExamplesMessages() {
508 'action=query&list=watchlist'
509 => 'apihelp-query+watchlist-example-simple',
510 'action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment'
511 => 'apihelp-query+watchlist-example-props',
512 'action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment'
513 => 'apihelp-query+watchlist-example-allrev',
514 'action=query&generator=watchlist&prop=info'
515 => 'apihelp-query+watchlist-example-generator',
516 'action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user'
517 => 'apihelp-query+watchlist-example-generator-rev',
518 'action=query&list=watchlist&wlowner=Example&wltoken=123ABC'
519 => 'apihelp-query+watchlist-example-wlowner',
523 public function getHelpUrls() {
524 return 'https://www.mediawiki.org/wiki/API:Watchlist';