| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\ChangeTags; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
use ManualLogEntry; |
| 25 |
use MediaWiki\Config\ServiceOptions; |
| 26 |
use MediaWiki\HookContainer\HookContainer; |
| 27 |
use MediaWiki\HookContainer\HookRunner; |
| 28 |
use MediaWiki\MainConfigNames; |
| 29 |
use MediaWiki\Status\Status; |
| 30 |
use MediaWiki\Storage\NameTableAccessException; |
| 31 |
use MediaWiki\Storage\NameTableStore; |
| 32 |
use MediaWiki\Title\Title; |
| 33 |
use MediaWiki\User\UserFactory; |
| 34 |
use MediaWiki\User\UserIdentity; |
| 35 |
use Psr\Log\LoggerInterface; |
| 36 |
use RecentChange; |
| 37 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 38 |
use Wikimedia\Rdbms\Database; |
| 39 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 40 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 41 |
use Wikimedia\Rdbms\RawSQLValue; |
| 42 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
class ChangeTagsStore { |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
private const CHANGE_TAG = 'change_tag'; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
private const CHANGE_TAG_DEF = 'change_tag_def'; |
| 61 |
|
| 62 |
public const DISPLAY_TABLE_ALIAS = 'changetagdisplay'; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public const CONSTRUCTOR_OPTIONS = [ |
| 68 |
MainConfigNames::SoftwareTags, |
| 69 |
MainConfigNames::UseTagFilter, |
| 70 |
]; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
private const DEFINED_SOFTWARE_TAGS = [ |
| 76 |
'mw-contentmodelchange', |
| 77 |
'mw-new-redirect', |
| 78 |
'mw-removed-redirect', |
| 79 |
'mw-changed-redirect-target', |
| 80 |
'mw-blank', |
| 81 |
'mw-replace', |
| 82 |
'mw-rollback', |
| 83 |
'mw-undo', |
| 84 |
'mw-manual-revert', |
| 85 |
'mw-reverted', |
| 86 |
'mw-server-side-upload', |
| 87 |
]; |
| 88 |
|
| 89 |
private IConnectionProvider $dbProvider; |
| 90 |
private LoggerInterface $logger; |
| 91 |
private ServiceOptions $options; |
| 92 |
private NameTableStore $changeTagDefStore; |
| 93 |
private WANObjectCache $wanCache; |
| 94 |
private HookRunner $hookRunner; |
| 95 |
private UserFactory $userFactory; |
| 96 |
private HookContainer $hookContainer; |
| 97 |
|
| 98 |
public function __construct( |
| 99 |
IConnectionProvider $dbProvider, |
| 100 |
NameTableStore $changeTagDefStore, |
| 101 |
WANObjectCache $wanCache, |
| 102 |
HookContainer $hookContainer, |
| 103 |
LoggerInterface $logger, |
| 104 |
UserFactory $userFactory, |
| 105 |
ServiceOptions $options |
| 106 |
) { |
| 107 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 108 |
$this->dbProvider = $dbProvider; |
| 109 |
$this->logger = $logger; |
| 110 |
$this->options = $options; |
| 111 |
$this->changeTagDefStore = $changeTagDefStore; |
| 112 |
$this->wanCache = $wanCache; |
| 113 |
$this->hookContainer = $hookContainer; |
| 114 |
$this->userFactory = $userFactory; |
| 115 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function getSoftwareTags( $all = false ): array { |
| 126 |
$coreTags = $this->options->get( MainConfigNames::SoftwareTags ); |
| 127 |
if ( !is_array( $coreTags ) ) { |
| 128 |
$this->logger->warning( 'wgSoftwareTags should be associative array of enabled tags. |
| 129 |
Please refer to documentation for the list of tags you can enable' ); |
| 130 |
return []; |
| 131 |
} |
| 132 |
|
| 133 |
$availableSoftwareTags = !$all ? |
| 134 |
array_keys( array_filter( $coreTags ) ) : |
| 135 |
array_keys( $coreTags ); |
| 136 |
|
| 137 |
return array_intersect( |
| 138 |
$availableSoftwareTags, |
| 139 |
self::DEFINED_SOFTWARE_TAGS |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public function getTagsWithData( |
| 155 |
IReadableDatabase $db, $rc_id = null, $rev_id = null, $log_id = null |
| 156 |
): array { |
| 157 |
if ( !$rc_id && !$rev_id && !$log_id ) { |
| 158 |
throw new InvalidArgumentException( |
| 159 |
'At least one of: RCID, revision ID, and log ID MUST be ' . |
| 160 |
'specified when loading tags from a change!' ); |
| 161 |
} |
| 162 |
|
| 163 |
$conds = array_filter( |
| 164 |
[ |
| 165 |
'ct_rc_id' => $rc_id, |
| 166 |
'ct_rev_id' => $rev_id, |
| 167 |
'ct_log_id' => $log_id, |
| 168 |
] |
| 169 |
); |
| 170 |
$result = $db->newSelectQueryBuilder() |
| 171 |
->select( [ 'ct_tag_id', 'ct_params' ] ) |
| 172 |
->from( self::CHANGE_TAG ) |
| 173 |
->where( $conds ) |
| 174 |
->caller( __METHOD__ ) |
| 175 |
->fetchResultSet(); |
| 176 |
|
| 177 |
$tags = []; |
| 178 |
foreach ( $result as $row ) { |
| 179 |
$tagName = $this->changeTagDefStore->getName( (int)$row->ct_tag_id ); |
| 180 |
$tags[$tagName] = $row->ct_params; |
| 181 |
} |
| 182 |
|
| 183 |
return $tags; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public function makeTagSummarySubquery( $tables ) { |
| 194 |
|
| 195 |
$tables = (array)$tables; |
| 196 |
|
| 197 |
|
| 198 |
if ( in_array( 'recentchanges', $tables ) ) { |
| 199 |
$join_cond = 'ct_rc_id=rc_id'; |
| 200 |
} elseif ( in_array( 'logging', $tables ) ) { |
| 201 |
$join_cond = 'ct_log_id=log_id'; |
| 202 |
} elseif ( in_array( 'revision', $tables ) ) { |
| 203 |
$join_cond = 'ct_rev_id=rev_id'; |
| 204 |
} elseif ( in_array( 'archive', $tables ) ) { |
| 205 |
$join_cond = 'ct_rev_id=ar_rev_id'; |
| 206 |
} else { |
| 207 |
throw new InvalidArgumentException( 'Unable to determine appropriate JOIN condition for tagging.' ); |
| 208 |
} |
| 209 |
|
| 210 |
return $this->dbProvider->getReplicaDatabase() |
| 211 |
->newSelectQueryBuilder() |
| 212 |
->table( self::CHANGE_TAG ) |
| 213 |
->join( self::CHANGE_TAG_DEF, null, 'ct_tag_id=ctd_id' ) |
| 214 |
->field( 'ctd_name' ) |
| 215 |
->where( $join_cond ) |
| 216 |
->buildGroupConcatField( ',' ); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public function defineTag( $tag ) { |
| 228 |
$dbw = $this->dbProvider->getPrimaryDatabase(); |
| 229 |
$dbw->newInsertQueryBuilder() |
| 230 |
->insertInto( self::CHANGE_TAG_DEF ) |
| 231 |
->row( [ |
| 232 |
'ctd_name' => $tag, |
| 233 |
'ctd_user_defined' => 1, |
| 234 |
'ctd_count' => 0 |
| 235 |
] ) |
| 236 |
->onDuplicateKeyUpdate() |
| 237 |
->uniqueIndexFields( [ 'ctd_name' ] ) |
| 238 |
->set( [ 'ctd_user_defined' => 1 ] ) |
| 239 |
->caller( __METHOD__ )->execute(); |
| 240 |
|
| 241 |
|
| 242 |
$this->purgeTagCacheAll(); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
public function undefineTag( $tag ) { |
| 254 |
$dbw = $this->dbProvider->getPrimaryDatabase(); |
| 255 |
|
| 256 |
$dbw->newUpdateQueryBuilder() |
| 257 |
->update( self::CHANGE_TAG_DEF ) |
| 258 |
->set( [ 'ctd_user_defined' => 0 ] ) |
| 259 |
->where( [ 'ctd_name' => $tag ] ) |
| 260 |
->caller( __METHOD__ )->execute(); |
| 261 |
|
| 262 |
$dbw->newDeleteQueryBuilder() |
| 263 |
->deleteFrom( self::CHANGE_TAG_DEF ) |
| 264 |
->where( [ 'ctd_name' => $tag, 'ctd_count' => 0 ] ) |
| 265 |
->caller( __METHOD__ )->execute(); |
| 266 |
|
| 267 |
|
| 268 |
$this->purgeTagCacheAll(); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public function logTagManagementAction( string $action, string $tag, string $reason, |
| 286 |
UserIdentity $user, $tagCount = null, array $logEntryTags = [] |
| 287 |
) { |
| 288 |
$dbw = $this->dbProvider->getPrimaryDatabase(); |
| 289 |
|
| 290 |
$logEntry = new ManualLogEntry( 'managetags', $action ); |
| 291 |
$logEntry->setPerformer( $user ); |
| 292 |
|
| 293 |
|
| 294 |
$logEntry->setTarget( Title::newFromText( 'Special:Tags' ) ); |
| 295 |
$logEntry->setComment( $reason ); |
| 296 |
|
| 297 |
$params = [ '4::tag' => $tag ]; |
| 298 |
if ( $tagCount !== null ) { |
| 299 |
$params['5:number:count'] = $tagCount; |
| 300 |
} |
| 301 |
$logEntry->setParameters( $params ); |
| 302 |
$logEntry->setRelations( [ 'Tag' => $tag ] ); |
| 303 |
$logEntry->addTags( $logEntryTags ); |
| 304 |
|
| 305 |
$logId = $logEntry->insert( $dbw ); |
| 306 |
$logEntry->publish( $logId ); |
| 307 |
return $logId; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public function deleteTagEverywhere( $tag ) { |
| 323 |
$dbw = $this->dbProvider->getPrimaryDatabase(); |
| 324 |
$dbw->startAtomic( __METHOD__ ); |
| 325 |
|
| 326 |
|
| 327 |
$tagId = $this->changeTagDefStore->getId( $tag ); |
| 328 |
|
| 329 |
|
| 330 |
$this->undefineTag( $tag ); |
| 331 |
|
| 332 |
|
| 333 |
$dbw->newDeleteQueryBuilder() |
| 334 |
->deleteFrom( self::CHANGE_TAG ) |
| 335 |
->where( [ 'ct_tag_id' => $tagId ] ) |
| 336 |
->caller( __METHOD__ )->execute(); |
| 337 |
$dbw->newDeleteQueryBuilder() |
| 338 |
->deleteFrom( self::CHANGE_TAG_DEF ) |
| 339 |
->where( [ 'ctd_name' => $tag ] ) |
| 340 |
->caller( __METHOD__ )->execute(); |
| 341 |
$dbw->endAtomic( __METHOD__ ); |
| 342 |
|
| 343 |
|
| 344 |
$status = Status::newGood(); |
| 345 |
$this->hookRunner->onChangeTagAfterDelete( $tag, $status ); |
| 346 |
|
| 347 |
if ( !$status->isOK() ) { |
| 348 |
$this->logger->debug( 'ChangeTagAfterDelete error condition downgraded to warning' ); |
| 349 |
$status->setOK( true ); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
$this->purgeTagCacheAll(); |
| 354 |
|
| 355 |
return $status; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public function purgeTagCacheAll() { |
| 364 |
$this->wanCache->touchCheckKey( $this->wanCache->makeKey( 'active-tags' ) ); |
| 365 |
$this->wanCache->touchCheckKey( $this->wanCache->makeKey( 'valid-tags-db' ) ); |
| 366 |
$this->wanCache->touchCheckKey( $this->wanCache->makeKey( 'valid-tags-hook' ) ); |
| 367 |
$this->wanCache->touchCheckKey( $this->wanCache->makeKey( 'tags-usage-statistics' ) ); |
| 368 |
|
| 369 |
$this->changeTagDefStore->reloadMap(); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
public function tagUsageStatistics(): array { |
| 379 |
$fname = __METHOD__; |
| 380 |
$dbProvider = $this->dbProvider; |
| 381 |
|
| 382 |
return $this->wanCache->getWithSetCallback( |
| 383 |
$this->wanCache->makeKey( 'tags-usage-statistics' ), |
| 384 |
WANObjectCache::TTL_MINUTE * 5, |
| 385 |
static function ( $oldValue, &$ttl, array &$setOpts ) use ( $fname, $dbProvider ) { |
| 386 |
$dbr = $dbProvider->getReplicaDatabase(); |
| 387 |
$res = $dbr->newSelectQueryBuilder() |
| 388 |
->select( [ 'ctd_name', 'ctd_count' ] ) |
| 389 |
->from( self::CHANGE_TAG_DEF ) |
| 390 |
->orderBy( 'ctd_count', SelectQueryBuilder::SORT_DESC ) |
| 391 |
->caller( $fname ) |
| 392 |
->fetchResultSet(); |
| 393 |
|
| 394 |
$out = []; |
| 395 |
foreach ( $res as $row ) { |
| 396 |
$out[$row->ctd_name] = $row->ctd_count; |
| 397 |
} |
| 398 |
|
| 399 |
return $out; |
| 400 |
}, |
| 401 |
[ |
| 402 |
'checkKeys' => [ $this->wanCache->makeKey( 'tags-usage-statistics' ) ], |
| 403 |
'lockTSE' => WANObjectCache::TTL_MINUTE * 5, |
| 404 |
'pcTTL' => WANObjectCache::TTL_PROC_LONG |
| 405 |
] |
| 406 |
); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
public function listExplicitlyDefinedTags() { |
| 418 |
$fname = __METHOD__; |
| 419 |
$dbProvider = $this->dbProvider; |
| 420 |
|
| 421 |
return $this->wanCache->getWithSetCallback( |
| 422 |
$this->wanCache->makeKey( 'valid-tags-db' ), |
| 423 |
WANObjectCache::TTL_MINUTE * 5, |
| 424 |
static function ( $oldValue, &$ttl, array &$setOpts ) use ( $fname, $dbProvider ) { |
| 425 |
$dbr = $dbProvider->getReplicaDatabase(); |
| 426 |
$setOpts += Database::getCacheSetOptions( $dbr ); |
| 427 |
$tags = $dbr->newSelectQueryBuilder() |
| 428 |
->select( 'ctd_name' ) |
| 429 |
->from( self::CHANGE_TAG_DEF ) |
| 430 |
->where( [ 'ctd_user_defined' => 1 ] ) |
| 431 |
->caller( $fname ) |
| 432 |
->fetchFieldValues(); |
| 433 |
|
| 434 |
return array_unique( $tags ); |
| 435 |
}, |
| 436 |
[ |
| 437 |
'checkKeys' => [ $this->wanCache->makeKey( 'valid-tags-db' ) ], |
| 438 |
'lockTSE' => WANObjectCache::TTL_MINUTE * 5, |
| 439 |
'pcTTL' => WANObjectCache::TTL_PROC_LONG |
| 440 |
] |
| 441 |
); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
public function listSoftwareDefinedTags() { |
| 454 |
|
| 455 |
$tags = $this->getSoftwareTags( true ); |
| 456 |
if ( !$this->hookContainer->isRegistered( 'ListDefinedTags' ) ) { |
| 457 |
return $tags; |
| 458 |
} |
| 459 |
$hookRunner = $this->hookRunner; |
| 460 |
$dbProvider = $this->dbProvider; |
| 461 |
return $this->wanCache->getWithSetCallback( |
| 462 |
$this->wanCache->makeKey( 'valid-tags-hook' ), |
| 463 |
WANObjectCache::TTL_MINUTE * 5, |
| 464 |
static function ( $oldValue, &$ttl, array &$setOpts ) use ( $tags, $hookRunner, $dbProvider ) { |
| 465 |
$setOpts += Database::getCacheSetOptions( $dbProvider->getReplicaDatabase() ); |
| 466 |
$hookRunner->onListDefinedTags( $tags ); |
| 467 |
return array_unique( $tags ); |
| 468 |
}, |
| 469 |
[ |
| 470 |
'checkKeys' => [ $this->wanCache->makeKey( 'valid-tags-hook' ) ], |
| 471 |
'lockTSE' => WANObjectCache::TTL_MINUTE * 5, |
| 472 |
'pcTTL' => WANObjectCache::TTL_PROC_LONG |
| 473 |
] |
| 474 |
); |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
public function getTags( IReadableDatabase $db, $rc_id = null, $rev_id = null, $log_id = null ) { |
| 488 |
return array_keys( $this->getTagsWithData( $db, $rc_id, $rev_id, $log_id ) ); |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
public function listDefinedTags() { |
| 499 |
$tags1 = $this->listExplicitlyDefinedTags(); |
| 500 |
$tags2 = $this->listSoftwareDefinedTags(); |
| 501 |
return array_values( array_unique( array_merge( $tags1, $tags2 ) ) ); |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
public function updateTags( $tagsToAdd, $tagsToRemove, &$rc_id = null, |
| 532 |
&$rev_id = null, &$log_id = null, $params = null, ?RecentChange $rc = null, |
| 533 |
?UserIdentity $user = null |
| 534 |
) { |
| 535 |
$tagsToAdd = array_filter( |
| 536 |
(array)$tagsToAdd, |
| 537 |
static function ( $value ) { |
| 538 |
return ( $value ?? '' ) !== ''; |
| 539 |
} |
| 540 |
); |
| 541 |
$tagsToRemove = array_filter( |
| 542 |
(array)$tagsToRemove, |
| 543 |
static function ( $value ) { |
| 544 |
return ( $value ?? '' ) !== ''; |
| 545 |
} |
| 546 |
); |
| 547 |
|
| 548 |
if ( !$rc_id && !$rev_id && !$log_id ) { |
| 549 |
throw new InvalidArgumentException( 'At least one of: RCID, revision ID, and log ID MUST be ' . |
| 550 |
'specified when adding or removing a tag from a change!' ); |
| 551 |
} |
| 552 |
|
| 553 |
$dbw = $this->dbProvider->getPrimaryDatabase(); |
| 554 |
|
| 555 |
|
| 556 |
if ( !$rc_id ) { |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
if ( $log_id ) { |
| 561 |
$rc_id = $dbw->newSelectQueryBuilder() |
| 562 |
->select( 'rc_id' ) |
| 563 |
->from( 'logging' ) |
| 564 |
->join( 'recentchanges', null, [ |
| 565 |
'rc_timestamp = log_timestamp', |
| 566 |
'rc_logid = log_id' |
| 567 |
] ) |
| 568 |
->where( [ 'log_id' => $log_id ] ) |
| 569 |
->caller( __METHOD__ ) |
| 570 |
->fetchField(); |
| 571 |
} elseif ( $rev_id ) { |
| 572 |
$rc_id = $dbw->newSelectQueryBuilder() |
| 573 |
->select( 'rc_id' ) |
| 574 |
->from( 'revision' ) |
| 575 |
->join( 'recentchanges', null, [ |
| 576 |
'rc_this_oldid = rev_id' |
| 577 |
] ) |
| 578 |
->where( [ 'rev_id' => $rev_id ] ) |
| 579 |
->caller( __METHOD__ ) |
| 580 |
->fetchField(); |
| 581 |
} |
| 582 |
} elseif ( !$log_id && !$rev_id ) { |
| 583 |
|
| 584 |
$log_id = $dbw->newSelectQueryBuilder() |
| 585 |
->select( 'rc_logid' ) |
| 586 |
->from( 'recentchanges' ) |
| 587 |
->where( [ 'rc_id' => $rc_id ] ) |
| 588 |
->caller( __METHOD__ ) |
| 589 |
->fetchField(); |
| 590 |
$rev_id = $dbw->newSelectQueryBuilder() |
| 591 |
->select( 'rc_this_oldid' ) |
| 592 |
->from( 'recentchanges' ) |
| 593 |
->where( [ 'rc_id' => $rc_id ] ) |
| 594 |
->caller( __METHOD__ ) |
| 595 |
->fetchField(); |
| 596 |
} |
| 597 |
|
| 598 |
if ( $log_id && !$rev_id ) { |
| 599 |
$rev_id = $dbw->newSelectQueryBuilder() |
| 600 |
->select( 'ls_value' ) |
| 601 |
->from( 'log_search' ) |
| 602 |
->where( [ 'ls_field' => 'associated_rev_id', 'ls_log_id' => $log_id ] ) |
| 603 |
->caller( __METHOD__ ) |
| 604 |
->fetchField(); |
| 605 |
} elseif ( !$log_id && $rev_id ) { |
| 606 |
$log_id = $dbw->newSelectQueryBuilder() |
| 607 |
->select( 'ls_log_id' ) |
| 608 |
->from( 'log_search' ) |
| 609 |
->where( [ 'ls_field' => 'associated_rev_id', 'ls_value' => (string)$rev_id ] ) |
| 610 |
->caller( __METHOD__ ) |
| 611 |
->fetchField(); |
| 612 |
} |
| 613 |
|
| 614 |
$prevTags = $this->getTags( $dbw, $rc_id, $rev_id, $log_id ); |
| 615 |
|
| 616 |
|
| 617 |
$tagsToAdd = array_values( array_diff( $tagsToAdd, $prevTags ) ); |
| 618 |
$newTags = array_unique( array_merge( $prevTags, $tagsToAdd ) ); |
| 619 |
|
| 620 |
|
| 621 |
$tagsToRemove = array_values( array_intersect( $tagsToRemove, $newTags ) ); |
| 622 |
$newTags = array_values( array_diff( $newTags, $tagsToRemove ) ); |
| 623 |
|
| 624 |
sort( $prevTags ); |
| 625 |
sort( $newTags ); |
| 626 |
if ( $prevTags == $newTags ) { |
| 627 |
return [ [], [], $prevTags ]; |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
if ( count( $tagsToAdd ) ) { |
| 632 |
$changeTagMapping = []; |
| 633 |
foreach ( $tagsToAdd as $tag ) { |
| 634 |
$changeTagMapping[$tag] = $this->changeTagDefStore->acquireId( $tag ); |
| 635 |
} |
| 636 |
$fname = __METHOD__; |
| 637 |
|
| 638 |
$dbw->onTransactionPreCommitOrIdle( static function () use ( $dbw, $tagsToAdd, $fname ) { |
| 639 |
$dbw->newUpdateQueryBuilder() |
| 640 |
->update( self::CHANGE_TAG_DEF ) |
| 641 |
->set( [ 'ctd_count' => new RawSQLValue( 'ctd_count + 1' ) ] ) |
| 642 |
->where( [ 'ctd_name' => $tagsToAdd ] ) |
| 643 |
->caller( $fname )->execute(); |
| 644 |
}, $fname ); |
| 645 |
|
| 646 |
$tagsRows = []; |
| 647 |
foreach ( $tagsToAdd as $tag ) { |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
$tagsRows[] = array_filter( |
| 653 |
[ |
| 654 |
'ct_rc_id' => $rc_id, |
| 655 |
'ct_log_id' => $log_id, |
| 656 |
'ct_rev_id' => $rev_id, |
| 657 |
'ct_params' => $params, |
| 658 |
'ct_tag_id' => $changeTagMapping[$tag] ?? null, |
| 659 |
] |
| 660 |
); |
| 661 |
|
| 662 |
} |
| 663 |
|
| 664 |
$dbw->newInsertQueryBuilder() |
| 665 |
->insertInto( self::CHANGE_TAG ) |
| 666 |
->ignore() |
| 667 |
->rows( $tagsRows ) |
| 668 |
->caller( __METHOD__ )->execute(); |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
if ( count( $tagsToRemove ) ) { |
| 673 |
$fname = __METHOD__; |
| 674 |
foreach ( $tagsToRemove as $tag ) { |
| 675 |
$conds = array_filter( |
| 676 |
[ |
| 677 |
'ct_rc_id' => $rc_id, |
| 678 |
'ct_log_id' => $log_id, |
| 679 |
'ct_rev_id' => $rev_id, |
| 680 |
'ct_tag_id' => $this->changeTagDefStore->getId( $tag ), |
| 681 |
] |
| 682 |
); |
| 683 |
$dbw->newDeleteQueryBuilder() |
| 684 |
->deleteFrom( self::CHANGE_TAG ) |
| 685 |
->where( $conds ) |
| 686 |
->caller( __METHOD__ )->execute(); |
| 687 |
if ( $dbw->affectedRows() ) { |
| 688 |
|
| 689 |
$dbw->onTransactionPreCommitOrIdle( static function () use ( $dbw, $tag, $fname ) { |
| 690 |
$dbw->newUpdateQueryBuilder() |
| 691 |
->update( self::CHANGE_TAG_DEF ) |
| 692 |
->set( [ 'ctd_count' => new RawSQLValue( 'ctd_count - 1' ) ] ) |
| 693 |
->where( [ 'ctd_name' => $tag ] ) |
| 694 |
->caller( $fname )->execute(); |
| 695 |
|
| 696 |
$dbw->newDeleteQueryBuilder() |
| 697 |
->deleteFrom( self::CHANGE_TAG_DEF ) |
| 698 |
->where( [ 'ctd_name' => $tag, 'ctd_count' => 0, 'ctd_user_defined' => 0 ] ) |
| 699 |
->caller( $fname )->execute(); |
| 700 |
}, $fname ); |
| 701 |
} |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
$userObj = $user ? $this->userFactory->newFromUserIdentity( $user ) : null; |
| 706 |
$this->hookRunner->onChangeTagsAfterUpdateTags( |
| 707 |
$tagsToAdd, $tagsToRemove, $prevTags, $rc_id, $rev_id, $log_id, $params, $rc, $userObj ); |
| 708 |
|
| 709 |
return [ $tagsToAdd, $tagsToRemove, $prevTags ]; |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
public function addTags( $tags, $rc_id = null, $rev_id = null, |
| 726 |
$log_id = null, $params = null, ?RecentChange $rc = null |
| 727 |
) { |
| 728 |
$result = $this->updateTags( $tags, null, $rc_id, $rev_id, $log_id, $params, $rc ); |
| 729 |
return (bool)$result[0]; |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
public function listSoftwareActivatedTags() { |
| 739 |
|
| 740 |
$tags = $this->getSoftwareTags(); |
| 741 |
if ( !$this->hookContainer->isRegistered( 'ChangeTagsListActive' ) ) { |
| 742 |
return $tags; |
| 743 |
} |
| 744 |
$hookRunner = $this->hookRunner; |
| 745 |
$dbProvider = $this->dbProvider; |
| 746 |
|
| 747 |
return $this->wanCache->getWithSetCallback( |
| 748 |
$this->wanCache->makeKey( 'active-tags' ), |
| 749 |
WANObjectCache::TTL_MINUTE * 5, |
| 750 |
static function ( $oldValue, &$ttl, array &$setOpts ) use ( $tags, $hookRunner, $dbProvider ) { |
| 751 |
$setOpts += Database::getCacheSetOptions( $dbProvider->getReplicaDatabase() ); |
| 752 |
|
| 753 |
|
| 754 |
$hookRunner->onChangeTagsListActive( $tags ); |
| 755 |
return $tags; |
| 756 |
}, |
| 757 |
[ |
| 758 |
'checkKeys' => [ $this->wanCache->makeKey( 'active-tags' ) ], |
| 759 |
'lockTSE' => WANObjectCache::TTL_MINUTE * 5, |
| 760 |
'pcTTL' => WANObjectCache::TTL_PROC_LONG |
| 761 |
] |
| 762 |
); |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
public function modifyDisplayQuery( &$tables, &$fields, &$conds, |
| 788 |
&$join_conds, &$options, $filter_tag = '', bool $exclude = false |
| 789 |
) { |
| 790 |
$useTagFilter = $this->options->get( MainConfigNames::UseTagFilter ); |
| 791 |
|
| 792 |
|
| 793 |
$tables = (array)$tables; |
| 794 |
$fields = (array)$fields; |
| 795 |
$conds = (array)$conds; |
| 796 |
$options = (array)$options; |
| 797 |
|
| 798 |
$fields['ts_tags'] = $this->makeTagSummarySubquery( $tables ); |
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
if ( in_array( 'recentchanges', $tables ) ) { |
| 805 |
$join_cond = self::DISPLAY_TABLE_ALIAS . '.ct_rc_id=rc_id'; |
| 806 |
} elseif ( in_array( 'logging', $tables ) ) { |
| 807 |
$join_cond = self::DISPLAY_TABLE_ALIAS . '.ct_log_id=log_id'; |
| 808 |
} elseif ( in_array( 'revision', $tables ) ) { |
| 809 |
$join_cond = self::DISPLAY_TABLE_ALIAS . '.ct_rev_id=rev_id'; |
| 810 |
} elseif ( in_array( 'archive', $tables ) ) { |
| 811 |
$join_cond = self::DISPLAY_TABLE_ALIAS . '.ct_rev_id=ar_rev_id'; |
| 812 |
} else { |
| 813 |
throw new InvalidArgumentException( 'Unable to determine appropriate JOIN condition for tagging.' ); |
| 814 |
} |
| 815 |
|
| 816 |
if ( !$useTagFilter ) { |
| 817 |
return; |
| 818 |
} |
| 819 |
|
| 820 |
if ( !is_array( $filter_tag ) ) { |
| 821 |
|
| 822 |
$filter_tag = (string)$filter_tag; |
| 823 |
} |
| 824 |
|
| 825 |
if ( $filter_tag !== [] && $filter_tag !== '' ) { |
| 826 |
|
| 827 |
|
| 828 |
$filterTagIds = []; |
| 829 |
foreach ( (array)$filter_tag as $filterTagName ) { |
| 830 |
try { |
| 831 |
$filterTagIds[] = $this->changeTagDefStore->getId( $filterTagName ); |
| 832 |
} catch ( NameTableAccessException $exception ) { |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
if ( $exclude ) { |
| 837 |
if ( $filterTagIds !== [] ) { |
| 838 |
$tables[self::DISPLAY_TABLE_ALIAS] = self::CHANGE_TAG; |
| 839 |
$join_conds[self::DISPLAY_TABLE_ALIAS] = [ |
| 840 |
'LEFT JOIN', |
| 841 |
[ $join_cond, self::DISPLAY_TABLE_ALIAS . '.ct_tag_id' => $filterTagIds ] |
| 842 |
]; |
| 843 |
$conds[self::DISPLAY_TABLE_ALIAS . '.ct_tag_id'] = null; |
| 844 |
} |
| 845 |
} else { |
| 846 |
$tables[self::DISPLAY_TABLE_ALIAS] = self::CHANGE_TAG; |
| 847 |
$join_conds[self::DISPLAY_TABLE_ALIAS] = [ 'JOIN', $join_cond ]; |
| 848 |
if ( $filterTagIds !== [] ) { |
| 849 |
$conds[self::DISPLAY_TABLE_ALIAS . '.ct_tag_id'] = $filterTagIds; |
| 850 |
} else { |
| 851 |
|
| 852 |
$conds[] = '0=1'; |
| 853 |
} |
| 854 |
|
| 855 |
if ( |
| 856 |
is_array( $filter_tag ) && count( $filter_tag ) > 1 && |
| 857 |
!in_array( 'DISTINCT', $options ) |
| 858 |
) { |
| 859 |
$options[] = 'DISTINCT'; |
| 860 |
} |
| 861 |
} |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
public function modifyDisplayQueryBuilder( |
| 883 |
SelectQueryBuilder $queryBuilder, |
| 884 |
$table, |
| 885 |
$filter_tag = '', |
| 886 |
bool $exclude = false |
| 887 |
) { |
| 888 |
$useTagFilter = $this->options->get( MainConfigNames::UseTagFilter ); |
| 889 |
$queryBuilder->field( $this->makeTagSummarySubquery( [ $table ] ), 'ts_tags' ); |
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
if ( $table === 'recentchanges' ) { |
| 896 |
$join_cond = self::DISPLAY_TABLE_ALIAS . '.ct_rc_id=rc_id'; |
| 897 |
} elseif ( $table === 'logging' ) { |
| 898 |
$join_cond = self::DISPLAY_TABLE_ALIAS . '.ct_log_id=log_id'; |
| 899 |
} elseif ( $table === 'revision' ) { |
| 900 |
$join_cond = self::DISPLAY_TABLE_ALIAS . '.ct_rev_id=rev_id'; |
| 901 |
} elseif ( $table === 'archive' ) { |
| 902 |
$join_cond = self::DISPLAY_TABLE_ALIAS . '.ct_rev_id=ar_rev_id'; |
| 903 |
} else { |
| 904 |
throw new InvalidArgumentException( 'Unable to determine appropriate JOIN condition for tagging.' ); |
| 905 |
} |
| 906 |
|
| 907 |
if ( !$useTagFilter ) { |
| 908 |
return; |
| 909 |
} |
| 910 |
|
| 911 |
if ( !is_array( $filter_tag ) ) { |
| 912 |
|
| 913 |
$filter_tag = (string)$filter_tag; |
| 914 |
} |
| 915 |
|
| 916 |
if ( $filter_tag !== [] && $filter_tag !== '' ) { |
| 917 |
|
| 918 |
|
| 919 |
$filterTagIds = []; |
| 920 |
foreach ( (array)$filter_tag as $filterTagName ) { |
| 921 |
try { |
| 922 |
$filterTagIds[] = $this->changeTagDefStore->getId( $filterTagName ); |
| 923 |
} catch ( NameTableAccessException $exception ) { |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
if ( $exclude ) { |
| 928 |
if ( $filterTagIds !== [] ) { |
| 929 |
$queryBuilder->leftJoin( |
| 930 |
self::CHANGE_TAG, |
| 931 |
self::DISPLAY_TABLE_ALIAS, |
| 932 |
[ $join_cond, self::DISPLAY_TABLE_ALIAS . '.ct_tag_id' => $filterTagIds ] |
| 933 |
); |
| 934 |
$queryBuilder->where( [ self::DISPLAY_TABLE_ALIAS . '.ct_tag_id' => null ] ); |
| 935 |
} |
| 936 |
} else { |
| 937 |
$queryBuilder->join( |
| 938 |
self::CHANGE_TAG, |
| 939 |
self::DISPLAY_TABLE_ALIAS, |
| 940 |
$join_cond |
| 941 |
); |
| 942 |
if ( $filterTagIds !== [] ) { |
| 943 |
$queryBuilder->where( [ self::DISPLAY_TABLE_ALIAS . '.ct_tag_id' => $filterTagIds ] ); |
| 944 |
} else { |
| 945 |
|
| 946 |
$queryBuilder->where( '0=1' ); |
| 947 |
} |
| 948 |
|
| 949 |
if ( |
| 950 |
is_array( $filter_tag ) && count( $filter_tag ) > 1 |
| 951 |
) { |
| 952 |
$queryBuilder->distinct(); |
| 953 |
} |
| 954 |
} |
| 955 |
} |
| 956 |
} |
| 957 |
} |