50 private $performer =
'';
56 private $pattern =
false;
59 private $typeCGI =
'';
65 private $performerRestrictionsEnforced =
false;
68 private $actionRestrictionsEnforced =
false;
83 private $linkBatchFactory;
86 private $actorNormalization;
107 public function __construct( $list, $types = [], $performer =
'', $page =
'',
108 $pattern =
false, $conds = [], $year =
false, $month =
false, $day =
false,
109 $tagFilter =
'', $action =
'', $logId = 0,
115 parent::__construct( $list->getContext() );
118 $this->mConds = $conds;
119 $this->mLogEventsList = $list;
122 $this->linkBatchFactory = $linkBatchFactory ?? $services->getLinkBatchFactory();
123 $this->actorNormalization = $actorNormalization ?? $services->getActorNormalization();
124 $this->logFormatterFactory = $logFormatterFactory ?? $services->getLogFormatterFactory();
127 $this->limitType( $types );
128 $this->limitFilterTypes();
129 $this->limitPerformer( $performer );
130 $this->limitTitle( $page, $pattern );
131 $this->limitAction( $action );
133 $this->mTagFilter = (string)$tagFilter;
134 $this->mTagInvert = (bool)$tagInvert;
138 $query = parent::getDefaultQuery();
139 $query[
'type'] = $this->typeCGI;
140 $query[
'user'] = $this->performer;
148 private function limitFilterTypes() {
154 foreach ( $filterTypes as $type => $hide ) {
156 $excludeTypes[] = $type;
159 if ( $excludeTypes ) {
160 $this->mConds[] = $this->mDb->expr(
'log_type',
'!=', $excludeTypes );
166 if ( count( $this->types ) ) {
171 $wpfilters = $this->
getRequest()->getArray(
"wpfilters" );
174 foreach ( $filterLogTypes as $type => $default ) {
176 if ( $wpfilters ===
null ) {
177 $hide = $this->
getRequest()->getBool(
"hide_{$type}_log", $default );
179 $hide = !in_array( $type, $wpfilters );
182 $filters[$type] = $hide;
195 private function limitType( $types ) {
198 $types = ( $types ===
'' ) ? [] : (array)$types;
200 $needReindex =
false;
201 foreach ( $types as $type ) {
202 if ( isset( $restrictions[$type] )
203 && !$this->
getAuthority()->isAllowed( $restrictions[$type] )
206 $types = array_diff( $types, [ $type ] );
209 if ( $needReindex ) {
212 $types = array_values( $types );
214 $this->types = $types;
220 $audience = ( $types || $this->
hasEqualsClause(
'log_id' ) ) ?
'user' :
'public';
221 $hideLogs = LogEventsList::getExcludeClause( $this->mDb, $audience, $this->
getAuthority() );
222 if ( $hideLogs !==
false ) {
223 $this->mConds[] = $hideLogs;
225 if ( count( $types ) ) {
226 $this->mConds[
'log_type'] = $types;
228 if ( count( $types ) == 1 ) {
229 $this->typeCGI = $types[0];
240 private function limitPerformer( $name ) {
245 $actorId = $this->actorNormalization->findActorIdByName( $name, $this->mDb );
249 $this->mConds[] =
'1 = 0';
253 $this->mConds[
'log_actor' ] = $actorId;
255 $this->enforcePerformerRestrictions();
257 $this->performer = $name;
268 private function limitTitle( $page, $pattern ) {
269 if ( !$page instanceof PageReference ) {
271 $page = Title::newFromText( $page );
278 $this->page = $titleFormatter->getPrefixedDBkey( $page );
279 $ns = $page->getNamespace();
284 $doUserRightsLogLike =
false;
285 if ( $this->types == [
'rights' ] ) {
286 $parts = explode( $interwikiDelimiter, $page->getDBkey() );
287 if ( count( $parts ) == 2 ) {
288 [ $name, $database ] = array_map(
'trim', $parts );
289 if ( str_contains( $database,
'*' ) ) {
290 $doUserRightsLogLike =
true;
308 $this->mConds[
'log_namespace'] = $ns;
309 if ( $doUserRightsLogLike ) {
311 $params = [ $name . $interwikiDelimiter ];
314 $databaseParts = explode(
'*', $database );
315 $databasePartCount = count( $databaseParts );
316 foreach ( $databaseParts as $i => $databasepart ) {
318 if ( $i < $databasePartCount - 1 ) {
322 $this->mConds[] = $db->expr(
'log_title', IExpression::LIKE,
new LikeValue( ...$params ) );
324 $this->mConds[] = $db->expr(
327 new LikeValue( $page->getDBkey(), $db->anyString() )
329 $this->pattern = $pattern;
331 $this->mConds[
'log_title'] = $page->getDBkey();
333 $this->enforceActionRestrictions();
341 private function limitAction( $action ) {
343 $type = $this->typeCGI;
344 if ( $type ===
'' ) {
349 if ( isset( $actions[$type] ) ) {
351 if ( $action !==
'' && isset( $actions[$type][$action] ) ) {
353 $this->mConds[
'log_action'] = $actions[$type][$action];
354 $this->action = $action;
367 $this->mConds[
'log_id'] = $logId;
376 $queryBuilder = DatabaseLogEntry::newSelectQueryBuilder( $this->mDb )
377 ->where( $this->mConds );
379 # Add log_search table if there are conditions on it.
380 # This filters the results to only include log rows that have
381 # log_search records with the specified ls_field and ls_value values.
382 if ( array_key_exists(
'ls_field', $this->mConds ) ) {
383 $queryBuilder->join(
'log_search',
null,
'ls_log_id=log_id' );
384 $queryBuilder->ignoreIndex( [
'log_search' =>
'ls_log_id' ] );
385 $queryBuilder->useIndex( [
'logging' =>
'PRIMARY' ] );
389 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
390 # to match a specific (ls_field,ls_value) tuple, then there will be
391 # no duplicate log rows. Otherwise, we need to remove the duplicates.
392 $queryBuilder->distinct();
394 } elseif ( array_key_exists(
'log_actor', $this->mConds ) ) {
396 $index =
'log_actor_time';
399 $index =
'log_actor_type_time';
403 $queryBuilder->useIndex( [
'logging' => $index ] );
410 if ( $this->mTagFilter ===
'' && !array_key_exists(
'ls_field', $this->mConds ) ) {
411 $queryBuilder->straightJoinOption();
415 if ( $maxExecTime ) {
416 $queryBuilder->setMaxExecutionTime( $maxExecTime );
419 # Add ChangeTags filter query
427 return $queryBuilder->getQueryInfo();
437 array_key_exists( $field, $this->mConds ) &&
438 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
443 return [ [
'log_timestamp',
'log_id' ] ];
447 $lb = $this->linkBatchFactory->newLinkBatch()->setCaller( __METHOD__ );
448 foreach ( $this->mResult as $row ) {
449 $lb->add( $row->log_namespace, $row->log_title );
450 $lb->addUser(
new UserIdentityValue( (
int)$row->log_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_ACTION;
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 ) .
543 ' != ' . LogPage::SUPPRESSED_USER;