49 private $performer =
'';
55 private $pattern =
false;
58 private $typeCGI =
'';
64 private $performerRestrictionsEnforced =
false;
67 private $actionRestrictionsEnforced =
false;
82 private $linkBatchFactory;
85 private $actorNormalization;
104 public function __construct( $list, $types = [], $performer =
'', $page =
'',
105 $pattern =
false, $conds = [], $year =
false, $month =
false, $day =
false,
106 $tagFilter =
'', $action =
'', $logId = 0,
111 parent::__construct( $list->getContext() );
114 $this->mConds = $conds;
115 $this->mLogEventsList = $list;
118 $this->linkBatchFactory = $linkBatchFactory ?? $services->getLinkBatchFactory();
119 $this->actorNormalization = $actorNormalization ?? $services->getActorNormalization();
122 $this->limitType( $types );
123 $this->limitFilterTypes();
124 $this->limitPerformer( $performer );
125 $this->limitTitle( $page, $pattern );
126 $this->limitAction( $action );
128 $this->mTagFilter = (string)$tagFilter;
129 $this->mTagInvert = (bool)$tagInvert;
133 $query = parent::getDefaultQuery();
134 $query[
'type'] = $this->typeCGI;
135 $query[
'user'] = $this->performer;
143 private function limitFilterTypes() {
149 foreach ( $filterTypes as $type => $hide ) {
151 $excludeTypes[] = $type;
154 if ( $excludeTypes ) {
155 $this->mConds[] = $this->mDb->expr(
'log_type',
'!=', $excludeTypes );
161 if ( count( $this->types ) ) {
166 $wpfilters = $this->
getRequest()->getArray(
"wpfilters" );
169 foreach ( $filterLogTypes as $type => $default ) {
171 if ( $wpfilters ===
null ) {
172 $hide = $this->
getRequest()->getBool(
"hide_{$type}_log", $default );
174 $hide = !in_array( $type, $wpfilters );
177 $filters[$type] = $hide;
190 private function limitType( $types ) {
193 $types = ( $types ===
'' ) ? [] : (array)$types;
195 $needReindex =
false;
196 foreach ( $types as $type ) {
197 if ( isset( $restrictions[$type] )
198 && !$this->
getAuthority()->isAllowed( $restrictions[$type] )
201 $types = array_diff( $types, [ $type ] );
204 if ( $needReindex ) {
207 $types = array_values( $types );
209 $this->types = $types;
215 $audience = ( $types || $this->
hasEqualsClause(
'log_id' ) ) ?
'user' :
'public';
217 if ( $hideLogs !==
false ) {
218 $this->mConds[] = $hideLogs;
220 if ( count( $types ) ) {
221 $this->mConds[
'log_type'] = $types;
223 if ( count( $types ) == 1 ) {
224 $this->typeCGI = $types[0];
235 private function limitPerformer( $name ) {
240 $actorId = $this->actorNormalization->findActorIdByName( $name, $this->mDb );
244 $this->mConds[] =
'1 = 0';
248 $this->mConds[
'log_actor' ] = $actorId;
250 $this->enforcePerformerRestrictions();
252 $this->performer = $name;
263 private function limitTitle( $page, $pattern ) {
264 if ( !$page instanceof PageReference ) {
266 $page = Title::newFromText( $page );
273 $this->page = $titleFormatter->getPrefixedDBkey( $page );
274 $ns = $page->getNamespace();
279 $doUserRightsLogLike =
false;
280 if ( $this->types == [
'rights' ] ) {
281 $parts = explode( $interwikiDelimiter, $page->getDBkey() );
282 if ( count( $parts ) == 2 ) {
283 [ $name, $database ] = array_map(
'trim', $parts );
284 if ( str_contains( $database,
'*' ) ) {
285 $doUserRightsLogLike =
true;
303 $this->mConds[
'log_namespace'] = $ns;
304 if ( $doUserRightsLogLike ) {
306 $params = [ $name . $interwikiDelimiter ];
309 $databaseParts = explode(
'*', $database );
310 $databasePartCount = count( $databaseParts );
311 foreach ( $databaseParts as $i => $databasepart ) {
313 if ( $i < $databasePartCount - 1 ) {
317 $this->mConds[] = $db->expr(
'log_title', IExpression::LIKE,
new LikeValue( ...
$params ) );
319 $this->mConds[] = $db->expr(
322 new LikeValue( $page->getDBkey(), $db->anyString() )
324 $this->pattern = $pattern;
326 $this->mConds[
'log_title'] = $page->getDBkey();
328 $this->enforceActionRestrictions();
336 private function limitAction( $action ) {
338 $type = $this->typeCGI;
339 if ( $type ===
'' ) {
344 if ( isset( $actions[$type] ) ) {
346 if ( $action !==
'' && isset( $actions[$type][$action] ) ) {
348 $this->mConds[
'log_action'] = $actions[$type][$action];
349 $this->action = $action;
362 $this->mConds[
'log_id'] = $logId;
371 $queryBuilder = DatabaseLogEntry::newSelectQueryBuilder( $this->mDb )
372 ->where( $this->mConds );
374 # Add log_search table if there are conditions on it.
375 # This filters the results to only include log rows that have
376 # log_search records with the specified ls_field and ls_value values.
377 if ( array_key_exists(
'ls_field', $this->mConds ) ) {
378 $queryBuilder->join(
'log_search',
null,
'ls_log_id=log_id' );
379 $queryBuilder->ignoreIndex( [
'log_search' =>
'ls_log_id' ] );
380 $queryBuilder->useIndex( [
'logging' =>
'PRIMARY' ] );
384 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
385 # to match a specific (ls_field,ls_value) tuple, then there will be
386 # no duplicate log rows. Otherwise, we need to remove the duplicates.
387 $queryBuilder->distinct();
389 } elseif ( array_key_exists(
'log_actor', $this->mConds ) ) {
391 $index =
'log_actor_time';
394 $index =
'log_actor_type_time';
398 $queryBuilder->useIndex( [
'logging' => $index ] );
405 if ( $this->mTagFilter ===
'' && !array_key_exists(
'ls_field', $this->mConds ) ) {
406 $queryBuilder->straightJoinOption();
410 if ( $maxExecTime ) {
411 $queryBuilder->setMaxExecutionTime( $maxExecTime );
414 # Add ChangeTags filter query
422 return $queryBuilder->getQueryInfo();
432 array_key_exists( $field, $this->mConds ) &&
433 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
438 return [ [
'log_timestamp',
'log_id' ] ];
442 $lb = $this->linkBatchFactory->newLinkBatch();
443 foreach ( $this->mResult as $row ) {
444 $lb->add( $row->log_namespace, $row->log_title );
445 $lb->add(
NS_USER, $row->log_user_text );
447 $formatter = LogFormatter::newFromRow( $row );
448 foreach ( $formatter->getPreloadTitles() as $title ) {
449 $lb->addObj( $title );
456 return $this->mLogEventsList->logLine( $row );
469 return $this->performer;
483 return $this->pattern;
499 return $this->mTagFilter;
503 return $this->mTagInvert;
507 return $this->action;
513 private function enforceActionRestrictions() {
514 if ( $this->actionRestrictionsEnforced ) {
517 $this->actionRestrictionsEnforced =
true;
518 if ( !$this->
getAuthority()->isAllowed(
'deletedhistory' ) ) {
519 $this->mConds[] = $this->mDb->bitAnd(
'log_deleted', LogPage::DELETED_ACTION ) .
' = 0';
520 } elseif ( !$this->
getAuthority()->isAllowedAny(
'suppressrevision',
'viewsuppressed' ) ) {
521 $this->mConds[] = $this->mDb->bitAnd(
'log_deleted', LogPage::SUPPRESSED_ACTION ) .
522 ' != ' . LogPage::SUPPRESSED_USER;
529 private function enforcePerformerRestrictions() {
531 if ( $this->performerRestrictionsEnforced ) {
534 $this->performerRestrictionsEnforced =
true;
535 if ( !$this->
getAuthority()->isAllowed(
'deletedhistory' ) ) {
536 $this->mConds[] = $this->mDb->bitAnd(
'log_deleted', LogPage::DELETED_USER ) .
' = 0';
537 } elseif ( !$this->
getAuthority()->isAllowedAny(
'suppressrevision',
'viewsuppressed' ) ) {
538 $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.