MediaWiki REL1_32
ApiQueryLogEvents.php
Go to the documentation of this file.
1<?php
25
32
34
35 public function __construct( ApiQuery $query, $moduleName ) {
36 parent::__construct( $query, $moduleName, 'le' );
37 }
38
39 private $fld_ids = false, $fld_title = false, $fld_type = false,
40 $fld_user = false, $fld_userid = false,
42 $fld_details = false, $fld_tags = false;
43
44 public function execute() {
46
48 $db = $this->getDB();
49 $this->commentStore = CommentStore::getStore();
50 $this->requireMaxOneParameter( $params, 'title', 'prefix', 'namespace' );
51
52 $prop = array_flip( $params['prop'] );
53
54 $this->fld_ids = isset( $prop['ids'] );
55 $this->fld_title = isset( $prop['title'] );
56 $this->fld_type = isset( $prop['type'] );
57 $this->fld_user = isset( $prop['user'] );
58 $this->fld_userid = isset( $prop['userid'] );
59 $this->fld_timestamp = isset( $prop['timestamp'] );
60 $this->fld_comment = isset( $prop['comment'] );
61 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
62 $this->fld_details = isset( $prop['details'] );
63 $this->fld_tags = isset( $prop['tags'] );
64
65 $hideLogs = LogEventsList::getExcludeClause( $db, 'user', $this->getUser() );
66 if ( $hideLogs !== false ) {
67 $this->addWhere( $hideLogs );
68 }
69
70 $actorMigration = ActorMigration::newMigration();
71 $actorQuery = $actorMigration->getJoin( 'log_user' );
72 $this->addTables( 'logging' );
73 $this->addTables( $actorQuery['tables'] );
74 $this->addTables( [ 'user', 'page' ] );
75 $this->addJoinConds( $actorQuery['joins'] );
76 $this->addJoinConds( [
77 'user' => [ 'LEFT JOIN',
78 'user_id=' . $actorQuery['fields']['log_user'] ],
79 'page' => [ 'LEFT JOIN',
80 [ 'log_namespace=page_namespace',
81 'log_title=page_title' ] ] ] );
82
83 $this->addFields( [
84 'log_id',
85 'log_type',
86 'log_action',
87 'log_timestamp',
88 'log_deleted',
89 ] );
90
91 $this->addFieldsIf( 'page_id', $this->fld_ids );
92 // log_page is the page_id saved at log time, whereas page_id is from a
93 // join at query time. This leads to different results in various
94 // scenarios, e.g. deletion, recreation.
95 $this->addFieldsIf( 'log_page', $this->fld_ids );
96 $this->addFieldsIf( $actorQuery['fields'] + [ 'user_name' ], $this->fld_user );
97 $this->addFieldsIf( $actorQuery['fields'], $this->fld_userid );
98 $this->addFieldsIf(
99 [ 'log_namespace', 'log_title' ],
100 $this->fld_title || $this->fld_parsedcomment
101 );
102 $this->addFieldsIf( 'log_params', $this->fld_details );
103
104 if ( $this->fld_comment || $this->fld_parsedcomment ) {
105 $commentQuery = $this->commentStore->getJoin( 'log_comment' );
106 $this->addTables( $commentQuery['tables'] );
107 $this->addFields( $commentQuery['fields'] );
108 $this->addJoinConds( $commentQuery['joins'] );
109 }
110
111 if ( $this->fld_tags ) {
112 $this->addTables( 'tag_summary' );
113 $this->addJoinConds( [ 'tag_summary' => [ 'LEFT JOIN', 'log_id=ts_log_id' ] ] );
114 $this->addFields( 'ts_tags' );
115 }
116
117 if ( !is_null( $params['tag'] ) ) {
118 $this->addTables( 'change_tag' );
119 $this->addJoinConds( [ 'change_tag' => [ 'INNER JOIN',
120 [ 'log_id=ct_log_id' ] ] ] );
121 if ( $wgChangeTagsSchemaMigrationStage > MIGRATION_WRITE_BOTH ) {
122 $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore();
123 try {
124 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
125 } catch ( NameTableAccessException $exception ) {
126 // Return nothing.
127 $this->addWhere( '1=0' );
128 }
129 } else {
130 $this->addWhereFld( 'ct_tag', $params['tag'] );
131 }
132 }
133
134 if ( !is_null( $params['action'] ) ) {
135 // Do validation of action param, list of allowed actions can contains wildcards
136 // Allow the param, when the actions is in the list or a wildcard version is listed.
137 $logAction = $params['action'];
138 if ( strpos( $logAction, '/' ) === false ) {
139 // all items in the list have a slash
140 $valid = false;
141 } else {
142 $logActions = array_flip( $this->getAllowedLogActions() );
143 list( $type, $action ) = explode( '/', $logAction, 2 );
144 $valid = isset( $logActions[$logAction] ) || isset( $logActions[$type . '/*'] );
145 }
146
147 if ( !$valid ) {
148 $encParamName = $this->encodeParamName( 'action' );
149 $this->dieWithError(
150 [ 'apierror-unrecognizedvalue', $encParamName, wfEscapeWikiText( $logAction ) ],
151 "unknown_$encParamName"
152 );
153 }
154
155 $this->addWhereFld( 'log_type', $type );
156 $this->addWhereFld( 'log_action', $action );
157 } elseif ( !is_null( $params['type'] ) ) {
158 $this->addWhereFld( 'log_type', $params['type'] );
159 }
160
162 'log_timestamp',
163 $params['dir'],
164 $params['start'],
165 $params['end']
166 );
167 // Include in ORDER BY for uniqueness
168 $this->addWhereRange( 'log_id', $params['dir'], null, null );
169
170 if ( !is_null( $params['continue'] ) ) {
171 $cont = explode( '|', $params['continue'] );
172 $this->dieContinueUsageIf( count( $cont ) != 2 );
173 $op = ( $params['dir'] === 'newer' ? '>' : '<' );
174 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
175 $continueId = (int)$cont[1];
176 $this->dieContinueUsageIf( $continueId != $cont[1] );
177 $this->addWhere( "log_timestamp $op $continueTimestamp OR " .
178 "(log_timestamp = $continueTimestamp AND " .
179 "log_id $op= $continueId)"
180 );
181 }
182
183 $limit = $params['limit'];
184 $this->addOption( 'LIMIT', $limit + 1 );
185
186 $user = $params['user'];
187 if ( !is_null( $user ) ) {
188 // Note the joins in $q are the same as those from ->getJoin() above
189 // so we only need to add 'conds' here.
190 $q = $actorMigration->getWhere(
191 $db, 'log_user', User::newFromName( $params['user'], false )
192 );
193 $this->addWhere( $q['conds'] );
194 }
195
196 $title = $params['title'];
197 if ( !is_null( $title ) ) {
198 $titleObj = Title::newFromText( $title );
199 if ( is_null( $titleObj ) ) {
200 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $title ) ] );
201 }
202 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
203 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
204 }
205
206 if ( $params['namespace'] !== null ) {
207 $this->addWhereFld( 'log_namespace', $params['namespace'] );
208 }
209
210 $prefix = $params['prefix'];
211
212 if ( !is_null( $prefix ) ) {
213 if ( $this->getConfig()->get( 'MiserMode' ) ) {
214 $this->dieWithError( 'apierror-prefixsearchdisabled' );
215 }
216
217 $title = Title::newFromText( $prefix );
218 if ( is_null( $title ) ) {
219 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $prefix ) ] );
220 }
221 $this->addWhereFld( 'log_namespace', $title->getNamespace() );
222 $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
223 }
224
225 // Paranoia: avoid brute force searches (T19342)
226 if ( $params['namespace'] !== null || !is_null( $title ) || !is_null( $user ) ) {
227 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
228 $titleBits = LogPage::DELETED_ACTION;
229 $userBits = LogPage::DELETED_USER;
230 } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
233 } else {
234 $titleBits = 0;
235 $userBits = 0;
236 }
237 if ( ( $params['namespace'] !== null || !is_null( $title ) ) && $titleBits ) {
238 $this->addWhere( $db->bitAnd( 'log_deleted', $titleBits ) . " != $titleBits" );
239 }
240 if ( !is_null( $user ) && $userBits ) {
241 $this->addWhere( $db->bitAnd( 'log_deleted', $userBits ) . " != $userBits" );
242 }
243 }
244
245 $count = 0;
246 $res = $this->select( __METHOD__ );
247 $result = $this->getResult();
248 foreach ( $res as $row ) {
249 if ( ++$count > $limit ) {
250 // We've reached the one extra which shows that there are
251 // additional pages to be had. Stop here...
252 $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" );
253 break;
254 }
255
256 $vals = $this->extractRowInfo( $row );
257 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
258 if ( !$fit ) {
259 $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" );
260 break;
261 }
262 }
263 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'item' );
264 }
265
266 private function extractRowInfo( $row ) {
267 $logEntry = DatabaseLogEntry::newFromRow( $row );
268 $vals = [
269 ApiResult::META_TYPE => 'assoc',
270 ];
271 $anyHidden = false;
272 $user = $this->getUser();
273
274 if ( $this->fld_ids ) {
275 $vals['logid'] = intval( $row->log_id );
276 }
277
278 if ( $this->fld_title || $this->fld_parsedcomment ) {
279 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
280 }
281
282 if ( $this->fld_title || $this->fld_ids || $this->fld_details && $row->log_params !== '' ) {
284 $vals['actionhidden'] = true;
285 $anyHidden = true;
286 }
287 if ( LogEventsList::userCan( $row, LogPage::DELETED_ACTION, $user ) ) {
288 if ( $this->fld_title ) {
289 ApiQueryBase::addTitleInfo( $vals, $title );
290 }
291 if ( $this->fld_ids ) {
292 $vals['pageid'] = intval( $row->page_id );
293 $vals['logpage'] = intval( $row->log_page );
294 }
295 if ( $this->fld_details ) {
296 $vals['params'] = LogFormatter::newFromEntry( $logEntry )->formatParametersForApi();
297 }
298 }
299 }
300
301 if ( $this->fld_type ) {
302 $vals['type'] = $row->log_type;
303 $vals['action'] = $row->log_action;
304 }
305
306 if ( $this->fld_user || $this->fld_userid ) {
308 $vals['userhidden'] = true;
309 $anyHidden = true;
310 }
311 if ( LogEventsList::userCan( $row, LogPage::DELETED_USER, $user ) ) {
312 if ( $this->fld_user ) {
313 $vals['user'] = $row->user_name ?? $row->log_user_text;
314 }
315 if ( $this->fld_userid ) {
316 $vals['userid'] = intval( $row->log_user );
317 }
318
319 if ( !$row->log_user ) {
320 $vals['anon'] = true;
321 }
322 }
323 }
324 if ( $this->fld_timestamp ) {
325 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->log_timestamp );
326 }
327
328 if ( $this->fld_comment || $this->fld_parsedcomment ) {
330 $vals['commenthidden'] = true;
331 $anyHidden = true;
332 }
333 if ( LogEventsList::userCan( $row, LogPage::DELETED_COMMENT, $user ) ) {
334 $comment = $this->commentStore->getComment( 'log_comment', $row )->text;
335 if ( $this->fld_comment ) {
336 $vals['comment'] = $comment;
337 }
338
339 if ( $this->fld_parsedcomment ) {
340 $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
341 }
342 }
343 }
344
345 if ( $this->fld_tags ) {
346 if ( $row->ts_tags ) {
347 $tags = explode( ',', $row->ts_tags );
348 ApiResult::setIndexedTagName( $tags, 'tag' );
349 $vals['tags'] = $tags;
350 } else {
351 $vals['tags'] = [];
352 }
353 }
354
355 if ( $anyHidden && LogEventsList::isDeleted( $row, LogPage::DELETED_RESTRICTED ) ) {
356 $vals['suppressed'] = true;
357 }
358
359 return $vals;
360 }
361
365 private function getAllowedLogActions() {
366 $config = $this->getConfig();
367 return array_keys( array_merge(
368 $config->get( 'LogActions' ),
369 $config->get( 'LogActionsHandlers' )
370 ) );
371 }
372
373 public function getCacheMode( $params ) {
374 if ( $this->userCanSeeRevDel() ) {
375 return 'private';
376 }
377 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
378 // formatComment() calls wfMessage() among other things
379 return 'anon-public-user-private';
380 } elseif ( LogEventsList::getExcludeClause( $this->getDB(), 'user', $this->getUser() )
381 === LogEventsList::getExcludeClause( $this->getDB(), 'public' )
382 ) { // Output can only contain public data.
383 return 'public';
384 } else {
385 return 'anon-public-user-private';
386 }
387 }
388
389 public function getAllowedParams( $flags = 0 ) {
390 $config = $this->getConfig();
391 $ret = [
392 'prop' => [
394 ApiBase::PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
396 'ids',
397 'title',
398 'type',
399 'user',
400 'userid',
401 'timestamp',
402 'comment',
403 'parsedcomment',
404 'details',
405 'tags'
406 ],
408 ],
409 'type' => [
411 ],
412 'action' => [
413 // validation on request is done in execute()
415 ? $this->getAllowedLogActions()
416 : null
417 ],
418 'start' => [
419 ApiBase::PARAM_TYPE => 'timestamp'
420 ],
421 'end' => [
422 ApiBase::PARAM_TYPE => 'timestamp'
423 ],
424 'dir' => [
425 ApiBase::PARAM_DFLT => 'older',
427 'newer',
428 'older'
429 ],
430 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
431 ],
432 'user' => [
433 ApiBase::PARAM_TYPE => 'user',
434 ],
435 'title' => null,
436 'namespace' => [
437 ApiBase::PARAM_TYPE => 'namespace',
439 ],
440 'prefix' => [],
441 'tag' => null,
442 'limit' => [
444 ApiBase::PARAM_TYPE => 'limit',
448 ],
449 'continue' => [
450 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
451 ],
452 ];
453
454 if ( $config->get( 'MiserMode' ) ) {
455 $ret['prefix'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
456 }
457
458 return $ret;
459 }
460
461 protected function getExamplesMessages() {
462 return [
463 'action=query&list=logevents'
464 => 'apihelp-query+logevents-example-simple',
465 ];
466 }
467
468 public function getHelpUrls() {
469 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Logevents';
470 }
471}
int $wgChangeTagsSchemaMigrationStage
change_tag table schema migration stage.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
const PARAM_MAX2
(integer) Max value allowed for the parameter for users with the apihighlimits right,...
Definition ApiBase.php:96
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:748
const PARAM_MAX
(integer) Max value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition ApiBase.php:90
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition ApiBase.php:1987
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition ApiBase.php:2155
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:87
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition ApiBase.php:48
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
Definition ApiBase.php:157
const PARAM_MIN
(integer) Lowest value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition ApiBase.php:99
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:252
getResult()
Get the result object.
Definition ApiBase.php:659
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:770
requireMaxOneParameter( $params, $required)
Die if more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:939
const PARAM_EXTRA_NAMESPACES
(int[]) When PARAM_TYPE is 'namespace', include these as additional possible values.
Definition ApiBase.php:186
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:124
const GET_VALUES_FOR_HELP
getAllowedParams() flag: When set, the result could take longer to generate, but should be more thoro...
Definition ApiBase.php:265
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:254
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:539
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition ApiBase.php:51
This is a base class for all Query modules.
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
addWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, and an ORDER BY clause to sort in the right direction.
addFields( $value)
Add a set of fields to select to the internal array.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
addTimestampWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, similar to addWhereRange, but converts $start and $end t...
getDB()
Get the Query database connection (read-only)
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
addWhereFld( $field, $value)
Equivalent to addWhere(array($field => $value))
addWhere( $value)
Add a set of WHERE clauses to the internal array.
userCanSeeRevDel()
Check whether the current user has permission to view revision-deleted fields.
Query action to List the log events, with optional filtering by various parameters.
getExamplesMessages()
Returns usage examples for this module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
__construct(ApiQuery $query, $moduleName)
getCacheMode( $params)
Get the cache mode for the data generated by this module.
getHelpUrls()
Return links to more detailed help pages about the module.
This is the main query class.
Definition ApiQuery.php:36
const META_TYPE
Key for the 'type' metadata item.
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
static newFromRow( $row)
Constructs new LogEntry from database result row.
Definition LogEntry.php:208
static formatComment( $comment, $title=null, $local=false, $wikiId=null)
This function is called by all recent changes variants, by the page history, and by the user contribu...
Definition Linker.php:1088
static getExcludeClause( $db, $audience='public', User $user=null)
SQL clause to skip forbidden log types for this user.
static userCan( $row, $field, User $user=null)
Determine if the current user is allowed to view a particular field of this log row,...
static isDeleted( $row, $field)
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
const DELETED_USER
Definition LogPage.php:36
const DELETED_RESTRICTED
Definition LogPage.php:37
const DELETED_COMMENT
Definition LogPage.php:35
static validTypes()
Get the list of valid log types.
Definition LogPage.php:194
const DELETED_ACTION
Definition LogPage.php:34
MediaWikiServices is the service locator for the application scope of MediaWiki.
Exception representing a failure to look up a row from a name table.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:592
We use the convention $dbr for read and $dbw for write to help you keep track of whether the database object is a the world will explode Or to be a subsequent write query which succeeded on the master may fail when replicated to the slave due to a unique key collision Replication on the slave will stop and it may take hours to repair the database and get it back online Setting read_only in my cnf on the slave will avoid this but given the dire we prefer to have as many checks as possible We provide a but the wrapper functions like select() and insert() are usually more convenient. They take care of things like table prefixes and escaping for you. If you really need to make your own SQL
$res
Definition database.txt:21
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2054
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition hooks.txt:1656
const NS_SPECIAL
Definition Defines.php:53
const NS_MEDIA
Definition Defines.php:52
const MIGRATION_WRITE_BOTH
Definition Defines.php:316
$params