Merge "Fix to avoid IE "compatibility view""
[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
378 switch ( $type ) {
379 case 'edit':
380 return RC_EDIT;
381 case 'new':
382 return RC_NEW;
383 case 'log':
384 return RC_LOG;
385 case 'external':
386 return RC_EXTERNAL;
387 default:
388 ApiBase::dieDebug( __METHOD__, "Unknown type '$type'" );
389 }
390 }
391
392 public function getAllowedParams() {
393 return array(
394 'allrev' => false,
395 'start' => array(
396 ApiBase::PARAM_TYPE => 'timestamp'
397 ),
398 'end' => array(
399 ApiBase::PARAM_TYPE => 'timestamp'
400 ),
401 'namespace' => array(
402 ApiBase::PARAM_ISMULTI => true,
403 ApiBase::PARAM_TYPE => 'namespace'
404 ),
405 'user' => array(
406 ApiBase::PARAM_TYPE => 'user',
407 ),
408 'excludeuser' => array(
409 ApiBase::PARAM_TYPE => 'user',
410 ),
411 'dir' => array(
412 ApiBase::PARAM_DFLT => 'older',
413 ApiBase::PARAM_TYPE => array(
414 'newer',
415 'older'
416 )
417 ),
418 'limit' => array(
419 ApiBase::PARAM_DFLT => 10,
420 ApiBase::PARAM_TYPE => 'limit',
421 ApiBase::PARAM_MIN => 1,
422 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
423 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
424 ),
425 'prop' => array(
426 ApiBase::PARAM_ISMULTI => true,
427 ApiBase::PARAM_DFLT => 'ids|title|flags',
428 ApiBase::PARAM_TYPE => array(
429 'ids',
430 'title',
431 'flags',
432 'user',
433 'userid',
434 'comment',
435 'parsedcomment',
436 'timestamp',
437 'patrol',
438 'sizes',
439 'notificationtimestamp',
440 'loginfo',
441 )
442 ),
443 'show' => array(
444 ApiBase::PARAM_ISMULTI => true,
445 ApiBase::PARAM_TYPE => array(
446 'minor',
447 '!minor',
448 'bot',
449 '!bot',
450 'anon',
451 '!anon',
452 'patrolled',
453 '!patrolled',
454 )
455 ),
456 'type' => array(
457 ApiBase::PARAM_ISMULTI => true,
458 ApiBase::PARAM_TYPE => array(
459 'edit',
460 'external',
461 'new',
462 'log',
463 )
464 ),
465 'owner' => array(
466 ApiBase::PARAM_TYPE => 'user'
467 ),
468 'token' => array(
469 ApiBase::PARAM_TYPE => 'string'
470 )
471 );
472 }
473
474 public function getParamDescription() {
475 $p = $this->getModulePrefix();
476
477 return array(
478 'allrev' => 'Include multiple revisions of the same page within given timeframe',
479 'start' => 'The timestamp to start enumerating from',
480 'end' => 'The timestamp to end enumerating',
481 'namespace' => 'Filter changes to only the given namespace(s)',
482 'user' => 'Only list changes by this user',
483 'excludeuser' => 'Don\'t list changes by this user',
484 'dir' => $this->getDirectionDescription( $p ),
485 'limit' => 'How many total results to return per request',
486 'prop' => array(
487 'Which additional items to get (non-generator mode only).',
488 ' ids - Adds revision ids and page ids',
489 ' title - Adds title of the page',
490 ' flags - Adds flags for the edit',
491 ' user - Adds the user who made the edit',
492 ' userid - Adds user id of whom made the edit',
493 ' comment - Adds comment of the edit',
494 ' parsedcomment - Adds parsed comment of the edit',
495 ' timestamp - Adds timestamp of the edit',
496 ' patrol - Tags edits that are patrolled',
497 ' sizes - Adds the old and new lengths of the page',
498 ' notificationtimestamp - Adds timestamp of when the user was last notified about the edit',
499 ' loginfo - Adds log information where appropriate',
500 ),
501 'show' => array(
502 'Show only items that meet this criteria.',
503 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
504 ),
505 'type' => array(
506 'Which types of changes to show',
507 ' edit - Regular page edits',
508 ' external - External changes',
509 ' new - Page creations',
510 ' log - Log entries',
511 ),
512 'owner' => 'The name of the user whose watchlist you\'d like to access',
513 'token' => 'Give a security token (settable in preferences) to ' .
514 'allow access to another user\'s watchlist'
515 );
516 }
517
518 public function getResultProperties() {
519 global $wgLogTypes;
520
521 return array(
522 '' => array(
523 'type' => array(
524 ApiBase::PROP_TYPE => array(
525 'edit',
526 'new',
527 'move',
528 'log',
529 'move over redirect'
530 )
531 )
532 ),
533 'ids' => array(
534 'pageid' => 'integer',
535 'revid' => 'integer',
536 'old_revid' => 'integer'
537 ),
538 'title' => array(
539 'ns' => 'namespace',
540 'title' => 'string'
541 ),
542 'user' => array(
543 'user' => 'string',
544 'anon' => 'boolean'
545 ),
546 'userid' => array(
547 'userid' => 'integer',
548 'anon' => 'boolean'
549 ),
550 'flags' => array(
551 'new' => 'boolean',
552 'minor' => 'boolean',
553 'bot' => 'boolean'
554 ),
555 'patrol' => array(
556 'patrolled' => 'boolean'
557 ),
558 'timestamp' => array(
559 'timestamp' => 'timestamp'
560 ),
561 'sizes' => array(
562 'oldlen' => 'integer',
563 'newlen' => 'integer'
564 ),
565 'notificationtimestamp' => array(
566 'notificationtimestamp' => array(
567 ApiBase::PROP_TYPE => 'timestamp',
568 ApiBase::PROP_NULLABLE => true
569 )
570 ),
571 'comment' => array(
572 'comment' => array(
573 ApiBase::PROP_TYPE => 'string',
574 ApiBase::PROP_NULLABLE => true
575 )
576 ),
577 'parsedcomment' => array(
578 'parsedcomment' => array(
579 ApiBase::PROP_TYPE => 'string',
580 ApiBase::PROP_NULLABLE => true
581 )
582 ),
583 'loginfo' => array(
584 'logid' => array(
585 ApiBase::PROP_TYPE => 'integer',
586 ApiBase::PROP_NULLABLE => true
587 ),
588 'logtype' => array(
589 ApiBase::PROP_TYPE => $wgLogTypes,
590 ApiBase::PROP_NULLABLE => true
591 ),
592 'logaction' => array(
593 ApiBase::PROP_TYPE => 'string',
594 ApiBase::PROP_NULLABLE => true
595 )
596 )
597 );
598 }
599
600 public function getDescription() {
601 return "Get all recent changes to pages in the logged in user's watchlist";
602 }
603
604 public function getPossibleErrors() {
605 return array_merge( parent::getPossibleErrors(), array(
606 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
607 array(
608 'code' => 'bad_wltoken',
609 'info' => 'Incorrect watchlist token provided -- ' .
610 'please set a correct token in Special:Preferences'
611 ),
612 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
613 array( 'code' => 'patrol', 'info' => 'patrol property is not available' ),
614 array( 'show' ),
615 array(
616 'code' => 'permissiondenied',
617 'info' => 'You need the patrol right to request the patrolled flag'
618 ),
619 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
620 ) );
621 }
622
623 public function getExamples() {
624 return array(
625 'api.php?action=query&list=watchlist',
626 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
627 'api.php?action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment',
628 'api.php?action=query&generator=watchlist&prop=info',
629 'api.php?action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user',
630 'api.php?action=query&list=watchlist&wlowner=Bob_Smith&wltoken=123ABC'
631 );
632 }
633
634 public function getHelpUrls() {
635 return 'https://www.mediawiki.org/wiki/API:Watchlist';
636 }
637 }