MediaWiki master
LogPager.php
Go to the documentation of this file.
1<?php
26namespace MediaWiki\Pager;
27
31use LogPage;
40
46 private $types = [];
47
49 private $performer = '';
50
52 private $page = '';
53
55 private $pattern = false;
56
58 private $typeCGI = '';
59
61 private $action = '';
62
64 private $performerRestrictionsEnforced = false;
65
67 private $actionRestrictionsEnforced = false;
68
70 private $mConds;
71
73 private $mTagFilter;
74
76 private $mTagInvert;
77
80
82 private $linkBatchFactory;
83
85 private $actorNormalization;
86 private LogFormatterFactory $logFormatterFactory;
87
106 public function __construct( $list, $types = [], $performer = '', $page = '',
107 $pattern = false, $conds = [], $year = false, $month = false, $day = false,
108 $tagFilter = '', $action = '', $logId = 0,
109 ?LinkBatchFactory $linkBatchFactory = null,
110 ?ActorNormalization $actorNormalization = null,
111 ?LogFormatterFactory $logFormatterFactory = null,
112 $tagInvert = false
113 ) {
114 parent::__construct( $list->getContext() );
115
116 $services = MediaWikiServices::getInstance();
117 $this->mConds = $conds;
118 $this->mLogEventsList = $list;
119
120 // Class is used directly in extensions - T266480
121 $this->linkBatchFactory = $linkBatchFactory ?? $services->getLinkBatchFactory();
122 $this->actorNormalization = $actorNormalization ?? $services->getActorNormalization();
123 $this->logFormatterFactory = $logFormatterFactory ?? $services->getLogFormatterFactory();
124
125 $this->limitLogId( $logId ); // set before types per T269761
126 $this->limitType( $types ); // also excludes hidden types
127 $this->limitFilterTypes();
128 $this->limitPerformer( $performer );
129 $this->limitTitle( $page, $pattern );
130 $this->limitAction( $action );
131 $this->getDateCond( $year, $month, $day );
132 $this->mTagFilter = (string)$tagFilter;
133 $this->mTagInvert = (bool)$tagInvert;
134 }
135
136 public function getDefaultQuery() {
137 $query = parent::getDefaultQuery();
138 $query['type'] = $this->typeCGI; // arrays won't work here
139 $query['user'] = $this->performer;
140 $query['day'] = $this->mDay;
141 $query['month'] = $this->mMonth;
142 $query['year'] = $this->mYear;
143
144 return $query;
145 }
146
147 private function limitFilterTypes() {
148 if ( $this->hasEqualsClause( 'log_id' ) ) { // T220834
149 return;
150 }
151 $filterTypes = $this->getFilterParams();
152 $excludeTypes = [];
153 foreach ( $filterTypes as $type => $hide ) {
154 if ( $hide ) {
155 $excludeTypes[] = $type;
156 }
157 }
158 if ( $excludeTypes ) {
159 $this->mConds[] = $this->mDb->expr( 'log_type', '!=', $excludeTypes );
160 }
161 }
162
163 public function getFilterParams() {
164 $filters = [];
165 if ( count( $this->types ) ) {
166 return $filters;
167 }
168
169 // FIXME: This is broken, values from HTMLForm should be used.
170 $wpfilters = $this->getRequest()->getArray( "wpfilters" );
171 $filterLogTypes = $this->getConfig()->get( MainConfigNames::FilterLogTypes );
172
173 foreach ( $filterLogTypes as $type => $default ) {
174 // Back-compat: Check old URL params if the new param wasn't passed
175 if ( $wpfilters === null ) {
176 $hide = $this->getRequest()->getBool( "hide_{$type}_log", $default );
177 } else {
178 $hide = !in_array( $type, $wpfilters );
179 }
180
181 $filters[$type] = $hide;
182 }
183
184 return $filters;
185 }
186
194 private function limitType( $types ) {
195 $restrictions = $this->getConfig()->get( MainConfigNames::LogRestrictions );
196 // If $types is not an array, make it an array
197 $types = ( $types === '' ) ? [] : (array)$types;
198 // Don't even show header for private logs; don't recognize it...
199 $needReindex = false;
200 foreach ( $types as $type ) {
201 if ( isset( $restrictions[$type] )
202 && !$this->getAuthority()->isAllowed( $restrictions[$type] )
203 ) {
204 $needReindex = true;
205 $types = array_diff( $types, [ $type ] );
206 }
207 }
208 if ( $needReindex ) {
209 // Lots of this code makes assumptions that
210 // the first entry in the array is $types[0].
211 $types = array_values( $types );
212 }
213 $this->types = $types;
214 // Don't show private logs to unprivileged users.
215 // Also, only show them upon specific request to avoid surprises.
216 // Exception: if we are showing only a single log entry based on the log id,
217 // we don't require that "specific request" so that the links-in-logs feature
218 // works. See T269761
219 $audience = ( $types || $this->hasEqualsClause( 'log_id' ) ) ? 'user' : 'public';
220 $hideLogs = LogEventsList::getExcludeClause( $this->mDb, $audience, $this->getAuthority() );
221 if ( $hideLogs !== false ) {
222 $this->mConds[] = $hideLogs;
223 }
224 if ( count( $types ) ) {
225 $this->mConds['log_type'] = $types;
226 // Set typeCGI; used in url param for paging
227 if ( count( $types ) == 1 ) {
228 $this->typeCGI = $types[0];
229 }
230 }
231 }
232
239 private function limitPerformer( $name ) {
240 if ( $name == '' ) {
241 return;
242 }
243
244 $actorId = $this->actorNormalization->findActorIdByName( $name, $this->mDb );
245
246 if ( !$actorId ) {
247 // Unknown user, match nothing.
248 $this->mConds[] = '1 = 0';
249 return;
250 }
251
252 $this->mConds[ 'log_actor' ] = $actorId;
253
254 $this->enforcePerformerRestrictions();
255
256 $this->performer = $name;
257 }
258
267 private function limitTitle( $page, $pattern ) {
268 if ( !$page instanceof PageReference ) {
269 // NOTE: For some types of logs, the title may be something strange, like "User:#12345"!
270 $page = Title::newFromText( $page );
271 if ( !$page ) {
272 return;
273 }
274 }
275
276 $titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
277 $this->page = $titleFormatter->getPrefixedDBkey( $page );
278 $ns = $page->getNamespace();
279 $db = $this->mDb;
280
281 $interwikiDelimiter = $this->getConfig()->get( MainConfigNames::UserrightsInterwikiDelimiter );
282
283 $doUserRightsLogLike = false;
284 if ( $this->types == [ 'rights' ] ) {
285 $parts = explode( $interwikiDelimiter, $page->getDBkey() );
286 if ( count( $parts ) == 2 ) {
287 [ $name, $database ] = array_map( 'trim', $parts );
288 if ( str_contains( $database, '*' ) ) {
289 $doUserRightsLogLike = true;
290 }
291 }
292 }
293
307 $this->mConds['log_namespace'] = $ns;
308 if ( $doUserRightsLogLike ) {
309 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable $name is set when reached here
310 $params = [ $name . $interwikiDelimiter ];
311 // @phan-suppress-next-next-line PhanPossiblyUndeclaredVariable $database is set when reached here
312 // @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal $database is set when reached here
313 $databaseParts = explode( '*', $database );
314 $databasePartCount = count( $databaseParts );
315 foreach ( $databaseParts as $i => $databasepart ) {
316 $params[] = $databasepart;
317 if ( $i < $databasePartCount - 1 ) {
318 $params[] = $db->anyString();
319 }
320 }
321 $this->mConds[] = $db->expr( 'log_title', IExpression::LIKE, new LikeValue( ...$params ) );
322 } elseif ( $pattern && !$this->getConfig()->get( MainConfigNames::MiserMode ) ) {
323 $this->mConds[] = $db->expr(
324 'log_title',
325 IExpression::LIKE,
326 new LikeValue( $page->getDBkey(), $db->anyString() )
327 );
328 $this->pattern = $pattern;
329 } else {
330 $this->mConds['log_title'] = $page->getDBkey();
331 }
332 $this->enforceActionRestrictions();
333 }
334
340 private function limitAction( $action ) {
341 // Allow to filter the log by actions
342 $type = $this->typeCGI;
343 if ( $type === '' ) {
344 // nothing to do
345 return;
346 }
347 $actions = $this->getConfig()->get( MainConfigNames::ActionFilteredLogs );
348 if ( isset( $actions[$type] ) ) {
349 // log type can be filtered by actions
350 if ( $action !== '' && isset( $actions[$type][$action] ) ) {
351 // add condition to query
352 $this->mConds['log_action'] = $actions[$type][$action];
353 $this->action = $action;
354 }
355 }
356 }
357
362 protected function limitLogId( $logId ) {
363 if ( !$logId ) {
364 return;
365 }
366 $this->mConds['log_id'] = $logId;
367 }
368
374 public function getQueryInfo() {
375 $queryBuilder = DatabaseLogEntry::newSelectQueryBuilder( $this->mDb )
376 ->where( $this->mConds );
377
378 # Add log_search table if there are conditions on it.
379 # This filters the results to only include log rows that have
380 # log_search records with the specified ls_field and ls_value values.
381 if ( array_key_exists( 'ls_field', $this->mConds ) ) {
382 $queryBuilder->join( 'log_search', null, 'ls_log_id=log_id' );
383 $queryBuilder->ignoreIndex( [ 'log_search' => 'ls_log_id' ] );
384 $queryBuilder->useIndex( [ 'logging' => 'PRIMARY' ] );
385 if ( !$this->hasEqualsClause( 'ls_field' )
386 || !$this->hasEqualsClause( 'ls_value' )
387 ) {
388 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
389 # to match a specific (ls_field,ls_value) tuple, then there will be
390 # no duplicate log rows. Otherwise, we need to remove the duplicates.
391 $queryBuilder->distinct();
392 }
393 } elseif ( array_key_exists( 'log_actor', $this->mConds ) ) {
394 // Optimizer doesn't pick the right index when a user has lots of log actions (T303089)
395 $index = 'log_actor_time';
396 foreach ( $this->getFilterParams() as $hide ) {
397 if ( !$hide ) {
398 $index = 'log_actor_type_time';
399 break;
400 }
401 }
402 $queryBuilder->useIndex( [ 'logging' => $index ] );
403 }
404
405 // T221458: MySQL/MariaDB (10.1.37) can sometimes irrationally decide that querying `actor` before
406 // `logging` and filesorting is somehow better than querying $limit+1 rows from `logging`.
407 // Tell it not to reorder the query. But not when tag filtering or log_search was used, as it
408 // seems as likely to be harmed as helped in that case.
409 if ( $this->mTagFilter === '' && !array_key_exists( 'ls_field', $this->mConds ) ) {
410 $queryBuilder->straightJoinOption();
411 }
412
414 if ( $maxExecTime ) {
415 $queryBuilder->setMaxExecutionTime( $maxExecTime );
416 }
417
418 # Add ChangeTags filter query
419 MediaWikiServices::getInstance()->getChangeTagsStore()->modifyDisplayQueryBuilder(
420 $queryBuilder,
421 'logging',
422 $this->mTagFilter,
423 $this->mTagInvert
424 );
425
426 return $queryBuilder->getQueryInfo();
427 }
428
434 protected function hasEqualsClause( $field ) {
435 return (
436 array_key_exists( $field, $this->mConds ) &&
437 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
438 );
439 }
440
441 public function getIndexField() {
442 return [ [ 'log_timestamp', 'log_id' ] ];
443 }
444
445 protected function doBatchLookups() {
446 $lb = $this->linkBatchFactory->newLinkBatch();
447 foreach ( $this->mResult as $row ) {
448 $lb->add( $row->log_namespace, $row->log_title );
449 $lb->add( NS_USER, $row->log_user_text );
450 $lb->add( NS_USER_TALK, $row->log_user_text );
451 $formatter = $this->logFormatterFactory->newFromRow( $row );
452 foreach ( $formatter->getPreloadTitles() as $title ) {
453 $lb->addObj( $title );
454 }
455 }
456 $lb->execute();
457 }
458
459 public function formatRow( $row ) {
460 return $this->mLogEventsList->logLine( $row );
461 }
462
463 public function getType() {
464 return $this->types;
465 }
466
472 public function getPerformer() {
473 return $this->performer;
474 }
475
479 public function getPage() {
480 return $this->page;
481 }
482
486 public function getPattern() {
487 return $this->pattern;
488 }
489
490 public function getYear() {
491 return $this->mYear;
492 }
493
494 public function getMonth() {
495 return $this->mMonth;
496 }
497
498 public function getDay() {
499 return $this->mDay;
500 }
501
502 public function getTagFilter() {
503 return $this->mTagFilter;
504 }
505
506 public function getTagInvert() {
507 return $this->mTagInvert;
508 }
509
510 public function getAction() {
511 return $this->action;
512 }
513
517 private function enforceActionRestrictions() {
518 if ( $this->actionRestrictionsEnforced ) {
519 return;
520 }
521 $this->actionRestrictionsEnforced = true;
522 if ( !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
523 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0';
524 } elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
525 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::SUPPRESSED_ACTION ) .
526 ' != ' . LogPage::SUPPRESSED_USER;
527 }
528 }
529
533 private function enforcePerformerRestrictions() {
534 // Same as enforceActionRestrictions(), except for _USER instead of _ACTION bits.
535 if ( $this->performerRestrictionsEnforced ) {
536 return;
537 }
538 $this->performerRestrictionsEnforced = true;
539 if ( !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
540 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0';
541 } elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
542 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::SUPPRESSED_USER ) .
544 }
545 }
546}
547
549class_alias( LogPager::class, 'LogPager' );
const NS_USER
Definition Defines.php:67
const NS_USER_TALK
Definition Defines.php:68
array $params
The job parameters.
A value class to process existing log entries.
static getExcludeClause( $db, $audience='public', ?Authority $performer=null)
SQL clause to skip forbidden log types for this user.
Class to simplify the use of log pages.
Definition LogPage.php:46
const SUPPRESSED_ACTION
Definition LogPage.php:54
A class containing constants representing the names of configuration variables.
const LogRestrictions
Name constant for the LogRestrictions setting, for use with Config::get()
const ActionFilteredLogs
Name constant for the ActionFilteredLogs setting, for use with Config::get()
const MaxExecutionTimeForExpensiveQueries
Name constant for the MaxExecutionTimeForExpensiveQueries setting, for use with Config::get()
const FilterLogTypes
Name constant for the FilterLogTypes setting, for use with Config::get()
const UserrightsInterwikiDelimiter
Name constant for the UserrightsInterwikiDelimiter setting, for use with Config::get()
const MiserMode
Name constant for the MiserMode setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
IReadableDatabase $mDb
getIndexField()
Returns the name of the index field.
Definition LogPager.php:441
getDefaultQuery()
Get an array of query parameters that should be put into self-links.
Definition LogPager.php:136
getPerformer()
Guaranteed to either return a valid title string or a Zero-Length String.
Definition LogPager.php:472
__construct( $list, $types=[], $performer='', $page='', $pattern=false, $conds=[], $year=false, $month=false, $day=false, $tagFilter='', $action='', $logId=0, ?LinkBatchFactory $linkBatchFactory=null, ?ActorNormalization $actorNormalization=null, ?LogFormatterFactory $logFormatterFactory=null, $tagInvert=false)
Definition LogPager.php:106
limitLogId( $logId)
Limit to the (single) specified log ID.
Definition LogPager.php:362
formatRow( $row)
Returns an HTML string representing the result row $row.
Definition LogPager.php:459
hasEqualsClause( $field)
Checks if $this->mConds has $field matched to a single value.
Definition LogPager.php:434
LogEventsList $mLogEventsList
Definition LogPager.php:79
doBatchLookups()
Called from getBody(), before getStartBody() is called and after doQuery() was called.
Definition LogPager.php:445
getQueryInfo()
Constructs the most part of the query.
Definition LogPager.php:374
IndexPager with a formatted navigation bar.
getDateCond( $year, $month, $day=-1)
Set and return the offset timestamp such that we can get all revisions with a timestamp up to the spe...
Represents a title within MediaWiki.
Definition Title.php:78
Content of like value.
Definition LikeValue.php:14
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
Service for dealing with the actor table.