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( 'LEFT 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', 'log_user_text', 'user_name' ), $this->fld_user
);
86 $this->addFieldsIf( 'log_user', $this->fld_userid
);
88 array( 'log_namespace', 'log_title' ),
89 $this->fld_title ||
$this->fld_parsedcomment
91 $this->addFieldsIf( 'log_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
92 $this->addFieldsIf( 'log_params', $this->fld_details
);
94 if ( $this->fld_tags
) {
95 $this->addTables( 'tag_summary' );
96 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', 'log_id=ts_log_id' ) ) );
97 $this->addFields( 'ts_tags' );
100 if ( !is_null( $params['tag'] ) ) {
101 $this->addTables( 'change_tag' );
102 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN',
103 array( 'log_id=ct_log_id' ) ) ) );
104 $this->addWhereFld( 'ct_tag', $params['tag'] );
105 $index['change_tag'] = 'change_tag_tag_id';
108 if ( !is_null( $params['action'] ) ) {
109 list( $type, $action ) = explode( '/', $params['action'] );
110 $this->addWhereFld( 'log_type', $type );
111 $this->addWhereFld( 'log_action', $action );
112 } elseif ( !is_null( $params['type'] ) ) {
113 $this->addWhereFld( 'log_type', $params['type'] );
114 $index['logging'] = 'type_time';
117 $this->addTimestampWhereRange(
124 $limit = $params['limit'];
125 $this->addOption( 'LIMIT', $limit +
1 );
127 $user = $params['user'];
128 if ( !is_null( $user ) ) {
129 $userid = User
::idFromName( $user );
131 $this->dieUsage( "User name $user not found", 'param_user' );
133 $this->addWhereFld( 'log_user', $userid );
134 $index['logging'] = 'user_time';
137 $title = $params['title'];
138 if ( !is_null( $title ) ) {
139 $titleObj = Title
::newFromText( $title );
140 if ( is_null( $titleObj ) ) {
141 $this->dieUsage( "Bad title value '$title'", 'param_title' );
143 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
144 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
146 // Use the title index in preference to the user index if there is a conflict
147 $index['logging'] = is_null( $user ) ?
'page_time' : array( 'page_time', 'user_time' );
150 $prefix = $params['prefix'];
152 if ( !is_null( $prefix ) ) {
154 if ( $wgMiserMode ) {
155 $this->dieUsage( 'Prefix search disabled in Miser Mode', 'prefixsearchdisabled' );
158 $title = Title
::newFromText( $prefix );
159 if ( is_null( $title ) ) {
160 $this->dieUsage( "Bad title value '$prefix'", 'param_prefix' );
162 $this->addWhereFld( 'log_namespace', $title->getNamespace() );
163 $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
166 $this->addOption( 'USE INDEX', $index );
168 // Paranoia: avoid brute force searches (bug 17342)
169 if ( !is_null( $title ) ) {
170 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage
::DELETED_ACTION
) . ' = 0' );
172 if ( !is_null( $user ) ) {
173 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage
::DELETED_USER
) . ' = 0' );
177 $res = $this->select( __METHOD__
);
178 $result = $this->getResult();
179 foreach ( $res as $row ) {
180 if ( ++
$count > $limit ) {
181 // We've reached the one extra which shows that there are
182 // additional pages to be had. Stop here...
183 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->log_timestamp
) );
187 $vals = $this->extractRowInfo( $row );
191 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
193 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->log_timestamp
) );
197 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
201 * @param $result ApiResult
203 * @param $params string
204 * @param $type string
205 * @param $action string
207 * @param $legacy bool
210 public static function addLogParams( $result, &$vals, $params, $type,
211 $action, $ts, $legacy = false
219 $targetKey = '4::target';
220 $noredirKey = '5::noredir';
223 if ( isset( $params[$targetKey] ) ) {
224 $title = Title
::newFromText( $params[$targetKey] );
227 ApiQueryBase
::addTitleInfo( $vals2, $title, 'new_' );
228 $vals[$type] = $vals2;
231 if ( isset( $params[$noredirKey] ) && $params[$noredirKey] ) {
232 $vals[$type]['suppressedredirect'] = '';
247 $vals2['cur'] = $params[$cur];
248 $vals2['prev'] = $params[$prev];
249 $vals2['auto'] = $params[$auto];
250 $vals[$type] = $vals2;
256 list( $vals2['old'], $vals2['new'] ) = $params;
258 $vals2['new'] = implode( ', ', $params['5::newgroups'] );
259 $vals2['old'] = implode( ', ', $params['4::oldgroups'] );
261 $vals[$type] = $vals2;
265 if ( $action == 'unblock' ) {
269 list( $vals2['duration'], $vals2['flags'] ) = $params;
271 // Indefinite blocks have no expiry time
272 if ( SpecialBlock
::parseExpiryInput( $params[0] ) !== wfGetDB( DB_SLAVE
)->getInfinity() ) {
273 $vals2['expiry'] = wfTimestamp( TS_ISO_8601
,
274 strtotime( $params[0], wfTimestamp( TS_UNIX
, $ts ) ) );
276 $vals[$type] = $vals2;
280 if ( isset( $params['img_timestamp'] ) ) {
281 $params['img_timestamp'] = wfTimestamp( TS_ISO_8601
, $params['img_timestamp'] );
285 if ( !is_null( $params ) ) {
286 $logParams = array();
287 // Keys like "4::paramname" can't be used for output so we change them to "paramname"
288 foreach ( $params as $key => $value ) {
289 if ( strpos( $key, ':' ) === false ) {
290 $logParams[$key] = $value;
293 $logParam = explode( ':', $key, 3 );
294 $logParams[$logParam[2]] = $value;
296 $result->setIndexedTagName( $logParams, 'param' );
297 $result->setIndexedTagName_recursive( $logParams, 'param' );
298 $vals = array_merge( $vals, $logParams );
304 private function extractRowInfo( $row ) {
305 $logEntry = DatabaseLogEntry
::newFromRow( $row );
308 if ( $this->fld_ids
) {
309 $vals['logid'] = intval( $row->log_id
);
310 $vals['pageid'] = intval( $row->page_id
);
313 if ( $this->fld_title ||
$this->fld_parsedcomment
) {
314 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
317 if ( $this->fld_title
) {
318 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
319 $vals['actionhidden'] = '';
321 ApiQueryBase
::addTitleInfo( $vals, $title );
325 if ( $this->fld_type ||
$this->fld_action
) {
326 $vals['type'] = $row->log_type
;
327 $vals['action'] = $row->log_action
;
330 if ( $this->fld_details
&& $row->log_params
!== '' ) {
331 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
332 $vals['actionhidden'] = '';
337 $logEntry->getParameters(),
338 $logEntry->getType(),
339 $logEntry->getSubtype(),
340 $logEntry->getTimestamp(),
341 $logEntry->isLegacy()
346 if ( $this->fld_user ||
$this->fld_userid
) {
347 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_USER
) ) {
348 $vals['userhidden'] = '';
350 if ( $this->fld_user
) {
351 $vals['user'] = $row->user_name
=== null ?
$row->log_user_text
: $row->user_name
;
353 if ( $this->fld_userid
) {
354 $vals['userid'] = $row->log_user
;
357 if ( !$row->log_user
) {
362 if ( $this->fld_timestamp
) {
363 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->log_timestamp
);
366 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) && isset( $row->log_comment
) ) {
367 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_COMMENT
) ) {
368 $vals['commenthidden'] = '';
370 if ( $this->fld_comment
) {
371 $vals['comment'] = $row->log_comment
;
374 if ( $this->fld_parsedcomment
) {
375 $vals['parsedcomment'] = Linker
::formatComment( $row->log_comment
, $title );
380 if ( $this->fld_tags
) {
381 if ( $row->ts_tags
) {
382 $tags = explode( ',', $row->ts_tags
);
383 $this->getResult()->setIndexedTagName( $tags, 'tag' );
384 $vals['tags'] = $tags;
386 $vals['tags'] = array();
393 public function getCacheMode( $params ) {
394 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
395 // formatComment() calls wfMessage() among other things
396 return 'anon-public-user-private';
397 } elseif ( LogEventsList
::getExcludeClause( $this->getDB(), 'user', $this->getUser() )
398 === LogEventsList
::getExcludeClause( $this->getDB(), 'public' )
399 ) { // Output can only contain public data.
402 return 'anon-public-user-private';
406 public function getAllowedParams() {
407 global $wgLogTypes, $wgLogActions, $wgLogActionsHandlers;
411 ApiBase
::PARAM_ISMULTI
=> true,
412 ApiBase
::PARAM_DFLT
=> 'ids|title|type|user|timestamp|comment|details',
413 ApiBase
::PARAM_TYPE
=> array(
427 ApiBase
::PARAM_TYPE
=> $wgLogTypes
430 ApiBase
::PARAM_TYPE
=> array_keys( array_merge( $wgLogActions, $wgLogActionsHandlers ) )
433 ApiBase
::PARAM_TYPE
=> 'timestamp'
436 ApiBase
::PARAM_TYPE
=> 'timestamp'
439 ApiBase
::PARAM_DFLT
=> 'older',
440 ApiBase
::PARAM_TYPE
=> array(
450 ApiBase
::PARAM_DFLT
=> 10,
451 ApiBase
::PARAM_TYPE
=> 'limit',
452 ApiBase
::PARAM_MIN
=> 1,
453 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
454 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
459 public function getParamDescription() {
460 $p = $this->getModulePrefix();
464 'Which properties to get',
465 ' ids - Adds the ID of the log event',
466 ' title - Adds the title of the page for the log event',
467 ' type - Adds the type of log event',
468 ' user - Adds the user responsible for the log event',
469 ' userid - Adds the user ID who was responsible for the log event',
470 ' timestamp - Adds the timestamp for the event',
471 ' comment - Adds the comment of the event',
472 ' parsedcomment - Adds the parsed comment of the event',
473 ' details - Lists additional details about the event',
474 ' tags - Lists tags for the event',
476 'type' => 'Filter log entries to only this type',
477 'action' => "Filter log actions to only this type. Overrides {$p}type",
478 'start' => 'The timestamp to start enumerating from',
479 'end' => 'The timestamp to end enumerating',
480 'dir' => $this->getDirectionDescription( $p ),
481 'user' => 'Filter entries to those made by the given user',
482 'title' => 'Filter entries to those related to a page',
483 'prefix' => 'Filter entries that start with this prefix. Disabled in Miser Mode',
484 'limit' => 'How many total event entries to return',
485 'tag' => 'Only list event entries tagged with this tag',
489 public function getResultProperties() {
494 'logid' => 'integer',
495 'pageid' => 'integer'
503 ApiBase
::PROP_TYPE
=> $wgLogTypes
508 'actionhidden' => 'boolean'
511 'userhidden' => 'boolean',
513 ApiBase
::PROP_TYPE
=> 'string',
514 ApiBase
::PROP_NULLABLE
=> true
519 'userhidden' => 'boolean',
521 ApiBase
::PROP_TYPE
=> 'integer',
522 ApiBase
::PROP_NULLABLE
=> true
526 'timestamp' => array(
527 'timestamp' => 'timestamp'
530 'commenthidden' => 'boolean',
532 ApiBase
::PROP_TYPE
=> 'string',
533 ApiBase
::PROP_NULLABLE
=> true
536 'parsedcomment' => array(
537 'commenthidden' => 'boolean',
538 'parsedcomment' => array(
539 ApiBase
::PROP_TYPE
=> 'string',
540 ApiBase
::PROP_NULLABLE
=> true
546 public function getDescription() {
547 return 'Get events from logs';
550 public function getPossibleErrors() {
551 return array_merge( parent
::getPossibleErrors(), array(
552 array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
553 array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
554 array( 'code' => 'param_prefix', 'info' => 'Bad title value \'prefix\'' ),
555 array( 'code' => 'prefixsearchdisabled', 'info' => 'Prefix search disabled in Miser Mode' ),
559 public function getExamples() {
561 'api.php?action=query&list=logevents'
565 public function getHelpUrls() {
566 return 'https://www.mediawiki.org/wiki/API:Logevents';