3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * Query action to List the log events, with optional filtering by various parameters.
28 class ApiQueryLogEvents
extends ApiQueryBase
{
30 private $commentStore;
32 public function __construct( ApiQuery
$query, $moduleName ) {
33 parent
::__construct( $query, $moduleName, 'le' );
36 private $fld_ids = false, $fld_title = false, $fld_type = false,
37 $fld_user = false, $fld_userid = false,
38 $fld_timestamp = false, $fld_comment = false, $fld_parsedcomment = false,
39 $fld_details = false, $fld_tags = false;
41 public function execute() {
42 $params = $this->extractRequestParams();
44 $this->commentStore
= CommentStore
::getStore();
45 $this->requireMaxOneParameter( $params, 'title', 'prefix', 'namespace' );
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_user
= isset( $prop['user'] );
53 $this->fld_userid
= isset( $prop['userid'] );
54 $this->fld_timestamp
= isset( $prop['timestamp'] );
55 $this->fld_comment
= isset( $prop['comment'] );
56 $this->fld_parsedcomment
= isset( $prop['parsedcomment'] );
57 $this->fld_details
= isset( $prop['details'] );
58 $this->fld_tags
= isset( $prop['tags'] );
60 $hideLogs = LogEventsList
::getExcludeClause( $db, 'user', $this->getUser() );
61 if ( $hideLogs !== false ) {
62 $this->addWhere( $hideLogs );
65 $actorMigration = ActorMigration
::newMigration();
66 $actorQuery = $actorMigration->getJoin( 'log_user' );
67 $this->addTables( 'logging' );
68 $this->addTables( $actorQuery['tables'] );
69 $this->addTables( [ 'user', 'page' ] );
70 $this->addJoinConds( $actorQuery['joins'] );
71 $this->addJoinConds( [
72 'user' => [ 'LEFT JOIN',
73 'user_id=' . $actorQuery['fields']['log_user'] ],
74 'page' => [ 'LEFT JOIN',
75 [ 'log_namespace=page_namespace',
76 'log_title=page_title' ] ] ] );
86 $this->addFieldsIf( 'page_id', $this->fld_ids
);
87 // log_page is the page_id saved at log time, whereas page_id is from a
88 // join at query time. This leads to different results in various
89 // scenarios, e.g. deletion, recreation.
90 $this->addFieldsIf( 'log_page', $this->fld_ids
);
91 $this->addFieldsIf( $actorQuery['fields'] +
[ 'user_name' ], $this->fld_user
);
92 $this->addFieldsIf( $actorQuery['fields'], $this->fld_userid
);
94 [ 'log_namespace', 'log_title' ],
95 $this->fld_title ||
$this->fld_parsedcomment
97 $this->addFieldsIf( 'log_params', $this->fld_details
);
99 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
100 $commentQuery = $this->commentStore
->getJoin( 'log_comment' );
101 $this->addTables( $commentQuery['tables'] );
102 $this->addFields( $commentQuery['fields'] );
103 $this->addJoinConds( $commentQuery['joins'] );
106 if ( $this->fld_tags
) {
107 $this->addTables( 'tag_summary' );
108 $this->addJoinConds( [ 'tag_summary' => [ 'LEFT JOIN', 'log_id=ts_log_id' ] ] );
109 $this->addFields( 'ts_tags' );
112 if ( !is_null( $params['tag'] ) ) {
113 $this->addTables( 'change_tag' );
114 $this->addJoinConds( [ 'change_tag' => [ 'INNER JOIN',
115 [ 'log_id=ct_log_id' ] ] ] );
116 $this->addWhereFld( 'ct_tag', $params['tag'] );
119 if ( !is_null( $params['action'] ) ) {
120 // Do validation of action param, list of allowed actions can contains wildcards
121 // Allow the param, when the actions is in the list or a wildcard version is listed.
122 $logAction = $params['action'];
123 if ( strpos( $logAction, '/' ) === false ) {
124 // all items in the list have a slash
127 $logActions = array_flip( $this->getAllowedLogActions() );
128 list( $type, $action ) = explode( '/', $logAction, 2 );
129 $valid = isset( $logActions[$logAction] ) ||
isset( $logActions[$type . '/*'] );
133 $encParamName = $this->encodeParamName( 'action' );
135 [ 'apierror-unrecognizedvalue', $encParamName, wfEscapeWikiText( $logAction ) ],
136 "unknown_$encParamName"
140 $this->addWhereFld( 'log_type', $type );
141 $this->addWhereFld( 'log_action', $action );
142 } elseif ( !is_null( $params['type'] ) ) {
143 $this->addWhereFld( 'log_type', $params['type'] );
146 $this->addTimestampWhereRange(
152 // Include in ORDER BY for uniqueness
153 $this->addWhereRange( 'log_id', $params['dir'], null, null );
155 if ( !is_null( $params['continue'] ) ) {
156 $cont = explode( '|', $params['continue'] );
157 $this->dieContinueUsageIf( count( $cont ) != 2 );
158 $op = ( $params['dir'] === 'newer' ?
'>' : '<' );
159 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
160 $continueId = (int)$cont[1];
161 $this->dieContinueUsageIf( $continueId != $cont[1] );
162 $this->addWhere( "log_timestamp $op $continueTimestamp OR " .
163 "(log_timestamp = $continueTimestamp AND " .
164 "log_id $op= $continueId)"
168 $limit = $params['limit'];
169 $this->addOption( 'LIMIT', $limit +
1 );
171 $user = $params['user'];
172 if ( !is_null( $user ) ) {
173 // Note the joins in $q are the same as those from ->getJoin() above
174 // so we only need to add 'conds' here.
175 // Don't query by user ID here, it might be able to use the
176 // log_user_text_time or log_user_text_type_time index.
177 $q = $actorMigration->getWhere(
178 $db, 'log_user', User
::newFromName( $params['user'], false ), false
180 $this->addWhere( $q['conds'] );
183 $title = $params['title'];
184 if ( !is_null( $title ) ) {
185 $titleObj = Title
::newFromText( $title );
186 if ( is_null( $titleObj ) ) {
187 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $title ) ] );
189 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
190 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
193 if ( $params['namespace'] !== null ) {
194 $this->addWhereFld( 'log_namespace', $params['namespace'] );
197 $prefix = $params['prefix'];
199 if ( !is_null( $prefix ) ) {
200 if ( $this->getConfig()->get( 'MiserMode' ) ) {
201 $this->dieWithError( 'apierror-prefixsearchdisabled' );
204 $title = Title
::newFromText( $prefix );
205 if ( is_null( $title ) ) {
206 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $prefix ) ] );
208 $this->addWhereFld( 'log_namespace', $title->getNamespace() );
209 $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
212 // Paranoia: avoid brute force searches (T19342)
213 if ( $params['namespace'] !== null ||
!is_null( $title ) ||
!is_null( $user ) ) {
214 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
215 $titleBits = LogPage
::DELETED_ACTION
;
216 $userBits = LogPage
::DELETED_USER
;
217 } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
218 $titleBits = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
219 $userBits = LogPage
::DELETED_USER | LogPage
::DELETED_RESTRICTED
;
224 if ( ( $params['namespace'] !== null ||
!is_null( $title ) ) && $titleBits ) {
225 $this->addWhere( $db->bitAnd( 'log_deleted', $titleBits ) . " != $titleBits" );
227 if ( !is_null( $user ) && $userBits ) {
228 $this->addWhere( $db->bitAnd( 'log_deleted', $userBits ) . " != $userBits" );
233 $res = $this->select( __METHOD__
);
234 $result = $this->getResult();
235 foreach ( $res as $row ) {
236 if ( ++
$count > $limit ) {
237 // We've reached the one extra which shows that there are
238 // additional pages to be had. Stop here...
239 $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" );
243 $vals = $this->extractRowInfo( $row );
244 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
246 $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" );
250 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'item' );
254 * @deprecated since 1.25 Use LogFormatter::formatParametersForApi instead
255 * @param ApiResult $result
256 * @param array &$vals
257 * @param string $params
258 * @param string $type
259 * @param string $action
261 * @param bool $legacy
264 public static function addLogParams( $result, &$vals, $params, $type,
265 $action, $ts, $legacy = false
267 wfDeprecated( __METHOD__
, '1.25' );
269 $entry = new ManualLogEntry( $type, $action );
270 $entry->setParameters( $params );
271 $entry->setTimestamp( $ts );
272 $entry->setLegacy( $legacy );
273 $formatter = LogFormatter
::newFromEntry( $entry );
274 $vals['params'] = $formatter->formatParametersForApi();
279 private function extractRowInfo( $row ) {
280 $logEntry = DatabaseLogEntry
::newFromRow( $row );
282 ApiResult
::META_TYPE
=> 'assoc',
285 $user = $this->getUser();
287 if ( $this->fld_ids
) {
288 $vals['logid'] = intval( $row->log_id
);
291 if ( $this->fld_title ||
$this->fld_parsedcomment
) {
292 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
295 if ( $this->fld_title ||
$this->fld_ids ||
$this->fld_details
&& $row->log_params
!== '' ) {
296 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
297 $vals['actionhidden'] = true;
300 if ( LogEventsList
::userCan( $row, LogPage
::DELETED_ACTION
, $user ) ) {
301 if ( $this->fld_title
) {
302 ApiQueryBase
::addTitleInfo( $vals, $title );
304 if ( $this->fld_ids
) {
305 $vals['pageid'] = intval( $row->page_id
);
306 $vals['logpage'] = intval( $row->log_page
);
308 if ( $this->fld_details
) {
309 $vals['params'] = LogFormatter
::newFromEntry( $logEntry )->formatParametersForApi();
314 if ( $this->fld_type
) {
315 $vals['type'] = $row->log_type
;
316 $vals['action'] = $row->log_action
;
319 if ( $this->fld_user ||
$this->fld_userid
) {
320 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_USER
) ) {
321 $vals['userhidden'] = true;
324 if ( LogEventsList
::userCan( $row, LogPage
::DELETED_USER
, $user ) ) {
325 if ( $this->fld_user
) {
326 $vals['user'] = $row->user_name
=== null ?
$row->log_user_text
: $row->user_name
;
328 if ( $this->fld_userid
) {
329 $vals['userid'] = intval( $row->log_user
);
332 if ( !$row->log_user
) {
333 $vals['anon'] = true;
337 if ( $this->fld_timestamp
) {
338 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->log_timestamp
);
341 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
342 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_COMMENT
) ) {
343 $vals['commenthidden'] = true;
346 if ( LogEventsList
::userCan( $row, LogPage
::DELETED_COMMENT
, $user ) ) {
347 $comment = $this->commentStore
->getComment( 'log_comment', $row )->text
;
348 if ( $this->fld_comment
) {
349 $vals['comment'] = $comment;
352 if ( $this->fld_parsedcomment
) {
353 $vals['parsedcomment'] = Linker
::formatComment( $comment, $title );
358 if ( $this->fld_tags
) {
359 if ( $row->ts_tags
) {
360 $tags = explode( ',', $row->ts_tags
);
361 ApiResult
::setIndexedTagName( $tags, 'tag' );
362 $vals['tags'] = $tags;
368 if ( $anyHidden && LogEventsList
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
) ) {
369 $vals['suppressed'] = true;
378 private function getAllowedLogActions() {
379 $config = $this->getConfig();
380 return array_keys( array_merge(
381 $config->get( 'LogActions' ),
382 $config->get( 'LogActionsHandlers' )
386 public function getCacheMode( $params ) {
387 if ( $this->userCanSeeRevDel() ) {
390 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
391 // formatComment() calls wfMessage() among other things
392 return 'anon-public-user-private';
393 } elseif ( LogEventsList
::getExcludeClause( $this->getDB(), 'user', $this->getUser() )
394 === LogEventsList
::getExcludeClause( $this->getDB(), 'public' )
395 ) { // Output can only contain public data.
398 return 'anon-public-user-private';
402 public function getAllowedParams( $flags = 0 ) {
403 $config = $this->getConfig();
406 ApiBase
::PARAM_ISMULTI
=> true,
407 ApiBase
::PARAM_DFLT
=> 'ids|title|type|user|timestamp|comment|details',
408 ApiBase
::PARAM_TYPE
=> [
420 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
423 ApiBase
::PARAM_TYPE
=> $config->get( 'LogTypes' )
426 // validation on request is done in execute()
427 ApiBase
::PARAM_TYPE
=> ( $flags & ApiBase
::GET_VALUES_FOR_HELP
)
428 ?
$this->getAllowedLogActions()
432 ApiBase
::PARAM_TYPE
=> 'timestamp'
435 ApiBase
::PARAM_TYPE
=> 'timestamp'
438 ApiBase
::PARAM_DFLT
=> 'older',
439 ApiBase
::PARAM_TYPE
=> [
443 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
446 ApiBase
::PARAM_TYPE
=> 'user',
450 ApiBase
::PARAM_TYPE
=> 'namespace',
451 ApiBase
::PARAM_EXTRA_NAMESPACES
=> [ NS_MEDIA
, NS_SPECIAL
],
456 ApiBase
::PARAM_DFLT
=> 10,
457 ApiBase
::PARAM_TYPE
=> 'limit',
458 ApiBase
::PARAM_MIN
=> 1,
459 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
460 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
463 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
467 if ( $config->get( 'MiserMode' ) ) {
468 $ret['prefix'][ApiBase
::PARAM_HELP_MSG
] = 'api-help-param-disabled-in-miser-mode';
474 protected function getExamplesMessages() {
476 'action=query&list=logevents'
477 => 'apihelp-query+logevents-example-simple',
481 public function getHelpUrls() {
482 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Logevents';