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