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
54 private function run( $resultPageSet = null ) {
55 $this->selectNamedDB( 'watchlist', DB_SLAVE
, 'watchlist' );
57 $params = $this->extractRequestParams();
59 $user = $this->getWatchlistUser( $params );
61 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
62 $prop = array_flip( $params['prop'] );
64 $this->fld_ids
= isset( $prop['ids'] );
65 $this->fld_title
= isset( $prop['title'] );
66 $this->fld_flags
= isset( $prop['flags'] );
67 $this->fld_user
= isset( $prop['user'] );
68 $this->fld_userid
= isset( $prop['userid'] );
69 $this->fld_comment
= isset( $prop['comment'] );
70 $this->fld_parsedcomment
= isset ( $prop['parsedcomment'] );
71 $this->fld_timestamp
= isset( $prop['timestamp'] );
72 $this->fld_sizes
= isset( $prop['sizes'] );
73 $this->fld_patrol
= isset( $prop['patrol'] );
74 $this->fld_notificationtimestamp
= isset( $prop['notificationtimestamp'] );
75 $this->fld_loginfo
= isset( $prop['loginfo'] );
77 if ( $this->fld_patrol
) {
78 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
79 $this->dieUsage( 'patrol property is not available', 'patrol' );
84 $this->addFields( array(
91 if ( is_null( $resultPageSet ) ) {
92 $this->addFields( array(
98 $this->addFieldsIf( array( 'rc_new', 'rc_minor', 'rc_bot' ), $this->fld_flags
);
99 $this->addFieldsIf( 'rc_user', $this->fld_user ||
$this->fld_userid
);
100 $this->addFieldsIf( 'rc_user_text', $this->fld_user
);
101 $this->addFieldsIf( 'rc_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
102 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrol
);
103 $this->addFieldsIf( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes
);
104 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp
);
105 $this->addFieldsIf( array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ), $this->fld_loginfo
);
106 } elseif ( $params['allrev'] ) {
107 $this->addFields( 'rc_this_oldid' );
109 $this->addFields( 'rc_cur_id' );
112 $this->addTables( array(
117 $userId = $user->getId();
118 $this->addJoinConds( array( 'watchlist' => array('INNER JOIN',
120 'wl_user' => $userId,
121 'wl_namespace=rc_namespace',
125 $this->addWhere( array(
129 $db = $this->getDB();
131 $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'],
132 $params['start'], $params['end'] );
133 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
135 if ( !$params['allrev'] ) {
136 $this->addTables( 'page' );
137 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN','rc_cur_id=page_id' ) ) );
138 $this->addWhere( 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG
);
141 if ( !is_null( $params['show'] ) ) {
142 $show = array_flip( $params['show'] );
144 /* Check for conflicting parameters. */
145 if ( ( isset ( $show['minor'] ) && isset ( $show['!minor'] ) )
146 ||
( isset ( $show['bot'] ) && isset ( $show['!bot'] ) )
147 ||
( isset ( $show['anon'] ) && isset ( $show['!anon'] ) )
148 ||
( isset ( $show['patrolled'] ) && isset ( $show['!patrolled'] ) )
151 $this->dieUsageMsg( 'show' );
154 // Check permissions.
155 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
156 $user = $this->getUser();
157 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
158 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
162 /* Add additional conditions to query depending upon parameters. */
163 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
164 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
165 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
166 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
167 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
168 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
169 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
170 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
173 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
174 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
176 if ( !is_null( $params['user'] ) ) {
177 $this->addWhereFld( 'rc_user_text', $params['user'] );
179 if ( !is_null( $params['excludeuser'] ) ) {
180 $this->addWhere( 'rc_user_text != ' . $db->addQuotes( $params['excludeuser'] ) );
183 // This is an index optimization for mysql, as done in the Special:Watchlist page
184 $this->addWhereIf( "rc_timestamp > ''", !isset( $params['start'] ) && !isset( $params['end'] ) && $db->getType() == 'mysql' );
186 $this->addOption( 'LIMIT', $params['limit'] +
1 );
190 $res = $this->select( __METHOD__
);
192 foreach ( $res as $row ) {
193 if ( ++
$count > $params['limit'] ) {
194 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
195 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
199 if ( is_null( $resultPageSet ) ) {
200 $vals = $this->extractRowInfo( $row );
201 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
203 $this->setContinueEnumParameter( 'start',
204 wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
208 if ( $params['allrev'] ) {
209 $ids[] = intval( $row->rc_this_oldid
);
211 $ids[] = intval( $row->rc_cur_id
);
216 if ( is_null( $resultPageSet ) ) {
217 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
218 } elseif ( $params['allrev'] ) {
219 $resultPageSet->populateFromRevisionIDs( $ids );
221 $resultPageSet->populateFromPageIDs( $ids );
225 private function extractRowInfo( $row ) {
228 if ( $this->fld_ids
) {
229 $vals['pageid'] = intval( $row->rc_cur_id
);
230 $vals['revid'] = intval( $row->rc_this_oldid
);
231 $vals['old_revid'] = intval( $row->rc_last_oldid
);
234 $title = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
236 if ( $this->fld_title
) {
237 ApiQueryBase
::addTitleInfo( $vals, $title );
240 if ( $this->fld_user ||
$this->fld_userid
) {
242 if ( $this->fld_user
) {
243 $vals['user'] = $row->rc_user_text
;
246 if ( $this->fld_userid
) {
247 $vals['user'] = $row->rc_user
;
250 if ( !$row->rc_user
) {
255 if ( $this->fld_flags
) {
256 if ( $row->rc_new
) {
259 if ( $row->rc_minor
) {
262 if ( $row->rc_bot
) {
267 if ( $this->fld_patrol
&& isset( $row->rc_patrolled
) ) {
268 $vals['patrolled'] = '';
271 if ( $this->fld_timestamp
) {
272 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
);
275 if ( $this->fld_sizes
) {
276 $vals['oldlen'] = intval( $row->rc_old_len
);
277 $vals['newlen'] = intval( $row->rc_new_len
);
280 if ( $this->fld_notificationtimestamp
) {
281 $vals['notificationtimestamp'] = ( $row->wl_notificationtimestamp
== null )
283 : wfTimestamp( TS_ISO_8601
, $row->wl_notificationtimestamp
);
286 if ( $this->fld_comment
&& isset( $row->rc_comment
) ) {
287 $vals['comment'] = $row->rc_comment
;
290 if ( $this->fld_parsedcomment
&& isset( $row->rc_comment
) ) {
291 $vals['parsedcomment'] = Linker
::formatComment( $row->rc_comment
, $title );
294 if ( $this->fld_loginfo
&& $row->rc_type
== RC_LOG
) {
295 $vals['logid'] = intval( $row->rc_logid
);
296 $vals['logtype'] = $row->rc_log_type
;
297 $vals['logaction'] = $row->rc_log_action
;
298 ApiQueryLogEvents
::addLogParams(
311 public function getAllowedParams() {
315 ApiBase
::PARAM_TYPE
=> 'timestamp'
318 ApiBase
::PARAM_TYPE
=> 'timestamp'
320 'namespace' => array (
321 ApiBase
::PARAM_ISMULTI
=> true,
322 ApiBase
::PARAM_TYPE
=> 'namespace'
325 ApiBase
::PARAM_TYPE
=> 'user',
327 'excludeuser' => array(
328 ApiBase
::PARAM_TYPE
=> 'user',
331 ApiBase
::PARAM_DFLT
=> 'older',
332 ApiBase
::PARAM_TYPE
=> array(
338 ApiBase
::PARAM_DFLT
=> 10,
339 ApiBase
::PARAM_TYPE
=> 'limit',
340 ApiBase
::PARAM_MIN
=> 1,
341 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
342 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
345 ApiBase
::PARAM_ISMULTI
=> true,
346 ApiBase
::PARAM_DFLT
=> 'ids|title|flags',
347 ApiBase
::PARAM_TYPE
=> array(
358 'notificationtimestamp',
363 ApiBase
::PARAM_ISMULTI
=> true,
364 ApiBase
::PARAM_TYPE
=> array(
376 ApiBase
::PARAM_TYPE
=> 'user'
379 ApiBase
::PARAM_TYPE
=> 'string'
384 public function getParamDescription() {
385 $p = $this->getModulePrefix();
387 'allrev' => 'Include multiple revisions of the same page within given timeframe',
388 'start' => 'The timestamp to start enumerating from',
389 'end' => 'The timestamp to end enumerating',
390 'namespace' => 'Filter changes to only the given namespace(s)',
391 'user' => 'Only list changes by this user',
392 'excludeuser' => 'Don\'t list changes by this user',
393 'dir' => $this->getDirectionDescription( $p ),
394 'limit' => 'How many total results to return per request',
396 'Which additional items to get (non-generator mode only).',
397 ' ids - Adds revision ids and page ids',
398 ' title - Adds title of the page',
399 ' flags - Adds flags for the edit',
400 ' user - Adds the user who made the edit',
401 ' userid - Adds user id of whom made the edit',
402 ' comment - Adds comment of the edit',
403 ' parsedcomment - Adds parsed comment of the edit',
404 ' timestamp - Adds timestamp of the edit',
405 ' patrol - Tags edits that are patrolled',
406 ' sizes - Adds the old and new lengths of the page',
407 ' notificationtimestamp - Adds timestamp of when the user was last notified about the edit',
408 ' loginfo - Adds log information where appropriate',
411 'Show only items that meet this criteria.',
412 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
414 'owner' => 'The name of the user whose watchlist you\'d like to access',
415 'token' => 'Give a security token (settable in preferences) to allow access to another user\'s watchlist'
419 public function getDescription() {
420 return "Get all recent changes to pages in the logged in user's watchlist";
423 public function getPossibleErrors() {
424 return array_merge( parent
::getPossibleErrors(), array(
425 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
426 array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ),
427 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
428 array( 'code' => 'patrol', 'info' => 'patrol property is not available' ),
430 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
431 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
435 public function getExamples() {
437 'api.php?action=query&list=watchlist',
438 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
439 'api.php?action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment',
440 'api.php?action=query&generator=watchlist&prop=info',
441 'api.php?action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user',
442 'api.php?action=query&list=watchlist&wlowner=Bob_Smith&wltoken=d8d562e9725ea1512894cdab28e5ceebc7f20237'
446 public function getHelpUrls() {
447 return 'https://www.mediawiki.org/wiki/API:Watchlist';
450 public function getVersion() {
451 return __CLASS__
. ': $Id$';