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->addJoinConds( array(
69 'user' => array( 'LEFT JOIN',
71 'page' => array( 'LEFT JOIN',
72 array( 'log_namespace=page_namespace',
73 'log_title=page_title' ) ) ) );
75 $this->addFields( array(
82 $this->addFieldsIf( array( 'log_id', 'page_id' ), $this->fld_ids
);
83 $this->addFieldsIf( array( 'log_user', 'log_user_text', 'user_name' ), $this->fld_user
);
84 $this->addFieldsIf( 'log_user', $this->fld_userid
);
86 array( 'log_namespace', 'log_title' ),
87 $this->fld_title ||
$this->fld_parsedcomment
89 $this->addFieldsIf( 'log_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
90 $this->addFieldsIf( 'log_params', $this->fld_details
);
92 if ( $this->fld_tags
) {
93 $this->addTables( 'tag_summary' );
94 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', 'log_id=ts_log_id' ) ) );
95 $this->addFields( 'ts_tags' );
98 if ( !is_null( $params['tag'] ) ) {
99 $this->addTables( 'change_tag' );
100 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN',
101 array( 'log_id=ct_log_id' ) ) ) );
102 $this->addWhereFld( 'ct_tag', $params['tag'] );
105 if ( !is_null( $params['action'] ) ) {
106 // Do validation of action param, list of allowed actions can contains wildcards
107 // Allow the param, when the actions is in the list or a wildcard version is listed.
108 $logAction = $params['action'];
109 if ( strpos( $logAction, '/' ) === false ) {
110 // all items in the list have a slash
113 $logActions = array_flip( $this->getAllowedLogActions() );
114 list( $type, $action ) = explode( '/', $logAction, 2 );
115 $valid = isset( $logActions[$logAction] ) ||
isset( $logActions[$type . '/*'] );
119 $valueName = $this->encodeParamName( 'action' );
121 "Unrecognized value for parameter '$valueName': {$logAction}",
126 $this->addWhereFld( 'log_type', $type );
127 $this->addWhereFld( 'log_action', $action );
128 } elseif ( !is_null( $params['type'] ) ) {
129 $this->addWhereFld( 'log_type', $params['type'] );
132 $this->addTimestampWhereRange(
139 $limit = $params['limit'];
140 $this->addOption( 'LIMIT', $limit +
1 );
142 $user = $params['user'];
143 if ( !is_null( $user ) ) {
144 $userid = User
::idFromName( $user );
146 $this->addWhereFld( 'log_user', $userid );
148 $this->addWhereFld( 'log_user_text', IP
::sanitizeIP( $user ) );
152 $title = $params['title'];
153 if ( !is_null( $title ) ) {
154 $titleObj = Title
::newFromText( $title );
155 if ( is_null( $titleObj ) ) {
156 $this->dieUsage( "Bad title value '$title'", 'param_title' );
158 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
159 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
162 $prefix = $params['prefix'];
164 if ( !is_null( $prefix ) ) {
166 if ( $wgMiserMode ) {
167 $this->dieUsage( 'Prefix search disabled in Miser Mode', 'prefixsearchdisabled' );
170 $title = Title
::newFromText( $prefix );
171 if ( is_null( $title ) ) {
172 $this->dieUsage( "Bad title value '$prefix'", 'param_prefix' );
174 $this->addWhereFld( 'log_namespace', $title->getNamespace() );
175 $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
178 // Paranoia: avoid brute force searches (bug 17342)
179 if ( !is_null( $title ) ||
!is_null( $user ) ) {
180 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
181 $titleBits = LogPage
::DELETED_ACTION
;
182 $userBits = LogPage
::DELETED_USER
;
183 } elseif ( !$this->getUser()->isAllowed( 'suppressrevision' ) ) {
184 $titleBits = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
185 $userBits = LogPage
::DELETED_USER | LogPage
::DELETED_RESTRICTED
;
190 if ( !is_null( $title ) && $titleBits ) {
191 $this->addWhere( $db->bitAnd( 'log_deleted', $titleBits ) . " != $titleBits" );
193 if ( !is_null( $user ) && $userBits ) {
194 $this->addWhere( $db->bitAnd( 'log_deleted', $userBits ) . " != $userBits" );
199 $res = $this->select( __METHOD__
);
200 $result = $this->getResult();
201 foreach ( $res as $row ) {
202 if ( ++
$count > $limit ) {
203 // We've reached the one extra which shows that there are
204 // additional pages to be had. Stop here...
205 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->log_timestamp
) );
209 $vals = $this->extractRowInfo( $row );
213 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
215 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->log_timestamp
) );
219 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
223 * @param $result ApiResult
225 * @param $params string
226 * @param $type string
227 * @param $action string
229 * @param $legacy bool
232 public static function addLogParams( $result, &$vals, $params, $type,
233 $action, $ts, $legacy = false
241 $targetKey = '4::target';
242 $noredirKey = '5::noredir';
245 if ( isset( $params[$targetKey] ) ) {
246 $title = Title
::newFromText( $params[$targetKey] );
249 ApiQueryBase
::addTitleInfo( $vals2, $title, 'new_' );
250 $vals[$type] = $vals2;
253 if ( isset( $params[$noredirKey] ) && $params[$noredirKey] ) {
254 $vals[$type]['suppressedredirect'] = '';
269 $vals2['cur'] = $params[$cur];
270 $vals2['prev'] = $params[$prev];
271 $vals2['auto'] = $params[$auto];
272 $vals[$type] = $vals2;
278 list( $vals2['old'], $vals2['new'] ) = $params;
280 $vals2['new'] = implode( ', ', $params['5::newgroups'] );
281 $vals2['old'] = implode( ', ', $params['4::oldgroups'] );
283 $vals[$type] = $vals2;
287 if ( $action == 'unblock' ) {
291 list( $vals2['duration'], $vals2['flags'] ) = $params;
293 // Indefinite blocks have no expiry time
294 if ( SpecialBlock
::parseExpiryInput( $params[0] ) !== wfGetDB( DB_SLAVE
)->getInfinity() ) {
295 $vals2['expiry'] = wfTimestamp( TS_ISO_8601
,
296 strtotime( $params[0], wfTimestamp( TS_UNIX
, $ts ) ) );
298 $vals[$type] = $vals2;
302 if ( isset( $params['img_timestamp'] ) ) {
303 $params['img_timestamp'] = wfTimestamp( TS_ISO_8601
, $params['img_timestamp'] );
307 if ( !is_null( $params ) ) {
308 $logParams = array();
309 // Keys like "4::paramname" can't be used for output so we change them to "paramname"
310 foreach ( $params as $key => $value ) {
311 if ( strpos( $key, ':' ) === false ) {
312 $logParams[$key] = $value;
315 $logParam = explode( ':', $key, 3 );
316 $logParams[$logParam[2]] = $value;
318 $result->setIndexedTagName( $logParams, 'param' );
319 $result->setIndexedTagName_recursive( $logParams, 'param' );
320 $vals = array_merge( $vals, $logParams );
326 private function extractRowInfo( $row ) {
327 $logEntry = DatabaseLogEntry
::newFromRow( $row );
330 $user = $this->getUser();
332 if ( $this->fld_ids
) {
333 $vals['logid'] = intval( $row->log_id
);
336 if ( $this->fld_title ||
$this->fld_parsedcomment
) {
337 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
340 if ( $this->fld_title ||
$this->fld_ids ||
$this->fld_details
&& $row->log_params
!== '' ) {
341 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
342 $vals['actionhidden'] = '';
345 if ( LogEventsList
::userCan( $row, LogPage
::DELETED_ACTION
, $user ) ) {
346 if ( $this->fld_title
) {
347 ApiQueryBase
::addTitleInfo( $vals, $title );
349 if ( $this->fld_ids
) {
350 $vals['pageid'] = intval( $row->page_id
);
352 if ( $this->fld_details
&& $row->log_params
!== '' ) {
356 $logEntry->getParameters(),
357 $logEntry->getType(),
358 $logEntry->getSubtype(),
359 $logEntry->getTimestamp(),
360 $logEntry->isLegacy()
366 if ( $this->fld_type ||
$this->fld_action
) {
367 $vals['type'] = $row->log_type
;
368 $vals['action'] = $row->log_action
;
371 if ( $this->fld_user ||
$this->fld_userid
) {
372 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_USER
) ) {
373 $vals['userhidden'] = '';
376 if ( LogEventsList
::userCan( $row, LogPage
::DELETED_USER
, $user ) ) {
377 if ( $this->fld_user
) {
378 $vals['user'] = $row->user_name
=== null ?
$row->log_user_text
: $row->user_name
;
380 if ( $this->fld_userid
) {
381 $vals['userid'] = $row->log_user
;
384 if ( !$row->log_user
) {
389 if ( $this->fld_timestamp
) {
390 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->log_timestamp
);
393 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) && isset( $row->log_comment
) ) {
394 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_COMMENT
) ) {
395 $vals['commenthidden'] = '';
398 if ( LogEventsList
::userCan( $row, LogPage
::DELETED_COMMENT
, $user ) ) {
399 if ( $this->fld_comment
) {
400 $vals['comment'] = $row->log_comment
;
403 if ( $this->fld_parsedcomment
) {
404 $vals['parsedcomment'] = Linker
::formatComment( $row->log_comment
, $title );
409 if ( $this->fld_tags
) {
410 if ( $row->ts_tags
) {
411 $tags = explode( ',', $row->ts_tags
);
412 $this->getResult()->setIndexedTagName( $tags, 'tag' );
413 $vals['tags'] = $tags;
415 $vals['tags'] = array();
419 if ( $anyHidden && LogEventsList
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
) ) {
420 $vals['suppressed'] = '';
426 private function getAllowedLogActions() {
427 global $wgLogActions, $wgLogActionsHandlers;
429 return array_keys( array_merge( $wgLogActions, $wgLogActionsHandlers ) );
432 public function getCacheMode( $params ) {
433 if ( $this->userCanSeeRevDel() ) {
436 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
437 // formatComment() calls wfMessage() among other things
438 return 'anon-public-user-private';
439 } elseif ( LogEventsList
::getExcludeClause( $this->getDB(), 'user', $this->getUser() )
440 === LogEventsList
::getExcludeClause( $this->getDB(), 'public' )
441 ) { // Output can only contain public data.
444 return 'anon-public-user-private';
448 public function getAllowedParams( $flags = 0 ) {
453 ApiBase
::PARAM_ISMULTI
=> true,
454 ApiBase
::PARAM_DFLT
=> 'ids|title|type|user|timestamp|comment|details',
455 ApiBase
::PARAM_TYPE
=> array(
469 ApiBase
::PARAM_TYPE
=> $wgLogTypes
472 // validation on request is done in execute()
473 ApiBase
::PARAM_TYPE
=> ( $flags & ApiBase
::GET_VALUES_FOR_HELP
)
474 ?
$this->getAllowedLogActions()
478 ApiBase
::PARAM_TYPE
=> 'timestamp'
481 ApiBase
::PARAM_TYPE
=> 'timestamp'
484 ApiBase
::PARAM_DFLT
=> 'older',
485 ApiBase
::PARAM_TYPE
=> array(
495 ApiBase
::PARAM_DFLT
=> 10,
496 ApiBase
::PARAM_TYPE
=> 'limit',
497 ApiBase
::PARAM_MIN
=> 1,
498 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
499 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
504 public function getParamDescription() {
505 $p = $this->getModulePrefix();
509 'Which properties to get',
510 ' ids - Adds the ID of the log event',
511 ' title - Adds the title of the page for the log event',
512 ' type - Adds the type of log event',
513 ' user - Adds the user responsible for the log event',
514 ' userid - Adds the user ID who was responsible for the log event',
515 ' timestamp - Adds the timestamp for the event',
516 ' comment - Adds the comment of the event',
517 ' parsedcomment - Adds the parsed comment of the event',
518 ' details - Lists additional details about the event',
519 ' tags - Lists tags for the event',
521 'type' => 'Filter log entries to only this type',
523 "Filter log actions to only this action. Overrides {$p}type",
524 "Wildcard actions like 'action/*' allows to specify any string for the asterisk"
526 'start' => 'The timestamp to start enumerating from',
527 'end' => 'The timestamp to end enumerating',
528 'dir' => $this->getDirectionDescription( $p ),
529 'user' => 'Filter entries to those made by the given user',
530 'title' => 'Filter entries to those related to a page',
531 'prefix' => 'Filter entries that start with this prefix. Disabled in Miser Mode',
532 'limit' => 'How many total event entries to return',
533 'tag' => 'Only list event entries tagged with this tag',
537 public function getResultProperties() {
542 'logid' => 'integer',
543 'pageid' => 'integer'
551 ApiBase
::PROP_TYPE
=> $wgLogTypes
556 'actionhidden' => 'boolean'
559 'userhidden' => 'boolean',
561 ApiBase
::PROP_TYPE
=> 'string',
562 ApiBase
::PROP_NULLABLE
=> true
567 'userhidden' => 'boolean',
569 ApiBase
::PROP_TYPE
=> 'integer',
570 ApiBase
::PROP_NULLABLE
=> true
574 'timestamp' => array(
575 'timestamp' => 'timestamp'
578 'commenthidden' => 'boolean',
580 ApiBase
::PROP_TYPE
=> 'string',
581 ApiBase
::PROP_NULLABLE
=> true
584 'parsedcomment' => array(
585 'commenthidden' => 'boolean',
586 'parsedcomment' => array(
587 ApiBase
::PROP_TYPE
=> 'string',
588 ApiBase
::PROP_NULLABLE
=> true
594 public function getDescription() {
595 return 'Get events from logs.';
598 public function getPossibleErrors() {
599 return array_merge( parent
::getPossibleErrors(), array(
600 array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
601 array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
602 array( 'code' => 'param_prefix', 'info' => 'Bad title value \'prefix\'' ),
603 array( 'code' => 'prefixsearchdisabled', 'info' => 'Prefix search disabled in Miser Mode' ),
607 public function getExamples() {
609 'api.php?action=query&list=logevents'
613 public function getHelpUrls() {
614 return 'https://www.mediawiki.org/wiki/API:Logevents';