Merge "Add licensing for extensions to Special:Version"
[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,
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,
51 $fld_loginfo = false;
52
53 /**
54 * @param $resultPageSet ApiPageSet
55 * @return void
56 */
57 private function run( $resultPageSet = null ) {
58 $this->selectNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
59
60 $params = $this->extractRequestParams();
61
62 $user = $this->getWatchlistUser( $params );
63
64 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
65 $prop = array_flip( $params['prop'] );
66
67 $this->fld_ids = isset( $prop['ids'] );
68 $this->fld_title = isset( $prop['title'] );
69 $this->fld_flags = isset( $prop['flags'] );
70 $this->fld_user = isset( $prop['user'] );
71 $this->fld_userid = isset( $prop['userid'] );
72 $this->fld_comment = isset( $prop['comment'] );
73 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
74 $this->fld_timestamp = isset( $prop['timestamp'] );
75 $this->fld_sizes = isset( $prop['sizes'] );
76 $this->fld_patrol = isset( $prop['patrol'] );
77 $this->fld_notificationtimestamp = isset( $prop['notificationtimestamp'] );
78 $this->fld_loginfo = isset( $prop['loginfo'] );
79
80 if ( $this->fld_patrol ) {
81 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
82 $this->dieUsage( 'patrol property is not available', 'patrol' );
83 }
84 }
85 }
86
87 $this->addFields( array(
88 'rc_namespace',
89 'rc_title',
90 'rc_timestamp',
91 'rc_type',
92 ) );
93
94 if ( is_null( $resultPageSet ) ) {
95 $this->addFields( array(
96 'rc_cur_id',
97 'rc_this_oldid',
98 'rc_last_oldid',
99 ) );
100
101 $this->addFieldsIf( array( 'rc_type', 'rc_minor', 'rc_bot' ), $this->fld_flags );
102 $this->addFieldsIf( 'rc_user', $this->fld_user || $this->fld_userid );
103 $this->addFieldsIf( 'rc_user_text', $this->fld_user );
104 $this->addFieldsIf( 'rc_comment', $this->fld_comment || $this->fld_parsedcomment );
105 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrol );
106 $this->addFieldsIf( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes );
107 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp );
108 $this->addFieldsIf(
109 array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ),
110 $this->fld_loginfo
111 );
112 } elseif ( $params['allrev'] ) {
113 $this->addFields( 'rc_this_oldid' );
114 } else {
115 $this->addFields( 'rc_cur_id' );
116 }
117
118 $this->addTables( array(
119 'recentchanges',
120 'watchlist',
121 ) );
122
123 $userId = $user->getId();
124 $this->addJoinConds( array( 'watchlist' => array( 'INNER JOIN',
125 array(
126 'wl_user' => $userId,
127 'wl_namespace=rc_namespace',
128 'wl_title=rc_title'
129 )
130 ) ) );
131
132 $this->addWhere( array(
133 'rc_deleted' => 0,
134 ) );
135
136 $db = $this->getDB();
137
138 $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'],
139 $params['start'], $params['end'] );
140 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
141
142 if ( !$params['allrev'] ) {
143 $this->addTables( 'page' );
144 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN', 'rc_cur_id=page_id' ) ) );
145 $this->addWhere( 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG );
146 }
147
148 if ( !is_null( $params['show'] ) ) {
149 $show = array_flip( $params['show'] );
150
151 /* Check for conflicting parameters. */
152 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
153 || ( isset( $show['bot'] ) && isset( $show['!bot'] ) )
154 || ( isset( $show['anon'] ) && isset( $show['!anon'] ) )
155 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
156 ) {
157 $this->dieUsageMsg( 'show' );
158 }
159
160 // Check permissions.
161 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
162 $user = $this->getUser();
163 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
164 $this->dieUsage(
165 'You need the patrol right to request the patrolled flag',
166 'permissiondenied'
167 );
168 }
169 }
170
171 /* Add additional conditions to query depending upon parameters. */
172 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
173 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
174 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
175 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
176 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
177 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
178 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
179 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
180 }
181
182 if ( !is_null( $params['type'] ) ) {
183 $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) );
184 }
185
186 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
187 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
188 }
189 if ( !is_null( $params['user'] ) ) {
190 $this->addWhereFld( 'rc_user_text', $params['user'] );
191 }
192 if ( !is_null( $params['excludeuser'] ) ) {
193 $this->addWhere( 'rc_user_text != ' . $db->addQuotes( $params['excludeuser'] ) );
194 }
195
196 // This is an index optimization for mysql, as done in the Special:Watchlist page
197 $this->addWhereIf(
198 "rc_timestamp > ''",
199 !isset( $params['start'] ) && !isset( $params['end'] ) && $db->getType() == 'mysql'
200 );
201
202 $this->addOption( 'LIMIT', $params['limit'] + 1 );
203
204 $ids = array();
205 $count = 0;
206 $res = $this->select( __METHOD__ );
207
208 foreach ( $res as $row ) {
209 if ( ++$count > $params['limit'] ) {
210 // We've reached the one extra which shows that there are
211 // additional pages to be had. Stop here...
212 $this->setContinueEnumParameter(
213 'start',
214 wfTimestamp( TS_ISO_8601, $row->rc_timestamp )
215 );
216 break;
217 }
218
219 if ( is_null( $resultPageSet ) ) {
220 $vals = $this->extractRowInfo( $row );
221 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
222 if ( !$fit ) {
223 $this->setContinueEnumParameter( 'start',
224 wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
225 break;
226 }
227 } else {
228 if ( $params['allrev'] ) {
229 $ids[] = intval( $row->rc_this_oldid );
230 } else {
231 $ids[] = intval( $row->rc_cur_id );
232 }
233 }
234 }
235
236 if ( is_null( $resultPageSet ) ) {
237 $this->getResult()->setIndexedTagName_internal(
238 array( 'query', $this->getModuleName() ),
239 'item'
240 );
241 } elseif ( $params['allrev'] ) {
242 $resultPageSet->populateFromRevisionIDs( $ids );
243 } else {
244 $resultPageSet->populateFromPageIDs( $ids );
245 }
246 }
247
248 private function extractRowInfo( $row ) {
249 $vals = array();
250
251 $type = intval( $row->rc_type );
252
253 /* Determine what kind of change this was. */
254 switch ( $type ) {
255 case RC_EDIT:
256 $vals['type'] = 'edit';
257 break;
258 case RC_NEW:
259 $vals['type'] = 'new';
260 break;
261 case RC_MOVE:
262 $vals['type'] = 'move';
263 break;
264 case RC_LOG:
265 $vals['type'] = 'log';
266 break;
267 case RC_EXTERNAL:
268 $vals['type'] = 'external';
269 break;
270 case RC_MOVE_OVER_REDIRECT:
271 $vals['type'] = 'move over redirect';
272 break;
273 default:
274 $vals['type'] = $type;
275 }
276
277 if ( $this->fld_ids ) {
278 $vals['pageid'] = intval( $row->rc_cur_id );
279 $vals['revid'] = intval( $row->rc_this_oldid );
280 $vals['old_revid'] = intval( $row->rc_last_oldid );
281 }
282
283 $title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
284
285 if ( $this->fld_title ) {
286 ApiQueryBase::addTitleInfo( $vals, $title );
287 }
288
289 if ( $this->fld_user || $this->fld_userid ) {
290
291 if ( $this->fld_userid ) {
292 $vals['userid'] = $row->rc_user;
293 // for backwards compatibility
294 $vals['user'] = $row->rc_user;
295 }
296
297 if ( $this->fld_user ) {
298 $vals['user'] = $row->rc_user_text;
299 }
300
301 if ( !$row->rc_user ) {
302 $vals['anon'] = '';
303 }
304 }
305
306 if ( $this->fld_flags ) {
307 if ( $row->rc_type == RC_NEW ) {
308 $vals['new'] = '';
309 }
310 if ( $row->rc_minor ) {
311 $vals['minor'] = '';
312 }
313 if ( $row->rc_bot ) {
314 $vals['bot'] = '';
315 }
316 }
317
318 if ( $this->fld_patrol && isset( $row->rc_patrolled ) ) {
319 $vals['patrolled'] = '';
320 }
321
322 if ( $this->fld_timestamp ) {
323 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp );
324 }
325
326 if ( $this->fld_sizes ) {
327 $vals['oldlen'] = intval( $row->rc_old_len );
328 $vals['newlen'] = intval( $row->rc_new_len );
329 }
330
331 if ( $this->fld_notificationtimestamp ) {
332 $vals['notificationtimestamp'] = ( $row->wl_notificationtimestamp == null )
333 ? ''
334 : wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
335 }
336
337 if ( $this->fld_comment && isset( $row->rc_comment ) ) {
338 $vals['comment'] = $row->rc_comment;
339 }
340
341 if ( $this->fld_parsedcomment && isset( $row->rc_comment ) ) {
342 $vals['parsedcomment'] = Linker::formatComment( $row->rc_comment, $title );
343 }
344
345 if ( $this->fld_loginfo && $row->rc_type == RC_LOG ) {
346 $vals['logid'] = intval( $row->rc_logid );
347 $vals['logtype'] = $row->rc_log_type;
348 $vals['logaction'] = $row->rc_log_action;
349 $logEntry = DatabaseLogEntry::newFromRow( (array)$row );
350 ApiQueryLogEvents::addLogParams(
351 $this->getResult(),
352 $vals,
353 $logEntry->getParameters(),
354 $logEntry->getType(),
355 $logEntry->getSubtype(),
356 $logEntry->getTimestamp()
357 );
358 }
359
360 return $vals;
361 }
362
363 /** Copied from ApiQueryRecentChanges.
364 *
365 * @param string $type
366 * @return string
367 */
368 private function parseRCType( $type ) {
369 if ( is_array( $type ) ) {
370 $retval = array();
371 foreach ( $type as $t ) {
372 $retval[] = $this->parseRCType( $t );
373 }
374
375 return $retval;
376 }
377 switch ( $type ) {
378 case 'edit':
379 return RC_EDIT;
380 case 'new':
381 return RC_NEW;
382 case 'log':
383 return RC_LOG;
384 case 'external':
385 return RC_EXTERNAL;
386 }
387 }
388
389 public function getAllowedParams() {
390 return array(
391 'allrev' => false,
392 'start' => array(
393 ApiBase::PARAM_TYPE => 'timestamp'
394 ),
395 'end' => array(
396 ApiBase::PARAM_TYPE => 'timestamp'
397 ),
398 'namespace' => array(
399 ApiBase::PARAM_ISMULTI => true,
400 ApiBase::PARAM_TYPE => 'namespace'
401 ),
402 'user' => array(
403 ApiBase::PARAM_TYPE => 'user',
404 ),
405 'excludeuser' => array(
406 ApiBase::PARAM_TYPE => 'user',
407 ),
408 'dir' => array(
409 ApiBase::PARAM_DFLT => 'older',
410 ApiBase::PARAM_TYPE => array(
411 'newer',
412 'older'
413 )
414 ),
415 'limit' => array(
416 ApiBase::PARAM_DFLT => 10,
417 ApiBase::PARAM_TYPE => 'limit',
418 ApiBase::PARAM_MIN => 1,
419 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
420 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
421 ),
422 'prop' => array(
423 ApiBase::PARAM_ISMULTI => true,
424 ApiBase::PARAM_DFLT => 'ids|title|flags',
425 ApiBase::PARAM_TYPE => array(
426 'ids',
427 'title',
428 'flags',
429 'user',
430 'userid',
431 'comment',
432 'parsedcomment',
433 'timestamp',
434 'patrol',
435 'sizes',
436 'notificationtimestamp',
437 'loginfo',
438 )
439 ),
440 'show' => array(
441 ApiBase::PARAM_ISMULTI => true,
442 ApiBase::PARAM_TYPE => array(
443 'minor',
444 '!minor',
445 'bot',
446 '!bot',
447 'anon',
448 '!anon',
449 'patrolled',
450 '!patrolled',
451 )
452 ),
453 'type' => array(
454 ApiBase::PARAM_ISMULTI => true,
455 ApiBase::PARAM_TYPE => array(
456 'edit',
457 'external',
458 'new',
459 'log',
460 )
461 ),
462 'owner' => array(
463 ApiBase::PARAM_TYPE => 'user'
464 ),
465 'token' => array(
466 ApiBase::PARAM_TYPE => 'string'
467 )
468 );
469 }
470
471 public function getParamDescription() {
472 $p = $this->getModulePrefix();
473
474 return array(
475 'allrev' => 'Include multiple revisions of the same page within given timeframe',
476 'start' => 'The timestamp to start enumerating from',
477 'end' => 'The timestamp to end enumerating',
478 'namespace' => 'Filter changes to only the given namespace(s)',
479 'user' => 'Only list changes by this user',
480 'excludeuser' => 'Don\'t list changes by this user',
481 'dir' => $this->getDirectionDescription( $p ),
482 'limit' => 'How many total results to return per request',
483 'prop' => array(
484 'Which additional items to get (non-generator mode only).',
485 ' ids - Adds revision ids and page ids',
486 ' title - Adds title of the page',
487 ' flags - Adds flags for the edit',
488 ' user - Adds the user who made the edit',
489 ' userid - Adds user id of whom made the edit',
490 ' comment - Adds comment of the edit',
491 ' parsedcomment - Adds parsed comment of the edit',
492 ' timestamp - Adds timestamp of the edit',
493 ' patrol - Tags edits that are patrolled',
494 ' sizes - Adds the old and new lengths of the page',
495 ' notificationtimestamp - Adds timestamp of when the user was last notified about the edit',
496 ' loginfo - Adds log information where appropriate',
497 ),
498 'show' => array(
499 'Show only items that meet this criteria.',
500 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
501 ),
502 'type' => array(
503 'Which types of changes to show',
504 ' edit - Regular page edits',
505 ' external - External changes',
506 ' new - Page creations',
507 ' log - Log entries',
508 ),
509 'owner' => 'The name of the user whose watchlist you\'d like to access',
510 'token' => 'Give a security token (settable in preferences) to ' .
511 'allow access to another user\'s watchlist'
512 );
513 }
514
515 public function getResultProperties() {
516 global $wgLogTypes;
517
518 return array(
519 '' => array(
520 'type' => array(
521 ApiBase::PROP_TYPE => array(
522 'edit',
523 'new',
524 'move',
525 'log',
526 'move over redirect'
527 )
528 )
529 ),
530 'ids' => array(
531 'pageid' => 'integer',
532 'revid' => 'integer',
533 'old_revid' => 'integer'
534 ),
535 'title' => array(
536 'ns' => 'namespace',
537 'title' => 'string'
538 ),
539 'user' => array(
540 'user' => 'string',
541 'anon' => 'boolean'
542 ),
543 'userid' => array(
544 'userid' => 'integer',
545 'anon' => 'boolean'
546 ),
547 'flags' => array(
548 'new' => 'boolean',
549 'minor' => 'boolean',
550 'bot' => 'boolean'
551 ),
552 'patrol' => array(
553 'patrolled' => 'boolean'
554 ),
555 'timestamp' => array(
556 'timestamp' => 'timestamp'
557 ),
558 'sizes' => array(
559 'oldlen' => 'integer',
560 'newlen' => 'integer'
561 ),
562 'notificationtimestamp' => array(
563 'notificationtimestamp' => array(
564 ApiBase::PROP_TYPE => 'timestamp',
565 ApiBase::PROP_NULLABLE => true
566 )
567 ),
568 'comment' => array(
569 'comment' => array(
570 ApiBase::PROP_TYPE => 'string',
571 ApiBase::PROP_NULLABLE => true
572 )
573 ),
574 'parsedcomment' => array(
575 'parsedcomment' => array(
576 ApiBase::PROP_TYPE => 'string',
577 ApiBase::PROP_NULLABLE => true
578 )
579 ),
580 'loginfo' => array(
581 'logid' => array(
582 ApiBase::PROP_TYPE => 'integer',
583 ApiBase::PROP_NULLABLE => true
584 ),
585 'logtype' => array(
586 ApiBase::PROP_TYPE => $wgLogTypes,
587 ApiBase::PROP_NULLABLE => true
588 ),
589 'logaction' => array(
590 ApiBase::PROP_TYPE => 'string',
591 ApiBase::PROP_NULLABLE => true
592 )
593 )
594 );
595 }
596
597 public function getDescription() {
598 return "Get all recent changes to pages in the logged in user's watchlist";
599 }
600
601 public function getPossibleErrors() {
602 return array_merge( parent::getPossibleErrors(), array(
603 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
604 array(
605 'code' => 'bad_wltoken',
606 'info' => 'Incorrect watchlist token provided -- ' .
607 'please set a correct token in Special:Preferences'
608 ),
609 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
610 array( 'code' => 'patrol', 'info' => 'patrol property is not available' ),
611 array( 'show' ),
612 array(
613 'code' => 'permissiondenied',
614 'info' => 'You need the patrol right to request the patrolled flag'
615 ),
616 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
617 ) );
618 }
619
620 public function getExamples() {
621 return array(
622 'api.php?action=query&list=watchlist',
623 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
624 'api.php?action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment',
625 'api.php?action=query&generator=watchlist&prop=info',
626 'api.php?action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user',
627 'api.php?action=query&list=watchlist&wlowner=Bob_Smith&wltoken=123ABC'
628 );
629 }
630
631 public function getHelpUrls() {
632 return 'https://www.mediawiki.org/wiki/API:Watchlist';
633 }
634 }