3 * Contain classes to list log entries
5 * Copyright © 2004 Brooke Vibber <bvibber@wikimedia.org>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
26 namespace MediaWiki\Pager
;
30 use LogFormatterFactory
;
32 use MediaWiki\Cache\LinkBatchFactory
;
33 use MediaWiki\MainConfigNames
;
34 use MediaWiki\MediaWikiServices
;
35 use MediaWiki\Page\PageReference
;
36 use MediaWiki\Title\Title
;
37 use MediaWiki\User\ActorNormalization
;
38 use Wikimedia\Rdbms\IExpression
;
39 use Wikimedia\Rdbms\LikeValue
;
44 class LogPager
extends ReverseChronologicalPager
{
45 /** @var array Log types */
48 /** @var string Events limited to those by performer when set */
49 private $performer = '';
51 /** @var string Events limited to those about this page when set */
55 private $pattern = false;
58 private $typeCGI = '';
64 private $performerRestrictionsEnforced = false;
67 private $actionRestrictionsEnforced = false;
78 /** @var LogEventsList */
79 public $mLogEventsList;
81 /** @var LinkBatchFactory */
82 private $linkBatchFactory;
84 /** @var ActorNormalization */
85 private $actorNormalization;
86 private LogFormatterFactory
$logFormatterFactory;
89 * @param LogEventsList $list
90 * @param string|array $types Log types to show
91 * @param string $performer The user who made the log entries
92 * @param string|PageReference $page The page the log entries are for
93 * @param bool $pattern Do a prefix search rather than an exact title match
94 * @param array $conds Extra conditions for the query
95 * @param int|bool $year The year to start from. Default: false
96 * @param int|bool $month The month to start from. Default: false
97 * @param int|bool $day The day to start from. Default: false
98 * @param string $tagFilter Tag
99 * @param string $action Specific action (subtype) requested
100 * @param int $logId Log entry ID, to limit to a single log entry.
101 * @param LinkBatchFactory|null $linkBatchFactory
102 * @param ActorNormalization|null $actorNormalization
103 * @param LogFormatterFactory|null $logFormatterFactory
104 * @param bool $tagInvert whether tags are filtered for (false) or out (true)
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,
114 parent
::__construct( $list->getContext() );
116 $services = MediaWikiServices
::getInstance();
117 $this->mConds
= $conds;
118 $this->mLogEventsList
= $list;
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();
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;
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
;
147 private function limitFilterTypes() {
148 if ( $this->hasEqualsClause( 'log_id' ) ) { // T220834
151 $filterTypes = $this->getFilterParams();
153 foreach ( $filterTypes as $type => $hide ) {
155 $excludeTypes[] = $type;
158 if ( $excludeTypes ) {
159 $this->mConds
[] = $this->mDb
->expr( 'log_type', '!=', $excludeTypes );
163 public function getFilterParams() {
165 if ( count( $this->types
) ) {
169 // FIXME: This is broken, values from HTMLForm should be used.
170 $wpfilters = $this->getRequest()->getArray( "wpfilters" );
171 $filterLogTypes = $this->getConfig()->get( MainConfigNames
::FilterLogTypes
);
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 );
178 $hide = !in_array( $type, $wpfilters );
181 $filters[$type] = $hide;
188 * Set the log reader to return only entries of the given type.
189 * Type restrictions enforced here
191 * @param string|array $types Log types ('upload', 'delete', etc);
192 * empty string means no restriction
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] )
205 $types = array_diff( $types, [ $type ] );
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 );
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;
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];
234 * Set the log reader to return only entries by the given user.
236 * @param string $name (In)valid user name
239 private function limitPerformer( $name ) {
244 $actorId = $this->actorNormalization
->findActorIdByName( $name, $this->mDb
);
247 // Unknown user, match nothing.
248 $this->mConds
[] = '1 = 0';
252 $this->mConds
[ 'log_actor' ] = $actorId;
254 $this->enforcePerformerRestrictions();
256 $this->performer
= $name;
260 * Set the log reader to return only entries affecting the given page.
261 * (For the block and rights logs, this is a user page.)
263 * @param string|PageReference $page
264 * @param bool $pattern
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 );
276 $titleFormatter = MediaWikiServices
::getInstance()->getTitleFormatter();
277 $this->page
= $titleFormatter->getPrefixedDBkey( $page );
278 $ns = $page->getNamespace();
281 $interwikiDelimiter = $this->getConfig()->get( MainConfigNames
::UserrightsInterwikiDelimiter
);
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;
295 * Using the (log_namespace, log_title, log_timestamp) index with a
296 * range scan (LIKE) on the first two parts, instead of simple equality,
297 * makes it unusable for sorting. Sorted retrieval using another index
298 * would be possible, but then we might have to scan arbitrarily many
299 * nodes of that index. Therefore, we need to avoid this if $wgMiserMode
302 * This is not a problem with simple title matches, because then we can
303 * use the log_page_time index. That should have no more than a few hundred
304 * log entries for even the busiest pages, so it can be safely scanned
305 * in full to satisfy an impossible condition on user or similar.
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();
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(
326 new LikeValue( $page->getDBkey(), $db->anyString() )
328 $this->pattern
= $pattern;
330 $this->mConds
['log_title'] = $page->getDBkey();
332 $this->enforceActionRestrictions();
336 * Set the log_action field to a specified value (or values)
338 * @param string $action
340 private function limitAction( $action ) {
341 // Allow to filter the log by actions
342 $type = $this->typeCGI
;
343 if ( $type === '' ) {
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;
359 * Limit to the (single) specified log ID.
360 * @param int $logId The log entry ID.
362 protected function limitLogId( $logId ) {
366 $this->mConds
['log_id'] = $logId;
370 * Constructs the most part of the query. Extra conditions are sprinkled in
371 * all over this class.
374 public function getQueryInfo() {
375 $queryBuilder = DatabaseLogEntry
::newSelectQueryBuilder( $this->mDb
)
376 ->where( $this->mConds
);
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' )
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();
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 ) {
398 $index = 'log_actor_type_time';
402 $queryBuilder->useIndex( [ 'logging' => $index ] );
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();
413 $maxExecTime = $this->getConfig()->get( MainConfigNames
::MaxExecutionTimeForExpensiveQueries
);
414 if ( $maxExecTime ) {
415 $queryBuilder->setMaxExecutionTime( $maxExecTime );
418 # Add ChangeTags filter query
419 MediaWikiServices
::getInstance()->getChangeTagsStore()->modifyDisplayQueryBuilder(
426 return $queryBuilder->getQueryInfo();
430 * Checks if $this->mConds has $field matched to a *single* value
431 * @param string $field
434 protected function hasEqualsClause( $field ) {
436 array_key_exists( $field, $this->mConds
) &&
437 ( !is_array( $this->mConds
[$field] ) ||
count( $this->mConds
[$field] ) == 1 )
441 public function getIndexField() {
442 return [ [ 'log_timestamp', 'log_id' ] ];
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 );
459 public function formatRow( $row ) {
460 return $this->mLogEventsList
->logLine( $row );
463 public function getType() {
468 * Guaranteed to either return a valid title string or a Zero-Length String
472 public function getPerformer() {
473 return $this->performer
;
479 public function getPage() {
486 public function getPattern() {
487 return $this->pattern
;
490 public function getYear() {
494 public function getMonth() {
495 return $this->mMonth
;
498 public function getDay() {
502 public function getTagFilter() {
503 return $this->mTagFilter
;
506 public function getTagInvert() {
507 return $this->mTagInvert
;
510 public function getAction() {
511 return $this->action
;
515 * Paranoia: avoid brute force searches (T19342)
517 private function enforceActionRestrictions() {
518 if ( $this->actionRestrictionsEnforced
) {
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
;
531 * Paranoia: avoid brute force searches (T19342)
533 private function enforcePerformerRestrictions() {
534 // Same as enforceActionRestrictions(), except for _USER instead of _ACTION bits.
535 if ( $this->performerRestrictionsEnforced
) {
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
) .
543 ' != ' . LogPage
::SUPPRESSED_ACTION
;
548 /** @deprecated class alias since 1.41 */
549 class_alias( LogPager
::class, 'LogPager' );