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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * This query action allows clients to retrieve a list of recently modified pages
34 * that are part of the logged-in user's watchlist.
38 class ApiQueryWatchlist
extends ApiQueryGeneratorBase
{
40 public function __construct( $query, $moduleName ) {
41 parent
::__construct( $query, $moduleName, 'wl' );
44 public function execute() {
48 public function executeGenerator( $resultPageSet ) {
49 $this->run( $resultPageSet );
52 private $fld_ids = false, $fld_title = false, $fld_patrol = false, $fld_flags = false,
53 $fld_timestamp = false, $fld_user = false, $fld_comment = false, $fld_parsedcomment = false, $fld_sizes = false,
54 $fld_notificationtimestamp = false, $fld_userid = false;
56 private function run( $resultPageSet = null ) {
57 $this->selectNamedDB( 'watchlist', DB_SLAVE
, 'watchlist' );
59 $params = $this->extractRequestParams();
61 $user = $this->getWatchlistUser( $params );
63 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
64 $prop = array_flip( $params['prop'] );
66 $this->fld_ids
= isset( $prop['ids'] );
67 $this->fld_title
= isset( $prop['title'] );
68 $this->fld_flags
= isset( $prop['flags'] );
69 $this->fld_user
= isset( $prop['user'] );
70 $this->fld_userid
= isset( $prop['userid'] );
71 $this->fld_comment
= isset( $prop['comment'] );
72 $this->fld_parsedcomment
= isset ( $prop['parsedcomment'] );
73 $this->fld_timestamp
= isset( $prop['timestamp'] );
74 $this->fld_sizes
= isset( $prop['sizes'] );
75 $this->fld_patrol
= isset( $prop['patrol'] );
76 $this->fld_notificationtimestamp
= isset( $prop['notificationtimestamp'] );
78 if ( $this->fld_patrol
) {
79 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
80 $this->dieUsage( 'patrol property is not available', 'patrol' );
85 $this->addFields( array(
91 if ( is_null( $resultPageSet ) ) {
92 $this->addFields( array(
97 $this->addFieldsIf( 'rc_new', $this->fld_flags
);
98 $this->addFieldsIf( 'rc_minor', $this->fld_flags
);
99 $this->addFieldsIf( '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( 'rc_old_len', $this->fld_sizes
);
105 $this->addFieldsIf( 'rc_new_len', $this->fld_sizes
);
106 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp
);
107 } elseif ( $params['allrev'] ) {
108 $this->addFields( 'rc_this_oldid' );
110 $this->addFields( 'rc_cur_id' );
113 $this->addTables( array(
119 $userId = $user->getId();
120 $this->addWhere( array(
121 'wl_namespace = rc_namespace',
122 'wl_title = rc_title',
123 'rc_cur_id = page_id',
124 'wl_user' => $userId,
128 $this->addWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
129 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
130 $this->addWhereIf( 'rc_this_oldid=page_latest', !$params['allrev'] );
132 if ( !is_null( $params['show'] ) ) {
133 $show = array_flip( $params['show'] );
135 /* Check for conflicting parameters. */
136 if ( ( isset ( $show['minor'] ) && isset ( $show['!minor'] ) )
137 ||
( isset ( $show['bot'] ) && isset ( $show['!bot'] ) )
138 ||
( isset ( $show['anon'] ) && isset ( $show['!anon'] ) )
139 ||
( isset ( $show['patrolled'] ) && isset ( $show['!patrolled'] ) )
142 $this->dieUsageMsg( array( 'show' ) );
145 // Check permissions.
146 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
148 if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
149 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
153 /* Add additional conditions to query depending upon parameters. */
154 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
155 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
156 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
157 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
158 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
159 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
160 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
161 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
164 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
165 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
167 if ( !is_null( $params['user'] ) ) {
168 $this->addWhereFld( 'rc_user_text', $params['user'] );
170 if ( !is_null( $params['excludeuser'] ) ) {
171 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
174 $db = $this->getDB();
176 // This is an index optimization for mysql, as done in the Special:Watchlist page
177 $this->addWhereIf( "rc_timestamp > ''", !isset( $params['start'] ) && !isset( $params['end'] ) && $db->getType() == 'mysql' );
179 $this->addOption( 'LIMIT', $params['limit'] +
1 );
183 $res = $this->select( __METHOD__
);
185 foreach ( $res as $row ) {
186 if ( ++
$count > $params['limit'] ) {
187 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
188 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
192 if ( is_null( $resultPageSet ) ) {
193 $vals = $this->extractRowInfo( $row );
194 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
196 $this->setContinueEnumParameter( 'start',
197 wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
201 if ( $params['allrev'] ) {
202 $ids[] = intval( $row->rc_this_oldid
);
204 $ids[] = intval( $row->rc_cur_id
);
209 if ( is_null( $resultPageSet ) ) {
210 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
211 } elseif ( $params['allrev'] ) {
212 $resultPageSet->populateFromRevisionIDs( $ids );
214 $resultPageSet->populateFromPageIDs( $ids );
218 private function extractRowInfo( $row ) {
221 if ( $this->fld_ids
) {
222 $vals['pageid'] = intval( $row->rc_cur_id
);
223 $vals['revid'] = intval( $row->rc_this_oldid
);
226 $title = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
228 if ( $this->fld_title
) {
229 ApiQueryBase
::addTitleInfo( $vals, $title );
232 if ( $this->fld_user ||
$this->fld_userid
) {
234 if ( $this->fld_user
) {
235 $vals['user'] = $row->rc_user_text
;
238 if ( $this->fld_userid
) {
239 $vals['user'] = $row->rc_user
;
242 if ( !$row->rc_user
) {
247 if ( $this->fld_flags
) {
248 if ( $row->rc_new
) {
251 if ( $row->rc_minor
) {
254 if ( $row->rc_bot
) {
259 if ( $this->fld_patrol
&& isset( $row->rc_patrolled
) ) {
260 $vals['patrolled'] = '';
263 if ( $this->fld_timestamp
) {
264 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
);
267 if ( $this->fld_sizes
) {
268 $vals['oldlen'] = intval( $row->rc_old_len
);
269 $vals['newlen'] = intval( $row->rc_new_len
);
272 if ( $this->fld_notificationtimestamp
) {
273 $vals['notificationtimestamp'] = ( $row->wl_notificationtimestamp
== null )
275 : wfTimestamp( TS_ISO_8601
, $row->wl_notificationtimestamp
);
278 if ( $this->fld_comment
&& isset( $row->rc_comment
) ) {
279 $vals['comment'] = $row->rc_comment
;
282 if ( $this->fld_parsedcomment
&& isset( $row->rc_comment
) ) {
284 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rc_comment
, $title );
290 public function getAllowedParams() {
294 ApiBase
::PARAM_TYPE
=> 'timestamp'
297 ApiBase
::PARAM_TYPE
=> 'timestamp'
299 'namespace' => array (
300 ApiBase
::PARAM_ISMULTI
=> true,
301 ApiBase
::PARAM_TYPE
=> 'namespace'
304 ApiBase
::PARAM_TYPE
=> 'user',
306 'excludeuser' => array(
307 ApiBase
::PARAM_TYPE
=> 'user',
310 ApiBase
::PARAM_DFLT
=> 'older',
311 ApiBase
::PARAM_TYPE
=> array(
317 ApiBase
::PARAM_DFLT
=> 10,
318 ApiBase
::PARAM_TYPE
=> 'limit',
319 ApiBase
::PARAM_MIN
=> 1,
320 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
321 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
324 ApiBase
::PARAM_ISMULTI
=> true,
325 ApiBase
::PARAM_DFLT
=> 'ids|title|flags',
326 ApiBase
::PARAM_TYPE
=> array(
337 'notificationtimestamp'
341 ApiBase
::PARAM_ISMULTI
=> true,
342 ApiBase
::PARAM_TYPE
=> array(
354 ApiBase
::PARAM_TYPE
=> 'user'
357 ApiBase
::PARAM_TYPE
=> 'string'
362 public function getParamDescription() {
364 'allrev' => 'Include multiple revisions of the same page within given timeframe',
365 'start' => 'The timestamp to start enumerating from',
366 'end' => 'The timestamp to end enumerating',
367 'namespace' => 'Filter changes to only the given namespace(s)',
368 'user' => 'Only list changes by this user',
369 'excludeuser' => 'Don\'t list changes by this user',
370 'dir' => 'In which direction to enumerate pages',
371 'limit' => 'How many total results to return per request',
373 'Which additional items to get (non-generator mode only).',
374 ' ids - Adds revision ids and page ids',
375 ' title - Adds title of the page',
376 ' flags - Adds flags for the edit',
377 ' user - Adds the user who made the edit',
378 ' userid - Adds user id of whom made the edit',
379 ' comment - Adds comment of the edit',
380 ' parsedcomment - Adds parsed comment of the edit',
381 ' timestamp - Adds timestamp of the edit',
382 ' patrol - Tags edits that are patrolled',
383 ' size - Adds the old and new lengths of the page',
384 ' notificationtimestamp - Adds timestamp of when the user was last notified about the edit',
387 'Show only items that meet this criteria.',
388 "For example, to see only minor edits done by logged-in users, set {$this->getModulePrefix()}show=minor|!anon"
390 'owner' => 'The name of the user whose watchlist you\'d like to access',
391 'token' => 'Give a security token (settable in preferences) to allow access to another user\'s watchlist'
395 public function getDescription() {
396 return "Get all recent changes to pages in the logged in user's watchlist";
399 public function getPossibleErrors() {
400 return array_merge( parent
::getPossibleErrors(), array(
401 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
402 array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ),
403 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
404 array( 'code' => 'patrol', 'info' => 'patrol property is not available' ),
406 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
407 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
411 protected function getExamples() {
413 'api.php?action=query&list=watchlist',
414 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
415 'api.php?action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment',
416 'api.php?action=query&generator=watchlist&prop=info',
417 'api.php?action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user',
418 'api.php?action=query&list=watchlist&wlowner=Bob_Smith&wltoken=d8d562e9725ea1512894cdab28e5ceebc7f20237'
422 public function getVersion() {
423 return __CLASS__
. ': $Id$';