42 private $performer =
'';
48 private $pattern =
false;
51 private $typeCGI =
'';
57 private $performerRestrictionsEnforced =
false;
60 private $actionRestrictionsEnforced =
false;
75 private $linkBatchFactory;
78 private $actorNormalization;
98 public function __construct( $list, $types = [], $performer =
'', $page =
'',
99 $pattern =
false, $conds = [], $year =
false, $month =
false, $day =
false,
100 $tagFilter =
'', $action =
'', $logId = 0,
106 parent::__construct( $list->getContext() );
108 $services = MediaWikiServices::getInstance();
109 $this->mConds = $conds;
110 $this->mLogEventsList = $list;
113 $this->linkBatchFactory = $linkBatchFactory ?? $services->getLinkBatchFactory();
114 $this->actorNormalization = $actorNormalization ?? $services->getActorNormalization();
117 $this->limitType( $types );
118 $this->limitFilterTypes();
119 $this->limitPerformer( $performer );
120 $this->limitTitle( $page, $pattern );
121 $this->limitAction( $action );
123 $this->mTagFilter = (string)$tagFilter;
124 $this->mTagInvert = (bool)$tagInvert;
128 $query = parent::getDefaultQuery();
129 $query[
'type'] = $this->typeCGI;
130 $query[
'user'] = $this->performer;
138 private function limitFilterTypes() {
143 foreach ( $filterTypes as
$type => $hide ) {
145 $this->mConds[] =
'log_type != ' . $this->mDb->addQuotes(
$type );
152 if ( count( $this->types ) ) {
156 $wpfilters = $this->
getRequest()->getArray(
"wpfilters" );
157 $filterLogTypes = $this->
getConfig()->get( MainConfigNames::FilterLogTypes );
159 foreach ( $filterLogTypes as
$type => $default ) {
161 if ( $wpfilters ===
null ) {
162 $hide = $this->
getRequest()->getBool(
"hide_{$type}_log", $default );
164 $hide = !in_array(
$type, $wpfilters );
167 $filters[
$type] = $hide;
180 private function limitType( $types ) {
181 $restrictions = $this->
getConfig()->get( MainConfigNames::LogRestrictions );
183 $types = ( $types ===
'' ) ? [] : (array)$types;
185 $needReindex =
false;
186 foreach ( $types as
$type ) {
187 if ( isset( $restrictions[
$type] )
191 $types = array_diff( $types, [
$type ] );
194 if ( $needReindex ) {
197 $types = array_values( $types );
199 $this->types = $types;
205 $audience = ( $types || $this->
hasEqualsClause(
'log_id' ) ) ?
'user' :
'public';
207 if ( $hideLogs !==
false ) {
208 $this->mConds[] = $hideLogs;
210 if ( count( $types ) ) {
211 $this->mConds[
'log_type'] = $types;
213 if ( count( $types ) == 1 ) {
214 $this->typeCGI = $types[0];
225 private function limitPerformer( $name ) {
230 $actorId = $this->actorNormalization->findActorIdByName( $name, $this->mDb );
234 $this->mConds[] =
'1 = 0';
238 $this->mConds[
'log_actor' ] = $actorId;
240 $this->enforcePerformerRestrictions();
242 $this->performer = $name;
253 private function limitTitle( $page, $pattern ) {
256 $page = Title::newFromText( $page );
262 $titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
263 $this->page = $titleFormatter->getPrefixedDBkey( $page );
264 $ns = $page->getNamespace();
267 $interwikiDelimiter = $this->
getConfig()->get( MainConfigNames::UserrightsInterwikiDelimiter );
269 $doUserRightsLogLike =
false;
270 if ( $this->types == [
'rights' ] ) {
271 $parts = explode( $interwikiDelimiter, $page->getDBkey() );
272 if ( count( $parts ) == 2 ) {
273 [ $name, $database ] = array_map(
'trim', $parts );
274 if ( strstr( $database,
'*' ) ) {
275 $doUserRightsLogLike =
true;
293 $this->mConds[
'log_namespace'] = $ns;
294 if ( $doUserRightsLogLike ) {
296 $params = [ $name . $interwikiDelimiter ];
299 $databaseParts = explode(
'*', $database );
300 $databasePartCount = count( $databaseParts );
301 foreach ( $databaseParts as $i => $databasepart ) {
302 $params[] = $databasepart;
303 if ( $i < $databasePartCount - 1 ) {
304 $params[] = $db->anyString();
307 $this->mConds[] =
'log_title' . $db->buildLike( ...$params );
308 } elseif ( $pattern && !$this->
getConfig()->
get( MainConfigNames::MiserMode ) ) {
309 $this->mConds[] =
'log_title' . $db->buildLike( $page->getDBkey(), $db->anyString() );
310 $this->pattern = $pattern;
312 $this->mConds[
'log_title'] = $page->getDBkey();
314 $this->enforceActionRestrictions();
322 private function limitAction( $action ) {
324 $type = $this->typeCGI;
325 if (
$type ===
'' ) {
329 $actions = $this->
getConfig()->get( MainConfigNames::ActionFilteredLogs );
330 if ( isset( $actions[
$type] ) ) {
332 $this->mLogEventsList->setAllowedActions( array_keys( $actions[
$type] ) );
333 if ( $action !==
'' && isset( $actions[
$type][$action] ) ) {
335 $this->mConds[
'log_action'] = $actions[
$type][$action];
336 $this->action = $action;
349 $this->mConds[
'log_id'] = $logId;
358 $basic = DatabaseLogEntry::getSelectQueryData();
360 $tables = $basic[
'tables'];
361 $fields = $basic[
'fields'];
362 $conds = $basic[
'conds'];
363 $options = $basic[
'options'];
364 $joins = $basic[
'join_conds'];
366 # Add log_search table if there are conditions on it.
367 # This filters the results to only include log rows that have
368 # log_search records with the specified ls_field and ls_value values.
369 if ( array_key_exists(
'ls_field', $this->mConds ) ) {
370 $tables[] =
'log_search';
371 $options[
'IGNORE INDEX'] = [
'log_search' =>
'ls_log_id' ];
372 $options[
'USE INDEX'] = [
'logging' =>
'PRIMARY' ];
376 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
377 # to match a specific (ls_field,ls_value) tuple, then there will be
378 # no duplicate log rows. Otherwise, we need to remove the duplicates.
379 $options[] =
'DISTINCT';
381 } elseif ( array_key_exists(
'log_actor', $this->mConds ) ) {
383 $index =
'log_actor_time';
386 $index =
'log_actor_type_time';
390 $options[
'USE INDEX'] = [
'logging' => $index ];
392 # Don't show duplicate rows when using log_search
393 $joins[
'log_search'] = [
'JOIN',
'ls_log_id=log_id' ];
399 if ( $this->mTagFilter ===
'' && !array_key_exists(
'ls_field', $this->mConds ) ) {
400 $options[] =
'STRAIGHT_JOIN';
403 $options[
'MAX_EXECUTION_TIME'] = $this->
getConfig()
404 ->get( MainConfigNames::MaxExecutionTimeForExpensiveQueries );
409 'conds' => array_merge( $conds, $this->mConds ),
410 'options' => $options,
411 'join_conds' => $joins,
413 # Add ChangeTags filter query
415 $info[
'join_conds'], $info[
'options'], $this->mTagFilter, $this->mTagInvert );
427 array_key_exists( $field, $this->mConds ) &&
428 ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 )
433 return [ [
'log_timestamp',
'log_id' ] ];
437 # Do a link batch query
439 $lb = $this->linkBatchFactory->newLinkBatch();
440 foreach ( $this->mResult as $row ) {
441 $lb->add( $row->log_namespace, $row->log_title );
442 $lb->add(
NS_USER, $row->log_user_text );
444 $formatter = LogFormatter::newFromRow( $row );
445 foreach ( $formatter->getPreloadTitles() as
$title ) {
450 $this->mResult->seek( 0 );
457 return $this->mLogEventsList->logLine( $row );
470 return $this->performer;
484 return $this->pattern;
500 return $this->mTagFilter;
504 return $this->mTagInvert;
508 return $this->action;
514 private function enforceActionRestrictions() {
515 if ( $this->actionRestrictionsEnforced ) {
518 $this->actionRestrictionsEnforced =
true;
519 if ( !$this->
getAuthority()->isAllowed(
'deletedhistory' ) ) {
520 $this->mConds[] = $this->mDb->bitAnd(
'log_deleted', LogPage::DELETED_ACTION ) .
' = 0';
521 } elseif ( !$this->
getAuthority()->isAllowedAny(
'suppressrevision',
'viewsuppressed' ) ) {
522 $this->mConds[] = $this->mDb->bitAnd(
'log_deleted', LogPage::SUPPRESSED_ACTION ) .
530 private function enforcePerformerRestrictions() {
532 if ( $this->performerRestrictionsEnforced ) {
535 $this->performerRestrictionsEnforced =
true;
536 if ( !$this->
getAuthority()->isAllowed(
'deletedhistory' ) ) {
537 $this->mConds[] = $this->mDb->bitAnd(
'log_deleted', LogPage::DELETED_USER ) .
' = 0';
538 } elseif ( !$this->
getAuthority()->isAllowedAny(
'suppressrevision',
'viewsuppressed' ) ) {
539 $this->mConds[] = $this->mDb->bitAnd(
'log_deleted', LogPage::SUPPRESSED_USER ) .