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( ApiQuery
$query, $moduleName ) {
35 parent
::__construct( $query, $moduleName, 'le' );
38 private $fld_ids = false, $fld_title = false, $fld_type = false,
39 $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();
46 $this->requireMaxOneParameter( $params, 'title', 'prefix', 'namespace' );
48 $prop = array_flip( $params['prop'] );
50 $this->fld_ids
= isset( $prop['ids'] );
51 $this->fld_title
= isset( $prop['title'] );
52 $this->fld_type
= isset( $prop['type'] );
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 // log_page is the page_id saved at log time, whereas page_id is from a
85 // join at query time. This leads to different results in various
86 // scenarios, e.g. deletion, recreation.
87 $this->addFieldsIf( 'log_page', $this->fld_ids
);
88 $this->addFieldsIf( array( 'log_user', 'log_user_text', 'user_name' ), $this->fld_user
);
89 $this->addFieldsIf( 'log_user', $this->fld_userid
);
91 array( 'log_namespace', 'log_title' ),
92 $this->fld_title ||
$this->fld_parsedcomment
94 $this->addFieldsIf( 'log_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
95 $this->addFieldsIf( 'log_params', $this->fld_details
);
97 if ( $this->fld_tags
) {
98 $this->addTables( 'tag_summary' );
99 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', 'log_id=ts_log_id' ) ) );
100 $this->addFields( 'ts_tags' );
103 if ( !is_null( $params['tag'] ) ) {
104 $this->addTables( 'change_tag' );
105 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN',
106 array( 'log_id=ct_log_id' ) ) ) );
107 $this->addWhereFld( 'ct_tag', $params['tag'] );
110 if ( !is_null( $params['action'] ) ) {
111 // Do validation of action param, list of allowed actions can contains wildcards
112 // Allow the param, when the actions is in the list or a wildcard version is listed.
113 $logAction = $params['action'];
114 if ( strpos( $logAction, '/' ) === false ) {
115 // all items in the list have a slash
118 $logActions = array_flip( $this->getAllowedLogActions() );
119 list( $type, $action ) = explode( '/', $logAction, 2 );
120 $valid = isset( $logActions[$logAction] ) ||
isset( $logActions[$type . '/*'] );
124 $valueName = $this->encodeParamName( 'action' );
126 "Unrecognized value for parameter '$valueName': {$logAction}",
131 $this->addWhereFld( 'log_type', $type );
132 $this->addWhereFld( 'log_action', $action );
133 } elseif ( !is_null( $params['type'] ) ) {
134 $this->addWhereFld( 'log_type', $params['type'] );
137 $this->addTimestampWhereRange(
143 // Include in ORDER BY for uniqueness
144 $this->addWhereRange( 'log_id', $params['dir'], null, null );
146 if ( !is_null( $params['continue'] ) ) {
147 $cont = explode( '|', $params['continue'] );
148 $this->dieContinueUsageIf( count( $cont ) != 2 );
149 $op = ( $params['dir'] === 'newer' ?
'>' : '<' );
150 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
151 $continueId = (int)$cont[1];
152 $this->dieContinueUsageIf( $continueId != $cont[1] );
153 $this->addWhere( "log_timestamp $op $continueTimestamp OR " .
154 "(log_timestamp = $continueTimestamp AND " .
155 "log_id $op= $continueId)"
159 $limit = $params['limit'];
160 $this->addOption( 'LIMIT', $limit +
1 );
162 $user = $params['user'];
163 if ( !is_null( $user ) ) {
164 $userid = User
::idFromName( $user );
166 $this->addWhereFld( 'log_user', $userid );
168 $this->addWhereFld( 'log_user_text', IP
::sanitizeIP( $user ) );
172 $title = $params['title'];
173 if ( !is_null( $title ) ) {
174 $titleObj = Title
::newFromText( $title );
175 if ( is_null( $titleObj ) ) {
176 $this->dieUsage( "Bad title value '$title'", 'param_title' );
178 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
179 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
182 if ( $params['namespace'] !== null ) {
183 $this->addWhereFld( 'log_namespace', $params['namespace'] );
186 $prefix = $params['prefix'];
188 if ( !is_null( $prefix ) ) {
189 if ( $this->getConfig()->get( 'MiserMode' ) ) {
190 $this->dieUsage( 'Prefix search disabled in Miser Mode', 'prefixsearchdisabled' );
193 $title = Title
::newFromText( $prefix );
194 if ( is_null( $title ) ) {
195 $this->dieUsage( "Bad title value '$prefix'", 'param_prefix' );
197 $this->addWhereFld( 'log_namespace', $title->getNamespace() );
198 $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
201 // Paranoia: avoid brute force searches (bug 17342)
202 if ( $params['namespace'] !== null ||
!is_null( $title ) ||
!is_null( $user ) ) {
203 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
204 $titleBits = LogPage
::DELETED_ACTION
;
205 $userBits = LogPage
::DELETED_USER
;
206 } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
207 $titleBits = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
208 $userBits = LogPage
::DELETED_USER | LogPage
::DELETED_RESTRICTED
;
213 if ( ( $params['namespace'] !== null ||
!is_null( $title ) ) && $titleBits ) {
214 $this->addWhere( $db->bitAnd( 'log_deleted', $titleBits ) . " != $titleBits" );
216 if ( !is_null( $user ) && $userBits ) {
217 $this->addWhere( $db->bitAnd( 'log_deleted', $userBits ) . " != $userBits" );
222 $res = $this->select( __METHOD__
);
223 $result = $this->getResult();
224 foreach ( $res as $row ) {
225 if ( ++
$count > $limit ) {
226 // We've reached the one extra which shows that there are
227 // additional pages to be had. Stop here...
228 $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" );
232 $vals = $this->extractRowInfo( $row );
236 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
238 $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" );
242 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
246 * @param ApiResult $result
248 * @param string $params
249 * @param string $type
250 * @param string $action
252 * @param bool $legacy
255 public static function addLogParams( $result, &$vals, $params, $type,
256 $action, $ts, $legacy = false
264 $targetKey = '4::target';
265 $noredirKey = '5::noredir';
268 if ( isset( $params[$targetKey] ) ) {
269 $title = Title
::newFromText( $params[$targetKey] );
272 ApiQueryBase
::addTitleInfo( $vals2, $title, 'new_' );
273 $vals[$type] = $vals2;
276 if ( isset( $params[$noredirKey] ) && $params[$noredirKey] ) {
277 $vals[$type]['suppressedredirect'] = '';
292 $vals2['cur'] = $params[$cur];
293 $vals2['prev'] = $params[$prev];
294 $vals2['auto'] = $params[$auto];
295 $vals[$type] = $vals2;
301 list( $vals2['old'], $vals2['new'] ) = $params;
303 $vals2['new'] = implode( ', ', $params['5::newgroups'] );
304 $vals2['old'] = implode( ', ', $params['4::oldgroups'] );
306 $vals[$type] = $vals2;
310 if ( $action == 'unblock' ) {
314 $vals2['duration'] = $params[0];
315 $vals2['flags'] = isset( $params[1] ) ?
$params[1] : '';
317 // Indefinite blocks have no expiry time
318 if ( SpecialBlock
::parseExpiryInput( $params[0] ) !== wfGetDB( DB_SLAVE
)->getInfinity() ) {
319 $vals2['expiry'] = wfTimestamp( TS_ISO_8601
,
320 strtotime( $params[0], wfTimestamp( TS_UNIX
, $ts ) ) );
322 $vals[$type] = $vals2;
326 if ( isset( $params['img_timestamp'] ) ) {
327 $params['img_timestamp'] = wfTimestamp( TS_ISO_8601
, $params['img_timestamp'] );
331 // replace the named parameter with numbered for backward compatibility
332 if ( isset( $params['4::dest'] ) ) {
333 $params[] = $params['4::dest'];
334 unset( $params['4::dest'] );
336 if ( isset( $params['5::mergepoint'] ) ) {
337 $params[] = $params['5::mergepoint'];
338 unset( $params['5::mergepoint'] );
342 if ( !is_null( $params ) ) {
343 $logParams = array();
344 // Keys like "4::paramname" can't be used for output so we change them to "paramname"
345 foreach ( $params as $key => $value ) {
346 if ( strpos( $key, ':' ) === false ) {
347 $logParams[$key] = $value;
350 $logParam = explode( ':', $key, 3 );
351 $logParams[$logParam[2]] = $value;
353 $result->setIndexedTagName( $logParams, 'param' );
354 $result->setIndexedTagName_recursive( $logParams, 'param' );
355 $vals = array_merge( $vals, $logParams );
361 private function extractRowInfo( $row ) {
362 $logEntry = DatabaseLogEntry
::newFromRow( $row );
365 $user = $this->getUser();
367 if ( $this->fld_ids
) {
368 $vals['logid'] = intval( $row->log_id
);
371 if ( $this->fld_title ||
$this->fld_parsedcomment
) {
372 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
375 if ( $this->fld_title ||
$this->fld_ids ||
$this->fld_details
&& $row->log_params
!== '' ) {
376 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
377 $vals['actionhidden'] = '';
380 if ( LogEventsList
::userCan( $row, LogPage
::DELETED_ACTION
, $user ) ) {
381 if ( $this->fld_title
) {
382 ApiQueryBase
::addTitleInfo( $vals, $title );
384 if ( $this->fld_ids
) {
385 $vals['pageid'] = intval( $row->page_id
);
386 $vals['logpage'] = intval( $row->log_page
);
388 if ( $this->fld_details
&& $row->log_params
!== '' ) {
392 $logEntry->getParameters(),
393 $logEntry->getType(),
394 $logEntry->getSubtype(),
395 $logEntry->getTimestamp(),
396 $logEntry->isLegacy()
402 if ( $this->fld_type
) {
403 $vals['type'] = $row->log_type
;
404 $vals['action'] = $row->log_action
;
407 if ( $this->fld_user ||
$this->fld_userid
) {
408 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_USER
) ) {
409 $vals['userhidden'] = '';
412 if ( LogEventsList
::userCan( $row, LogPage
::DELETED_USER
, $user ) ) {
413 if ( $this->fld_user
) {
414 $vals['user'] = $row->user_name
=== null ?
$row->log_user_text
: $row->user_name
;
416 if ( $this->fld_userid
) {
417 $vals['userid'] = intval( $row->log_user
);
420 if ( !$row->log_user
) {
425 if ( $this->fld_timestamp
) {
426 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->log_timestamp
);
429 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) && isset( $row->log_comment
) ) {
430 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_COMMENT
) ) {
431 $vals['commenthidden'] = '';
434 if ( LogEventsList
::userCan( $row, LogPage
::DELETED_COMMENT
, $user ) ) {
435 if ( $this->fld_comment
) {
436 $vals['comment'] = $row->log_comment
;
439 if ( $this->fld_parsedcomment
) {
440 $vals['parsedcomment'] = Linker
::formatComment( $row->log_comment
, $title );
445 if ( $this->fld_tags
) {
446 if ( $row->ts_tags
) {
447 $tags = explode( ',', $row->ts_tags
);
448 $this->getResult()->setIndexedTagName( $tags, 'tag' );
449 $vals['tags'] = $tags;
451 $vals['tags'] = array();
455 if ( $anyHidden && LogEventsList
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
) ) {
456 $vals['suppressed'] = '';
465 private function getAllowedLogActions() {
466 $config = $this->getConfig();
467 return array_keys( array_merge( $config->get( 'LogActions' ), $config->get( 'LogActionsHandlers' ) ) );
470 public function getCacheMode( $params ) {
471 if ( $this->userCanSeeRevDel() ) {
474 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
475 // formatComment() calls wfMessage() among other things
476 return 'anon-public-user-private';
477 } elseif ( LogEventsList
::getExcludeClause( $this->getDB(), 'user', $this->getUser() )
478 === LogEventsList
::getExcludeClause( $this->getDB(), 'public' )
479 ) { // Output can only contain public data.
482 return 'anon-public-user-private';
486 public function getAllowedParams( $flags = 0 ) {
487 $config = $this->getConfig();
490 ApiBase
::PARAM_ISMULTI
=> true,
491 ApiBase
::PARAM_DFLT
=> 'ids|title|type|user|timestamp|comment|details',
492 ApiBase
::PARAM_TYPE
=> array(
506 ApiBase
::PARAM_TYPE
=> $config->get( 'LogTypes' )
509 // validation on request is done in execute()
510 ApiBase
::PARAM_TYPE
=> ( $flags & ApiBase
::GET_VALUES_FOR_HELP
)
511 ?
$this->getAllowedLogActions()
515 ApiBase
::PARAM_TYPE
=> 'timestamp'
518 ApiBase
::PARAM_TYPE
=> 'timestamp'
521 ApiBase
::PARAM_DFLT
=> 'older',
522 ApiBase
::PARAM_TYPE
=> array(
526 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
530 'namespace' => array(
531 ApiBase
::PARAM_TYPE
=> 'namespace'
536 ApiBase
::PARAM_DFLT
=> 10,
537 ApiBase
::PARAM_TYPE
=> 'limit',
538 ApiBase
::PARAM_MIN
=> 1,
539 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
540 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
543 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
547 if ( $config->get( 'MiserMode' ) ) {
548 $ret['prefix'][ApiBase
::PARAM_HELP_MSG
] = 'api-help-param-disabled-in-miser-mode';
554 protected function getExamplesMessages() {
556 'action=query&list=logevents'
557 => 'apihelp-query+logevents-example-simple',
561 public function getHelpUrls() {
562 return 'https://www.mediawiki.org/wiki/API:Logevents';