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(
83 $this->addFieldsIf( '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 // Do validation of action param, list of allowed actions can contains wildcards
108 // Allow the param, when the actions is in the list or a wildcard version is listed.
109 $logAction = $params['action'];
110 if ( strpos( $logAction, '/' ) === false ) {
111 // all items in the list have a slash
114 $logActions = array_flip( $this->getAllowedLogActions() );
115 list( $type, $action ) = explode( '/', $logAction, 2 );
116 $valid = isset( $logActions[$logAction] ) ||
isset( $logActions[$type . '/*'] );
120 $valueName = $this->encodeParamName( 'action' );
122 "Unrecognized value for parameter '$valueName': {$logAction}",
127 $this->addWhereFld( 'log_type', $type );
128 $this->addWhereFld( 'log_action', $action );
129 } elseif ( !is_null( $params['type'] ) ) {
130 $this->addWhereFld( 'log_type', $params['type'] );
133 $this->addTimestampWhereRange(
139 // Include in ORDER BY for uniqueness
140 $this->addWhereRange( 'log_id', $params['dir'], null, null );
142 if ( !is_null( $params['continue'] ) ) {
143 $cont = explode( '|', $params['continue'] );
144 $this->dieContinueUsageIf( count( $cont ) != 2 );
145 $op = ( $params['dir'] === 'newer' ?
'>' : '<' );
146 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
147 $continueId = (int)$cont[1];
148 $this->dieContinueUsageIf( $continueId != $cont[1] );
149 $this->addWhere( "log_timestamp $op $continueTimestamp OR " .
150 "(log_timestamp = $continueTimestamp AND " .
151 "log_id $op= $continueId)"
155 $limit = $params['limit'];
156 $this->addOption( 'LIMIT', $limit +
1 );
158 $user = $params['user'];
159 if ( !is_null( $user ) ) {
160 $userid = User
::idFromName( $user );
162 $this->addWhereFld( 'log_user', $userid );
164 $this->addWhereFld( 'log_user_text', IP
::sanitizeIP( $user ) );
168 $title = $params['title'];
169 if ( !is_null( $title ) ) {
170 $titleObj = Title
::newFromText( $title );
171 if ( is_null( $titleObj ) ) {
172 $this->dieUsage( "Bad title value '$title'", 'param_title' );
174 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
175 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
178 $prefix = $params['prefix'];
180 if ( !is_null( $prefix ) ) {
182 if ( $wgMiserMode ) {
183 $this->dieUsage( 'Prefix search disabled in Miser Mode', 'prefixsearchdisabled' );
186 $title = Title
::newFromText( $prefix );
187 if ( is_null( $title ) ) {
188 $this->dieUsage( "Bad title value '$prefix'", 'param_prefix' );
190 $this->addWhereFld( 'log_namespace', $title->getNamespace() );
191 $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
194 // Paranoia: avoid brute force searches (bug 17342)
195 if ( !is_null( $title ) ||
!is_null( $user ) ) {
196 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
197 $titleBits = LogPage
::DELETED_ACTION
;
198 $userBits = LogPage
::DELETED_USER
;
199 } elseif ( !$this->getUser()->isAllowed( 'suppressrevision' ) ) {
200 $titleBits = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
201 $userBits = LogPage
::DELETED_USER | LogPage
::DELETED_RESTRICTED
;
206 if ( !is_null( $title ) && $titleBits ) {
207 $this->addWhere( $db->bitAnd( 'log_deleted', $titleBits ) . " != $titleBits" );
209 if ( !is_null( $user ) && $userBits ) {
210 $this->addWhere( $db->bitAnd( 'log_deleted', $userBits ) . " != $userBits" );
215 $res = $this->select( __METHOD__
);
216 $result = $this->getResult();
217 foreach ( $res as $row ) {
218 if ( ++
$count > $limit ) {
219 // We've reached the one extra which shows that there are
220 // additional pages to be had. Stop here...
221 $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" );
225 $vals = $this->extractRowInfo( $row );
229 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
231 $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" );
235 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
239 * @param ApiResult $result
241 * @param string $params
242 * @param string $type
243 * @param string $action
245 * @param bool $legacy
248 public static function addLogParams( $result, &$vals, $params, $type,
249 $action, $ts, $legacy = false
257 $targetKey = '4::target';
258 $noredirKey = '5::noredir';
261 if ( isset( $params[$targetKey] ) ) {
262 $title = Title
::newFromText( $params[$targetKey] );
265 ApiQueryBase
::addTitleInfo( $vals2, $title, 'new_' );
266 $vals[$type] = $vals2;
269 if ( isset( $params[$noredirKey] ) && $params[$noredirKey] ) {
270 $vals[$type]['suppressedredirect'] = '';
285 $vals2['cur'] = $params[$cur];
286 $vals2['prev'] = $params[$prev];
287 $vals2['auto'] = $params[$auto];
288 $vals[$type] = $vals2;
294 list( $vals2['old'], $vals2['new'] ) = $params;
296 $vals2['new'] = implode( ', ', $params['5::newgroups'] );
297 $vals2['old'] = implode( ', ', $params['4::oldgroups'] );
299 $vals[$type] = $vals2;
303 if ( $action == 'unblock' ) {
307 list( $vals2['duration'], $vals2['flags'] ) = $params;
309 // Indefinite blocks have no expiry time
310 if ( SpecialBlock
::parseExpiryInput( $params[0] ) !== wfGetDB( DB_SLAVE
)->getInfinity() ) {
311 $vals2['expiry'] = wfTimestamp( TS_ISO_8601
,
312 strtotime( $params[0], wfTimestamp( TS_UNIX
, $ts ) ) );
314 $vals[$type] = $vals2;
318 if ( isset( $params['img_timestamp'] ) ) {
319 $params['img_timestamp'] = wfTimestamp( TS_ISO_8601
, $params['img_timestamp'] );
323 if ( !is_null( $params ) ) {
324 $logParams = array();
325 // Keys like "4::paramname" can't be used for output so we change them to "paramname"
326 foreach ( $params as $key => $value ) {
327 if ( strpos( $key, ':' ) === false ) {
328 $logParams[$key] = $value;
331 $logParam = explode( ':', $key, 3 );
332 $logParams[$logParam[2]] = $value;
334 $result->setIndexedTagName( $logParams, 'param' );
335 $result->setIndexedTagName_recursive( $logParams, 'param' );
336 $vals = array_merge( $vals, $logParams );
342 private function extractRowInfo( $row ) {
343 $logEntry = DatabaseLogEntry
::newFromRow( $row );
346 $user = $this->getUser();
348 if ( $this->fld_ids
) {
349 $vals['logid'] = intval( $row->log_id
);
352 if ( $this->fld_title ||
$this->fld_parsedcomment
) {
353 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
356 if ( $this->fld_title ||
$this->fld_ids ||
$this->fld_details
&& $row->log_params
!== '' ) {
357 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
358 $vals['actionhidden'] = '';
361 if ( LogEventsList
::userCan( $row, LogPage
::DELETED_ACTION
, $user ) ) {
362 if ( $this->fld_title
) {
363 ApiQueryBase
::addTitleInfo( $vals, $title );
365 if ( $this->fld_ids
) {
366 $vals['pageid'] = intval( $row->page_id
);
368 if ( $this->fld_details
&& $row->log_params
!== '' ) {
372 $logEntry->getParameters(),
373 $logEntry->getType(),
374 $logEntry->getSubtype(),
375 $logEntry->getTimestamp(),
376 $logEntry->isLegacy()
382 if ( $this->fld_type ||
$this->fld_action
) {
383 $vals['type'] = $row->log_type
;
384 $vals['action'] = $row->log_action
;
387 if ( $this->fld_user ||
$this->fld_userid
) {
388 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_USER
) ) {
389 $vals['userhidden'] = '';
392 if ( LogEventsList
::userCan( $row, LogPage
::DELETED_USER
, $user ) ) {
393 if ( $this->fld_user
) {
394 $vals['user'] = $row->user_name
=== null ?
$row->log_user_text
: $row->user_name
;
396 if ( $this->fld_userid
) {
397 $vals['userid'] = $row->log_user
;
400 if ( !$row->log_user
) {
405 if ( $this->fld_timestamp
) {
406 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->log_timestamp
);
409 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) && isset( $row->log_comment
) ) {
410 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_COMMENT
) ) {
411 $vals['commenthidden'] = '';
414 if ( LogEventsList
::userCan( $row, LogPage
::DELETED_COMMENT
, $user ) ) {
415 if ( $this->fld_comment
) {
416 $vals['comment'] = $row->log_comment
;
419 if ( $this->fld_parsedcomment
) {
420 $vals['parsedcomment'] = Linker
::formatComment( $row->log_comment
, $title );
425 if ( $this->fld_tags
) {
426 if ( $row->ts_tags
) {
427 $tags = explode( ',', $row->ts_tags
);
428 $this->getResult()->setIndexedTagName( $tags, 'tag' );
429 $vals['tags'] = $tags;
431 $vals['tags'] = array();
435 if ( $anyHidden && LogEventsList
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
) ) {
436 $vals['suppressed'] = '';
442 private function getAllowedLogActions() {
443 global $wgLogActions, $wgLogActionsHandlers;
445 return array_keys( array_merge( $wgLogActions, $wgLogActionsHandlers ) );
448 public function getCacheMode( $params ) {
449 if ( $this->userCanSeeRevDel() ) {
452 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
453 // formatComment() calls wfMessage() among other things
454 return 'anon-public-user-private';
455 } elseif ( LogEventsList
::getExcludeClause( $this->getDB(), 'user', $this->getUser() )
456 === LogEventsList
::getExcludeClause( $this->getDB(), 'public' )
457 ) { // Output can only contain public data.
460 return 'anon-public-user-private';
464 public function getAllowedParams( $flags = 0 ) {
469 ApiBase
::PARAM_ISMULTI
=> true,
470 ApiBase
::PARAM_DFLT
=> 'ids|title|type|user|timestamp|comment|details',
471 ApiBase
::PARAM_TYPE
=> array(
485 ApiBase
::PARAM_TYPE
=> $wgLogTypes
488 // validation on request is done in execute()
489 ApiBase
::PARAM_TYPE
=> ( $flags & ApiBase
::GET_VALUES_FOR_HELP
)
490 ?
$this->getAllowedLogActions()
494 ApiBase
::PARAM_TYPE
=> 'timestamp'
497 ApiBase
::PARAM_TYPE
=> 'timestamp'
500 ApiBase
::PARAM_DFLT
=> 'older',
501 ApiBase
::PARAM_TYPE
=> array(
511 ApiBase
::PARAM_DFLT
=> 10,
512 ApiBase
::PARAM_TYPE
=> 'limit',
513 ApiBase
::PARAM_MIN
=> 1,
514 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
515 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
521 public function getParamDescription() {
522 $p = $this->getModulePrefix();
526 'Which properties to get',
527 ' ids - Adds the ID of the log event',
528 ' title - Adds the title of the page for the log event',
529 ' type - Adds the type of log event',
530 ' user - Adds the user responsible for the log event',
531 ' userid - Adds the user ID who was responsible for the log event',
532 ' timestamp - Adds the timestamp for the event',
533 ' comment - Adds the comment of the event',
534 ' parsedcomment - Adds the parsed comment of the event',
535 ' details - Lists additional details about the event',
536 ' tags - Lists tags for the event',
538 'type' => 'Filter log entries to only this type',
540 "Filter log actions to only this action. Overrides {$p}type",
541 "Wildcard actions like 'action/*' allows to specify any string for the asterisk"
543 'start' => 'The timestamp to start enumerating from',
544 'end' => 'The timestamp to end enumerating',
545 'dir' => $this->getDirectionDescription( $p ),
546 'user' => 'Filter entries to those made by the given user',
547 'title' => 'Filter entries to those related to a page',
548 'prefix' => 'Filter entries that start with this prefix. Disabled in Miser Mode',
549 'limit' => 'How many total event entries to return',
550 'tag' => 'Only list event entries tagged with this tag',
551 'continue' => 'When more results are available, use this to continue',
555 public function getResultProperties() {
560 'logid' => 'integer',
561 'pageid' => 'integer'
569 ApiBase
::PROP_TYPE
=> $wgLogTypes
574 'actionhidden' => 'boolean'
577 'userhidden' => 'boolean',
579 ApiBase
::PROP_TYPE
=> 'string',
580 ApiBase
::PROP_NULLABLE
=> true
585 'userhidden' => 'boolean',
587 ApiBase
::PROP_TYPE
=> 'integer',
588 ApiBase
::PROP_NULLABLE
=> true
592 'timestamp' => array(
593 'timestamp' => 'timestamp'
596 'commenthidden' => 'boolean',
598 ApiBase
::PROP_TYPE
=> 'string',
599 ApiBase
::PROP_NULLABLE
=> true
602 'parsedcomment' => array(
603 'commenthidden' => 'boolean',
604 'parsedcomment' => array(
605 ApiBase
::PROP_TYPE
=> 'string',
606 ApiBase
::PROP_NULLABLE
=> true
612 public function getDescription() {
613 return 'Get events from logs.';
616 public function getPossibleErrors() {
617 return array_merge( parent
::getPossibleErrors(), array(
618 array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
619 array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
620 array( 'code' => 'param_prefix', 'info' => 'Bad title value \'prefix\'' ),
621 array( 'code' => 'prefixsearchdisabled', 'info' => 'Prefix search disabled in Miser Mode' ),
625 public function getExamples() {
627 'api.php?action=query&list=logevents'
631 public function getHelpUrls() {
632 return 'https://www.mediawiki.org/wiki/API:Logevents';