MediaWiki master
LogPager.php
Go to the documentation of this file.
1<?php
26namespace MediaWiki\Pager;
27
30use LogFormatter;
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
104 public function __construct( $list, $types = [], $performer = '', $page = '',
105 $pattern = false, $conds = [], $year = false, $month = false, $day = false,
106 $tagFilter = '', $action = '', $logId = 0,
107 LinkBatchFactory $linkBatchFactory = null,
108 ActorNormalization $actorNormalization = null,
109 $tagInvert = false
110 ) {
111 parent::__construct( $list->getContext() );
112
113 $services = MediaWikiServices::getInstance();
114 $this->mConds = $conds;
115 $this->mLogEventsList = $list;
116
117 // Class is used directly in extensions - T266480
118 $this->linkBatchFactory = $linkBatchFactory ?? $services->getLinkBatchFactory();
119 $this->actorNormalization = $actorNormalization ?? $services->getActorNormalization();
120
121 $this->limitLogId( $logId ); // set before types per T269761
122 $this->limitType( $types ); // also excludes hidden types
123 $this->limitFilterTypes();
124 $this->limitPerformer( $performer );
125 $this->limitTitle( $page, $pattern );
126 $this->limitAction( $action );
127 $this->getDateCond( $year, $month, $day );
128 $this->mTagFilter = (string)$tagFilter;
129 $this->mTagInvert = (bool)$tagInvert;
130 }
131
132 public function getDefaultQuery() {
133 $query = parent::getDefaultQuery();
134 $query['type'] = $this->typeCGI; // arrays won't work here
135 $query['user'] = $this->performer;
136 $query['day'] = $this->mDay;
137 $query['month'] = $this->mMonth;
138 $query['year'] = $this->mYear;
139
140 return $query;
141 }
142
143 private function limitFilterTypes() {
144 if ( $this->hasEqualsClause( 'log_id' ) ) { // T220834
145 return;
146 }
147 $filterTypes = $this->getFilterParams();
148 foreach ( $filterTypes as $type => $hide ) {
149 if ( $hide ) {
150 $this->mConds[] = 'log_type != ' . $this->mDb->addQuotes( $type );
151 }
152 }
153 }
154
155 public function getFilterParams() {
156 $filters = [];
157 if ( count( $this->types ) ) {
158 return $filters;
159 }
160
161 // FIXME: This is broken, values from HTMLForm should be used.
162 $wpfilters = $this->getRequest()->getArray( "wpfilters" );
163 $filterLogTypes = $this->getConfig()->get( MainConfigNames::FilterLogTypes );
164
165 foreach ( $filterLogTypes as $type => $default ) {
166 // Back-compat: Check old URL params if the new param wasn't passed
167 if ( $wpfilters === null ) {
168 $hide = $this->getRequest()->getBool( "hide_{$type}_log", $default );
169 } else {
170 $hide = !in_array( $type, $wpfilters );
171 }
172
173 $filters[$type] = $hide;
174 }
175
176 return $filters;
177 }
178
186 private function limitType( $types ) {
187 $restrictions = $this->getConfig()->get( MainConfigNames::LogRestrictions );
188 // If $types is not an array, make it an array
189 $types = ( $types === '' ) ? [] : (array)$types;
190 // Don't even show header for private logs; don't recognize it...
191 $needReindex = false;
192 foreach ( $types as $type ) {
193 if ( isset( $restrictions[$type] )
194 && !$this->getAuthority()->isAllowed( $restrictions[$type] )
195 ) {
196 $needReindex = true;
197 $types = array_diff( $types, [ $type ] );
198 }
199 }
200 if ( $needReindex ) {
201 // Lots of this code makes assumptions that
202 // the first entry in the array is $types[0].
203 $types = array_values( $types );
204 }
205 $this->types = $types;
206 // Don't show private logs to unprivileged users.
207 // Also, only show them upon specific request to avoid surprises.
208 // Exception: if we are showing only a single log entry based on the log id,
209 // we don't require that "specific request" so that the links-in-logs feature
210 // works. See T269761
211 $audience = ( $types || $this->hasEqualsClause( 'log_id' ) ) ? 'user' : 'public';
212 $hideLogs = LogEventsList::getExcludeClause( $this->mDb, $audience, $this->getAuthority() );
213 if ( $hideLogs !== false ) {
214 $this->mConds[] = $hideLogs;
215 }
216 if ( count( $types ) ) {
217 $this->mConds['log_type'] = $types;
218 // Set typeCGI; used in url param for paging
219 if ( count( $types ) == 1 ) {
220 $this->typeCGI = $types[0];
221 }
222 }
223 }
224
231 private function limitPerformer( $name ) {
232 if ( $name == '' ) {
233 return;
234 }
235
236 $actorId = $this->actorNormalization->findActorIdByName( $name, $this->mDb );
237
238 if ( !$actorId ) {
239 // Unknown user, match nothing.
240 $this->mConds[] = '1 = 0';
241 return;
242 }
243
244 $this->mConds[ 'log_actor' ] = $actorId;
245
246 $this->enforcePerformerRestrictions();
247
248 $this->performer = $name;
249 }
250
259 private function limitTitle( $page, $pattern ) {
260 if ( !$page instanceof PageReference ) {
261 // NOTE: For some types of logs, the title may be something strange, like "User:#12345"!
262 $page = Title::newFromText( $page );
263 if ( !$page ) {
264 return;
265 }
266 }
267
268 $titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
269 $this->page = $titleFormatter->getPrefixedDBkey( $page );
270 $ns = $page->getNamespace();
271 $db = $this->mDb;
272
273 $interwikiDelimiter = $this->getConfig()->get( MainConfigNames::UserrightsInterwikiDelimiter );
274
275 $doUserRightsLogLike = false;
276 if ( $this->types == [ 'rights' ] ) {
277 $parts = explode( $interwikiDelimiter, $page->getDBkey() );
278 if ( count( $parts ) == 2 ) {
279 [ $name, $database ] = array_map( 'trim', $parts );
280 if ( str_contains( $database, '*' ) ) {
281 $doUserRightsLogLike = true;
282 }
283 }
284 }
285
299 $this->mConds['log_namespace'] = $ns;
300 if ( $doUserRightsLogLike ) {
301 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable $name is set when reached here
302 $params = [ $name . $interwikiDelimiter ];
303 // @phan-suppress-next-next-line PhanPossiblyUndeclaredVariable $database is set when reached here
304 // @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal $database is set when reached here
305 $databaseParts = explode( '*', $database );
306 $databasePartCount = count( $databaseParts );
307 foreach ( $databaseParts as $i => $databasepart ) {
308 $params[] = $databasepart;
309 if ( $i < $databasePartCount - 1 ) {
310 $params[] = $db->anyString();
311 }
312 }
313 $this->mConds[] = $db->expr( 'log_title', IExpression::LIKE, new LikeValue( ...$params ) );
314 } elseif ( $pattern && !$this->getConfig()->get( MainConfigNames::MiserMode ) ) {
315 $this->mConds[] = $db->expr(
316 'log_title',
317 IExpression::LIKE,
318 new LikeValue( $page->getDBkey(), $db->anyString() )
319 );
320 $this->pattern = $pattern;
321 } else {
322 $this->mConds['log_title'] = $page->getDBkey();
323 }
324 $this->enforceActionRestrictions();
325 }
326
332 private function limitAction( $action ) {
333 // Allow to filter the log by actions
334 $type = $this->typeCGI;
335 if ( $type === '' ) {
336 // nothing to do
337 return;
338 }
339 $actions = $this->getConfig()->get( MainConfigNames::ActionFilteredLogs );
340 if ( isset( $actions[$type] ) ) {
341 // log type can be filtered by actions
342 if ( $action !== '' && isset( $actions[$type][$action] ) ) {
343 // add condition to query
344 $this->mConds['log_action'] = $actions[$type][$action];
345 $this->action = $action;
346 }
347 }
348 }
349
354 protected function limitLogId( $logId ) {
355 if ( !$logId ) {
356 return;
357 }
358 $this->mConds['log_id'] = $logId;
359 }
360
366 public function getQueryInfo() {
367 $queryBuilder = DatabaseLogEntry::newSelectQueryBuilder( $this->mDb )
368 ->where( $this->mConds );
369
370 # Add log_search table if there are conditions on it.
371 # This filters the results to only include log rows that have
372 # log_search records with the specified ls_field and ls_value values.
373 if ( array_key_exists( 'ls_field', $this->mConds ) ) {
374 $queryBuilder->join( 'log_search', null, 'ls_log_id=log_id' );
375 $queryBuilder->ignoreIndex( [ 'log_search' => 'ls_log_id' ] );
376 $queryBuilder->useIndex( [ 'logging' => 'PRIMARY' ] );
377 if ( !$this->hasEqualsClause( 'ls_field' )
378 || !$this->hasEqualsClause( 'ls_value' )
379 ) {
380 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
381 # to match a specific (ls_field,ls_value) tuple, then there will be
382 # no duplicate log rows. Otherwise, we need to remove the duplicates.
383 $queryBuilder->distinct();
384 }
385 } elseif ( array_key_exists( 'log_actor', $this->mConds ) ) {
386 // Optimizer doesn't pick the right index when a user has lots of log actions (T303089)
387 $index = 'log_actor_time';
388 foreach ( $this->getFilterParams() as $hide ) {
389 if ( !$hide ) {
390 $index = 'log_actor_type_time';
391 break;
392 }
393 }
394 $queryBuilder->useIndex( [ 'logging' => $index ] );
395 }
396
397 // T221458: MySQL/MariaDB (10.1.37) can sometimes irrationally decide that querying `actor` before
398 // `logging` and filesorting is somehow better than querying $limit+1 rows from `logging`.
399 // Tell it not to reorder the query. But not when tag filtering or log_search was used, as it
400 // seems as likely to be harmed as helped in that case.
401 if ( $this->mTagFilter === '' && !array_key_exists( 'ls_field', $this->mConds ) ) {
402 $queryBuilder->option( 'STRAIGHT_JOIN' );
403 }
404
406 if ( $maxExecTime ) {
407 $queryBuilder->setMaxExecutionTime( $maxExecTime );
408 }
409
410 # Add ChangeTags filter query
411 MediaWikiServices::getInstance()->getChangeTagsStore()->modifyDisplayQueryBuilder(
412 $queryBuilder,
413 'logging',
414 $this->mTagFilter,
415 $this->mTagInvert
416 );
417
418 return $queryBuilder->getQueryInfo();
419 }
420
426 protected function hasEqualsClause( $field ) {
427 return (
428 array_key_exists( $field, $this->mConds ) &&
429 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
430 );
431 }
432
433 public function getIndexField() {
434 return [ [ 'log_timestamp', 'log_id' ] ];
435 }
436
437 protected function doBatchLookups() {
438 $lb = $this->linkBatchFactory->newLinkBatch();
439 foreach ( $this->mResult as $row ) {
440 $lb->add( $row->log_namespace, $row->log_title );
441 $lb->add( NS_USER, $row->log_user_text );
442 $lb->add( NS_USER_TALK, $row->log_user_text );
443 $formatter = LogFormatter::newFromRow( $row );
444 foreach ( $formatter->getPreloadTitles() as $title ) {
445 $lb->addObj( $title );
446 }
447 }
448 $lb->execute();
449 }
450
451 public function formatRow( $row ) {
452 return $this->mLogEventsList->logLine( $row );
453 }
454
455 public function getType() {
456 return $this->types;
457 }
458
464 public function getPerformer() {
465 return $this->performer;
466 }
467
471 public function getPage() {
472 return $this->page;
473 }
474
478 public function getPattern() {
479 return $this->pattern;
480 }
481
482 public function getYear() {
483 return $this->mYear;
484 }
485
486 public function getMonth() {
487 return $this->mMonth;
488 }
489
490 public function getDay() {
491 return $this->mDay;
492 }
493
494 public function getTagFilter() {
495 return $this->mTagFilter;
496 }
497
498 public function getTagInvert() {
499 return $this->mTagInvert;
500 }
501
502 public function getAction() {
503 return $this->action;
504 }
505
509 private function enforceActionRestrictions() {
510 if ( $this->actionRestrictionsEnforced ) {
511 return;
512 }
513 $this->actionRestrictionsEnforced = true;
514 if ( !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
515 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0';
516 } elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
517 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::SUPPRESSED_ACTION ) .
518 ' != ' . LogPage::SUPPRESSED_USER;
519 }
520 }
521
525 private function enforcePerformerRestrictions() {
526 // Same as enforceActionRestrictions(), except for _USER instead of _ACTION bits.
527 if ( $this->performerRestrictionsEnforced ) {
528 return;
529 }
530 $this->performerRestrictionsEnforced = true;
531 if ( !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
532 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0';
533 } elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
534 $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::SUPPRESSED_USER ) .
536 }
537 }
538}
539
541class_alias( LogPager::class, 'LogPager' );
const NS_USER
Definition Defines.php:66
const NS_USER_TALK
Definition Defines.php:67
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.
Implements the default log formatting.
Class to simplify the use of log pages.
Definition LogPage.php:44
const SUPPRESSED_ACTION
Definition LogPage.php:52
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:433
getDefaultQuery()
Get an array of query parameters that should be put into self-links.
Definition LogPager.php:132
getPerformer()
Guaranteed to either return a valid title string or a Zero-Length String.
Definition LogPager.php:464
__construct( $list, $types=[], $performer='', $page='', $pattern=false, $conds=[], $year=false, $month=false, $day=false, $tagFilter='', $action='', $logId=0, LinkBatchFactory $linkBatchFactory=null, ActorNormalization $actorNormalization=null, $tagInvert=false)
Definition LogPager.php:104
limitLogId( $logId)
Limit to the (single) specified log ID.
Definition LogPager.php:354
formatRow( $row)
Returns an HTML string representing the result row $row.
Definition LogPager.php:451
hasEqualsClause( $field)
Checks if $this->mConds has $field matched to a single value.
Definition LogPager.php:426
LogEventsList $mLogEventsList
Definition LogPager.php:79
doBatchLookups()
Called from getBody(), before getStartBody() is called and after doQuery() was called.
Definition LogPager.php:437
getQueryInfo()
Constructs the most part of the query.
Definition LogPager.php:366
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.