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 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'] );
113 $this->addTimestampWhereRange(
120 $limit = $params['limit'];
121 $this->addOption( 'LIMIT', $limit +
1 );
123 $user = $params['user'];
124 if ( !is_null( $user ) ) {
125 $userid = User
::idFromName( $user );
127 $this->addWhereFld( 'log_user', $userid );
129 $this->addWhereFld( 'log_user_text', IP
::sanitizeIP( $user ) );
133 $title = $params['title'];
134 if ( !is_null( $title ) ) {
135 $titleObj = Title
::newFromText( $title );
136 if ( is_null( $titleObj ) ) {
137 $this->dieUsage( "Bad title value '$title'", 'param_title' );
139 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
140 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
143 $prefix = $params['prefix'];
145 if ( !is_null( $prefix ) ) {
147 if ( $wgMiserMode ) {
148 $this->dieUsage( 'Prefix search disabled in Miser Mode', 'prefixsearchdisabled' );
151 $title = Title
::newFromText( $prefix );
152 if ( is_null( $title ) ) {
153 $this->dieUsage( "Bad title value '$prefix'", 'param_prefix' );
155 $this->addWhereFld( 'log_namespace', $title->getNamespace() );
156 $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
159 // Paranoia: avoid brute force searches (bug 17342)
160 if ( !is_null( $title ) ||
!is_null( $user ) ) {
161 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
162 $titleBits = LogPage
::DELETED_ACTION
;
163 $userBits = LogPage
::DELETED_USER
;
164 } elseif ( !$this->getUser()->isAllowed( 'suppressrevision' ) ) {
165 $titleBits = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
166 $userBits = LogPage
::DELETED_USER | LogPage
::DELETED_RESTRICTED
;
171 if ( !is_null( $title ) && $titleBits ) {
172 $this->addWhere( $db->bitAnd( 'log_deleted', $titleBits ) . " != $titleBits" );
174 if ( !is_null( $user ) && $userBits ) {
175 $this->addWhere( $db->bitAnd( 'log_deleted', $userBits ) . " != $userBits" );
180 $res = $this->select( __METHOD__
);
181 $result = $this->getResult();
182 foreach ( $res as $row ) {
183 if ( ++
$count > $limit ) {
184 // We've reached the one extra which shows that there are
185 // additional pages to be had. Stop here...
186 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->log_timestamp
) );
190 $vals = $this->extractRowInfo( $row );
194 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
196 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->log_timestamp
) );
200 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
204 * @param $result ApiResult
206 * @param $params string
207 * @param $type string
208 * @param $action string
210 * @param $legacy bool
213 public static function addLogParams( $result, &$vals, $params, $type,
214 $action, $ts, $legacy = false
222 $targetKey = '4::target';
223 $noredirKey = '5::noredir';
226 if ( isset( $params[$targetKey] ) ) {
227 $title = Title
::newFromText( $params[$targetKey] );
230 ApiQueryBase
::addTitleInfo( $vals2, $title, 'new_' );
231 $vals[$type] = $vals2;
234 if ( isset( $params[$noredirKey] ) && $params[$noredirKey] ) {
235 $vals[$type]['suppressedredirect'] = '';
250 $vals2['cur'] = $params[$cur];
251 $vals2['prev'] = $params[$prev];
252 $vals2['auto'] = $params[$auto];
253 $vals[$type] = $vals2;
259 list( $vals2['old'], $vals2['new'] ) = $params;
261 $vals2['new'] = implode( ', ', $params['5::newgroups'] );
262 $vals2['old'] = implode( ', ', $params['4::oldgroups'] );
264 $vals[$type] = $vals2;
268 if ( $action == 'unblock' ) {
272 list( $vals2['duration'], $vals2['flags'] ) = $params;
274 // Indefinite blocks have no expiry time
275 if ( SpecialBlock
::parseExpiryInput( $params[0] ) !== wfGetDB( DB_SLAVE
)->getInfinity() ) {
276 $vals2['expiry'] = wfTimestamp( TS_ISO_8601
,
277 strtotime( $params[0], wfTimestamp( TS_UNIX
, $ts ) ) );
279 $vals[$type] = $vals2;
283 if ( isset( $params['img_timestamp'] ) ) {
284 $params['img_timestamp'] = wfTimestamp( TS_ISO_8601
, $params['img_timestamp'] );
288 if ( !is_null( $params ) ) {
289 $logParams = array();
290 // Keys like "4::paramname" can't be used for output so we change them to "paramname"
291 foreach ( $params as $key => $value ) {
292 if ( strpos( $key, ':' ) === false ) {
293 $logParams[$key] = $value;
296 $logParam = explode( ':', $key, 3 );
297 $logParams[$logParam[2]] = $value;
299 $result->setIndexedTagName( $logParams, 'param' );
300 $result->setIndexedTagName_recursive( $logParams, 'param' );
301 $vals = array_merge( $vals, $logParams );
307 private function extractRowInfo( $row ) {
308 $logEntry = DatabaseLogEntry
::newFromRow( $row );
311 $user = $this->getUser();
313 if ( $this->fld_ids
) {
314 $vals['logid'] = intval( $row->log_id
);
317 if ( $this->fld_title ||
$this->fld_parsedcomment
) {
318 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
321 if ( $this->fld_title ||
$this->fld_ids ||
$this->fld_details
&& $row->log_params
!== '' ) {
322 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
323 $vals['actionhidden'] = '';
326 if ( LogEventsList
::userCan( $row, LogPage
::DELETED_ACTION
, $user ) ) {
327 if ( $this->fld_title
) {
328 ApiQueryBase
::addTitleInfo( $vals, $title );
330 if ( $this->fld_ids
) {
331 $vals['pageid'] = intval( $row->page_id
);
333 if ( $this->fld_details
&& $row->log_params
!== '' ) {
337 $logEntry->getParameters(),
338 $logEntry->getType(),
339 $logEntry->getSubtype(),
340 $logEntry->getTimestamp(),
341 $logEntry->isLegacy()
347 if ( $this->fld_type ||
$this->fld_action
) {
348 $vals['type'] = $row->log_type
;
349 $vals['action'] = $row->log_action
;
352 if ( $this->fld_user ||
$this->fld_userid
) {
353 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_USER
) ) {
354 $vals['userhidden'] = '';
357 if ( LogEventsList
::userCan( $row, LogPage
::DELETED_USER
, $user ) ) {
358 if ( $this->fld_user
) {
359 $vals['user'] = $row->user_name
=== null ?
$row->log_user_text
: $row->user_name
;
361 if ( $this->fld_userid
) {
362 $vals['userid'] = $row->log_user
;
365 if ( !$row->log_user
) {
370 if ( $this->fld_timestamp
) {
371 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->log_timestamp
);
374 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) && isset( $row->log_comment
) ) {
375 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_COMMENT
) ) {
376 $vals['commenthidden'] = '';
379 if ( LogEventsList
::userCan( $row, LogPage
::DELETED_COMMENT
, $user ) ) {
380 if ( $this->fld_comment
) {
381 $vals['comment'] = $row->log_comment
;
384 if ( $this->fld_parsedcomment
) {
385 $vals['parsedcomment'] = Linker
::formatComment( $row->log_comment
, $title );
390 if ( $this->fld_tags
) {
391 if ( $row->ts_tags
) {
392 $tags = explode( ',', $row->ts_tags
);
393 $this->getResult()->setIndexedTagName( $tags, 'tag' );
394 $vals['tags'] = $tags;
396 $vals['tags'] = array();
400 if ( $anyHidden && LogEventsList
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
) ) {
401 $vals['suppressed'] = '';
407 public function getCacheMode( $params ) {
408 if ( $this->userCanSeeRevDel() ) {
411 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
412 // formatComment() calls wfMessage() among other things
413 return 'anon-public-user-private';
414 } elseif ( LogEventsList
::getExcludeClause( $this->getDB(), 'user', $this->getUser() )
415 === LogEventsList
::getExcludeClause( $this->getDB(), 'public' )
416 ) { // Output can only contain public data.
419 return 'anon-public-user-private';
423 public function getAllowedParams() {
424 global $wgLogTypes, $wgLogActions, $wgLogActionsHandlers;
428 ApiBase
::PARAM_ISMULTI
=> true,
429 ApiBase
::PARAM_DFLT
=> 'ids|title|type|user|timestamp|comment|details',
430 ApiBase
::PARAM_TYPE
=> array(
444 ApiBase
::PARAM_TYPE
=> $wgLogTypes
447 ApiBase
::PARAM_TYPE
=> array_keys( array_merge( $wgLogActions, $wgLogActionsHandlers ) )
450 ApiBase
::PARAM_TYPE
=> 'timestamp'
453 ApiBase
::PARAM_TYPE
=> 'timestamp'
456 ApiBase
::PARAM_DFLT
=> 'older',
457 ApiBase
::PARAM_TYPE
=> array(
467 ApiBase
::PARAM_DFLT
=> 10,
468 ApiBase
::PARAM_TYPE
=> 'limit',
469 ApiBase
::PARAM_MIN
=> 1,
470 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
471 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
476 public function getParamDescription() {
477 $p = $this->getModulePrefix();
481 'Which properties to get',
482 ' ids - Adds the ID of the log event',
483 ' title - Adds the title of the page for the log event',
484 ' type - Adds the type of log event',
485 ' user - Adds the user responsible for the log event',
486 ' userid - Adds the user ID who was responsible for the log event',
487 ' timestamp - Adds the timestamp for the event',
488 ' comment - Adds the comment of the event',
489 ' parsedcomment - Adds the parsed comment of the event',
490 ' details - Lists additional details about the event',
491 ' tags - Lists tags for the event',
493 'type' => 'Filter log entries to only this type',
494 'action' => "Filter log actions to only this type. Overrides {$p}type",
495 'start' => 'The timestamp to start enumerating from',
496 'end' => 'The timestamp to end enumerating',
497 'dir' => $this->getDirectionDescription( $p ),
498 'user' => 'Filter entries to those made by the given user',
499 'title' => 'Filter entries to those related to a page',
500 'prefix' => 'Filter entries that start with this prefix. Disabled in Miser Mode',
501 'limit' => 'How many total event entries to return',
502 'tag' => 'Only list event entries tagged with this tag',
506 public function getResultProperties() {
511 'logid' => 'integer',
512 'pageid' => 'integer'
520 ApiBase
::PROP_TYPE
=> $wgLogTypes
525 'actionhidden' => 'boolean'
528 'userhidden' => 'boolean',
530 ApiBase
::PROP_TYPE
=> 'string',
531 ApiBase
::PROP_NULLABLE
=> true
536 'userhidden' => 'boolean',
538 ApiBase
::PROP_TYPE
=> 'integer',
539 ApiBase
::PROP_NULLABLE
=> true
543 'timestamp' => array(
544 'timestamp' => 'timestamp'
547 'commenthidden' => 'boolean',
549 ApiBase
::PROP_TYPE
=> 'string',
550 ApiBase
::PROP_NULLABLE
=> true
553 'parsedcomment' => array(
554 'commenthidden' => 'boolean',
555 'parsedcomment' => array(
556 ApiBase
::PROP_TYPE
=> 'string',
557 ApiBase
::PROP_NULLABLE
=> true
563 public function getDescription() {
564 return 'Get events from logs';
567 public function getPossibleErrors() {
568 return array_merge( parent
::getPossibleErrors(), array(
569 array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
570 array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
571 array( 'code' => 'param_prefix', 'info' => 'Bad title value \'prefix\'' ),
572 array( 'code' => 'prefixsearchdisabled', 'info' => 'Prefix search disabled in Miser Mode' ),
576 public function getExamples() {
578 'api.php?action=query&list=logevents'
582 public function getHelpUrls() {
583 return 'https://www.mediawiki.org/wiki/API:Logevents';