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
28 * Query action to List the log events, with optional filtering by various parameters.
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();
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( 'JOIN',
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(
84 $this->addFieldsIf( array( 'log_id', 'page_id' ), $this->fld_ids
);
85 $this->addFieldsIf( array( 'log_user', 'user_name' ), $this->fld_user
);
86 $this->addFieldsIf( 'user_id', $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 global $wgOldChangeTagsIndex;
102 $index['change_tag'] = $wgOldChangeTagsIndex ?
'ct_tag' : 'change_tag_tag_id';
105 if ( !is_null( $params['action'] ) ) {
106 list( $type, $action ) = explode( '/', $params['action'] );
107 $this->addWhereFld( 'log_type', $type );
108 $this->addWhereFld( 'log_action', $action );
109 } elseif ( !is_null( $params['type'] ) ) {
110 $this->addWhereFld( 'log_type', $params['type'] );
111 $index['logging'] = 'type_time';
114 $this->addTimestampWhereRange( 'log_timestamp', $params['dir'], $params['start'], $params['end'] );
116 $limit = $params['limit'];
117 $this->addOption( 'LIMIT', $limit +
1 );
119 $user = $params['user'];
120 if ( !is_null( $user ) ) {
121 $userid = User
::idFromName( $user );
123 $this->dieUsage( "User name $user not found", 'param_user' );
125 $this->addWhereFld( 'log_user', $userid );
126 $index['logging'] = 'user_time';
129 $title = $params['title'];
130 if ( !is_null( $title ) ) {
131 $titleObj = Title
::newFromText( $title );
132 if ( is_null( $titleObj ) ) {
133 $this->dieUsage( "Bad title value '$title'", 'param_title' );
135 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
136 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
138 // Use the title index in preference to the user index if there is a conflict
139 $index['logging'] = is_null( $user ) ?
'page_time' : array( 'page_time', 'user_time' );
142 $prefix = $params['prefix'];
144 if ( !is_null( $prefix ) ) {
146 if ( $wgMiserMode ) {
147 $this->dieUsage( 'Prefix search disabled in Miser Mode', 'prefixsearchdisabled' );
150 $title = Title
::newFromText( $prefix );
151 if ( is_null( $title ) ) {
152 $this->dieUsage( "Bad title value '$prefix'", 'param_prefix' );
154 $this->addWhereFld( 'log_namespace', $title->getNamespace() );
155 $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
158 $this->addOption( 'USE INDEX', $index );
160 // Paranoia: avoid brute force searches (bug 17342)
161 if ( !is_null( $title ) ) {
162 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage
::DELETED_ACTION
) . ' = 0' );
164 if ( !is_null( $user ) ) {
165 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage
::DELETED_USER
) . ' = 0' );
169 $res = $this->select( __METHOD__
);
170 $result = $this->getResult();
171 foreach ( $res as $row ) {
172 if ( ++
$count > $limit ) {
173 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
174 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->log_timestamp
) );
178 $vals = $this->extractRowInfo( $row );
182 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
184 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->log_timestamp
) );
188 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
192 * @param $result ApiResult
194 * @param $params string
195 * @param $type string
196 * @param $action string
198 * @param $legacy bool
201 public static function addLogParams( $result, &$vals, $params, $type, $action, $ts, $legacy = false ) {
208 $targetKey = '4::target';
209 $noredirKey = '5::noredir';
212 if ( isset( $params[$targetKey] ) ) {
213 $title = Title
::newFromText( $params[$targetKey] );
216 ApiQueryBase
::addTitleInfo( $vals2, $title, 'new_' );
217 $vals[$type] = $vals2;
220 if ( isset( $params[$noredirKey] ) && $params[$noredirKey] ) {
221 $vals[$type]['suppressedredirect'] = '';
236 $vals2['cur'] = $params[$cur];
237 $vals2['prev'] = $params[$prev];
238 $vals2['auto'] = $params[$auto];
239 $vals[$type] = $vals2;
245 list( $vals2['old'], $vals2['new'] ) = $params;
247 $vals2['new'] = implode( ', ', $params['5::newgroups'] );
248 $vals2['old'] = implode( ', ', $params['4::oldgroups'] );
250 $vals[$type] = $vals2;
254 if ( $action == 'unblock' ) {
258 list( $vals2['duration'], $vals2['flags'] ) = $params;
260 // Indefinite blocks have no expiry time
261 if ( SpecialBlock
::parseExpiryInput( $params[0] ) !== wfGetDB( DB_SLAVE
)->getInfinity() ) {
262 $vals2['expiry'] = wfTimestamp( TS_ISO_8601
,
263 strtotime( $params[0], wfTimestamp( TS_UNIX
, $ts ) ) );
265 $vals[$type] = $vals2;
269 if ( !is_null( $params ) ) {
270 $logParams = array();
271 // Keys like "4::paramname" can't be used for output so we change them to "paramname"
272 foreach ( $params as $key => $value ) {
273 if ( strpos( $key, ':' ) === false ) {
274 $logParams[$key] = $value;
277 $logParam = explode( ':', $key, 3 );
278 $logParams[$logParam[2]] = $value;
280 $result->setIndexedTagName( $logParams, 'param' );
281 $result->setIndexedTagName_recursive( $logParams, 'param' );
282 $vals = array_merge( $vals, $logParams );
287 private function extractRowInfo( $row ) {
288 $logEntry = DatabaseLogEntry
::newFromRow( $row );
291 if ( $this->fld_ids
) {
292 $vals['logid'] = intval( $row->log_id
);
293 $vals['pageid'] = intval( $row->page_id
);
296 if ( $this->fld_title ||
$this->fld_parsedcomment
) {
297 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
300 if ( $this->fld_title
) {
301 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
302 $vals['actionhidden'] = '';
304 ApiQueryBase
::addTitleInfo( $vals, $title );
308 if ( $this->fld_type ||
$this->fld_action
) {
309 $vals['type'] = $row->log_type
;
310 $vals['action'] = $row->log_action
;
313 if ( $this->fld_details
&& $row->log_params
!== '' ) {
314 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
315 $vals['actionhidden'] = '';
320 $logEntry->getParameters(),
321 $logEntry->getType(),
322 $logEntry->getSubtype(),
323 $logEntry->getTimestamp(),
324 $logEntry->isLegacy()
329 if ( $this->fld_user ||
$this->fld_userid
) {
330 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_USER
) ) {
331 $vals['userhidden'] = '';
333 if ( $this->fld_user
) {
334 $vals['user'] = $row->user_name
;
336 if ( $this->fld_userid
) {
337 $vals['userid'] = $row->user_id
;
340 if ( !$row->log_user
) {
345 if ( $this->fld_timestamp
) {
346 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->log_timestamp
);
349 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) && isset( $row->log_comment
) ) {
350 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_COMMENT
) ) {
351 $vals['commenthidden'] = '';
353 if ( $this->fld_comment
) {
354 $vals['comment'] = $row->log_comment
;
357 if ( $this->fld_parsedcomment
) {
358 $vals['parsedcomment'] = Linker
::formatComment( $row->log_comment
, $title );
363 if ( $this->fld_tags
) {
364 if ( $row->ts_tags
) {
365 $tags = explode( ',', $row->ts_tags
);
366 $this->getResult()->setIndexedTagName( $tags, 'tag' );
367 $vals['tags'] = $tags;
369 $vals['tags'] = array();
376 public function getCacheMode( $params ) {
377 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
378 // formatComment() calls wfMessage() among other things
379 return 'anon-public-user-private';
380 } elseif ( LogEventsList
::getExcludeClause( $this->getDB(), 'user', $this->getUser() )
381 === LogEventsList
::getExcludeClause( $this->getDB(), 'public' )
382 ) { // Output can only contain public data.
385 return 'anon-public-user-private';
389 public function getAllowedParams() {
390 global $wgLogTypes, $wgLogActions, $wgLogActionsHandlers;
393 ApiBase
::PARAM_ISMULTI
=> true,
394 ApiBase
::PARAM_DFLT
=> 'ids|title|type|user|timestamp|comment|details',
395 ApiBase
::PARAM_TYPE
=> array(
409 ApiBase
::PARAM_TYPE
=> $wgLogTypes
412 ApiBase
::PARAM_TYPE
=> array_keys( array_merge( $wgLogActions, $wgLogActionsHandlers ) )
415 ApiBase
::PARAM_TYPE
=> 'timestamp'
418 ApiBase
::PARAM_TYPE
=> 'timestamp'
421 ApiBase
::PARAM_DFLT
=> 'older',
422 ApiBase
::PARAM_TYPE
=> array(
432 ApiBase
::PARAM_DFLT
=> 10,
433 ApiBase
::PARAM_TYPE
=> 'limit',
434 ApiBase
::PARAM_MIN
=> 1,
435 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
436 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
441 public function getParamDescription() {
442 $p = $this->getModulePrefix();
445 'Which properties to get',
446 ' ids - Adds the ID of the log event',
447 ' title - Adds the title of the page for the log event',
448 ' type - Adds the type of log event',
449 ' user - Adds the user responsible for the log event',
450 ' userid - Adds the user ID who was responsible for the log event',
451 ' timestamp - Adds the timestamp for the event',
452 ' comment - Adds the comment of the event',
453 ' parsedcomment - Adds the parsed comment of the event',
454 ' details - Lists additional details about the event',
455 ' tags - Lists tags for the event',
457 'type' => 'Filter log entries to only this type',
458 'action' => "Filter log actions to only this type. Overrides {$p}type",
459 'start' => 'The timestamp to start enumerating from',
460 'end' => 'The timestamp to end enumerating',
461 'dir' => $this->getDirectionDescription( $p ),
462 'user' => 'Filter entries to those made by the given user',
463 'title' => 'Filter entries to those related to a page',
464 'prefix' => 'Filter entries that start with this prefix. Disabled in Miser Mode',
465 'limit' => 'How many total event entries to return',
466 'tag' => 'Only list event entries tagged with this tag',
470 public function getResultProperties() {
474 'logid' => 'integer',
475 'pageid' => 'integer'
483 ApiBase
::PROP_TYPE
=> $wgLogTypes
488 'actionhidden' => 'boolean'
491 'userhidden' => 'boolean',
493 ApiBase
::PROP_TYPE
=> 'string',
494 ApiBase
::PROP_NULLABLE
=> true
499 'userhidden' => 'boolean',
501 ApiBase
::PROP_TYPE
=> 'integer',
502 ApiBase
::PROP_NULLABLE
=> true
506 'timestamp' => array(
507 'timestamp' => 'timestamp'
510 'commenthidden' => 'boolean',
512 ApiBase
::PROP_TYPE
=> 'string',
513 ApiBase
::PROP_NULLABLE
=> true
516 'parsedcomment' => array(
517 'commenthidden' => 'boolean',
518 'parsedcomment' => array(
519 ApiBase
::PROP_TYPE
=> 'string',
520 ApiBase
::PROP_NULLABLE
=> true
526 public function getDescription() {
527 return 'Get events from logs';
530 public function getPossibleErrors() {
531 return array_merge( parent
::getPossibleErrors(), array(
532 array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
533 array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
534 array( 'code' => 'param_prefix', 'info' => 'Bad title value \'prefix\'' ),
535 array( 'code' => 'prefixsearchdisabled', 'info' => 'Prefix search disabled in Miser Mode' ),
539 public function getExamples() {
541 'api.php?action=query&list=logevents'
545 public function getHelpUrls() {
546 return 'https://www.mediawiki.org/wiki/API:Logevents';