49 private $performer =
'';
55 private $pattern =
false;
58 private $typeCGI =
'';
64 private $performerRestrictionsEnforced =
false;
67 private $actionRestrictionsEnforced =
false;
82 private $linkBatchFactory;
85 private $actorNormalization;
106 public function __construct( $list, $types = [], $performer =
'', $page =
'',
107 $pattern =
false, $conds = [], $year =
false, $month =
false, $day =
false,
108 $tagFilter =
'', $action =
'', $logId = 0,
114 parent::__construct( $list->getContext() );
117 $this->mConds = $conds;
118 $this->mLogEventsList = $list;
121 $this->linkBatchFactory = $linkBatchFactory ?? $services->getLinkBatchFactory();
122 $this->actorNormalization = $actorNormalization ?? $services->getActorNormalization();
123 $this->logFormatterFactory = $logFormatterFactory ?? $services->getLogFormatterFactory();
126 $this->limitType( $types );
127 $this->limitFilterTypes();
128 $this->limitPerformer( $performer );
129 $this->limitTitle( $page, $pattern );
130 $this->limitAction( $action );
132 $this->mTagFilter = (string)$tagFilter;
133 $this->mTagInvert = (bool)$tagInvert;
137 $query = parent::getDefaultQuery();
138 $query[
'type'] = $this->typeCGI;
139 $query[
'user'] = $this->performer;
147 private function limitFilterTypes() {
153 foreach ( $filterTypes as $type => $hide ) {
155 $excludeTypes[] = $type;
158 if ( $excludeTypes ) {
159 $this->mConds[] = $this->mDb->expr(
'log_type',
'!=', $excludeTypes );
165 if ( count( $this->types ) ) {
170 $wpfilters = $this->
getRequest()->getArray(
"wpfilters" );
173 foreach ( $filterLogTypes as $type => $default ) {
175 if ( $wpfilters ===
null ) {
176 $hide = $this->
getRequest()->getBool(
"hide_{$type}_log", $default );
178 $hide = !in_array( $type, $wpfilters );
181 $filters[$type] = $hide;
194 private function limitType( $types ) {
197 $types = ( $types ===
'' ) ? [] : (array)$types;
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 ) {
211 $types = array_values( $types );
213 $this->types = $types;
219 $audience = ( $types || $this->
hasEqualsClause(
'log_id' ) ) ?
'user' :
'public';
221 if ( $hideLogs !==
false ) {
222 $this->mConds[] = $hideLogs;
224 if ( count( $types ) ) {
225 $this->mConds[
'log_type'] = $types;
227 if ( count( $types ) == 1 ) {
228 $this->typeCGI = $types[0];
239 private function limitPerformer( $name ) {
244 $actorId = $this->actorNormalization->findActorIdByName( $name, $this->mDb );
248 $this->mConds[] =
'1 = 0';
252 $this->mConds[
'log_actor' ] = $actorId;
254 $this->enforcePerformerRestrictions();
256 $this->performer = $name;
267 private function limitTitle( $page, $pattern ) {
268 if ( !$page instanceof PageReference ) {
270 $page = Title::newFromText( $page );
277 $this->page = $titleFormatter->getPrefixedDBkey( $page );
278 $ns = $page->getNamespace();
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;
307 $this->mConds[
'log_namespace'] = $ns;
308 if ( $doUserRightsLogLike ) {
310 $params = [ $name . $interwikiDelimiter ];
313 $databaseParts = explode(
'*', $database );
314 $databasePartCount = count( $databaseParts );
315 foreach ( $databaseParts as $i => $databasepart ) {
317 if ( $i < $databasePartCount - 1 ) {
321 $this->mConds[] = $db->expr(
'log_title', IExpression::LIKE,
new LikeValue( ...
$params ) );
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();
340 private function limitAction( $action ) {
342 $type = $this->typeCGI;
343 if ( $type ===
'' ) {
348 if ( isset( $actions[$type] ) ) {
350 if ( $action !==
'' && isset( $actions[$type][$action] ) ) {
352 $this->mConds[
'log_action'] = $actions[$type][$action];
353 $this->action = $action;
366 $this->mConds[
'log_id'] = $logId;
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' ] );
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 ) ) {
395 $index =
'log_actor_time';
398 $index =
'log_actor_type_time';
402 $queryBuilder->useIndex( [
'logging' => $index ] );
409 if ( $this->mTagFilter ===
'' && !array_key_exists(
'ls_field', $this->mConds ) ) {
410 $queryBuilder->straightJoinOption();
414 if ( $maxExecTime ) {
415 $queryBuilder->setMaxExecutionTime( $maxExecTime );
418 # Add ChangeTags filter query
426 return $queryBuilder->getQueryInfo();
436 array_key_exists( $field, $this->mConds ) &&
437 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
442 return [ [
'log_timestamp',
'log_id' ] ];
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 );
451 $formatter = $this->logFormatterFactory->newFromRow( $row );
452 foreach ( $formatter->getPreloadTitles() as $title ) {
453 $lb->addObj( $title );
460 return $this->mLogEventsList->logLine( $row );
473 return $this->performer;
487 return $this->pattern;
503 return $this->mTagFilter;
507 return $this->mTagInvert;
511 return $this->action;
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;
533 private function enforcePerformerRestrictions() {
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 ) .
A value class to process existing log entries.
Class to simplify the use of log pages.