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