Merge "Typo fix"
[mediawiki.git] / includes / api / ApiQueryLogEvents.php
blob1a2719edc72cf50899ac7cff75b887ee064373ac
1 <?php
2 /**
5 * Created on Oct 16, 2006
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
24 * @file
27 /**
28 * Query action to List the log events, with optional filtering by various parameters.
30 * @ingroup API
32 class ApiQueryLogEvents extends ApiQueryBase {
34 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'le' );
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;
43 public function execute() {
44 $params = $this->extractRequestParams();
45 $db = $this->getDB();
47 $prop = array_flip( $params['prop'] );
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'] );
61 $hideLogs = LogEventsList::getExcludeClause( $db, 'user', $this->getUser() );
62 if ( $hideLogs !== false ) {
63 $this->addWhere( $hideLogs );
66 // Order is significant here
67 $this->addTables( array( 'logging', 'user', 'page' ) );
68 $this->addOption( 'STRAIGHT_JOIN' );
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 $index = array( 'logging' => 'times' ); // default, may change
77 $this->addFields( array(
78 'log_type',
79 'log_action',
80 'log_timestamp',
81 'log_deleted',
82 ) );
84 $this->addFieldsIf( array( 'log_id', 'page_id' ), $this->fld_ids );
85 $this->addFieldsIf( array( 'log_user', 'log_user_text', 'user_name' ), $this->fld_user );
86 $this->addFieldsIf( 'log_user', $this->fld_userid );
87 $this->addFieldsIf( array( 'log_namespace', 'log_title' ), $this->fld_title || $this->fld_parsedcomment );
88 $this->addFieldsIf( 'log_comment', $this->fld_comment || $this->fld_parsedcomment );
89 $this->addFieldsIf( 'log_params', $this->fld_details );
91 if ( $this->fld_tags ) {
92 $this->addTables( 'tag_summary' );
93 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', 'log_id=ts_log_id' ) ) );
94 $this->addFields( 'ts_tags' );
97 if ( !is_null( $params['tag'] ) ) {
98 $this->addTables( 'change_tag' );
99 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'log_id=ct_log_id' ) ) ) );
100 $this->addWhereFld( 'ct_tag', $params['tag'] );
101 $index['change_tag'] = 'change_tag_tag_id';
104 if ( !is_null( $params['action'] ) ) {
105 list( $type, $action ) = explode( '/', $params['action'] );
106 $this->addWhereFld( 'log_type', $type );
107 $this->addWhereFld( 'log_action', $action );
108 } elseif ( !is_null( $params['type'] ) ) {
109 $this->addWhereFld( 'log_type', $params['type'] );
110 $index['logging'] = 'type_time';
113 $this->addTimestampWhereRange( 'log_timestamp', $params['dir'], $params['start'], $params['end'] );
115 $limit = $params['limit'];
116 $this->addOption( 'LIMIT', $limit + 1 );
118 $user = $params['user'];
119 if ( !is_null( $user ) ) {
120 $userid = User::idFromName( $user );
121 if ( !$userid ) {
122 $this->dieUsage( "User name $user not found", 'param_user' );
124 $this->addWhereFld( 'log_user', $userid );
125 $index['logging'] = 'user_time';
128 $title = $params['title'];
129 if ( !is_null( $title ) ) {
130 $titleObj = Title::newFromText( $title );
131 if ( is_null( $titleObj ) ) {
132 $this->dieUsage( "Bad title value '$title'", 'param_title' );
134 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
135 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
137 // Use the title index in preference to the user index if there is a conflict
138 $index['logging'] = is_null( $user ) ? 'page_time' : array( 'page_time', 'user_time' );
141 $prefix = $params['prefix'];
143 if ( !is_null( $prefix ) ) {
144 global $wgMiserMode;
145 if ( $wgMiserMode ) {
146 $this->dieUsage( 'Prefix search disabled in Miser Mode', 'prefixsearchdisabled' );
149 $title = Title::newFromText( $prefix );
150 if ( is_null( $title ) ) {
151 $this->dieUsage( "Bad title value '$prefix'", 'param_prefix' );
153 $this->addWhereFld( 'log_namespace', $title->getNamespace() );
154 $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
157 $this->addOption( 'USE INDEX', $index );
159 // Paranoia: avoid brute force searches (bug 17342)
160 if ( !is_null( $title ) ) {
161 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0' );
163 if ( !is_null( $user ) ) {
164 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0' );
167 $count = 0;
168 $res = $this->select( __METHOD__ );
169 $result = $this->getResult();
170 foreach ( $res as $row ) {
171 if ( ++ $count > $limit ) {
172 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
173 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) );
174 break;
177 $vals = $this->extractRowInfo( $row );
178 if ( !$vals ) {
179 continue;
181 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
182 if ( !$fit ) {
183 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) );
184 break;
187 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
191 * @param $result ApiResult
192 * @param $vals array
193 * @param $params string
194 * @param $type string
195 * @param $action string
196 * @param $ts
197 * @param $legacy bool
198 * @return array
200 public static function addLogParams( $result, &$vals, $params, $type, $action, $ts, $legacy = false ) {
201 switch ( $type ) {
202 case 'move':
203 if ( $legacy ) {
204 $targetKey = 0;
205 $noredirKey = 1;
206 } else {
207 $targetKey = '4::target';
208 $noredirKey = '5::noredir';
211 if ( isset( $params[$targetKey] ) ) {
212 $title = Title::newFromText( $params[$targetKey] );
213 if ( $title ) {
214 $vals2 = array();
215 ApiQueryBase::addTitleInfo( $vals2, $title, 'new_' );
216 $vals[$type] = $vals2;
219 if ( isset( $params[$noredirKey] ) && $params[$noredirKey] ) {
220 $vals[$type]['suppressedredirect'] = '';
222 $params = null;
223 break;
224 case 'patrol':
225 if ( $legacy ) {
226 $cur = 0;
227 $prev = 1;
228 $auto = 2;
229 } else {
230 $cur = '4::curid';
231 $prev = '5::previd';
232 $auto = '6::auto';
234 $vals2 = array();
235 $vals2['cur'] = $params[$cur];
236 $vals2['prev'] = $params[$prev];
237 $vals2['auto'] = $params[$auto];
238 $vals[$type] = $vals2;
239 $params = null;
240 break;
241 case 'rights':
242 $vals2 = array();
243 if ( $legacy ) {
244 list( $vals2['old'], $vals2['new'] ) = $params;
245 } else {
246 $vals2['new'] = implode( ', ', $params['5::newgroups'] );
247 $vals2['old'] = implode( ', ', $params['4::oldgroups'] );
249 $vals[$type] = $vals2;
250 $params = null;
251 break;
252 case 'block':
253 if ( $action == 'unblock' ) {
254 break;
256 $vals2 = array();
257 list( $vals2['duration'], $vals2['flags'] ) = $params;
259 // Indefinite blocks have no expiry time
260 if ( SpecialBlock::parseExpiryInput( $params[0] ) !== wfGetDB( DB_SLAVE )->getInfinity() ) {
261 $vals2['expiry'] = wfTimestamp( TS_ISO_8601,
262 strtotime( $params[0], wfTimestamp( TS_UNIX, $ts ) ) );
264 $vals[$type] = $vals2;
265 $params = null;
266 break;
267 case 'upload':
268 if ( isset( $params['img_timestamp'] ) ) {
269 $params['img_timestamp'] = wfTimestamp( TS_ISO_8601, $params['img_timestamp'] );
271 break;
273 if ( !is_null( $params ) ) {
274 $logParams = array();
275 // Keys like "4::paramname" can't be used for output so we change them to "paramname"
276 foreach ( $params as $key => $value ) {
277 if ( strpos( $key, ':' ) === false ) {
278 $logParams[$key] = $value;
279 continue;
281 $logParam = explode( ':', $key, 3 );
282 $logParams[$logParam[2]] = $value;
284 $result->setIndexedTagName( $logParams, 'param' );
285 $result->setIndexedTagName_recursive( $logParams, 'param' );
286 $vals = array_merge( $vals, $logParams );
288 return $vals;
291 private function extractRowInfo( $row ) {
292 $logEntry = DatabaseLogEntry::newFromRow( $row );
293 $vals = array();
295 if ( $this->fld_ids ) {
296 $vals['logid'] = intval( $row->log_id );
297 $vals['pageid'] = intval( $row->page_id );
300 if ( $this->fld_title || $this->fld_parsedcomment ) {
301 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
304 if ( $this->fld_title ) {
305 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
306 $vals['actionhidden'] = '';
307 } else {
308 ApiQueryBase::addTitleInfo( $vals, $title );
312 if ( $this->fld_type || $this->fld_action ) {
313 $vals['type'] = $row->log_type;
314 $vals['action'] = $row->log_action;
317 if ( $this->fld_details && $row->log_params !== '' ) {
318 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
319 $vals['actionhidden'] = '';
320 } else {
321 self::addLogParams(
322 $this->getResult(),
323 $vals,
324 $logEntry->getParameters(),
325 $logEntry->getType(),
326 $logEntry->getSubtype(),
327 $logEntry->getTimestamp(),
328 $logEntry->isLegacy()
333 if ( $this->fld_user || $this->fld_userid ) {
334 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
335 $vals['userhidden'] = '';
336 } else {
337 if ( $this->fld_user ) {
338 $vals['user'] = $row->user_name === null ? $row->log_user_text : $row->user_name;
340 if ( $this->fld_userid ) {
341 $vals['userid'] = $row->log_user;
344 if ( !$row->log_user ) {
345 $vals['anon'] = '';
349 if ( $this->fld_timestamp ) {
350 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->log_timestamp );
353 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->log_comment ) ) {
354 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
355 $vals['commenthidden'] = '';
356 } else {
357 if ( $this->fld_comment ) {
358 $vals['comment'] = $row->log_comment;
361 if ( $this->fld_parsedcomment ) {
362 $vals['parsedcomment'] = Linker::formatComment( $row->log_comment, $title );
367 if ( $this->fld_tags ) {
368 if ( $row->ts_tags ) {
369 $tags = explode( ',', $row->ts_tags );
370 $this->getResult()->setIndexedTagName( $tags, 'tag' );
371 $vals['tags'] = $tags;
372 } else {
373 $vals['tags'] = array();
377 return $vals;
380 public function getCacheMode( $params ) {
381 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
382 // formatComment() calls wfMessage() among other things
383 return 'anon-public-user-private';
384 } elseif ( LogEventsList::getExcludeClause( $this->getDB(), 'user', $this->getUser() )
385 === LogEventsList::getExcludeClause( $this->getDB(), 'public' )
386 ) { // Output can only contain public data.
387 return 'public';
388 } else {
389 return 'anon-public-user-private';
393 public function getAllowedParams() {
394 global $wgLogTypes, $wgLogActions, $wgLogActionsHandlers;
395 return array(
396 'prop' => array(
397 ApiBase::PARAM_ISMULTI => true,
398 ApiBase::PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
399 ApiBase::PARAM_TYPE => array(
400 'ids',
401 'title',
402 'type',
403 'user',
404 'userid',
405 'timestamp',
406 'comment',
407 'parsedcomment',
408 'details',
409 'tags'
412 'type' => array(
413 ApiBase::PARAM_TYPE => $wgLogTypes
415 'action' => array(
416 ApiBase::PARAM_TYPE => array_keys( array_merge( $wgLogActions, $wgLogActionsHandlers ) )
418 'start' => array(
419 ApiBase::PARAM_TYPE => 'timestamp'
421 'end' => array(
422 ApiBase::PARAM_TYPE => 'timestamp'
424 'dir' => array(
425 ApiBase::PARAM_DFLT => 'older',
426 ApiBase::PARAM_TYPE => array(
427 'newer',
428 'older'
431 'user' => null,
432 'title' => null,
433 'prefix' => null,
434 'tag' => null,
435 'limit' => array(
436 ApiBase::PARAM_DFLT => 10,
437 ApiBase::PARAM_TYPE => 'limit',
438 ApiBase::PARAM_MIN => 1,
439 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
440 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
445 public function getParamDescription() {
446 $p = $this->getModulePrefix();
447 return array(
448 'prop' => array(
449 'Which properties to get',
450 ' ids - Adds the ID of the log event',
451 ' title - Adds the title of the page for the log event',
452 ' type - Adds the type of log event',
453 ' user - Adds the user responsible for the log event',
454 ' userid - Adds the user ID who was responsible for the log event',
455 ' timestamp - Adds the timestamp for the event',
456 ' comment - Adds the comment of the event',
457 ' parsedcomment - Adds the parsed comment of the event',
458 ' details - Lists additional details about the event',
459 ' tags - Lists tags for the event',
461 'type' => 'Filter log entries to only this type',
462 'action' => "Filter log actions to only this type. Overrides {$p}type",
463 'start' => 'The timestamp to start enumerating from',
464 'end' => 'The timestamp to end enumerating',
465 'dir' => $this->getDirectionDescription( $p ),
466 'user' => 'Filter entries to those made by the given user',
467 'title' => 'Filter entries to those related to a page',
468 'prefix' => 'Filter entries that start with this prefix. Disabled in Miser Mode',
469 'limit' => 'How many total event entries to return',
470 'tag' => 'Only list event entries tagged with this tag',
474 public function getResultProperties() {
475 global $wgLogTypes;
476 return array(
477 'ids' => array(
478 'logid' => 'integer',
479 'pageid' => 'integer'
481 'title' => array(
482 'ns' => 'namespace',
483 'title' => 'string'
485 'type' => array(
486 'type' => array(
487 ApiBase::PROP_TYPE => $wgLogTypes
489 'action' => 'string'
491 'details' => array(
492 'actionhidden' => 'boolean'
494 'user' => array(
495 'userhidden' => 'boolean',
496 'user' => array(
497 ApiBase::PROP_TYPE => 'string',
498 ApiBase::PROP_NULLABLE => true
500 'anon' => 'boolean'
502 'userid' => array(
503 'userhidden' => 'boolean',
504 'userid' => array(
505 ApiBase::PROP_TYPE => 'integer',
506 ApiBase::PROP_NULLABLE => true
508 'anon' => 'boolean'
510 'timestamp' => array(
511 'timestamp' => 'timestamp'
513 'comment' => array(
514 'commenthidden' => 'boolean',
515 'comment' => array(
516 ApiBase::PROP_TYPE => 'string',
517 ApiBase::PROP_NULLABLE => true
520 'parsedcomment' => array(
521 'commenthidden' => 'boolean',
522 'parsedcomment' => array(
523 ApiBase::PROP_TYPE => 'string',
524 ApiBase::PROP_NULLABLE => true
530 public function getDescription() {
531 return 'Get events from logs';
534 public function getPossibleErrors() {
535 return array_merge( parent::getPossibleErrors(), array(
536 array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
537 array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
538 array( 'code' => 'param_prefix', 'info' => 'Bad title value \'prefix\'' ),
539 array( 'code' => 'prefixsearchdisabled', 'info' => 'Prefix search disabled in Miser Mode' ),
540 ) );
543 public function getExamples() {
544 return array(
545 'api.php?action=query&list=logevents'
549 public function getHelpUrls() {
550 return 'https://www.mediawiki.org/wiki/API:Logevents';