| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
namespace MediaWiki\Pager; |
| 27 |
|
| 28 |
use DatabaseLogEntry; |
| 29 |
use LogEventsList; |
| 30 |
use LogFormatterFactory; |
| 31 |
use LogPage; |
| 32 |
use MediaWiki\Cache\LinkBatchFactory; |
| 33 |
use MediaWiki\MainConfigNames; |
| 34 |
use MediaWiki\MediaWikiServices; |
| 35 |
use MediaWiki\Page\PageReference; |
| 36 |
use MediaWiki\Title\Title; |
| 37 |
use MediaWiki\User\ActorNormalization; |
| 38 |
use Wikimedia\Rdbms\IExpression; |
| 39 |
use Wikimedia\Rdbms\LikeValue; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class LogPager extends ReverseChronologicalPager { |
| 45 |
|
| 46 |
private $types = []; |
| 47 |
|
| 48 |
|
| 49 |
private $performer = ''; |
| 50 |
|
| 51 |
|
| 52 |
private $page = ''; |
| 53 |
|
| 54 |
|
| 55 |
private $pattern = false; |
| 56 |
|
| 57 |
|
| 58 |
private $typeCGI = ''; |
| 59 |
|
| 60 |
|
| 61 |
private $action = ''; |
| 62 |
|
| 63 |
|
| 64 |
private $performerRestrictionsEnforced = false; |
| 65 |
|
| 66 |
|
| 67 |
private $actionRestrictionsEnforced = false; |
| 68 |
|
| 69 |
|
| 70 |
private $mConds; |
| 71 |
|
| 72 |
|
| 73 |
private $mTagFilter; |
| 74 |
|
| 75 |
|
| 76 |
private $mTagInvert; |
| 77 |
|
| 78 |
|
| 79 |
public $mLogEventsList; |
| 80 |
|
| 81 |
|
| 82 |
private $linkBatchFactory; |
| 83 |
|
| 84 |
|
| 85 |
private $actorNormalization; |
| 86 |
private LogFormatterFactory $logFormatterFactory; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function __construct( $list, $types = [], $performer = '', $page = '', |
| 107 |
$pattern = false, $conds = [], $year = false, $month = false, $day = false, |
| 108 |
$tagFilter = '', $action = '', $logId = 0, |
| 109 |
?LinkBatchFactory $linkBatchFactory = null, |
| 110 |
?ActorNormalization $actorNormalization = null, |
| 111 |
?LogFormatterFactory $logFormatterFactory = null, |
| 112 |
$tagInvert = false |
| 113 |
) { |
| 114 |
parent::__construct( $list->getContext() ); |
| 115 |
|
| 116 |
$services = MediaWikiServices::getInstance(); |
| 117 |
$this->mConds = $conds; |
| 118 |
$this->mLogEventsList = $list; |
| 119 |
|
| 120 |
|
| 121 |
$this->linkBatchFactory = $linkBatchFactory ?? $services->getLinkBatchFactory(); |
| 122 |
$this->actorNormalization = $actorNormalization ?? $services->getActorNormalization(); |
| 123 |
$this->logFormatterFactory = $logFormatterFactory ?? $services->getLogFormatterFactory(); |
| 124 |
|
| 125 |
$this->limitLogId( $logId ); |
| 126 |
$this->limitType( $types ); |
| 127 |
$this->limitFilterTypes(); |
| 128 |
$this->limitPerformer( $performer ); |
| 129 |
$this->limitTitle( $page, $pattern ); |
| 130 |
$this->limitAction( $action ); |
| 131 |
$this->getDateCond( $year, $month, $day ); |
| 132 |
$this->mTagFilter = (string)$tagFilter; |
| 133 |
$this->mTagInvert = (bool)$tagInvert; |
| 134 |
} |
| 135 |
|
| 136 |
public function getDefaultQuery() { |
| 137 |
$query = parent::getDefaultQuery(); |
| 138 |
$query['type'] = $this->typeCGI; |
| 139 |
$query['user'] = $this->performer; |
| 140 |
$query['day'] = $this->mDay; |
| 141 |
$query['month'] = $this->mMonth; |
| 142 |
$query['year'] = $this->mYear; |
| 143 |
|
| 144 |
return $query; |
| 145 |
} |
| 146 |
|
| 147 |
private function limitFilterTypes() { |
| 148 |
if ( $this->hasEqualsClause( 'log_id' ) ) { |
| 149 |
return; |
| 150 |
} |
| 151 |
$filterTypes = $this->getFilterParams(); |
| 152 |
$excludeTypes = []; |
| 153 |
foreach ( $filterTypes as $type => $hide ) { |
| 154 |
if ( $hide ) { |
| 155 |
$excludeTypes[] = $type; |
| 156 |
} |
| 157 |
} |
| 158 |
if ( $excludeTypes ) { |
| 159 |
$this->mConds[] = $this->mDb->expr( 'log_type', '!=', $excludeTypes ); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
public function getFilterParams() { |
| 164 |
$filters = []; |
| 165 |
if ( count( $this->types ) ) { |
| 166 |
return $filters; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
$wpfilters = $this->getRequest()->getArray( "wpfilters" ); |
| 171 |
$filterLogTypes = $this->getConfig()->get( MainConfigNames::FilterLogTypes ); |
| 172 |
|
| 173 |
foreach ( $filterLogTypes as $type => $default ) { |
| 174 |
|
| 175 |
if ( $wpfilters === null ) { |
| 176 |
$hide = $this->getRequest()->getBool( "hide_{$type}_log", $default ); |
| 177 |
} else { |
| 178 |
$hide = !in_array( $type, $wpfilters ); |
| 179 |
} |
| 180 |
|
| 181 |
$filters[$type] = $hide; |
| 182 |
} |
| 183 |
|
| 184 |
return $filters; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
private function limitType( $types ) { |
| 195 |
$restrictions = $this->getConfig()->get( MainConfigNames::LogRestrictions ); |
| 196 |
|
| 197 |
$types = ( $types === '' ) ? [] : (array)$types; |
| 198 |
|
| 199 |
$needReindex = false; |
| 200 |
foreach ( $types as $type ) { |
| 201 |
if ( isset( $restrictions[$type] ) |
| 202 |
&& !$this->getAuthority()->isAllowed( $restrictions[$type] ) |
| 203 |
) { |
| 204 |
$needReindex = true; |
| 205 |
$types = array_diff( $types, [ $type ] ); |
| 206 |
} |
| 207 |
} |
| 208 |
if ( $needReindex ) { |
| 209 |
|
| 210 |
|
| 211 |
$types = array_values( $types ); |
| 212 |
} |
| 213 |
$this->types = $types; |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
$audience = ( $types || $this->hasEqualsClause( 'log_id' ) ) ? 'user' : 'public'; |
| 220 |
$hideLogs = LogEventsList::getExcludeClause( $this->mDb, $audience, $this->getAuthority() ); |
| 221 |
if ( $hideLogs !== false ) { |
| 222 |
$this->mConds[] = $hideLogs; |
| 223 |
} |
| 224 |
if ( count( $types ) ) { |
| 225 |
$this->mConds['log_type'] = $types; |
| 226 |
|
| 227 |
if ( count( $types ) == 1 ) { |
| 228 |
$this->typeCGI = $types[0]; |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
private function limitPerformer( $name ) { |
| 240 |
if ( $name == '' ) { |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
$actorId = $this->actorNormalization->findActorIdByName( $name, $this->mDb ); |
| 245 |
|
| 246 |
if ( !$actorId ) { |
| 247 |
|
| 248 |
$this->mConds[] = '1 = 0'; |
| 249 |
return; |
| 250 |
} |
| 251 |
|
| 252 |
$this->mConds[ 'log_actor' ] = $actorId; |
| 253 |
|
| 254 |
$this->enforcePerformerRestrictions(); |
| 255 |
|
| 256 |
$this->performer = $name; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
private function limitTitle( $page, $pattern ) { |
| 268 |
if ( !$page instanceof PageReference ) { |
| 269 |
|
| 270 |
$page = Title::newFromText( $page ); |
| 271 |
if ( !$page ) { |
| 272 |
return; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
$titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter(); |
| 277 |
$this->page = $titleFormatter->getPrefixedDBkey( $page ); |
| 278 |
$ns = $page->getNamespace(); |
| 279 |
$db = $this->mDb; |
| 280 |
|
| 281 |
$interwikiDelimiter = $this->getConfig()->get( MainConfigNames::UserrightsInterwikiDelimiter ); |
| 282 |
|
| 283 |
$doUserRightsLogLike = false; |
| 284 |
if ( $this->types == [ 'rights' ] ) { |
| 285 |
$parts = explode( $interwikiDelimiter, $page->getDBkey() ); |
| 286 |
if ( count( $parts ) == 2 ) { |
| 287 |
[ $name, $database ] = array_map( 'trim', $parts ); |
| 288 |
if ( str_contains( $database, '*' ) ) { |
| 289 |
$doUserRightsLogLike = true; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
$this->mConds['log_namespace'] = $ns; |
| 308 |
if ( $doUserRightsLogLike ) { |
| 309 |
|
| 310 |
$params = [ $name . $interwikiDelimiter ]; |
| 311 |
|
| 312 |
|
| 313 |
$databaseParts = explode( '*', $database ); |
| 314 |
$databasePartCount = count( $databaseParts ); |
| 315 |
foreach ( $databaseParts as $i => $databasepart ) { |
| 316 |
$params[] = $databasepart; |
| 317 |
if ( $i < $databasePartCount - 1 ) { |
| 318 |
$params[] = $db->anyString(); |
| 319 |
} |
| 320 |
} |
| 321 |
$this->mConds[] = $db->expr( 'log_title', IExpression::LIKE, new LikeValue( ...$params ) ); |
| 322 |
} elseif ( $pattern && !$this->getConfig()->get( MainConfigNames::MiserMode ) ) { |
| 323 |
$this->mConds[] = $db->expr( |
| 324 |
'log_title', |
| 325 |
IExpression::LIKE, |
| 326 |
new LikeValue( $page->getDBkey(), $db->anyString() ) |
| 327 |
); |
| 328 |
$this->pattern = $pattern; |
| 329 |
} else { |
| 330 |
$this->mConds['log_title'] = $page->getDBkey(); |
| 331 |
} |
| 332 |
$this->enforceActionRestrictions(); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
private function limitAction( $action ) { |
| 341 |
|
| 342 |
$type = $this->typeCGI; |
| 343 |
if ( $type === '' ) { |
| 344 |
|
| 345 |
return; |
| 346 |
} |
| 347 |
$actions = $this->getConfig()->get( MainConfigNames::ActionFilteredLogs ); |
| 348 |
if ( isset( $actions[$type] ) ) { |
| 349 |
|
| 350 |
if ( $action !== '' && isset( $actions[$type][$action] ) ) { |
| 351 |
|
| 352 |
$this->mConds['log_action'] = $actions[$type][$action]; |
| 353 |
$this->action = $action; |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
protected function limitLogId( $logId ) { |
| 363 |
if ( !$logId ) { |
| 364 |
return; |
| 365 |
} |
| 366 |
$this->mConds['log_id'] = $logId; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
public function getQueryInfo() { |
| 375 |
$queryBuilder = DatabaseLogEntry::newSelectQueryBuilder( $this->mDb ) |
| 376 |
->where( $this->mConds ); |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
if ( array_key_exists( 'ls_field', $this->mConds ) ) { |
| 382 |
$queryBuilder->join( 'log_search', null, 'ls_log_id=log_id' ); |
| 383 |
$queryBuilder->ignoreIndex( [ 'log_search' => 'ls_log_id' ] ); |
| 384 |
$queryBuilder->useIndex( [ 'logging' => 'PRIMARY' ] ); |
| 385 |
if ( !$this->hasEqualsClause( 'ls_field' ) |
| 386 |
|| !$this->hasEqualsClause( 'ls_value' ) |
| 387 |
) { |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
$queryBuilder->distinct(); |
| 392 |
} |
| 393 |
} elseif ( array_key_exists( 'log_actor', $this->mConds ) ) { |
| 394 |
|
| 395 |
$index = 'log_actor_time'; |
| 396 |
foreach ( $this->getFilterParams() as $hide ) { |
| 397 |
if ( !$hide ) { |
| 398 |
$index = 'log_actor_type_time'; |
| 399 |
break; |
| 400 |
} |
| 401 |
} |
| 402 |
$queryBuilder->useIndex( [ 'logging' => $index ] ); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
if ( $this->mTagFilter === '' && !array_key_exists( 'ls_field', $this->mConds ) ) { |
| 410 |
$queryBuilder->straightJoinOption(); |
| 411 |
} |
| 412 |
|
| 413 |
$maxExecTime = $this->getConfig()->get( MainConfigNames::MaxExecutionTimeForExpensiveQueries ); |
| 414 |
if ( $maxExecTime ) { |
| 415 |
$queryBuilder->setMaxExecutionTime( $maxExecTime ); |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
MediaWikiServices::getInstance()->getChangeTagsStore()->modifyDisplayQueryBuilder( |
| 420 |
$queryBuilder, |
| 421 |
'logging', |
| 422 |
$this->mTagFilter, |
| 423 |
$this->mTagInvert |
| 424 |
); |
| 425 |
|
| 426 |
return $queryBuilder->getQueryInfo(); |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
protected function hasEqualsClause( $field ) { |
| 435 |
return ( |
| 436 |
array_key_exists( $field, $this->mConds ) && |
| 437 |
( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 ) |
| 438 |
); |
| 439 |
} |
| 440 |
|
| 441 |
public function getIndexField() { |
| 442 |
return [ [ 'log_timestamp', 'log_id' ] ]; |
| 443 |
} |
| 444 |
|
| 445 |
protected function doBatchLookups() { |
| 446 |
$lb = $this->linkBatchFactory->newLinkBatch(); |
| 447 |
foreach ( $this->mResult as $row ) { |
| 448 |
$lb->add( $row->log_namespace, $row->log_title ); |
| 449 |
$lb->add( NS_USER, $row->log_user_text ); |
| 450 |
$lb->add( NS_USER_TALK, $row->log_user_text ); |
| 451 |
$formatter = $this->logFormatterFactory->newFromRow( $row ); |
| 452 |
foreach ( $formatter->getPreloadTitles() as $title ) { |
| 453 |
$lb->addObj( $title ); |
| 454 |
} |
| 455 |
} |
| 456 |
$lb->execute(); |
| 457 |
} |
| 458 |
|
| 459 |
public function formatRow( $row ) { |
| 460 |
return $this->mLogEventsList->logLine( $row ); |
| 461 |
} |
| 462 |
|
| 463 |
public function getType() { |
| 464 |
return $this->types; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
public function getPerformer() { |
| 473 |
return $this->performer; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
public function getPage() { |
| 480 |
return $this->page; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
public function getPattern() { |
| 487 |
return $this->pattern; |
| 488 |
} |
| 489 |
|
| 490 |
public function getYear() { |
| 491 |
return $this->mYear; |
| 492 |
} |
| 493 |
|
| 494 |
public function getMonth() { |
| 495 |
return $this->mMonth; |
| 496 |
} |
| 497 |
|
| 498 |
public function getDay() { |
| 499 |
return $this->mDay; |
| 500 |
} |
| 501 |
|
| 502 |
public function getTagFilter() { |
| 503 |
return $this->mTagFilter; |
| 504 |
} |
| 505 |
|
| 506 |
public function getTagInvert() { |
| 507 |
return $this->mTagInvert; |
| 508 |
} |
| 509 |
|
| 510 |
public function getAction() { |
| 511 |
return $this->action; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
private function enforceActionRestrictions() { |
| 518 |
if ( $this->actionRestrictionsEnforced ) { |
| 519 |
return; |
| 520 |
} |
| 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; |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
private function enforcePerformerRestrictions() { |
| 534 |
|
| 535 |
if ( $this->performerRestrictionsEnforced ) { |
| 536 |
return; |
| 537 |
} |
| 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; |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
class_alias( LogPager::class, 'LogPager' ); |