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
);
302 $vals['pageid'] = intval( $row->page_id
);
305 if ( $this->fld_title ||
$this->fld_parsedcomment
) {
306 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
309 if ( $this->fld_title
) {
310 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
311 $vals['actionhidden'] = '';
313 ApiQueryBase
::addTitleInfo( $vals, $title );
317 if ( $this->fld_type ||
$this->fld_action
) {
318 $vals['type'] = $row->log_type
;
319 $vals['action'] = $row->log_action
;
322 if ( $this->fld_details
&& $row->log_params
!== '' ) {
323 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
324 $vals['actionhidden'] = '';
329 $logEntry->getParameters(),
330 $logEntry->getType(),
331 $logEntry->getSubtype(),
332 $logEntry->getTimestamp(),
333 $logEntry->isLegacy()
338 if ( $this->fld_user ||
$this->fld_userid
) {
339 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_USER
) ) {
340 $vals['userhidden'] = '';
342 if ( $this->fld_user
) {
343 $vals['user'] = $row->user_name
=== null ?
$row->log_user_text
: $row->user_name
;
345 if ( $this->fld_userid
) {
346 $vals['userid'] = $row->log_user
;
349 if ( !$row->log_user
) {
354 if ( $this->fld_timestamp
) {
355 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->log_timestamp
);
358 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) && isset( $row->log_comment
) ) {
359 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_COMMENT
) ) {
360 $vals['commenthidden'] = '';
362 if ( $this->fld_comment
) {
363 $vals['comment'] = $row->log_comment
;
366 if ( $this->fld_parsedcomment
) {
367 $vals['parsedcomment'] = Linker
::formatComment( $row->log_comment
, $title );
372 if ( $this->fld_tags
) {
373 if ( $row->ts_tags
) {
374 $tags = explode( ',', $row->ts_tags
);
375 $this->getResult()->setIndexedTagName( $tags, 'tag' );
376 $vals['tags'] = $tags;
378 $vals['tags'] = array();
385 public function getCacheMode( $params ) {
386 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
387 // formatComment() calls wfMessage() among other things
388 return 'anon-public-user-private';
389 } elseif ( LogEventsList
::getExcludeClause( $this->getDB(), 'user', $this->getUser() )
390 === LogEventsList
::getExcludeClause( $this->getDB(), 'public' )
391 ) { // Output can only contain public data.
394 return 'anon-public-user-private';
398 public function getAllowedParams() {
399 global $wgLogTypes, $wgLogActions, $wgLogActionsHandlers;
403 ApiBase
::PARAM_ISMULTI
=> true,
404 ApiBase
::PARAM_DFLT
=> 'ids|title|type|user|timestamp|comment|details',
405 ApiBase
::PARAM_TYPE
=> array(
419 ApiBase
::PARAM_TYPE
=> $wgLogTypes
422 ApiBase
::PARAM_TYPE
=> array_keys( array_merge( $wgLogActions, $wgLogActionsHandlers ) )
425 ApiBase
::PARAM_TYPE
=> 'timestamp'
428 ApiBase
::PARAM_TYPE
=> 'timestamp'
431 ApiBase
::PARAM_DFLT
=> 'older',
432 ApiBase
::PARAM_TYPE
=> array(
442 ApiBase
::PARAM_DFLT
=> 10,
443 ApiBase
::PARAM_TYPE
=> 'limit',
444 ApiBase
::PARAM_MIN
=> 1,
445 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
446 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
451 public function getParamDescription() {
452 $p = $this->getModulePrefix();
456 'Which properties to get',
457 ' ids - Adds the ID of the log event',
458 ' title - Adds the title of the page for the log event',
459 ' type - Adds the type of log event',
460 ' user - Adds the user responsible for the log event',
461 ' userid - Adds the user ID who was responsible for the log event',
462 ' timestamp - Adds the timestamp for the event',
463 ' comment - Adds the comment of the event',
464 ' parsedcomment - Adds the parsed comment of the event',
465 ' details - Lists additional details about the event',
466 ' tags - Lists tags for the event',
468 'type' => 'Filter log entries to only this type',
469 'action' => "Filter log actions to only this type. Overrides {$p}type",
470 'start' => 'The timestamp to start enumerating from',
471 'end' => 'The timestamp to end enumerating',
472 'dir' => $this->getDirectionDescription( $p ),
473 'user' => 'Filter entries to those made by the given user',
474 'title' => 'Filter entries to those related to a page',
475 'prefix' => 'Filter entries that start with this prefix. Disabled in Miser Mode',
476 'limit' => 'How many total event entries to return',
477 'tag' => 'Only list event entries tagged with this tag',
481 public function getResultProperties() {
486 'logid' => 'integer',
487 'pageid' => 'integer'
495 ApiBase
::PROP_TYPE
=> $wgLogTypes
500 'actionhidden' => 'boolean'
503 'userhidden' => 'boolean',
505 ApiBase
::PROP_TYPE
=> 'string',
506 ApiBase
::PROP_NULLABLE
=> true
511 'userhidden' => 'boolean',
513 ApiBase
::PROP_TYPE
=> 'integer',
514 ApiBase
::PROP_NULLABLE
=> true
518 'timestamp' => array(
519 'timestamp' => 'timestamp'
522 'commenthidden' => 'boolean',
524 ApiBase
::PROP_TYPE
=> 'string',
525 ApiBase
::PROP_NULLABLE
=> true
528 'parsedcomment' => array(
529 'commenthidden' => 'boolean',
530 'parsedcomment' => array(
531 ApiBase
::PROP_TYPE
=> 'string',
532 ApiBase
::PROP_NULLABLE
=> true
538 public function getDescription() {
539 return 'Get events from logs';
542 public function getPossibleErrors() {
543 return array_merge( parent
::getPossibleErrors(), array(
544 array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
545 array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
546 array( 'code' => 'param_prefix', 'info' => 'Bad title value \'prefix\'' ),
547 array( 'code' => 'prefixsearchdisabled', 'info' => 'Prefix search disabled in Miser Mode' ),
551 public function getExamples() {
553 'api.php?action=query&list=logevents'
557 public function getHelpUrls() {
558 return 'https://www.mediawiki.org/wiki/API:Logevents';