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() {
148 foreach ( $filterTypes as $type => $hide ) {
150 $this->mConds[] =
'log_type != ' . $this->mDb->addQuotes( $type );
157 if ( count( $this->types ) ) {
162 $wpfilters = $this->
getRequest()->getArray(
"wpfilters" );
165 foreach ( $filterLogTypes as $type => $default ) {
167 if ( $wpfilters ===
null ) {
168 $hide = $this->
getRequest()->getBool(
"hide_{$type}_log", $default );
170 $hide = !in_array( $type, $wpfilters );
173 $filters[$type] = $hide;
186 private function limitType( $types ) {
189 $types = ( $types ===
'' ) ? [] : (array)$types;
191 $needReindex =
false;
192 foreach ( $types as $type ) {
193 if ( isset( $restrictions[$type] )
194 && !$this->
getAuthority()->isAllowed( $restrictions[$type] )
197 $types = array_diff( $types, [ $type ] );
200 if ( $needReindex ) {
203 $types = array_values( $types );
205 $this->types = $types;
211 $audience = ( $types || $this->
hasEqualsClause(
'log_id' ) ) ?
'user' :
'public';
213 if ( $hideLogs !==
false ) {
214 $this->mConds[] = $hideLogs;
216 if ( count( $types ) ) {
217 $this->mConds[
'log_type'] = $types;
219 if ( count( $types ) == 1 ) {
220 $this->typeCGI = $types[0];
231 private function limitPerformer( $name ) {
236 $actorId = $this->actorNormalization->findActorIdByName( $name, $this->mDb );
240 $this->mConds[] =
'1 = 0';
244 $this->mConds[
'log_actor' ] = $actorId;
246 $this->enforcePerformerRestrictions();
248 $this->performer = $name;
259 private function limitTitle( $page, $pattern ) {
260 if ( !$page instanceof PageReference ) {
262 $page = Title::newFromText( $page );
269 $this->page = $titleFormatter->getPrefixedDBkey( $page );
270 $ns = $page->getNamespace();
275 $doUserRightsLogLike =
false;
276 if ( $this->types == [
'rights' ] ) {
277 $parts = explode( $interwikiDelimiter, $page->getDBkey() );
278 if ( count( $parts ) == 2 ) {
279 [ $name, $database ] = array_map(
'trim', $parts );
280 if ( str_contains( $database,
'*' ) ) {
281 $doUserRightsLogLike =
true;
299 $this->mConds[
'log_namespace'] = $ns;
300 if ( $doUserRightsLogLike ) {
302 $params = [ $name . $interwikiDelimiter ];
305 $databaseParts = explode(
'*', $database );
306 $databasePartCount = count( $databaseParts );
307 foreach ( $databaseParts as $i => $databasepart ) {
309 if ( $i < $databasePartCount - 1 ) {
313 $this->mConds[] = $db->expr(
'log_title', IExpression::LIKE,
new LikeValue( ...
$params ) );
315 $this->mConds[] = $db->expr(
318 new LikeValue( $page->getDBkey(), $db->anyString() )
320 $this->pattern = $pattern;
322 $this->mConds[
'log_title'] = $page->getDBkey();
324 $this->enforceActionRestrictions();
332 private function limitAction( $action ) {
334 $type = $this->typeCGI;
335 if ( $type ===
'' ) {
340 if ( isset( $actions[$type] ) ) {
342 if ( $action !==
'' && isset( $actions[$type][$action] ) ) {
344 $this->mConds[
'log_action'] = $actions[$type][$action];
345 $this->action = $action;
358 $this->mConds[
'log_id'] = $logId;
367 $queryBuilder = DatabaseLogEntry::newSelectQueryBuilder( $this->mDb )
368 ->where( $this->mConds );
370 # Add log_search table if there are conditions on it.
371 # This filters the results to only include log rows that have
372 # log_search records with the specified ls_field and ls_value values.
373 if ( array_key_exists(
'ls_field', $this->mConds ) ) {
374 $queryBuilder->join(
'log_search',
null,
'ls_log_id=log_id' );
375 $queryBuilder->ignoreIndex( [
'log_search' =>
'ls_log_id' ] );
376 $queryBuilder->useIndex( [
'logging' =>
'PRIMARY' ] );
380 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
381 # to match a specific (ls_field,ls_value) tuple, then there will be
382 # no duplicate log rows. Otherwise, we need to remove the duplicates.
383 $queryBuilder->distinct();
385 } elseif ( array_key_exists(
'log_actor', $this->mConds ) ) {
387 $index =
'log_actor_time';
390 $index =
'log_actor_type_time';
394 $queryBuilder->useIndex( [
'logging' => $index ] );
401 if ( $this->mTagFilter ===
'' && !array_key_exists(
'ls_field', $this->mConds ) ) {
402 $queryBuilder->option(
'STRAIGHT_JOIN' );
406 if ( $maxExecTime ) {
407 $queryBuilder->setMaxExecutionTime( $maxExecTime );
410 # Add ChangeTags filter query
418 return $queryBuilder->getQueryInfo();
428 array_key_exists( $field, $this->mConds ) &&
429 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
434 return [ [
'log_timestamp',
'log_id' ] ];
438 $lb = $this->linkBatchFactory->newLinkBatch();
439 foreach ( $this->mResult as $row ) {
440 $lb->add( $row->log_namespace, $row->log_title );
441 $lb->add(
NS_USER, $row->log_user_text );
443 $formatter = LogFormatter::newFromRow( $row );
444 foreach ( $formatter->getPreloadTitles() as $title ) {
445 $lb->addObj( $title );
452 return $this->mLogEventsList->logLine( $row );
465 return $this->performer;
479 return $this->pattern;
495 return $this->mTagFilter;
499 return $this->mTagInvert;
503 return $this->action;
509 private function enforceActionRestrictions() {
510 if ( $this->actionRestrictionsEnforced ) {
513 $this->actionRestrictionsEnforced =
true;
514 if ( !$this->
getAuthority()->isAllowed(
'deletedhistory' ) ) {
515 $this->mConds[] = $this->mDb->bitAnd(
'log_deleted', LogPage::DELETED_ACTION ) .
' = 0';
516 } elseif ( !$this->
getAuthority()->isAllowedAny(
'suppressrevision',
'viewsuppressed' ) ) {
517 $this->mConds[] = $this->mDb->bitAnd(
'log_deleted', LogPage::SUPPRESSED_ACTION ) .
518 ' != ' . LogPage::SUPPRESSED_USER;
525 private function enforcePerformerRestrictions() {
527 if ( $this->performerRestrictionsEnforced ) {
530 $this->performerRestrictionsEnforced =
true;
531 if ( !$this->
getAuthority()->isAllowed(
'deletedhistory' ) ) {
532 $this->mConds[] = $this->mDb->bitAnd(
'log_deleted', LogPage::DELETED_USER ) .
' = 0';
533 } elseif ( !$this->
getAuthority()->isAllowedAny(
'suppressrevision',
'viewsuppressed' ) ) {
534 $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.