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' ) ) ) );
76 $this->addFields( array(
83 $this->addFieldsIf( array( 'log_id', '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
);
87 array( 'log_namespace', 'log_title' ),
88 $this->fld_title ||
$this->fld_parsedcomment
90 $this->addFieldsIf( 'log_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
91 $this->addFieldsIf( 'log_params', $this->fld_details
);
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' );
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'] );
106 if ( !is_null( $params['action'] ) ) {
107 list( $type, $action ) = explode( '/', $params['action'] );
108 $this->addWhereFld( 'log_type', $type );
109 $this->addWhereFld( 'log_action', $action );
110 } elseif ( !is_null( $params['type'] ) ) {
111 $this->addWhereFld( 'log_type', $params['type'] );
114 $this->addTimestampWhereRange(
121 $limit = $params['limit'];
122 $this->addOption( 'LIMIT', $limit +
1 );
124 $user = $params['user'];
125 if ( !is_null( $user ) ) {
126 $userid = User
::idFromName( $user );
128 $this->addWhereFld( 'log_user', $userid );
130 $this->addWhereFld( 'log_user_text', IP
::sanitizeIP( $user ) );
134 $title = $params['title'];
135 if ( !is_null( $title ) ) {
136 $titleObj = Title
::newFromText( $title );
137 if ( is_null( $titleObj ) ) {
138 $this->dieUsage( "Bad title value '$title'", 'param_title' );
140 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
141 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
144 $prefix = $params['prefix'];
146 if ( !is_null( $prefix ) ) {
148 if ( $wgMiserMode ) {
149 $this->dieUsage( 'Prefix search disabled in Miser Mode', 'prefixsearchdisabled' );
152 $title = Title
::newFromText( $prefix );
153 if ( is_null( $title ) ) {
154 $this->dieUsage( "Bad title value '$prefix'", 'param_prefix' );
156 $this->addWhereFld( 'log_namespace', $title->getNamespace() );
157 $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
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
174 // additional pages to be had. Stop here...
175 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->log_timestamp
) );
179 $vals = $this->extractRowInfo( $row );
183 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
185 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->log_timestamp
) );
189 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
193 * @param $result ApiResult
195 * @param $params string
196 * @param $type string
197 * @param $action string
199 * @param $legacy bool
202 public static function addLogParams( $result, &$vals, $params, $type,
203 $action, $ts, $legacy = false
211 $targetKey = '4::target';
212 $noredirKey = '5::noredir';
215 if ( isset( $params[$targetKey] ) ) {
216 $title = Title
::newFromText( $params[$targetKey] );
219 ApiQueryBase
::addTitleInfo( $vals2, $title, 'new_' );
220 $vals[$type] = $vals2;
223 if ( isset( $params[$noredirKey] ) && $params[$noredirKey] ) {
224 $vals[$type]['suppressedredirect'] = '';
239 $vals2['cur'] = $params[$cur];
240 $vals2['prev'] = $params[$prev];
241 $vals2['auto'] = $params[$auto];
242 $vals[$type] = $vals2;
248 list( $vals2['old'], $vals2['new'] ) = $params;
250 $vals2['new'] = implode( ', ', $params['5::newgroups'] );
251 $vals2['old'] = implode( ', ', $params['4::oldgroups'] );
253 $vals[$type] = $vals2;
257 if ( $action == 'unblock' ) {
261 list( $vals2['duration'], $vals2['flags'] ) = $params;
263 // Indefinite blocks have no expiry time
264 if ( SpecialBlock
::parseExpiryInput( $params[0] ) !== wfGetDB( DB_SLAVE
)->getInfinity() ) {
265 $vals2['expiry'] = wfTimestamp( TS_ISO_8601
,
266 strtotime( $params[0], wfTimestamp( TS_UNIX
, $ts ) ) );
268 $vals[$type] = $vals2;
272 if ( isset( $params['img_timestamp'] ) ) {
273 $params['img_timestamp'] = wfTimestamp( TS_ISO_8601
, $params['img_timestamp'] );
277 if ( !is_null( $params ) ) {
278 $logParams = array();
279 // Keys like "4::paramname" can't be used for output so we change them to "paramname"
280 foreach ( $params as $key => $value ) {
281 if ( strpos( $key, ':' ) === false ) {
282 $logParams[$key] = $value;
285 $logParam = explode( ':', $key, 3 );
286 $logParams[$logParam[2]] = $value;
288 $result->setIndexedTagName( $logParams, 'param' );
289 $result->setIndexedTagName_recursive( $logParams, 'param' );
290 $vals = array_merge( $vals, $logParams );
296 private function extractRowInfo( $row ) {
297 $logEntry = DatabaseLogEntry
::newFromRow( $row );
300 if ( $this->fld_ids
) {
301 $vals['logid'] = intval( $row->log_id
);
304 if ( $this->fld_title ||
$this->fld_parsedcomment
) {
305 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
308 if ( $this->fld_title ||
$this->fld_ids
) {
309 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
310 $vals['actionhidden'] = '';
312 if ( $this->fld_title
) {
313 ApiQueryBase
::addTitleInfo( $vals, $title );
315 if ( $this->fld_ids
) {
316 $vals['pageid'] = intval( $row->page_id
);
321 if ( $this->fld_type ||
$this->fld_action
) {
322 $vals['type'] = $row->log_type
;
323 $vals['action'] = $row->log_action
;
326 if ( $this->fld_details
&& $row->log_params
!== '' ) {
327 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
328 $vals['actionhidden'] = '';
333 $logEntry->getParameters(),
334 $logEntry->getType(),
335 $logEntry->getSubtype(),
336 $logEntry->getTimestamp(),
337 $logEntry->isLegacy()
342 if ( $this->fld_user ||
$this->fld_userid
) {
343 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_USER
) ) {
344 $vals['userhidden'] = '';
346 if ( $this->fld_user
) {
347 $vals['user'] = $row->user_name
=== null ?
$row->log_user_text
: $row->user_name
;
349 if ( $this->fld_userid
) {
350 $vals['userid'] = $row->log_user
;
353 if ( !$row->log_user
) {
358 if ( $this->fld_timestamp
) {
359 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->log_timestamp
);
362 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) && isset( $row->log_comment
) ) {
363 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_COMMENT
) ) {
364 $vals['commenthidden'] = '';
366 if ( $this->fld_comment
) {
367 $vals['comment'] = $row->log_comment
;
370 if ( $this->fld_parsedcomment
) {
371 $vals['parsedcomment'] = Linker
::formatComment( $row->log_comment
, $title );
376 if ( $this->fld_tags
) {
377 if ( $row->ts_tags
) {
378 $tags = explode( ',', $row->ts_tags
);
379 $this->getResult()->setIndexedTagName( $tags, 'tag' );
380 $vals['tags'] = $tags;
382 $vals['tags'] = array();
389 public function getCacheMode( $params ) {
390 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
391 // formatComment() calls wfMessage() among other things
392 return 'anon-public-user-private';
393 } elseif ( LogEventsList
::getExcludeClause( $this->getDB(), 'user', $this->getUser() )
394 === LogEventsList
::getExcludeClause( $this->getDB(), 'public' )
395 ) { // Output can only contain public data.
398 return 'anon-public-user-private';
402 public function getAllowedParams() {
403 global $wgLogTypes, $wgLogActions, $wgLogActionsHandlers;
407 ApiBase
::PARAM_ISMULTI
=> true,
408 ApiBase
::PARAM_DFLT
=> 'ids|title|type|user|timestamp|comment|details',
409 ApiBase
::PARAM_TYPE
=> array(
423 ApiBase
::PARAM_TYPE
=> $wgLogTypes
426 ApiBase
::PARAM_TYPE
=> array_keys( array_merge( $wgLogActions, $wgLogActionsHandlers ) )
429 ApiBase
::PARAM_TYPE
=> 'timestamp'
432 ApiBase
::PARAM_TYPE
=> 'timestamp'
435 ApiBase
::PARAM_DFLT
=> 'older',
436 ApiBase
::PARAM_TYPE
=> array(
446 ApiBase
::PARAM_DFLT
=> 10,
447 ApiBase
::PARAM_TYPE
=> 'limit',
448 ApiBase
::PARAM_MIN
=> 1,
449 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
450 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
455 public function getParamDescription() {
456 $p = $this->getModulePrefix();
460 'Which properties to get',
461 ' ids - Adds the ID of the log event',
462 ' title - Adds the title of the page for the log event',
463 ' type - Adds the type of log event',
464 ' user - Adds the user responsible for the log event',
465 ' userid - Adds the user ID who was responsible for the log event',
466 ' timestamp - Adds the timestamp for the event',
467 ' comment - Adds the comment of the event',
468 ' parsedcomment - Adds the parsed comment of the event',
469 ' details - Lists additional details about the event',
470 ' tags - Lists tags for the event',
472 'type' => 'Filter log entries to only this type',
473 'action' => "Filter log actions to only this type. Overrides {$p}type",
474 'start' => 'The timestamp to start enumerating from',
475 'end' => 'The timestamp to end enumerating',
476 'dir' => $this->getDirectionDescription( $p ),
477 'user' => 'Filter entries to those made by the given user',
478 'title' => 'Filter entries to those related to a page',
479 'prefix' => 'Filter entries that start with this prefix. Disabled in Miser Mode',
480 'limit' => 'How many total event entries to return',
481 'tag' => 'Only list event entries tagged with this tag',
485 public function getResultProperties() {
490 'logid' => 'integer',
491 'pageid' => 'integer'
499 ApiBase
::PROP_TYPE
=> $wgLogTypes
504 'actionhidden' => 'boolean'
507 'userhidden' => 'boolean',
509 ApiBase
::PROP_TYPE
=> 'string',
510 ApiBase
::PROP_NULLABLE
=> true
515 'userhidden' => 'boolean',
517 ApiBase
::PROP_TYPE
=> 'integer',
518 ApiBase
::PROP_NULLABLE
=> true
522 'timestamp' => array(
523 'timestamp' => 'timestamp'
526 'commenthidden' => 'boolean',
528 ApiBase
::PROP_TYPE
=> 'string',
529 ApiBase
::PROP_NULLABLE
=> true
532 'parsedcomment' => array(
533 'commenthidden' => 'boolean',
534 'parsedcomment' => array(
535 ApiBase
::PROP_TYPE
=> 'string',
536 ApiBase
::PROP_NULLABLE
=> true
542 public function getDescription() {
543 return 'Get events from logs';
546 public function getPossibleErrors() {
547 return array_merge( parent
::getPossibleErrors(), array(
548 array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
549 array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
550 array( 'code' => 'param_prefix', 'info' => 'Bad title value \'prefix\'' ),
551 array( 'code' => 'prefixsearchdisabled', 'info' => 'Prefix search disabled in Miser Mode' ),
555 public function getExamples() {
557 'api.php?action=query&list=logevents'
561 public function getHelpUrls() {
562 return 'https://www.mediawiki.org/wiki/API:Logevents';