Merge "Add a maintenance script to list variants and languages with then"
[lhc/web/wiklou.git] / includes / api / ApiQueryLogEvents.php
1 <?php
2 /**
3 *
4 *
5 * Created on Oct 16, 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 * Query action to List the log events, with optional filtering by various parameters.
29 *
30 * @ingroup API
31 */
32 class ApiQueryLogEvents extends ApiQueryBase {
33
34 public function __construct( ApiQuery $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'le' );
36 }
37
38 private $fld_ids = false, $fld_title = false, $fld_type = false,
39 $fld_action = false, $fld_user = false, $fld_userid = false,
40 $fld_timestamp = false, $fld_comment = false, $fld_parsedcomment = false,
41 $fld_details = false, $fld_tags = false;
42
43 public function execute() {
44 $params = $this->extractRequestParams();
45 $db = $this->getDB();
46 $this->requireMaxOneParameter( $params, 'title', 'prefix', 'namespace' );
47
48 $prop = array_flip( $params['prop'] );
49
50 $this->fld_ids = isset( $prop['ids'] );
51 $this->fld_title = isset( $prop['title'] );
52 $this->fld_type = isset( $prop['type'] );
53 $this->fld_action = isset( $prop['action'] );
54 $this->fld_user = isset( $prop['user'] );
55 $this->fld_userid = isset( $prop['userid'] );
56 $this->fld_timestamp = isset( $prop['timestamp'] );
57 $this->fld_comment = isset( $prop['comment'] );
58 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
59 $this->fld_details = isset( $prop['details'] );
60 $this->fld_tags = isset( $prop['tags'] );
61
62 $hideLogs = LogEventsList::getExcludeClause( $db, 'user', $this->getUser() );
63 if ( $hideLogs !== false ) {
64 $this->addWhere( $hideLogs );
65 }
66
67 // Order is significant here
68 $this->addTables( array( 'logging', 'user', 'page' ) );
69 $this->addJoinConds( array(
70 'user' => array( 'LEFT JOIN',
71 'user_id=log_user' ),
72 'page' => array( 'LEFT JOIN',
73 array( 'log_namespace=page_namespace',
74 'log_title=page_title' ) ) ) );
75
76 $this->addFields( array(
77 'log_id',
78 'log_type',
79 'log_action',
80 'log_timestamp',
81 'log_deleted',
82 ) );
83
84 $this->addFieldsIf( 'page_id', $this->fld_ids );
85 // log_page is the page_id saved at log time, whereas page_id is from a
86 // join at query time. This leads to different results in various
87 // scenarios, e.g. deletion, recreation.
88 $this->addFieldsIf( 'log_page', $this->fld_ids );
89 $this->addFieldsIf( array( 'log_user', 'log_user_text', 'user_name' ), $this->fld_user );
90 $this->addFieldsIf( 'log_user', $this->fld_userid );
91 $this->addFieldsIf(
92 array( 'log_namespace', 'log_title' ),
93 $this->fld_title || $this->fld_parsedcomment
94 );
95 $this->addFieldsIf( 'log_comment', $this->fld_comment || $this->fld_parsedcomment );
96 $this->addFieldsIf( 'log_params', $this->fld_details );
97
98 if ( $this->fld_tags ) {
99 $this->addTables( 'tag_summary' );
100 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', 'log_id=ts_log_id' ) ) );
101 $this->addFields( 'ts_tags' );
102 }
103
104 if ( !is_null( $params['tag'] ) ) {
105 $this->addTables( 'change_tag' );
106 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN',
107 array( 'log_id=ct_log_id' ) ) ) );
108 $this->addWhereFld( 'ct_tag', $params['tag'] );
109 }
110
111 if ( !is_null( $params['action'] ) ) {
112 // Do validation of action param, list of allowed actions can contains wildcards
113 // Allow the param, when the actions is in the list or a wildcard version is listed.
114 $logAction = $params['action'];
115 if ( strpos( $logAction, '/' ) === false ) {
116 // all items in the list have a slash
117 $valid = false;
118 } else {
119 $logActions = array_flip( $this->getAllowedLogActions() );
120 list( $type, $action ) = explode( '/', $logAction, 2 );
121 $valid = isset( $logActions[$logAction] ) || isset( $logActions[$type . '/*'] );
122 }
123
124 if ( !$valid ) {
125 $valueName = $this->encodeParamName( 'action' );
126 $this->dieUsage(
127 "Unrecognized value for parameter '$valueName': {$logAction}",
128 "unknown_$valueName"
129 );
130 }
131
132 $this->addWhereFld( 'log_type', $type );
133 $this->addWhereFld( 'log_action', $action );
134 } elseif ( !is_null( $params['type'] ) ) {
135 $this->addWhereFld( 'log_type', $params['type'] );
136 }
137
138 $this->addTimestampWhereRange(
139 'log_timestamp',
140 $params['dir'],
141 $params['start'],
142 $params['end']
143 );
144 // Include in ORDER BY for uniqueness
145 $this->addWhereRange( 'log_id', $params['dir'], null, null );
146
147 if ( !is_null( $params['continue'] ) ) {
148 $cont = explode( '|', $params['continue'] );
149 $this->dieContinueUsageIf( count( $cont ) != 2 );
150 $op = ( $params['dir'] === 'newer' ? '>' : '<' );
151 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
152 $continueId = (int)$cont[1];
153 $this->dieContinueUsageIf( $continueId != $cont[1] );
154 $this->addWhere( "log_timestamp $op $continueTimestamp OR " .
155 "(log_timestamp = $continueTimestamp AND " .
156 "log_id $op= $continueId)"
157 );
158 }
159
160 $limit = $params['limit'];
161 $this->addOption( 'LIMIT', $limit + 1 );
162
163 $user = $params['user'];
164 if ( !is_null( $user ) ) {
165 $userid = User::idFromName( $user );
166 if ( $userid ) {
167 $this->addWhereFld( 'log_user', $userid );
168 } else {
169 $this->addWhereFld( 'log_user_text', IP::sanitizeIP( $user ) );
170 }
171 }
172
173 $title = $params['title'];
174 if ( !is_null( $title ) ) {
175 $titleObj = Title::newFromText( $title );
176 if ( is_null( $titleObj ) ) {
177 $this->dieUsage( "Bad title value '$title'", 'param_title' );
178 }
179 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
180 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
181 }
182
183 if ( $params['namespace'] !== null ) {
184 $this->addWhereFld( 'log_namespace', $params['namespace'] );
185 }
186
187 $prefix = $params['prefix'];
188
189 if ( !is_null( $prefix ) ) {
190 global $wgMiserMode;
191 if ( $wgMiserMode ) {
192 $this->dieUsage( 'Prefix search disabled in Miser Mode', 'prefixsearchdisabled' );
193 }
194
195 $title = Title::newFromText( $prefix );
196 if ( is_null( $title ) ) {
197 $this->dieUsage( "Bad title value '$prefix'", 'param_prefix' );
198 }
199 $this->addWhereFld( 'log_namespace', $title->getNamespace() );
200 $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
201 }
202
203 // Paranoia: avoid brute force searches (bug 17342)
204 if ( $params['namespace'] !== null || !is_null( $title ) || !is_null( $user ) ) {
205 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
206 $titleBits = LogPage::DELETED_ACTION;
207 $userBits = LogPage::DELETED_USER;
208 } elseif ( !$this->getUser()->isAllowed( 'suppressrevision' ) ) {
209 $titleBits = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
210 $userBits = LogPage::DELETED_USER | LogPage::DELETED_RESTRICTED;
211 } else {
212 $titleBits = 0;
213 $userBits = 0;
214 }
215 if ( ( $params['namespace'] !== null || !is_null( $title ) ) && $titleBits ) {
216 $this->addWhere( $db->bitAnd( 'log_deleted', $titleBits ) . " != $titleBits" );
217 }
218 if ( !is_null( $user ) && $userBits ) {
219 $this->addWhere( $db->bitAnd( 'log_deleted', $userBits ) . " != $userBits" );
220 }
221 }
222
223 $count = 0;
224 $res = $this->select( __METHOD__ );
225 $result = $this->getResult();
226 foreach ( $res as $row ) {
227 if ( ++$count > $limit ) {
228 // We've reached the one extra which shows that there are
229 // additional pages to be had. Stop here...
230 $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" );
231 break;
232 }
233
234 $vals = $this->extractRowInfo( $row );
235 if ( !$vals ) {
236 continue;
237 }
238 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
239 if ( !$fit ) {
240 $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" );
241 break;
242 }
243 }
244 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
245 }
246
247 /**
248 * @param ApiResult $result
249 * @param array $vals
250 * @param string $params
251 * @param string $type
252 * @param string $action
253 * @param string $ts
254 * @param bool $legacy
255 * @return array
256 */
257 public static function addLogParams( $result, &$vals, $params, $type,
258 $action, $ts, $legacy = false
259 ) {
260 switch ( $type ) {
261 case 'move':
262 if ( $legacy ) {
263 $targetKey = 0;
264 $noredirKey = 1;
265 } else {
266 $targetKey = '4::target';
267 $noredirKey = '5::noredir';
268 }
269
270 if ( isset( $params[$targetKey] ) ) {
271 $title = Title::newFromText( $params[$targetKey] );
272 if ( $title ) {
273 $vals2 = array();
274 ApiQueryBase::addTitleInfo( $vals2, $title, 'new_' );
275 $vals[$type] = $vals2;
276 }
277 }
278 if ( isset( $params[$noredirKey] ) && $params[$noredirKey] ) {
279 $vals[$type]['suppressedredirect'] = '';
280 }
281 $params = null;
282 break;
283 case 'patrol':
284 if ( $legacy ) {
285 $cur = 0;
286 $prev = 1;
287 $auto = 2;
288 } else {
289 $cur = '4::curid';
290 $prev = '5::previd';
291 $auto = '6::auto';
292 }
293 $vals2 = array();
294 $vals2['cur'] = $params[$cur];
295 $vals2['prev'] = $params[$prev];
296 $vals2['auto'] = $params[$auto];
297 $vals[$type] = $vals2;
298 $params = null;
299 break;
300 case 'rights':
301 $vals2 = array();
302 if ( $legacy ) {
303 list( $vals2['old'], $vals2['new'] ) = $params;
304 } else {
305 $vals2['new'] = implode( ', ', $params['5::newgroups'] );
306 $vals2['old'] = implode( ', ', $params['4::oldgroups'] );
307 }
308 $vals[$type] = $vals2;
309 $params = null;
310 break;
311 case 'block':
312 if ( $action == 'unblock' ) {
313 break;
314 }
315 $vals2 = array();
316 list( $vals2['duration'], $vals2['flags'] ) = $params;
317
318 // Indefinite blocks have no expiry time
319 if ( SpecialBlock::parseExpiryInput( $params[0] ) !== wfGetDB( DB_SLAVE )->getInfinity() ) {
320 $vals2['expiry'] = wfTimestamp( TS_ISO_8601,
321 strtotime( $params[0], wfTimestamp( TS_UNIX, $ts ) ) );
322 }
323 $vals[$type] = $vals2;
324 $params = null;
325 break;
326 case 'upload':
327 if ( isset( $params['img_timestamp'] ) ) {
328 $params['img_timestamp'] = wfTimestamp( TS_ISO_8601, $params['img_timestamp'] );
329 }
330 break;
331 }
332 if ( !is_null( $params ) ) {
333 $logParams = array();
334 // Keys like "4::paramname" can't be used for output so we change them to "paramname"
335 foreach ( $params as $key => $value ) {
336 if ( strpos( $key, ':' ) === false ) {
337 $logParams[$key] = $value;
338 continue;
339 }
340 $logParam = explode( ':', $key, 3 );
341 $logParams[$logParam[2]] = $value;
342 }
343 $result->setIndexedTagName( $logParams, 'param' );
344 $result->setIndexedTagName_recursive( $logParams, 'param' );
345 $vals = array_merge( $vals, $logParams );
346 }
347
348 return $vals;
349 }
350
351 private function extractRowInfo( $row ) {
352 $logEntry = DatabaseLogEntry::newFromRow( $row );
353 $vals = array();
354 $anyHidden = false;
355 $user = $this->getUser();
356
357 if ( $this->fld_ids ) {
358 $vals['logid'] = intval( $row->log_id );
359 }
360
361 if ( $this->fld_title || $this->fld_parsedcomment ) {
362 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
363 }
364
365 if ( $this->fld_title || $this->fld_ids || $this->fld_details && $row->log_params !== '' ) {
366 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
367 $vals['actionhidden'] = '';
368 $anyHidden = true;
369 }
370 if ( LogEventsList::userCan( $row, LogPage::DELETED_ACTION, $user ) ) {
371 if ( $this->fld_title ) {
372 ApiQueryBase::addTitleInfo( $vals, $title );
373 }
374 if ( $this->fld_ids ) {
375 $vals['pageid'] = intval( $row->page_id );
376 $vals['logpage'] = intval( $row->log_page );
377 }
378 if ( $this->fld_details && $row->log_params !== '' ) {
379 self::addLogParams(
380 $this->getResult(),
381 $vals,
382 $logEntry->getParameters(),
383 $logEntry->getType(),
384 $logEntry->getSubtype(),
385 $logEntry->getTimestamp(),
386 $logEntry->isLegacy()
387 );
388 }
389 }
390 }
391
392 if ( $this->fld_type || $this->fld_action ) {
393 $vals['type'] = $row->log_type;
394 $vals['action'] = $row->log_action;
395 }
396
397 if ( $this->fld_user || $this->fld_userid ) {
398 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
399 $vals['userhidden'] = '';
400 $anyHidden = true;
401 }
402 if ( LogEventsList::userCan( $row, LogPage::DELETED_USER, $user ) ) {
403 if ( $this->fld_user ) {
404 $vals['user'] = $row->user_name === null ? $row->log_user_text : $row->user_name;
405 }
406 if ( $this->fld_userid ) {
407 $vals['userid'] = $row->log_user;
408 }
409
410 if ( !$row->log_user ) {
411 $vals['anon'] = '';
412 }
413 }
414 }
415 if ( $this->fld_timestamp ) {
416 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->log_timestamp );
417 }
418
419 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->log_comment ) ) {
420 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
421 $vals['commenthidden'] = '';
422 $anyHidden = true;
423 }
424 if ( LogEventsList::userCan( $row, LogPage::DELETED_COMMENT, $user ) ) {
425 if ( $this->fld_comment ) {
426 $vals['comment'] = $row->log_comment;
427 }
428
429 if ( $this->fld_parsedcomment ) {
430 $vals['parsedcomment'] = Linker::formatComment( $row->log_comment, $title );
431 }
432 }
433 }
434
435 if ( $this->fld_tags ) {
436 if ( $row->ts_tags ) {
437 $tags = explode( ',', $row->ts_tags );
438 $this->getResult()->setIndexedTagName( $tags, 'tag' );
439 $vals['tags'] = $tags;
440 } else {
441 $vals['tags'] = array();
442 }
443 }
444
445 if ( $anyHidden && LogEventsList::isDeleted( $row, LogPage::DELETED_RESTRICTED ) ) {
446 $vals['suppressed'] = '';
447 }
448
449 return $vals;
450 }
451
452 private function getAllowedLogActions() {
453 global $wgLogActions, $wgLogActionsHandlers;
454
455 return array_keys( array_merge( $wgLogActions, $wgLogActionsHandlers ) );
456 }
457
458 public function getCacheMode( $params ) {
459 if ( $this->userCanSeeRevDel() ) {
460 return 'private';
461 }
462 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
463 // formatComment() calls wfMessage() among other things
464 return 'anon-public-user-private';
465 } elseif ( LogEventsList::getExcludeClause( $this->getDB(), 'user', $this->getUser() )
466 === LogEventsList::getExcludeClause( $this->getDB(), 'public' )
467 ) { // Output can only contain public data.
468 return 'public';
469 } else {
470 return 'anon-public-user-private';
471 }
472 }
473
474 public function getAllowedParams( $flags = 0 ) {
475 global $wgLogTypes;
476
477 return array(
478 'prop' => array(
479 ApiBase::PARAM_ISMULTI => true,
480 ApiBase::PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
481 ApiBase::PARAM_TYPE => array(
482 'ids',
483 'title',
484 'type',
485 'user',
486 'userid',
487 'timestamp',
488 'comment',
489 'parsedcomment',
490 'details',
491 'tags'
492 )
493 ),
494 'type' => array(
495 ApiBase::PARAM_TYPE => $wgLogTypes
496 ),
497 'action' => array(
498 // validation on request is done in execute()
499 ApiBase::PARAM_TYPE => ( $flags & ApiBase::GET_VALUES_FOR_HELP )
500 ? $this->getAllowedLogActions()
501 : null
502 ),
503 'start' => array(
504 ApiBase::PARAM_TYPE => 'timestamp'
505 ),
506 'end' => array(
507 ApiBase::PARAM_TYPE => 'timestamp'
508 ),
509 'dir' => array(
510 ApiBase::PARAM_DFLT => 'older',
511 ApiBase::PARAM_TYPE => array(
512 'newer',
513 'older'
514 )
515 ),
516 'user' => null,
517 'title' => null,
518 'namespace' => array(
519 ApiBase::PARAM_TYPE => 'namespace'
520 ),
521 'prefix' => null,
522 'tag' => null,
523 'limit' => array(
524 ApiBase::PARAM_DFLT => 10,
525 ApiBase::PARAM_TYPE => 'limit',
526 ApiBase::PARAM_MIN => 1,
527 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
528 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
529 ),
530 'continue' => null,
531 );
532 }
533
534 public function getParamDescription() {
535 $p = $this->getModulePrefix();
536
537 return array(
538 'prop' => array(
539 'Which properties to get',
540 ' ids - Adds the ID of the log event',
541 ' title - Adds the title of the page for the log event',
542 ' type - Adds the type of log event',
543 ' user - Adds the user responsible for the log event',
544 ' userid - Adds the user ID who was responsible for the log event',
545 ' timestamp - Adds the timestamp for the event',
546 ' comment - Adds the comment of the event',
547 ' parsedcomment - Adds the parsed comment of the event',
548 ' details - Lists additional details about the event',
549 ' tags - Lists tags for the event',
550 ),
551 'type' => 'Filter log entries to only this type',
552 'action' => array(
553 "Filter log actions to only this action. Overrides {$p}type",
554 "Wildcard actions like 'action/*' allows to specify any string for the asterisk"
555 ),
556 'start' => 'The timestamp to start enumerating from',
557 'end' => 'The timestamp to end enumerating',
558 'dir' => $this->getDirectionDescription( $p ),
559 'user' => 'Filter entries to those made by the given user',
560 'title' => 'Filter entries to those related to a page',
561 'namespace' => 'Filter entries to those in the given namespace',
562 'prefix' => 'Filter entries that start with this prefix. Disabled in Miser Mode',
563 'limit' => 'How many total event entries to return',
564 'tag' => 'Only list event entries tagged with this tag',
565 'continue' => 'When more results are available, use this to continue',
566 );
567 }
568
569 public function getResultProperties() {
570 global $wgLogTypes;
571
572 return array(
573 'ids' => array(
574 'logid' => 'integer',
575 'pageid' => 'integer'
576 ),
577 'title' => array(
578 'ns' => 'namespace',
579 'title' => 'string'
580 ),
581 'type' => array(
582 'type' => array(
583 ApiBase::PROP_TYPE => $wgLogTypes
584 ),
585 'action' => 'string'
586 ),
587 'details' => array(
588 'actionhidden' => 'boolean'
589 ),
590 'user' => array(
591 'userhidden' => 'boolean',
592 'user' => array(
593 ApiBase::PROP_TYPE => 'string',
594 ApiBase::PROP_NULLABLE => true
595 ),
596 'anon' => 'boolean'
597 ),
598 'userid' => array(
599 'userhidden' => 'boolean',
600 'userid' => array(
601 ApiBase::PROP_TYPE => 'integer',
602 ApiBase::PROP_NULLABLE => true
603 ),
604 'anon' => 'boolean'
605 ),
606 'timestamp' => array(
607 'timestamp' => 'timestamp'
608 ),
609 'comment' => array(
610 'commenthidden' => 'boolean',
611 'comment' => array(
612 ApiBase::PROP_TYPE => 'string',
613 ApiBase::PROP_NULLABLE => true
614 )
615 ),
616 'parsedcomment' => array(
617 'commenthidden' => 'boolean',
618 'parsedcomment' => array(
619 ApiBase::PROP_TYPE => 'string',
620 ApiBase::PROP_NULLABLE => true
621 )
622 )
623 );
624 }
625
626 public function getDescription() {
627 return 'Get events from logs.';
628 }
629
630 public function getPossibleErrors() {
631 return array_merge(
632 parent::getPossibleErrors(),
633 $this->getRequireMaxOneParameterErrorMessages(
634 array( 'title', 'prefix', 'namespace' ) ),
635 array(
636 array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
637 array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
638 array( 'code' => 'param_prefix', 'info' => 'Bad title value \'prefix\'' ),
639 array( 'code' => 'prefixsearchdisabled',
640 'info' => 'Prefix search disabled in Miser Mode' ),
641 )
642 );
643 }
644
645 public function getExamples() {
646 return array(
647 'api.php?action=query&list=logevents'
648 );
649 }
650
651 public function getHelpUrls() {
652 return 'https://www.mediawiki.org/wiki/API:Logevents';
653 }
654 }