47 private $performer =
'';
53 private $pattern =
false;
56 private $typeCGI =
'';
62 private $performerRestrictionsEnforced =
false;
65 private $actionRestrictionsEnforced =
false;
80 private $linkBatchFactory;
83 private $actorNormalization;
102 public function __construct( $list, $types = [], $performer =
'', $page =
'',
103 $pattern =
false, $conds = [], $year =
false, $month =
false, $day =
false,
104 $tagFilter =
'', $action =
'', $logId = 0,
109 parent::__construct( $list->getContext() );
112 $this->mConds = $conds;
113 $this->mLogEventsList = $list;
116 $this->linkBatchFactory = $linkBatchFactory ?? $services->getLinkBatchFactory();
117 $this->actorNormalization = $actorNormalization ?? $services->getActorNormalization();
120 $this->limitType( $types );
121 $this->limitFilterTypes();
122 $this->limitPerformer( $performer );
123 $this->limitTitle( $page, $pattern );
124 $this->limitAction( $action );
126 $this->mTagFilter = (string)$tagFilter;
127 $this->mTagInvert = (bool)$tagInvert;
131 $query = parent::getDefaultQuery();
132 $query[
'type'] = $this->typeCGI;
133 $query[
'user'] = $this->performer;
141 private function limitFilterTypes() {
146 foreach ( $filterTypes as $type => $hide ) {
148 $this->mConds[] =
'log_type != ' . $this->mDb->addQuotes( $type );
155 if ( count( $this->types ) ) {
160 $wpfilters = $this->
getRequest()->getArray(
"wpfilters" );
163 foreach ( $filterLogTypes as $type => $default ) {
165 if ( $wpfilters ===
null ) {
166 $hide = $this->
getRequest()->getBool(
"hide_{$type}_log", $default );
168 $hide = !in_array( $type, $wpfilters );
171 $filters[$type] = $hide;
184 private function limitType( $types ) {
187 $types = ( $types ===
'' ) ? [] : (array)$types;
189 $needReindex =
false;
190 foreach ( $types as $type ) {
191 if ( isset( $restrictions[$type] )
192 && !$this->
getAuthority()->isAllowed( $restrictions[$type] )
195 $types = array_diff( $types, [ $type ] );
198 if ( $needReindex ) {
201 $types = array_values( $types );
203 $this->types = $types;
209 $audience = ( $types || $this->
hasEqualsClause(
'log_id' ) ) ?
'user' :
'public';
211 if ( $hideLogs !==
false ) {
212 $this->mConds[] = $hideLogs;
214 if ( count( $types ) ) {
215 $this->mConds[
'log_type'] = $types;
217 if ( count( $types ) == 1 ) {
218 $this->typeCGI = $types[0];
229 private function limitPerformer( $name ) {
234 $actorId = $this->actorNormalization->findActorIdByName( $name, $this->mDb );
238 $this->mConds[] =
'1 = 0';
242 $this->mConds[
'log_actor' ] = $actorId;
244 $this->enforcePerformerRestrictions();
246 $this->performer = $name;
257 private function limitTitle( $page, $pattern ) {
258 if ( !$page instanceof PageReference ) {
260 $page = Title::newFromText( $page );
267 $this->page = $titleFormatter->getPrefixedDBkey( $page );
268 $ns = $page->getNamespace();
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,
'*' ) ) {
279 $doUserRightsLogLike =
true;
297 $this->mConds[
'log_namespace'] = $ns;
298 if ( $doUserRightsLogLike ) {
300 $params = [ $name . $interwikiDelimiter ];
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();
311 $this->mConds[] =
'log_title' . $db->buildLike( ...$params );
313 $this->mConds[] =
'log_title' . $db->buildLike( $page->getDBkey(), $db->anyString() );
314 $this->pattern = $pattern;
316 $this->mConds[
'log_title'] = $page->getDBkey();
318 $this->enforceActionRestrictions();
326 private function limitAction( $action ) {
328 $type = $this->typeCGI;
329 if ( $type ===
'' ) {
334 if ( isset( $actions[$type] ) ) {
336 if ( $action !==
'' && isset( $actions[$type][$action] ) ) {
338 $this->mConds[
'log_action'] = $actions[$type][$action];
339 $this->action = $action;
352 $this->mConds[
'log_id'] = $logId;
361 $queryBuilder = DatabaseLogEntry::newSelectQueryBuilder( $this->mDb )
362 ->where( $this->mConds );
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' ] );
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();
379 } elseif ( array_key_exists(
'log_actor', $this->mConds ) ) {
381 $index =
'log_actor_time';
384 $index =
'log_actor_type_time';
388 $queryBuilder->useIndex( [
'logging' => $index ] );
395 if ( $this->mTagFilter ===
'' && !array_key_exists(
'ls_field', $this->mConds ) ) {
396 $queryBuilder->option(
'STRAIGHT_JOIN' );
400 if ( $maxExecTime ) {
401 $queryBuilder->setMaxExecutionTime( $maxExecTime );
404 # Add ChangeTags filter query
412 return $queryBuilder->getQueryInfo();
422 array_key_exists( $field, $this->mConds ) &&
423 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
428 return [ [
'log_timestamp',
'log_id' ] ];
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 );
437 $formatter = LogFormatter::newFromRow( $row );
438 foreach ( $formatter->getPreloadTitles() as $title ) {
439 $lb->addObj( $title );
446 return $this->mLogEventsList->logLine( $row );
459 return $this->performer;
473 return $this->pattern;
489 return $this->mTagFilter;
493 return $this->mTagInvert;
497 return $this->action;
503 private function enforceActionRestrictions() {
504 if ( $this->actionRestrictionsEnforced ) {
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;
519 private function enforcePerformerRestrictions() {
521 if ( $this->performerRestrictionsEnforced ) {
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 ) .
A value class to process existing log entries.
Class to simplify the use of log pages.