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