29use Wikimedia\Timestamp\TimestampFormat as TS;
39 private $formattedComments;
51 parent::__construct( $query, $moduleName,
'le' );
54 private bool $fld_ids =
false;
55 private bool $fld_title =
false;
56 private bool $fld_type =
false;
57 private bool $fld_user =
false;
58 private bool $fld_userid =
false;
59 private bool $fld_timestamp =
false;
60 private bool $fld_comment =
false;
61 private bool $fld_parsedcomment =
false;
62 private bool $fld_details =
false;
63 private bool $fld_tags =
false;
70 $prop = array_fill_keys( $params[
'prop'],
true );
72 $this->fld_ids = isset( $prop[
'ids'] );
73 $this->fld_title = isset( $prop[
'title'] );
74 $this->fld_type = isset( $prop[
'type'] );
75 $this->fld_user = isset( $prop[
'user'] );
76 $this->fld_userid = isset( $prop[
'userid'] );
77 $this->fld_timestamp = isset( $prop[
'timestamp'] );
78 $this->fld_comment = isset( $prop[
'comment'] );
79 $this->fld_parsedcomment = isset( $prop[
'parsedcomment'] );
80 $this->fld_details = isset( $prop[
'details'] );
81 $this->fld_tags = isset( $prop[
'tags'] );
83 $hideLogs = LogEventsList::getExcludeClause( $db,
'user', $this->
getAuthority() );
84 if ( $hideLogs !==
false ) {
98 if ( $params[
'ids'] ) {
102 $user = $params[
'user'];
103 if ( $this->fld_user || $this->fld_userid || $user !==
null ) {
106 'actor' => [
'JOIN',
'actor_id=log_actor' ],
108 $this->
addFieldsIf( [
'actor_name',
'actor_user' ], $this->fld_user );
109 $this->
addFieldsIf(
'actor_user', $this->fld_userid );
110 if ( $user !==
null ) {
115 if ( $this->fld_ids ) {
118 'page' => [
'LEFT JOIN',
119 [
'log_namespace=page_namespace',
120 'log_title=page_title' ] ]
125 $this->
addFields( [
'page_id',
'log_page' ] );
128 [
'log_namespace',
'log_title' ],
129 $this->fld_title || $this->fld_parsedcomment
131 $this->
addFieldsIf(
'log_params', $this->fld_details || $this->fld_ids );
133 if ( $this->fld_comment || $this->fld_parsedcomment ) {
134 $commentQuery = $this->commentStore->getJoin(
'log_comment' );
135 $this->
addTables( $commentQuery[
'tables'] );
136 $this->
addFields( $commentQuery[
'fields'] );
140 if ( $this->fld_tags ) {
142 'ts_tags' => $this->changeTagsStore->makeTagSummarySubquery(
'logging', $this->getAuthority() )
146 if ( $params[
'tag'] !==
null ) {
147 if ( !$this->changeTagsStore->canViewTag( $params[
'tag'], $this->getAuthority() ) ) {
153 [
'log_id=ct_log_id' ] ] ] );
155 $this->
addWhereFld(
'ct_tag_id', $this->changeTagDefStore->getId( $params[
'tag'] ) );
162 if ( $params[
'action'] !==
null ) {
165 $logAction = $params[
'action'];
166 if ( !str_contains( $logAction,
'/' ) ) {
170 $logActions = array_fill_keys( $this->getAllowedLogActions(),
true );
171 [ $type, $action ] = explode(
'/', $logAction, 2 );
172 $valid = isset( $logActions[$logAction] ) || isset( $logActions[$type .
'/*'] );
178 [
'apierror-unrecognizedvalue', $encParamName,
wfEscapeWikiText( $logAction ) ],
179 "unknown_$encParamName"
187 } elseif ( $params[
'type'] !==
null ) {
198 $this->
addWhereRange(
'log_id', $params[
'dir'],
null,
null );
200 if ( $params[
'continue'] !==
null ) {
202 $op = ( $params[
'dir'] ===
'newer' ?
'>=' :
'<=' );
203 $this->
addWhere( $db->buildComparison( $op, [
204 'log_timestamp' => $db->timestamp( $cont[0] ),
205 'log_id' => $cont[1],
209 $limit = $params[
'limit'];
212 $title = $params[
'title'];
213 if ( $title !==
null ) {
214 $titleObj = Title::newFromText( $title );
215 if ( $titleObj ===
null || $titleObj->isExternal() ) {
218 $this->
addWhereFld(
'log_namespace', $titleObj->getNamespace() );
219 $this->
addWhereFld(
'log_title', $titleObj->getDBkey() );
222 if ( $params[
'namespace'] !==
null ) {
223 $this->
addWhereFld(
'log_namespace', $params[
'namespace'] );
226 $prefix = $params[
'prefix'];
228 if ( $prefix !==
null ) {
233 $title = Title::newFromText( $prefix );
234 if ( $title ===
null || $title->isExternal() ) {
237 $this->
addWhereFld(
'log_namespace', $title->getNamespace() );
239 $db->expr(
'log_title', IExpression::LIKE,
new LikeValue( $title->getDBkey(), $db->anyString() ) )
244 if ( $params[
'namespace'] !==
null || $title !==
null || $user !==
null ) {
245 if ( !$this->
getAuthority()->isAllowed(
'deletedhistory' ) ) {
246 $titleBits = LogPage::DELETED_ACTION;
247 $userBits = LogPage::DELETED_USER;
248 } elseif ( !$this->
getAuthority()->isAllowedAny(
'suppressrevision',
'viewsuppressed' ) ) {
249 $titleBits = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
250 $userBits = LogPage::DELETED_USER | LogPage::DELETED_RESTRICTED;
255 if ( ( $params[
'namespace'] !==
null || $title !==
null ) && $titleBits ) {
256 $this->
addWhere( $db->bitAnd(
'log_deleted', $titleBits ) .
" != $titleBits" );
258 if ( $user !==
null && $userBits ) {
259 $this->
addWhere( $db->bitAnd(
'log_deleted', $userBits ) .
" != $userBits" );
268 if ( $params[
'tag'] ===
null && $user ===
null ) {
273 'MAX_EXECUTION_TIME',
278 $res = $this->
select( __METHOD__ );
280 if ( $this->fld_title ) {
283 if ( $this->fld_parsedcomment ) {
284 $this->formattedComments = $this->commentFormatter->formatItems(
285 $this->commentFormatter->rows( $res )
286 ->commentKey(
'log_comment' )
287 ->indexField(
'log_id' )
288 ->namespaceField(
'log_namespace' )
289 ->titleField(
'log_title' )
294 foreach ( $res as $row ) {
295 if ( ++$count > $limit ) {
302 $vals = $this->extractRowInfo( $row );
303 $fit = $result->addValue( [
'query', $this->
getModuleName() ],
null, $vals );
309 $result->addIndexedTagName( [
'query', $this->
getModuleName() ],
'item' );
312 private function extractRowInfo( \stdClass $row ): array {
319 if ( $this->fld_ids ) {
320 $vals[
'logid'] = (int)$row->log_id;
323 if ( $this->fld_title ) {
328 if ( $this->fld_title || $this->fld_ids || ( $this->fld_details && $row->log_params !==
'' ) ) {
329 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
330 $vals[
'actionhidden'] =
true;
333 if ( LogEventsList::userCan( $row, LogPage::DELETED_ACTION, $authority ) ) {
334 if ( $this->fld_title ) {
339 if ( $this->fld_ids ) {
340 $vals[
'pageid'] = (int)$row->page_id;
341 $vals[
'logpage'] = (int)$row->log_page;
342 $revId = $logEntry->getAssociatedRevId();
344 $vals[
'revid'] = (int)$revId;
347 if ( $this->fld_details ) {
348 $vals[
'params'] = $this->logFormatterFactory->newFromEntry( $logEntry )->formatParametersForApi();
353 if ( $this->fld_type ) {
354 $vals[
'type'] = $row->log_type;
355 $vals[
'action'] = $row->log_action;
358 if ( $this->fld_user || $this->fld_userid ) {
359 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
360 $vals[
'userhidden'] =
true;
363 if ( LogEventsList::userCan( $row, LogPage::DELETED_USER, $authority ) ) {
364 if ( $this->fld_user ) {
365 $vals[
'user'] = $row->actor_name;
367 if ( $this->fld_userid ) {
368 $vals[
'userid'] = (int)$row->actor_user;
371 if ( isset( $vals[
'user'] ) && $this->userNameUtils->isTemp( $vals[
'user'] ) ) {
372 $vals[
'temp'] =
true;
375 if ( !$row->actor_user ) {
376 $vals[
'anon'] =
true;
380 if ( $this->fld_timestamp ) {
381 $vals[
'timestamp'] =
wfTimestamp( TS::ISO_8601, $row->log_timestamp );
384 if ( $this->fld_comment || $this->fld_parsedcomment ) {
385 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
386 $vals[
'commenthidden'] =
true;
389 if ( LogEventsList::userCan( $row, LogPage::DELETED_COMMENT, $authority ) ) {
390 if ( $this->fld_comment ) {
391 $vals[
'comment'] = $this->commentStore->getComment(
'log_comment', $row )->text;
394 if ( $this->fld_parsedcomment ) {
396 $vals[
'parsedcomment'] = $this->formattedComments[$row->log_id];
401 if ( $this->fld_tags ) {
402 if ( $row->ts_tags ) {
403 $tags = explode(
',', $row->ts_tags );
405 $vals[
'tags'] = $tags;
411 if ( $anyHidden && LogEventsList::isDeleted( $row, LogPage::DELETED_RESTRICTED ) ) {
412 $vals[
'suppressed'] =
true;
421 private function getAllowedLogActions() {
423 return array_keys( array_merge(
431 if ( $this->userCanSeeRevDel() ) {
434 return 'anon-public-user-private';
439 $config = $this->getConfig();
441 $logActions = $this->getAllowedLogActions();
448 ParamValidator::PARAM_ISMULTI =>
true,
449 ParamValidator::PARAM_DEFAULT =>
'ids|title|type|user|timestamp|comment|details',
450 ParamValidator::PARAM_TYPE => [
465 ParamValidator::PARAM_TYPE => LogPage::validTypes(),
469 ParamValidator::PARAM_TYPE => $logActions
472 ParamValidator::PARAM_TYPE =>
'timestamp'
475 ParamValidator::PARAM_TYPE =>
'timestamp'
478 ParamValidator::PARAM_DEFAULT =>
'older',
479 ParamValidator::PARAM_TYPE => [
485 'newer' =>
'api-help-paramvalue-direction-newer',
486 'older' =>
'api-help-paramvalue-direction-older',
490 ParamValidator::PARAM_TYPE =>
'integer',
491 ParamValidator::PARAM_ISMULTI => true
494 ParamValidator::PARAM_TYPE =>
'user',
495 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'temp',
'id',
'interwiki' ],
499 ParamValidator::PARAM_TYPE =>
'namespace',
505 ParamValidator::PARAM_DEFAULT => 10,
506 ParamValidator::PARAM_TYPE =>
'limit',
507 IntegerDef::PARAM_MIN => 1,
526 'action=query&list=logevents'
527 =>
'apihelp-query+logevents-example-simple',
533 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Logevents';
538class_alias( ApiQueryLogEvents::class,
'ApiQueryLogEvents' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
A value class to process existing log entries.
Class to simplify the use of log pages.
A class containing constants representing the names of configuration variables.
const MaxExecutionTimeForExpensiveQueries
Name constant for the MaxExecutionTimeForExpensiveQueries setting, for use with Config::get()
const LogActionsHandlers
Name constant for the LogActionsHandlers setting, for use with Config::get()
const LogActions
Name constant for the LogActions setting, for use with Config::get()
const MiserMode
Name constant for the MiserMode setting, for use with Config::get()