Merge "Move list generation out of TablePager::getLimitSelect()"
[lhc/web/wiklou.git] / includes / api / ApiQueryWatchlist.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 25, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
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.
30 *
31 * @ingroup API
32 */
33 class ApiQueryWatchlist extends ApiQueryGeneratorBase {
34
35 public function __construct( $query, $moduleName ) {
36 parent::__construct( $query, $moduleName, 'wl' );
37 }
38
39 public function execute() {
40 $this->run();
41 }
42
43 public function executeGenerator( $resultPageSet ) {
44 $this->run( $resultPageSet );
45 }
46
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;
50
51 /**
52 * @param $resultPageSet ApiPageSet
53 * @return void
54 */
55 private function run( $resultPageSet = null ) {
56 $this->selectNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
57
58 $params = $this->extractRequestParams();
59
60 $user = $this->getWatchlistUser( $params );
61
62 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
63 $prop = array_flip( $params['prop'] );
64
65 $this->fld_ids = isset( $prop['ids'] );
66 $this->fld_title = isset( $prop['title'] );
67 $this->fld_flags = isset( $prop['flags'] );
68 $this->fld_user = isset( $prop['user'] );
69 $this->fld_userid = isset( $prop['userid'] );
70 $this->fld_comment = isset( $prop['comment'] );
71 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
72 $this->fld_timestamp = isset( $prop['timestamp'] );
73 $this->fld_sizes = isset( $prop['sizes'] );
74 $this->fld_patrol = isset( $prop['patrol'] );
75 $this->fld_notificationtimestamp = isset( $prop['notificationtimestamp'] );
76 $this->fld_loginfo = isset( $prop['loginfo'] );
77
78 if ( $this->fld_patrol ) {
79 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
80 $this->dieUsage( 'patrol property is not available', 'patrol' );
81 }
82 }
83 }
84
85 $this->addFields( array(
86 'rc_namespace',
87 'rc_title',
88 'rc_timestamp',
89 'rc_type',
90 ) );
91
92 if ( is_null( $resultPageSet ) ) {
93 $this->addFields( array(
94 'rc_cur_id',
95 'rc_this_oldid',
96 'rc_last_oldid',
97 ) );
98
99 $this->addFieldsIf( array( 'rc_type', 'rc_minor', '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( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes );
105 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp );
106 $this->addFieldsIf( array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ), $this->fld_loginfo );
107 } elseif ( $params['allrev'] ) {
108 $this->addFields( 'rc_this_oldid' );
109 } else {
110 $this->addFields( 'rc_cur_id' );
111 }
112
113 $this->addTables( array(
114 'recentchanges',
115 'watchlist',
116 ) );
117
118 $userId = $user->getId();
119 $this->addJoinConds( array( 'watchlist' => array( 'INNER JOIN',
120 array(
121 'wl_user' => $userId,
122 'wl_namespace=rc_namespace',
123 'wl_title=rc_title'
124 ) ) ) );
125
126 $this->addWhere( array(
127 'rc_deleted' => 0,
128 ) );
129
130 $db = $this->getDB();
131
132 $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'],
133 $params['start'], $params['end'] );
134 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
135
136 if ( !$params['allrev'] ) {
137 $this->addTables( 'page' );
138 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN', 'rc_cur_id=page_id' ) ) );
139 $this->addWhere( 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG );
140 }
141
142 if ( !is_null( $params['show'] ) ) {
143 $show = array_flip( $params['show'] );
144
145 /* Check for conflicting parameters. */
146 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
147 || ( isset( $show['bot'] ) && isset( $show['!bot'] ) )
148 || ( isset( $show['anon'] ) && isset( $show['!anon'] ) )
149 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
150 ) {
151 $this->dieUsageMsg( 'show' );
152 }
153
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' );
159 }
160 }
161
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'] ) );
171 }
172
173 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
174 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
175 }
176 if ( !is_null( $params['user'] ) ) {
177 $this->addWhereFld( 'rc_user_text', $params['user'] );
178 }
179 if ( !is_null( $params['excludeuser'] ) ) {
180 $this->addWhere( 'rc_user_text != ' . $db->addQuotes( $params['excludeuser'] ) );
181 }
182
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' );
185
186 $this->addOption( 'LIMIT', $params['limit'] + 1 );
187
188 $ids = array();
189 $count = 0;
190 $res = $this->select( __METHOD__ );
191
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 ) );
196 break;
197 }
198
199 if ( is_null( $resultPageSet ) ) {
200 $vals = $this->extractRowInfo( $row );
201 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
202 if ( !$fit ) {
203 $this->setContinueEnumParameter( 'start',
204 wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
205 break;
206 }
207 } else {
208 if ( $params['allrev'] ) {
209 $ids[] = intval( $row->rc_this_oldid );
210 } else {
211 $ids[] = intval( $row->rc_cur_id );
212 }
213 }
214 }
215
216 if ( is_null( $resultPageSet ) ) {
217 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
218 } elseif ( $params['allrev'] ) {
219 $resultPageSet->populateFromRevisionIDs( $ids );
220 } else {
221 $resultPageSet->populateFromPageIDs( $ids );
222 }
223 }
224
225 private function extractRowInfo( $row ) {
226 $vals = array();
227
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 );
232 }
233
234 $title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
235
236 if ( $this->fld_title ) {
237 ApiQueryBase::addTitleInfo( $vals, $title );
238 }
239
240 if ( $this->fld_user || $this->fld_userid ) {
241
242 if ( $this->fld_userid ) {
243 $vals['userid'] = $row->rc_user;
244 // for backwards compatibility
245 $vals['user'] = $row->rc_user;
246 }
247
248 if ( $this->fld_user ) {
249 $vals['user'] = $row->rc_user_text;
250 }
251
252 if ( !$row->rc_user ) {
253 $vals['anon'] = '';
254 }
255 }
256
257 if ( $this->fld_flags ) {
258 if ( $row->rc_type == RC_NEW ) {
259 $vals['new'] = '';
260 }
261 if ( $row->rc_minor ) {
262 $vals['minor'] = '';
263 }
264 if ( $row->rc_bot ) {
265 $vals['bot'] = '';
266 }
267 }
268
269 if ( $this->fld_patrol && isset( $row->rc_patrolled ) ) {
270 $vals['patrolled'] = '';
271 }
272
273 if ( $this->fld_timestamp ) {
274 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp );
275 }
276
277 if ( $this->fld_sizes ) {
278 $vals['oldlen'] = intval( $row->rc_old_len );
279 $vals['newlen'] = intval( $row->rc_new_len );
280 }
281
282 if ( $this->fld_notificationtimestamp ) {
283 $vals['notificationtimestamp'] = ( $row->wl_notificationtimestamp == null )
284 ? ''
285 : wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
286 }
287
288 if ( $this->fld_comment && isset( $row->rc_comment ) ) {
289 $vals['comment'] = $row->rc_comment;
290 }
291
292 if ( $this->fld_parsedcomment && isset( $row->rc_comment ) ) {
293 $vals['parsedcomment'] = Linker::formatComment( $row->rc_comment, $title );
294 }
295
296 if ( $this->fld_loginfo && $row->rc_type == RC_LOG ) {
297 $vals['logid'] = intval( $row->rc_logid );
298 $vals['logtype'] = $row->rc_log_type;
299 $vals['logaction'] = $row->rc_log_action;
300 $logEntry = DatabaseLogEntry::newFromRow( (array)$row );
301 ApiQueryLogEvents::addLogParams(
302 $this->getResult(),
303 $vals,
304 $logEntry->getParameters(),
305 $logEntry->getType(),
306 $logEntry->getSubtype(),
307 $logEntry->getTimestamp()
308 );
309 }
310
311 return $vals;
312 }
313
314 public function getAllowedParams() {
315 return array(
316 'allrev' => false,
317 'start' => array(
318 ApiBase::PARAM_TYPE => 'timestamp'
319 ),
320 'end' => array(
321 ApiBase::PARAM_TYPE => 'timestamp'
322 ),
323 'namespace' => array(
324 ApiBase::PARAM_ISMULTI => true,
325 ApiBase::PARAM_TYPE => 'namespace'
326 ),
327 'user' => array(
328 ApiBase::PARAM_TYPE => 'user',
329 ),
330 'excludeuser' => array(
331 ApiBase::PARAM_TYPE => 'user',
332 ),
333 'dir' => array(
334 ApiBase::PARAM_DFLT => 'older',
335 ApiBase::PARAM_TYPE => array(
336 'newer',
337 'older'
338 )
339 ),
340 'limit' => array(
341 ApiBase::PARAM_DFLT => 10,
342 ApiBase::PARAM_TYPE => 'limit',
343 ApiBase::PARAM_MIN => 1,
344 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
345 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
346 ),
347 'prop' => array(
348 ApiBase::PARAM_ISMULTI => true,
349 ApiBase::PARAM_DFLT => 'ids|title|flags',
350 ApiBase::PARAM_TYPE => array(
351 'ids',
352 'title',
353 'flags',
354 'user',
355 'userid',
356 'comment',
357 'parsedcomment',
358 'timestamp',
359 'patrol',
360 'sizes',
361 'notificationtimestamp',
362 'loginfo',
363 )
364 ),
365 'show' => array(
366 ApiBase::PARAM_ISMULTI => true,
367 ApiBase::PARAM_TYPE => array(
368 'minor',
369 '!minor',
370 'bot',
371 '!bot',
372 'anon',
373 '!anon',
374 'patrolled',
375 '!patrolled',
376 )
377 ),
378 'owner' => array(
379 ApiBase::PARAM_TYPE => 'user'
380 ),
381 'token' => array(
382 ApiBase::PARAM_TYPE => 'string'
383 )
384 );
385 }
386
387 public function getParamDescription() {
388 $p = $this->getModulePrefix();
389 return array(
390 'allrev' => 'Include multiple revisions of the same page within given timeframe',
391 'start' => 'The timestamp to start enumerating from',
392 'end' => 'The timestamp to end enumerating',
393 'namespace' => 'Filter changes to only the given namespace(s)',
394 'user' => 'Only list changes by this user',
395 'excludeuser' => 'Don\'t list changes by this user',
396 'dir' => $this->getDirectionDescription( $p ),
397 'limit' => 'How many total results to return per request',
398 'prop' => array(
399 'Which additional items to get (non-generator mode only).',
400 ' ids - Adds revision ids and page ids',
401 ' title - Adds title of the page',
402 ' flags - Adds flags for the edit',
403 ' user - Adds the user who made the edit',
404 ' userid - Adds user id of whom made the edit',
405 ' comment - Adds comment of the edit',
406 ' parsedcomment - Adds parsed comment of the edit',
407 ' timestamp - Adds timestamp of the edit',
408 ' patrol - Tags edits that are patrolled',
409 ' sizes - Adds the old and new lengths of the page',
410 ' notificationtimestamp - Adds timestamp of when the user was last notified about the edit',
411 ' loginfo - Adds log information where appropriate',
412 ),
413 'show' => array(
414 'Show only items that meet this criteria.',
415 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
416 ),
417 'owner' => 'The name of the user whose watchlist you\'d like to access',
418 'token' => 'Give a security token (settable in preferences) to allow access to another user\'s watchlist'
419 );
420 }
421
422 public function getResultProperties() {
423 global $wgLogTypes;
424 return array(
425 'ids' => array(
426 'pageid' => 'integer',
427 'revid' => 'integer',
428 'old_revid' => 'integer'
429 ),
430 'title' => array(
431 'ns' => 'namespace',
432 'title' => 'string'
433 ),
434 'user' => array(
435 'user' => 'string',
436 'anon' => 'boolean'
437 ),
438 'userid' => array(
439 'userid' => 'integer',
440 'anon' => 'boolean'
441 ),
442 'flags' => array(
443 'new' => 'boolean',
444 'minor' => 'boolean',
445 'bot' => 'boolean'
446 ),
447 'patrol' => array(
448 'patrolled' => 'boolean'
449 ),
450 'timestamp' => array(
451 'timestamp' => 'timestamp'
452 ),
453 'sizes' => array(
454 'oldlen' => 'integer',
455 'newlen' => 'integer'
456 ),
457 'notificationtimestamp' => array(
458 'notificationtimestamp' => array(
459 ApiBase::PROP_TYPE => 'timestamp',
460 ApiBase::PROP_NULLABLE => true
461 )
462 ),
463 'comment' => array(
464 'comment' => array(
465 ApiBase::PROP_TYPE => 'string',
466 ApiBase::PROP_NULLABLE => true
467 )
468 ),
469 'parsedcomment' => array(
470 'parsedcomment' => array(
471 ApiBase::PROP_TYPE => 'string',
472 ApiBase::PROP_NULLABLE => true
473 )
474 ),
475 'loginfo' => array(
476 'logid' => array(
477 ApiBase::PROP_TYPE => 'integer',
478 ApiBase::PROP_NULLABLE => true
479 ),
480 'logtype' => array(
481 ApiBase::PROP_TYPE => $wgLogTypes,
482 ApiBase::PROP_NULLABLE => true
483 ),
484 'logaction' => array(
485 ApiBase::PROP_TYPE => 'string',
486 ApiBase::PROP_NULLABLE => true
487 )
488 )
489 );
490 }
491
492 public function getDescription() {
493 return "Get all recent changes to pages in the logged in user's watchlist";
494 }
495
496 public function getPossibleErrors() {
497 return array_merge( parent::getPossibleErrors(), array(
498 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
499 array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ),
500 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
501 array( 'code' => 'patrol', 'info' => 'patrol property is not available' ),
502 array( 'show' ),
503 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
504 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
505 ) );
506 }
507
508 public function getExamples() {
509 return array(
510 'api.php?action=query&list=watchlist',
511 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
512 'api.php?action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment',
513 'api.php?action=query&generator=watchlist&prop=info',
514 'api.php?action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user',
515 'api.php?action=query&list=watchlist&wlowner=Bob_Smith&wltoken=123ABC'
516 );
517 }
518
519 public function getHelpUrls() {
520 return 'https://www.mediawiki.org/wiki/API:Watchlist';
521 }
522 }