Merge "(bug 17602) fix Monobook action tabs not quite touching the page body"
[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['type'] ) ) {
174 $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) );
175 }
176
177 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
178 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
179 }
180 if ( !is_null( $params['user'] ) ) {
181 $this->addWhereFld( 'rc_user_text', $params['user'] );
182 }
183 if ( !is_null( $params['excludeuser'] ) ) {
184 $this->addWhere( 'rc_user_text != ' . $db->addQuotes( $params['excludeuser'] ) );
185 }
186
187 // This is an index optimization for mysql, as done in the Special:Watchlist page
188 $this->addWhereIf( "rc_timestamp > ''", !isset( $params['start'] ) && !isset( $params['end'] ) && $db->getType() == 'mysql' );
189
190 $this->addOption( 'LIMIT', $params['limit'] + 1 );
191
192 $ids = array();
193 $count = 0;
194 $res = $this->select( __METHOD__ );
195
196 foreach ( $res as $row ) {
197 if ( ++ $count > $params['limit'] ) {
198 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
199 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
200 break;
201 }
202
203 if ( is_null( $resultPageSet ) ) {
204 $vals = $this->extractRowInfo( $row );
205 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
206 if ( !$fit ) {
207 $this->setContinueEnumParameter( 'start',
208 wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
209 break;
210 }
211 } else {
212 if ( $params['allrev'] ) {
213 $ids[] = intval( $row->rc_this_oldid );
214 } else {
215 $ids[] = intval( $row->rc_cur_id );
216 }
217 }
218 }
219
220 if ( is_null( $resultPageSet ) ) {
221 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
222 } elseif ( $params['allrev'] ) {
223 $resultPageSet->populateFromRevisionIDs( $ids );
224 } else {
225 $resultPageSet->populateFromPageIDs( $ids );
226 }
227 }
228
229 private function extractRowInfo( $row ) {
230 $vals = array();
231
232 $type = intval( $row->rc_type );
233
234 /* Determine what kind of change this was. */
235 switch ( $type ) {
236 case RC_EDIT:
237 $vals['type'] = 'edit';
238 break;
239 case RC_NEW:
240 $vals['type'] = 'new';
241 break;
242 case RC_MOVE:
243 $vals['type'] = 'move';
244 break;
245 case RC_LOG:
246 $vals['type'] = 'log';
247 break;
248 case RC_EXTERNAL:
249 $vals['type'] = 'external';
250 break;
251 case RC_MOVE_OVER_REDIRECT:
252 $vals['type'] = 'move over redirect';
253 break;
254 default:
255 $vals['type'] = $type;
256 }
257
258 if ( $this->fld_ids ) {
259 $vals['pageid'] = intval( $row->rc_cur_id );
260 $vals['revid'] = intval( $row->rc_this_oldid );
261 $vals['old_revid'] = intval( $row->rc_last_oldid );
262 }
263
264 $title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
265
266 if ( $this->fld_title ) {
267 ApiQueryBase::addTitleInfo( $vals, $title );
268 }
269
270 if ( $this->fld_user || $this->fld_userid ) {
271
272 if ( $this->fld_userid ) {
273 $vals['userid'] = $row->rc_user;
274 // for backwards compatibility
275 $vals['user'] = $row->rc_user;
276 }
277
278 if ( $this->fld_user ) {
279 $vals['user'] = $row->rc_user_text;
280 }
281
282 if ( !$row->rc_user ) {
283 $vals['anon'] = '';
284 }
285 }
286
287 if ( $this->fld_flags ) {
288 if ( $row->rc_type == RC_NEW ) {
289 $vals['new'] = '';
290 }
291 if ( $row->rc_minor ) {
292 $vals['minor'] = '';
293 }
294 if ( $row->rc_bot ) {
295 $vals['bot'] = '';
296 }
297 }
298
299 if ( $this->fld_patrol && isset( $row->rc_patrolled ) ) {
300 $vals['patrolled'] = '';
301 }
302
303 if ( $this->fld_timestamp ) {
304 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp );
305 }
306
307 if ( $this->fld_sizes ) {
308 $vals['oldlen'] = intval( $row->rc_old_len );
309 $vals['newlen'] = intval( $row->rc_new_len );
310 }
311
312 if ( $this->fld_notificationtimestamp ) {
313 $vals['notificationtimestamp'] = ( $row->wl_notificationtimestamp == null )
314 ? ''
315 : wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
316 }
317
318 if ( $this->fld_comment && isset( $row->rc_comment ) ) {
319 $vals['comment'] = $row->rc_comment;
320 }
321
322 if ( $this->fld_parsedcomment && isset( $row->rc_comment ) ) {
323 $vals['parsedcomment'] = Linker::formatComment( $row->rc_comment, $title );
324 }
325
326 if ( $this->fld_loginfo && $row->rc_type == RC_LOG ) {
327 $vals['logid'] = intval( $row->rc_logid );
328 $vals['logtype'] = $row->rc_log_type;
329 $vals['logaction'] = $row->rc_log_action;
330 $logEntry = DatabaseLogEntry::newFromRow( (array)$row );
331 ApiQueryLogEvents::addLogParams(
332 $this->getResult(),
333 $vals,
334 $logEntry->getParameters(),
335 $logEntry->getType(),
336 $logEntry->getSubtype(),
337 $logEntry->getTimestamp()
338 );
339 }
340
341 return $vals;
342 }
343
344 /* Copied from ApiQueryRecentChanges. */
345 private function parseRCType( $type ) {
346 if ( is_array( $type ) ) {
347 $retval = array();
348 foreach ( $type as $t ) {
349 $retval[] = $this->parseRCType( $t );
350 }
351 return $retval;
352 }
353 switch ( $type ) {
354 case 'edit':
355 return RC_EDIT;
356 case 'new':
357 return RC_NEW;
358 case 'log':
359 return RC_LOG;
360 case 'external':
361 return RC_EXTERNAL;
362 }
363 }
364
365 public function getAllowedParams() {
366 return array(
367 'allrev' => false,
368 'start' => array(
369 ApiBase::PARAM_TYPE => 'timestamp'
370 ),
371 'end' => array(
372 ApiBase::PARAM_TYPE => 'timestamp'
373 ),
374 'namespace' => array(
375 ApiBase::PARAM_ISMULTI => true,
376 ApiBase::PARAM_TYPE => 'namespace'
377 ),
378 'user' => array(
379 ApiBase::PARAM_TYPE => 'user',
380 ),
381 'excludeuser' => array(
382 ApiBase::PARAM_TYPE => 'user',
383 ),
384 'dir' => array(
385 ApiBase::PARAM_DFLT => 'older',
386 ApiBase::PARAM_TYPE => array(
387 'newer',
388 'older'
389 )
390 ),
391 'limit' => array(
392 ApiBase::PARAM_DFLT => 10,
393 ApiBase::PARAM_TYPE => 'limit',
394 ApiBase::PARAM_MIN => 1,
395 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
396 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
397 ),
398 'prop' => array(
399 ApiBase::PARAM_ISMULTI => true,
400 ApiBase::PARAM_DFLT => 'ids|title|flags',
401 ApiBase::PARAM_TYPE => array(
402 'ids',
403 'title',
404 'flags',
405 'user',
406 'userid',
407 'comment',
408 'parsedcomment',
409 'timestamp',
410 'patrol',
411 'sizes',
412 'notificationtimestamp',
413 'loginfo',
414 )
415 ),
416 'show' => array(
417 ApiBase::PARAM_ISMULTI => true,
418 ApiBase::PARAM_TYPE => array(
419 'minor',
420 '!minor',
421 'bot',
422 '!bot',
423 'anon',
424 '!anon',
425 'patrolled',
426 '!patrolled',
427 )
428 ),
429 'type' => array(
430 ApiBase::PARAM_ISMULTI => true,
431 ApiBase::PARAM_TYPE => array(
432 'edit',
433 'external',
434 'new',
435 'log',
436 )
437 ),
438 'owner' => array(
439 ApiBase::PARAM_TYPE => 'user'
440 ),
441 'token' => array(
442 ApiBase::PARAM_TYPE => 'string'
443 )
444 );
445 }
446
447 public function getParamDescription() {
448 $p = $this->getModulePrefix();
449 return array(
450 'allrev' => 'Include multiple revisions of the same page within given timeframe',
451 'start' => 'The timestamp to start enumerating from',
452 'end' => 'The timestamp to end enumerating',
453 'namespace' => 'Filter changes to only the given namespace(s)',
454 'user' => 'Only list changes by this user',
455 'excludeuser' => 'Don\'t list changes by this user',
456 'dir' => $this->getDirectionDescription( $p ),
457 'limit' => 'How many total results to return per request',
458 'prop' => array(
459 'Which additional items to get (non-generator mode only).',
460 ' ids - Adds revision ids and page ids',
461 ' title - Adds title of the page',
462 ' flags - Adds flags for the edit',
463 ' user - Adds the user who made the edit',
464 ' userid - Adds user id of whom made the edit',
465 ' comment - Adds comment of the edit',
466 ' parsedcomment - Adds parsed comment of the edit',
467 ' timestamp - Adds timestamp of the edit',
468 ' patrol - Tags edits that are patrolled',
469 ' sizes - Adds the old and new lengths of the page',
470 ' notificationtimestamp - Adds timestamp of when the user was last notified about the edit',
471 ' loginfo - Adds log information where appropriate',
472 ),
473 'show' => array(
474 'Show only items that meet this criteria.',
475 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
476 ),
477 'type' => array(
478 'Which types of changes to show',
479 ' edit - Regular page edits',
480 ' external - External changes',
481 ' new - Page creations',
482 ' log - Log entries',
483 ),
484 'owner' => 'The name of the user whose watchlist you\'d like to access',
485 'token' => 'Give a security token (settable in preferences) to allow access to another user\'s watchlist'
486 );
487 }
488
489 public function getResultProperties() {
490 global $wgLogTypes;
491 return array(
492 '' => array(
493 'type' => array(
494 ApiBase::PROP_TYPE => array(
495 'edit',
496 'new',
497 'move',
498 'log',
499 'move over redirect'
500 )
501 )
502 ),
503 'ids' => array(
504 'pageid' => 'integer',
505 'revid' => 'integer',
506 'old_revid' => 'integer'
507 ),
508 'title' => array(
509 'ns' => 'namespace',
510 'title' => 'string'
511 ),
512 'user' => array(
513 'user' => 'string',
514 'anon' => 'boolean'
515 ),
516 'userid' => array(
517 'userid' => 'integer',
518 'anon' => 'boolean'
519 ),
520 'flags' => array(
521 'new' => 'boolean',
522 'minor' => 'boolean',
523 'bot' => 'boolean'
524 ),
525 'patrol' => array(
526 'patrolled' => 'boolean'
527 ),
528 'timestamp' => array(
529 'timestamp' => 'timestamp'
530 ),
531 'sizes' => array(
532 'oldlen' => 'integer',
533 'newlen' => 'integer'
534 ),
535 'notificationtimestamp' => array(
536 'notificationtimestamp' => array(
537 ApiBase::PROP_TYPE => 'timestamp',
538 ApiBase::PROP_NULLABLE => true
539 )
540 ),
541 'comment' => array(
542 'comment' => array(
543 ApiBase::PROP_TYPE => 'string',
544 ApiBase::PROP_NULLABLE => true
545 )
546 ),
547 'parsedcomment' => array(
548 'parsedcomment' => array(
549 ApiBase::PROP_TYPE => 'string',
550 ApiBase::PROP_NULLABLE => true
551 )
552 ),
553 'loginfo' => array(
554 'logid' => array(
555 ApiBase::PROP_TYPE => 'integer',
556 ApiBase::PROP_NULLABLE => true
557 ),
558 'logtype' => array(
559 ApiBase::PROP_TYPE => $wgLogTypes,
560 ApiBase::PROP_NULLABLE => true
561 ),
562 'logaction' => array(
563 ApiBase::PROP_TYPE => 'string',
564 ApiBase::PROP_NULLABLE => true
565 )
566 )
567 );
568 }
569
570 public function getDescription() {
571 return "Get all recent changes to pages in the logged in user's watchlist";
572 }
573
574 public function getPossibleErrors() {
575 return array_merge( parent::getPossibleErrors(), array(
576 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
577 array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ),
578 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
579 array( 'code' => 'patrol', 'info' => 'patrol property is not available' ),
580 array( 'show' ),
581 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
582 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
583 ) );
584 }
585
586 public function getExamples() {
587 return array(
588 'api.php?action=query&list=watchlist',
589 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
590 'api.php?action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment',
591 'api.php?action=query&generator=watchlist&prop=info',
592 'api.php?action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user',
593 'api.php?action=query&list=watchlist&wlowner=Bob_Smith&wltoken=123ABC'
594 );
595 }
596
597 public function getHelpUrls() {
598 return 'https://www.mediawiki.org/wiki/API:Watchlist';
599 }
600 }