| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Watchlist; |
| 4 |
|
| 5 |
use DateInterval; |
| 6 |
use JobQueueGroup; |
| 7 |
use LogicException; |
| 8 |
use MapCacheLRU; |
| 9 |
use MediaWiki\Cache\LinkBatchFactory; |
| 10 |
use MediaWiki\Config\ServiceOptions; |
| 11 |
use MediaWiki\Deferred\DeferredUpdates; |
| 12 |
use MediaWiki\Linker\LinkTarget; |
| 13 |
use MediaWiki\MainConfigNames; |
| 14 |
use MediaWiki\Page\PageIdentity; |
| 15 |
use MediaWiki\Revision\RevisionLookup; |
| 16 |
use MediaWiki\Title\NamespaceInfo; |
| 17 |
use MediaWiki\Title\TitleValue; |
| 18 |
use MediaWiki\User\UserIdentity; |
| 19 |
use MediaWiki\Utils\MWTimestamp; |
| 20 |
use stdClass; |
| 21 |
use Wikimedia\Assert\Assert; |
| 22 |
use Wikimedia\ObjectCache\BagOStuff; |
| 23 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 24 |
use Wikimedia\ParamValidator\TypeDef\ExpiryDef; |
| 25 |
use Wikimedia\Rdbms\IDatabase; |
| 26 |
use Wikimedia\Rdbms\ILBFactory; |
| 27 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 28 |
use Wikimedia\Rdbms\IResultWrapper; |
| 29 |
use Wikimedia\Rdbms\ReadOnlyMode; |
| 30 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 31 |
use Wikimedia\ScopedCallback; |
| 32 |
use Wikimedia\Stats\StatsFactory; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class WatchedItemStore implements WatchedItemStoreInterface { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public const CONSTRUCTOR_OPTIONS = [ |
| 48 |
MainConfigNames::UpdateRowsPerQuery, |
| 49 |
MainConfigNames::WatchlistExpiry, |
| 50 |
MainConfigNames::WatchlistExpiryMaxDuration, |
| 51 |
MainConfigNames::WatchlistPurgeRate, |
| 52 |
]; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private $lbFactory; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
private $queueGroup; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
private $stash; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private $readOnlyMode; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
private $cache; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
private $latestUpdateCache; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
private $cacheIndex = []; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
private $deferredUpdatesAddCallableUpdateCallback; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
private $updateRowsPerQuery; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
private $nsInfo; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
private $revisionLookup; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
private $expiryEnabled; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
private $linkBatchFactory; |
| 122 |
|
| 123 |
|
| 124 |
private $statsFactory; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
private $maxExpiryDuration; |
| 130 |
|
| 131 |
|
| 132 |
private $watchlistPurgeRate; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function __construct( |
| 147 |
ServiceOptions $options, |
| 148 |
ILBFactory $lbFactory, |
| 149 |
JobQueueGroup $queueGroup, |
| 150 |
BagOStuff $stash, |
| 151 |
HashBagOStuff $cache, |
| 152 |
ReadOnlyMode $readOnlyMode, |
| 153 |
NamespaceInfo $nsInfo, |
| 154 |
RevisionLookup $revisionLookup, |
| 155 |
LinkBatchFactory $linkBatchFactory, |
| 156 |
StatsFactory $statsFactory |
| 157 |
) { |
| 158 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 159 |
$this->updateRowsPerQuery = $options->get( MainConfigNames::UpdateRowsPerQuery ); |
| 160 |
$this->expiryEnabled = $options->get( MainConfigNames::WatchlistExpiry ); |
| 161 |
$this->maxExpiryDuration = $options->get( MainConfigNames::WatchlistExpiryMaxDuration ); |
| 162 |
$this->watchlistPurgeRate = $options->get( MainConfigNames::WatchlistPurgeRate ); |
| 163 |
|
| 164 |
$this->lbFactory = $lbFactory; |
| 165 |
$this->queueGroup = $queueGroup; |
| 166 |
$this->stash = $stash; |
| 167 |
$this->cache = $cache; |
| 168 |
$this->readOnlyMode = $readOnlyMode; |
| 169 |
$this->deferredUpdatesAddCallableUpdateCallback = |
| 170 |
[ DeferredUpdates::class, 'addCallableUpdate' ]; |
| 171 |
$this->nsInfo = $nsInfo; |
| 172 |
$this->revisionLookup = $revisionLookup; |
| 173 |
$this->linkBatchFactory = $linkBatchFactory; |
| 174 |
$this->statsFactory = $statsFactory; |
| 175 |
|
| 176 |
$this->latestUpdateCache = new HashBagOStuff( [ 'maxKeys' => 3 ] ); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function overrideDeferredUpdatesAddCallableUpdateCallback( callable $callback ) { |
| 190 |
if ( !defined( 'MW_PHPUNIT_TEST' ) ) { |
| 191 |
throw new LogicException( |
| 192 |
'Cannot override DeferredUpdates::addCallableUpdate callback in operation.' |
| 193 |
); |
| 194 |
} |
| 195 |
$previousValue = $this->deferredUpdatesAddCallableUpdateCallback; |
| 196 |
$this->deferredUpdatesAddCallableUpdateCallback = $callback; |
| 197 |
return new ScopedCallback( function () use ( $previousValue ) { |
| 198 |
$this->deferredUpdatesAddCallableUpdateCallback = $previousValue; |
| 199 |
} ); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
private function getCacheKey( UserIdentity $user, $target ): string { |
| 208 |
return $this->cache->makeKey( |
| 209 |
(string)$target->getNamespace(), |
| 210 |
$target->getDBkey(), |
| 211 |
(string)$user->getId() |
| 212 |
); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
private function cache( WatchedItem $item ) { |
| 219 |
$user = $item->getUserIdentity(); |
| 220 |
$target = $item->getTarget(); |
| 221 |
$key = $this->getCacheKey( $user, $target ); |
| 222 |
$this->cache->set( $key, $item ); |
| 223 |
$this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] = $key; |
| 224 |
$this->statsFactory->getCounter( 'WatchedItemStore_cache_total' ) |
| 225 |
->copyToStatsdAt( 'WatchedItemStore.cache' ) |
| 226 |
->increment(); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
private function uncache( UserIdentity $user, $target ) { |
| 234 |
$this->cache->delete( $this->getCacheKey( $user, $target ) ); |
| 235 |
unset( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] ); |
| 236 |
$this->statsFactory->getCounter( 'WatchedItemStore_uncache_total' ) |
| 237 |
->copyToStatsdAt( 'WatchedItemStore.uncache' ) |
| 238 |
->increment(); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
private function uncacheLinkTarget( $target ) { |
| 245 |
$this->statsFactory->getCounter( 'WatchedItemStore_uncacheLinkTarget_total' ) |
| 246 |
->copyToStatsdAt( 'WatchedItemStore.uncacheLinkTarget' ) |
| 247 |
->increment(); |
| 248 |
if ( !isset( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()] ) ) { |
| 249 |
return; |
| 250 |
} |
| 251 |
|
| 252 |
$uncacheLinkTargetItemsTotal = $this->statsFactory |
| 253 |
->getCounter( 'WatchedItemStore_uncacheLinkTarget_items_total' ) |
| 254 |
->copyToStatsdAt( 'WatchedItemStore.uncacheLinkTarget.items' ); |
| 255 |
|
| 256 |
foreach ( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()] as $key ) { |
| 257 |
$uncacheLinkTargetItemsTotal->increment(); |
| 258 |
$this->cache->delete( $key ); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
private function uncacheUser( UserIdentity $user ) { |
| 266 |
$this->statsFactory->getCounter( 'WatchedItemStore_uncacheUser_total' ) |
| 267 |
->copyToStatsdAt( 'WatchedItemStore.uncacheUser' ) |
| 268 |
->increment(); |
| 269 |
$uncacheUserItemsTotal = $this->statsFactory->getCounter( 'WatchedItemStore_uncacheUser_items_total' ) |
| 270 |
->copyToStatsdAt( 'WatchedItemStore.uncacheUser.items' ); |
| 271 |
|
| 272 |
foreach ( $this->cacheIndex as $dbKeyArray ) { |
| 273 |
foreach ( $dbKeyArray as $userArray ) { |
| 274 |
if ( isset( $userArray[$user->getId()] ) ) { |
| 275 |
$uncacheUserItemsTotal->increment(); |
| 276 |
$this->cache->delete( $userArray[$user->getId()] ); |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
$pageSeenKey = $this->getPageSeenTimestampsKey( $user ); |
| 282 |
$this->latestUpdateCache->delete( $pageSeenKey ); |
| 283 |
$this->stash->delete( $pageSeenKey ); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
private function getCached( UserIdentity $user, $target ) { |
| 293 |
return $this->cache->get( $this->getCacheKey( $user, $target ) ); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
private function modifyQueryBuilderForExpiry( |
| 304 |
SelectQueryBuilder $queryBuilder, |
| 305 |
IReadableDatabase $db |
| 306 |
) { |
| 307 |
if ( $this->expiryEnabled ) { |
| 308 |
$queryBuilder->where( $db->expr( 'we_expiry', '=', null )->or( 'we_expiry', '>', $db->timestamp() ) ); |
| 309 |
$queryBuilder->leftJoin( 'watchlist_expiry', null, 'wl_id = we_item' ); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
public function clearUserWatchedItems( UserIdentity $user ): bool { |
| 324 |
if ( $this->mustClearWatchedItemsUsingJobQueue( $user ) ) { |
| 325 |
return false; |
| 326 |
} |
| 327 |
|
| 328 |
$dbw = $this->lbFactory->getPrimaryDatabase(); |
| 329 |
|
| 330 |
if ( $this->expiryEnabled ) { |
| 331 |
$ticket = $this->lbFactory->getEmptyTransactionTicket( __METHOD__ ); |
| 332 |
|
| 333 |
$wlIds = $dbw->newSelectQueryBuilder() |
| 334 |
->select( 'wl_id' ) |
| 335 |
->from( 'watchlist' ) |
| 336 |
->where( [ 'wl_user' => $user->getId() ] ) |
| 337 |
->caller( __METHOD__ ) |
| 338 |
->fetchFieldValues(); |
| 339 |
if ( $wlIds ) { |
| 340 |
|
| 341 |
$dbw->newDeleteQueryBuilder() |
| 342 |
->deleteFrom( 'watchlist' ) |
| 343 |
->where( [ 'wl_id' => $wlIds ] ) |
| 344 |
->caller( __METHOD__ )->execute(); |
| 345 |
|
| 346 |
$dbw->newDeleteQueryBuilder() |
| 347 |
->deleteFrom( 'watchlist_expiry' ) |
| 348 |
->where( [ 'we_item' => $wlIds ] ) |
| 349 |
->caller( __METHOD__ )->execute(); |
| 350 |
} |
| 351 |
$this->lbFactory->commitAndWaitForReplication( __METHOD__, $ticket ); |
| 352 |
} else { |
| 353 |
$dbw->newDeleteQueryBuilder() |
| 354 |
->deleteFrom( 'watchlist' ) |
| 355 |
->where( [ 'wl_user' => $user->getId() ] ) |
| 356 |
->caller( __METHOD__ )->execute(); |
| 357 |
} |
| 358 |
|
| 359 |
$this->uncacheAllItemsForUser( $user ); |
| 360 |
|
| 361 |
return true; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
public function mustClearWatchedItemsUsingJobQueue( UserIdentity $user ): bool { |
| 369 |
return $this->countWatchedItems( $user ) > $this->updateRowsPerQuery; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
private function uncacheAllItemsForUser( UserIdentity $user ) { |
| 376 |
$userId = $user->getId(); |
| 377 |
foreach ( $this->cacheIndex as $ns => $dbKeyIndex ) { |
| 378 |
foreach ( $dbKeyIndex as $dbKey => $userIndex ) { |
| 379 |
if ( array_key_exists( $userId, $userIndex ) ) { |
| 380 |
$this->cache->delete( $userIndex[$userId] ); |
| 381 |
unset( $this->cacheIndex[$ns][$dbKey][$userId] ); |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
foreach ( $this->cacheIndex as $ns => $dbKeyIndex ) { |
| 388 |
foreach ( $dbKeyIndex as $dbKey => $userIndex ) { |
| 389 |
if ( empty( $this->cacheIndex[$ns][$dbKey] ) ) { |
| 390 |
unset( $this->cacheIndex[$ns][$dbKey] ); |
| 391 |
} |
| 392 |
} |
| 393 |
if ( empty( $this->cacheIndex[$ns] ) ) { |
| 394 |
unset( $this->cacheIndex[$ns] ); |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
public function clearUserWatchedItemsUsingJobQueue( UserIdentity $user ) { |
| 407 |
$job = ClearUserWatchlistJob::newForUser( $user, $this->getMaxId() ); |
| 408 |
$this->queueGroup->push( $job ); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
public function maybeEnqueueWatchlistExpiryJob(): void { |
| 415 |
if ( !$this->expiryEnabled ) { |
| 416 |
|
| 417 |
return; |
| 418 |
} |
| 419 |
|
| 420 |
$max = mt_getrandmax(); |
| 421 |
if ( mt_rand( 0, $max ) < $max * $this->watchlistPurgeRate ) { |
| 422 |
|
| 423 |
$this->queueGroup->lazyPush( new WatchlistExpiryJob() ); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
public function getMaxId(): int { |
| 432 |
return (int)$this->lbFactory->getReplicaDatabase()->newSelectQueryBuilder() |
| 433 |
->select( 'MAX(wl_id)' ) |
| 434 |
->from( 'watchlist' ) |
| 435 |
->caller( __METHOD__ ) |
| 436 |
->fetchField(); |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
public function countWatchedItems( UserIdentity $user ): int { |
| 445 |
$dbr = $this->lbFactory->getReplicaDatabase(); |
| 446 |
$queryBuilder = $dbr->newSelectQueryBuilder() |
| 447 |
->select( 'COUNT(*)' ) |
| 448 |
->from( 'watchlist' ) |
| 449 |
->where( [ 'wl_user' => $user->getId() ] ) |
| 450 |
->caller( __METHOD__ ); |
| 451 |
|
| 452 |
$this->modifyQueryBuilderForExpiry( $queryBuilder, $dbr ); |
| 453 |
|
| 454 |
return (int)$queryBuilder->fetchField(); |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
public function countWatchers( $target ): int { |
| 463 |
$dbr = $this->lbFactory->getReplicaDatabase(); |
| 464 |
$queryBuilder = $dbr->newSelectQueryBuilder() |
| 465 |
->select( 'COUNT(*)' ) |
| 466 |
->from( 'watchlist' ) |
| 467 |
->where( [ |
| 468 |
'wl_namespace' => $target->getNamespace(), |
| 469 |
'wl_title' => $target->getDBkey() |
| 470 |
] ) |
| 471 |
->caller( __METHOD__ ); |
| 472 |
|
| 473 |
$this->modifyQueryBuilderForExpiry( $queryBuilder, $dbr ); |
| 474 |
|
| 475 |
return (int)$queryBuilder->fetchField(); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
public function countVisitingWatchers( $target, $threshold ): int { |
| 485 |
$dbr = $this->lbFactory->getReplicaDatabase(); |
| 486 |
$queryBuilder = $dbr->newSelectQueryBuilder() |
| 487 |
->select( 'COUNT(*)' ) |
| 488 |
->from( 'watchlist' ) |
| 489 |
->where( [ |
| 490 |
'wl_namespace' => $target->getNamespace(), |
| 491 |
'wl_title' => $target->getDBkey(), |
| 492 |
$dbr->expr( 'wl_notificationtimestamp', '>=', $dbr->timestamp( $threshold ) ) |
| 493 |
->or( 'wl_notificationtimestamp', '=', null ) |
| 494 |
] ) |
| 495 |
->caller( __METHOD__ ); |
| 496 |
|
| 497 |
$this->modifyQueryBuilderForExpiry( $queryBuilder, $dbr ); |
| 498 |
|
| 499 |
return (int)$queryBuilder->fetchField(); |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
public function removeWatchBatchForUser( UserIdentity $user, array $titles ): bool { |
| 508 |
if ( !$user->isRegistered() || $this->readOnlyMode->isReadOnly() ) { |
| 509 |
return false; |
| 510 |
} |
| 511 |
if ( !$titles ) { |
| 512 |
return true; |
| 513 |
} |
| 514 |
|
| 515 |
$rows = $this->getTitleDbKeysGroupedByNamespace( $titles ); |
| 516 |
$this->uncacheTitlesForUser( $user, $titles ); |
| 517 |
|
| 518 |
$dbw = $this->lbFactory->getPrimaryDatabase(); |
| 519 |
$ticket = count( $titles ) > $this->updateRowsPerQuery ? |
| 520 |
$this->lbFactory->getEmptyTransactionTicket( __METHOD__ ) : null; |
| 521 |
$affectedRows = 0; |
| 522 |
|
| 523 |
|
| 524 |
foreach ( $rows as $namespace => $namespaceTitles ) { |
| 525 |
$rowBatches = array_chunk( $namespaceTitles, $this->updateRowsPerQuery ); |
| 526 |
foreach ( $rowBatches as $toDelete ) { |
| 527 |
|
| 528 |
$wlIds = $dbw->newSelectQueryBuilder() |
| 529 |
->select( 'wl_id' ) |
| 530 |
->from( 'watchlist' ) |
| 531 |
->where( |
| 532 |
[ |
| 533 |
'wl_user' => $user->getId(), |
| 534 |
'wl_namespace' => $namespace, |
| 535 |
'wl_title' => $toDelete |
| 536 |
] |
| 537 |
) |
| 538 |
->caller( __METHOD__ ) |
| 539 |
->fetchFieldValues(); |
| 540 |
|
| 541 |
if ( $wlIds ) { |
| 542 |
|
| 543 |
$dbw->newDeleteQueryBuilder() |
| 544 |
->deleteFrom( 'watchlist' ) |
| 545 |
->where( [ 'wl_id' => $wlIds ] ) |
| 546 |
->caller( __METHOD__ )->execute(); |
| 547 |
$affectedRows += $dbw->affectedRows(); |
| 548 |
|
| 549 |
if ( $this->expiryEnabled ) { |
| 550 |
$dbw->newDeleteQueryBuilder() |
| 551 |
->deleteFrom( 'watchlist_expiry' ) |
| 552 |
->where( [ 'we_item' => $wlIds ] ) |
| 553 |
->caller( __METHOD__ )->execute(); |
| 554 |
$affectedRows += $dbw->affectedRows(); |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
if ( $ticket ) { |
| 559 |
$this->lbFactory->commitAndWaitForReplication( __METHOD__, $ticket ); |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
return (bool)$affectedRows; |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
public function countWatchersMultiple( array $targets, array $options = [] ): array { |
| 575 |
$linkTargets = array_map( static function ( $target ) { |
| 576 |
if ( !$target instanceof LinkTarget ) { |
| 577 |
return new TitleValue( $target->getNamespace(), $target->getDBkey() ); |
| 578 |
} |
| 579 |
return $target; |
| 580 |
}, $targets ); |
| 581 |
$lb = $this->linkBatchFactory->newLinkBatch( $linkTargets ); |
| 582 |
$dbr = $this->lbFactory->getReplicaDatabase(); |
| 583 |
$queryBuilder = $dbr->newSelectQueryBuilder(); |
| 584 |
$queryBuilder |
| 585 |
->select( [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ] ) |
| 586 |
->from( 'watchlist' ) |
| 587 |
->where( [ $lb->constructSet( 'wl', $dbr ) ] ) |
| 588 |
->groupBy( [ 'wl_namespace', 'wl_title' ] ) |
| 589 |
->caller( __METHOD__ ); |
| 590 |
|
| 591 |
if ( array_key_exists( 'minimumWatchers', $options ) ) { |
| 592 |
$queryBuilder->having( 'COUNT(*) >= ' . (int)$options['minimumWatchers'] ); |
| 593 |
} |
| 594 |
|
| 595 |
$this->modifyQueryBuilderForExpiry( $queryBuilder, $dbr ); |
| 596 |
|
| 597 |
$res = $queryBuilder->fetchResultSet(); |
| 598 |
|
| 599 |
$watchCounts = []; |
| 600 |
foreach ( $targets as $linkTarget ) { |
| 601 |
$watchCounts[$linkTarget->getNamespace()][$linkTarget->getDBkey()] = 0; |
| 602 |
} |
| 603 |
|
| 604 |
foreach ( $res as $row ) { |
| 605 |
$watchCounts[$row->wl_namespace][$row->wl_title] = (int)$row->watchers; |
| 606 |
} |
| 607 |
|
| 608 |
return $watchCounts; |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
public function countVisitingWatchersMultiple( |
| 620 |
array $targetsWithVisitThresholds, |
| 621 |
$minimumWatchers = null |
| 622 |
): array { |
| 623 |
if ( $targetsWithVisitThresholds === [] ) { |
| 624 |
|
| 625 |
return []; |
| 626 |
} |
| 627 |
|
| 628 |
$dbr = $this->lbFactory->getReplicaDatabase(); |
| 629 |
$queryBuilder = $dbr->newSelectQueryBuilder() |
| 630 |
->select( [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ] ) |
| 631 |
->from( 'watchlist' ) |
| 632 |
->where( [ $this->getVisitingWatchersCondition( $dbr, $targetsWithVisitThresholds ) ] ) |
| 633 |
->groupBy( [ 'wl_namespace', 'wl_title' ] ) |
| 634 |
->caller( __METHOD__ ); |
| 635 |
if ( $minimumWatchers !== null ) { |
| 636 |
$queryBuilder->having( 'COUNT(*) >= ' . (int)$minimumWatchers ); |
| 637 |
} |
| 638 |
$this->modifyQueryBuilderForExpiry( $queryBuilder, $dbr ); |
| 639 |
|
| 640 |
$res = $queryBuilder->fetchResultSet(); |
| 641 |
|
| 642 |
$watcherCounts = []; |
| 643 |
foreach ( $targetsWithVisitThresholds as [ $target ] ) { |
| 644 |
|
| 645 |
$watcherCounts[$target->getNamespace()][$target->getDBkey()] = 0; |
| 646 |
} |
| 647 |
|
| 648 |
foreach ( $res as $row ) { |
| 649 |
$watcherCounts[$row->wl_namespace][$row->wl_title] = (int)$row->watchers; |
| 650 |
} |
| 651 |
|
| 652 |
return $watcherCounts; |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
private function getVisitingWatchersCondition( |
| 664 |
IReadableDatabase $db, |
| 665 |
array $targetsWithVisitThresholds |
| 666 |
): string { |
| 667 |
$missingTargets = []; |
| 668 |
$namespaceConds = []; |
| 669 |
foreach ( $targetsWithVisitThresholds as [ $target, $threshold ] ) { |
| 670 |
if ( $threshold === null ) { |
| 671 |
$missingTargets[] = $target; |
| 672 |
continue; |
| 673 |
} |
| 674 |
|
| 675 |
$namespaceConds[$target->getNamespace()][] = $db->expr( 'wl_title', '=', $target->getDBkey() ) |
| 676 |
->andExpr( |
| 677 |
$db->expr( 'wl_notificationtimestamp', '>=', $db->timestamp( $threshold ) ) |
| 678 |
->or( 'wl_notificationtimestamp', '=', null ) |
| 679 |
); |
| 680 |
} |
| 681 |
|
| 682 |
$conds = []; |
| 683 |
foreach ( $namespaceConds as $namespace => $pageConds ) { |
| 684 |
$conds[] = $db->makeList( [ |
| 685 |
'wl_namespace = ' . $namespace, |
| 686 |
'(' . $db->makeList( $pageConds, LIST_OR ) . ')' |
| 687 |
], LIST_AND ); |
| 688 |
} |
| 689 |
|
| 690 |
if ( $missingTargets ) { |
| 691 |
$lb = $this->linkBatchFactory->newLinkBatch( $missingTargets ); |
| 692 |
$conds[] = $lb->constructSet( 'wl', $db ); |
| 693 |
} |
| 694 |
|
| 695 |
return $db->makeList( $conds, LIST_OR ); |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
public function getWatchedItem( UserIdentity $user, $target ) { |
| 705 |
if ( !$user->isRegistered() ) { |
| 706 |
return false; |
| 707 |
} |
| 708 |
|
| 709 |
$cached = $this->getCached( $user, $target ); |
| 710 |
if ( $cached && !$cached->isExpired() ) { |
| 711 |
$this->statsFactory->getCounter( 'WatchedItemStore_getWatchedItem_accesses_total' ) |
| 712 |
->setLabel( 'status', 'hit' ) |
| 713 |
->copyToStatsdAt( 'WatchedItemStore.getWatchedItem.cached' ) |
| 714 |
->increment(); |
| 715 |
return $cached; |
| 716 |
} |
| 717 |
$this->statsFactory->getCounter( 'WatchedItemStore_getWatchedItem_accesses_total' ) |
| 718 |
->setLabel( 'status', 'miss' ) |
| 719 |
->copyToStatsdAt( 'WatchedItemStore.getWatchedItem.load' ) |
| 720 |
->increment(); |
| 721 |
return $this->loadWatchedItem( $user, $target ); |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
public function loadWatchedItem( UserIdentity $user, $target ) { |
| 731 |
$item = $this->loadWatchedItemsBatch( $user, [ $target ] ); |
| 732 |
return $item ? $item[0] : false; |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
public function loadWatchedItemsBatch( UserIdentity $user, array $targets ) { |
| 742 |
|
| 743 |
if ( !$user->isRegistered() ) { |
| 744 |
return false; |
| 745 |
} |
| 746 |
|
| 747 |
$dbr = $this->lbFactory->getReplicaDatabase(); |
| 748 |
|
| 749 |
$rows = $this->fetchWatchedItems( |
| 750 |
$dbr, |
| 751 |
$user, |
| 752 |
[ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ], |
| 753 |
[], |
| 754 |
$targets |
| 755 |
); |
| 756 |
|
| 757 |
if ( !$rows ) { |
| 758 |
return false; |
| 759 |
} |
| 760 |
|
| 761 |
$items = []; |
| 762 |
foreach ( $rows as $row ) { |
| 763 |
|
| 764 |
$target = new TitleValue( (int)$row->wl_namespace, $row->wl_title ); |
| 765 |
$item = $this->getWatchedItemFromRow( $user, $target, $row ); |
| 766 |
$this->cache( $item ); |
| 767 |
$items[] = $item; |
| 768 |
} |
| 769 |
|
| 770 |
return $items; |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
public function getWatchedItemsForUser( UserIdentity $user, array $options = [] ): array { |
| 784 |
$options += [ 'forWrite' => false ]; |
| 785 |
$vars = [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ]; |
| 786 |
$orderBy = []; |
| 787 |
if ( $options['forWrite'] ) { |
| 788 |
$db = $this->lbFactory->getPrimaryDatabase(); |
| 789 |
} else { |
| 790 |
$db = $this->lbFactory->getReplicaDatabase(); |
| 791 |
} |
| 792 |
if ( array_key_exists( 'sort', $options ) ) { |
| 793 |
Assert::parameter( |
| 794 |
( in_array( $options['sort'], [ self::SORT_ASC, self::SORT_DESC ] ) ), |
| 795 |
'$options[\'sort\']', |
| 796 |
'must be SORT_ASC or SORT_DESC' |
| 797 |
); |
| 798 |
$orderBy[] = "wl_namespace {$options['sort']}"; |
| 799 |
if ( $this->expiryEnabled |
| 800 |
&& array_key_exists( 'sortByExpiry', $options ) |
| 801 |
&& $options['sortByExpiry'] |
| 802 |
) { |
| 803 |
|
| 804 |
$vars['wl_has_expiry'] = $db->conditional( 'we_expiry IS NULL', '0', '1' ); |
| 805 |
|
| 806 |
|
| 807 |
$orderBy[] = "wl_has_expiry DESC"; |
| 808 |
$orderBy[] = "we_expiry ASC"; |
| 809 |
} |
| 810 |
|
| 811 |
$orderBy[] = "wl_title {$options['sort']}"; |
| 812 |
} |
| 813 |
|
| 814 |
$res = $this->fetchWatchedItems( |
| 815 |
$db, |
| 816 |
$user, |
| 817 |
$vars, |
| 818 |
$orderBy |
| 819 |
); |
| 820 |
|
| 821 |
$watchedItems = []; |
| 822 |
foreach ( $res as $row ) { |
| 823 |
|
| 824 |
$target = new TitleValue( (int)$row->wl_namespace, $row->wl_title ); |
| 825 |
|
| 826 |
$watchedItems[] = $this->getWatchedItemFromRow( $user, $target, $row ); |
| 827 |
} |
| 828 |
|
| 829 |
return $watchedItems; |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
private function getWatchedItemFromRow( |
| 840 |
UserIdentity $user, |
| 841 |
$target, |
| 842 |
stdClass $row |
| 843 |
): WatchedItem { |
| 844 |
return new WatchedItem( |
| 845 |
$user, |
| 846 |
$target, |
| 847 |
$this->getLatestNotificationTimestamp( |
| 848 |
$row->wl_notificationtimestamp, $user, $target ), |
| 849 |
wfTimestampOrNull( TS_ISO_8601, $row->we_expiry ?? null ) |
| 850 |
); |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
private function fetchWatchedItems( |
| 867 |
IReadableDatabase $db, |
| 868 |
UserIdentity $user, |
| 869 |
array $vars, |
| 870 |
array $orderBy = [], |
| 871 |
$target = null |
| 872 |
) { |
| 873 |
$dbMethod = 'select'; |
| 874 |
$queryBuilder = $db->newSelectQueryBuilder() |
| 875 |
->select( $vars ) |
| 876 |
->from( 'watchlist' ) |
| 877 |
->where( [ 'wl_user' => $user->getId() ] ) |
| 878 |
->caller( __METHOD__ ); |
| 879 |
if ( $target ) { |
| 880 |
if ( $target instanceof LinkTarget || $target instanceof PageIdentity ) { |
| 881 |
$queryBuilder->where( [ |
| 882 |
'wl_namespace' => $target->getNamespace(), |
| 883 |
'wl_title' => $target->getDBkey(), |
| 884 |
] ); |
| 885 |
$dbMethod = 'selectRow'; |
| 886 |
} else { |
| 887 |
$titleConds = []; |
| 888 |
foreach ( $target as $linkTarget ) { |
| 889 |
$titleConds[] = $db->makeList( |
| 890 |
[ |
| 891 |
'wl_namespace' => $linkTarget->getNamespace(), |
| 892 |
'wl_title' => $linkTarget->getDBkey(), |
| 893 |
], |
| 894 |
$db::LIST_AND |
| 895 |
); |
| 896 |
} |
| 897 |
$queryBuilder->where( $db->makeList( $titleConds, $db::LIST_OR ) ); |
| 898 |
} |
| 899 |
} |
| 900 |
|
| 901 |
$this->modifyQueryBuilderForExpiry( $queryBuilder, $db ); |
| 902 |
if ( $this->expiryEnabled ) { |
| 903 |
$queryBuilder->field( 'we_expiry' ); |
| 904 |
} |
| 905 |
if ( $orderBy ) { |
| 906 |
$queryBuilder->orderBy( $orderBy ); |
| 907 |
} |
| 908 |
|
| 909 |
if ( $dbMethod == 'selectRow' ) { |
| 910 |
return $queryBuilder->fetchRow(); |
| 911 |
} |
| 912 |
return $queryBuilder->fetchResultSet(); |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
public function isWatched( UserIdentity $user, $target ): bool { |
| 922 |
return (bool)$this->getWatchedItem( $user, $target ); |
| 923 |
} |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
public function isTempWatched( UserIdentity $user, $target ): bool { |
| 933 |
$item = $this->getWatchedItem( $user, $target ); |
| 934 |
return $item && $item->getExpiry(); |
| 935 |
} |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
public function getNotificationTimestampsBatch( UserIdentity $user, array $targets ): array { |
| 945 |
$timestamps = []; |
| 946 |
foreach ( $targets as $target ) { |
| 947 |
$timestamps[$target->getNamespace()][$target->getDBkey()] = false; |
| 948 |
} |
| 949 |
|
| 950 |
if ( !$user->isRegistered() ) { |
| 951 |
return $timestamps; |
| 952 |
} |
| 953 |
|
| 954 |
$targetsToLoad = []; |
| 955 |
foreach ( $targets as $target ) { |
| 956 |
$cachedItem = $this->getCached( $user, $target ); |
| 957 |
if ( $cachedItem ) { |
| 958 |
$timestamps[$target->getNamespace()][$target->getDBkey()] = |
| 959 |
$cachedItem->getNotificationTimestamp(); |
| 960 |
} else { |
| 961 |
$targetsToLoad[] = $target; |
| 962 |
} |
| 963 |
} |
| 964 |
|
| 965 |
if ( !$targetsToLoad ) { |
| 966 |
return $timestamps; |
| 967 |
} |
| 968 |
|
| 969 |
$dbr = $this->lbFactory->getReplicaDatabase(); |
| 970 |
|
| 971 |
$lb = $this->linkBatchFactory->newLinkBatch( $targetsToLoad ); |
| 972 |
$res = $dbr->newSelectQueryBuilder() |
| 973 |
->select( [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ] ) |
| 974 |
->from( 'watchlist' ) |
| 975 |
->where( [ |
| 976 |
$lb->constructSet( 'wl', $dbr ), |
| 977 |
'wl_user' => $user->getId(), |
| 978 |
] ) |
| 979 |
->caller( __METHOD__ ) |
| 980 |
->fetchResultSet(); |
| 981 |
|
| 982 |
foreach ( $res as $row ) { |
| 983 |
|
| 984 |
$target = new TitleValue( (int)$row->wl_namespace, $row->wl_title ); |
| 985 |
$timestamps[$row->wl_namespace][$row->wl_title] = |
| 986 |
$this->getLatestNotificationTimestamp( |
| 987 |
$row->wl_notificationtimestamp, $user, $target ); |
| 988 |
} |
| 989 |
|
| 990 |
return $timestamps; |
| 991 |
} |
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
public function addWatch( UserIdentity $user, $target, ?string $expiry = null ) { |
| 1002 |
$this->addWatchBatchForUser( $user, [ $target ], $expiry ); |
| 1003 |
|
| 1004 |
if ( $this->expiryEnabled && !$expiry ) { |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
$this->loadWatchedItem( $user, $target ); |
| 1010 |
} else { |
| 1011 |
|
| 1012 |
|
| 1013 |
$expiry = ExpiryDef::normalizeUsingMaxExpiry( $expiry, $this->maxExpiryDuration, TS_ISO_8601 ); |
| 1014 |
$item = new WatchedItem( |
| 1015 |
$user, |
| 1016 |
$target, |
| 1017 |
null, |
| 1018 |
$expiry |
| 1019 |
); |
| 1020 |
$this->cache( $item ); |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
public function addWatchBatchForUser( |
| 1038 |
UserIdentity $user, |
| 1039 |
array $targets, |
| 1040 |
?string $expiry = null |
| 1041 |
): bool { |
| 1042 |
|
| 1043 |
if ( !$user->isRegistered() || $this->readOnlyMode->isReadOnly() ) { |
| 1044 |
return false; |
| 1045 |
} |
| 1046 |
|
| 1047 |
if ( !$targets ) { |
| 1048 |
return true; |
| 1049 |
} |
| 1050 |
$expiry = ExpiryDef::normalizeUsingMaxExpiry( $expiry, $this->maxExpiryDuration, TS_ISO_8601 ); |
| 1051 |
$rows = []; |
| 1052 |
foreach ( $targets as $target ) { |
| 1053 |
$rows[] = [ |
| 1054 |
'wl_user' => $user->getId(), |
| 1055 |
'wl_namespace' => $target->getNamespace(), |
| 1056 |
'wl_title' => $target->getDBkey(), |
| 1057 |
'wl_notificationtimestamp' => null, |
| 1058 |
]; |
| 1059 |
$this->uncache( $user, $target ); |
| 1060 |
} |
| 1061 |
|
| 1062 |
$dbw = $this->lbFactory->getPrimaryDatabase(); |
| 1063 |
$ticket = count( $targets ) > $this->updateRowsPerQuery ? |
| 1064 |
$this->lbFactory->getEmptyTransactionTicket( __METHOD__ ) : null; |
| 1065 |
$affectedRows = 0; |
| 1066 |
$rowBatches = array_chunk( $rows, $this->updateRowsPerQuery ); |
| 1067 |
foreach ( $rowBatches as $toInsert ) { |
| 1068 |
|
| 1069 |
|
| 1070 |
$dbw->newInsertQueryBuilder() |
| 1071 |
->insertInto( 'watchlist' ) |
| 1072 |
->ignore() |
| 1073 |
->rows( $toInsert ) |
| 1074 |
->caller( __METHOD__ )->execute(); |
| 1075 |
$affectedRows += $dbw->affectedRows(); |
| 1076 |
|
| 1077 |
if ( $this->expiryEnabled ) { |
| 1078 |
$affectedRows += $this->updateOrDeleteExpiries( $dbw, $user->getId(), $toInsert, $expiry ); |
| 1079 |
} |
| 1080 |
|
| 1081 |
if ( $ticket ) { |
| 1082 |
$this->lbFactory->commitAndWaitForReplication( __METHOD__, $ticket ); |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
return (bool)$affectedRows; |
| 1087 |
} |
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
private function updateOrDeleteExpiries( |
| 1099 |
IDatabase $dbw, |
| 1100 |
int $userId, |
| 1101 |
array $rows, |
| 1102 |
?string $expiry = null |
| 1103 |
): int { |
| 1104 |
if ( !$expiry ) { |
| 1105 |
|
| 1106 |
return 0; |
| 1107 |
} |
| 1108 |
|
| 1109 |
|
| 1110 |
$conds = []; |
| 1111 |
foreach ( $rows as $row ) { |
| 1112 |
$conds[] = $dbw->makeList( |
| 1113 |
[ |
| 1114 |
'wl_user' => $userId, |
| 1115 |
'wl_namespace' => $row['wl_namespace'], |
| 1116 |
'wl_title' => $row['wl_title'] |
| 1117 |
], |
| 1118 |
$dbw::LIST_AND |
| 1119 |
); |
| 1120 |
} |
| 1121 |
$cond = $dbw->makeList( $conds, $dbw::LIST_OR ); |
| 1122 |
|
| 1123 |
if ( wfIsInfinity( $expiry ) ) { |
| 1124 |
|
| 1125 |
$dbw->deleteJoin( |
| 1126 |
'watchlist_expiry', |
| 1127 |
'watchlist', |
| 1128 |
'we_item', |
| 1129 |
'wl_id', |
| 1130 |
[ $cond ], |
| 1131 |
__METHOD__ |
| 1132 |
); |
| 1133 |
|
| 1134 |
return $dbw->affectedRows(); |
| 1135 |
} |
| 1136 |
|
| 1137 |
return $this->updateExpiries( $dbw, $expiry, $cond ); |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
private function updateExpiries( IDatabase $dbw, string $expiry, string $cond ): int { |
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
$wlIds = $dbw->newSelectQueryBuilder() |
| 1152 |
->select( 'wl_id' ) |
| 1153 |
->from( 'watchlist' ) |
| 1154 |
->where( $cond ) |
| 1155 |
->caller( __METHOD__ ) |
| 1156 |
->fetchFieldValues(); |
| 1157 |
|
| 1158 |
if ( !$wlIds ) { |
| 1159 |
return 0; |
| 1160 |
} |
| 1161 |
|
| 1162 |
$expiry = $dbw->timestamp( $expiry ); |
| 1163 |
$weRows = []; |
| 1164 |
foreach ( $wlIds as $wlId ) { |
| 1165 |
$weRows[] = [ |
| 1166 |
'we_item' => $wlId, |
| 1167 |
'we_expiry' => $expiry |
| 1168 |
]; |
| 1169 |
} |
| 1170 |
|
| 1171 |
|
| 1172 |
$dbw->newInsertQueryBuilder() |
| 1173 |
->insertInto( 'watchlist_expiry' ) |
| 1174 |
->rows( $weRows ) |
| 1175 |
->onDuplicateKeyUpdate() |
| 1176 |
->uniqueIndexFields( [ 'we_item' ] ) |
| 1177 |
->set( [ 'we_expiry' => $expiry ] ) |
| 1178 |
->caller( __METHOD__ )->execute(); |
| 1179 |
|
| 1180 |
return $dbw->affectedRows(); |
| 1181 |
} |
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
public function removeWatch( UserIdentity $user, $target ): bool { |
| 1190 |
return $this->removeWatchBatchForUser( $user, [ $target ] ); |
| 1191 |
} |
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
public function setNotificationTimestampsForUser( |
| 1211 |
UserIdentity $user, |
| 1212 |
$timestamp, |
| 1213 |
array $targets = [] |
| 1214 |
): bool { |
| 1215 |
|
| 1216 |
if ( !$user->isRegistered() || $this->readOnlyMode->isReadOnly() ) { |
| 1217 |
return false; |
| 1218 |
} |
| 1219 |
|
| 1220 |
if ( !$targets ) { |
| 1221 |
|
| 1222 |
$this->resetAllNotificationTimestampsForUser( $user, $timestamp ); |
| 1223 |
return true; |
| 1224 |
} |
| 1225 |
|
| 1226 |
$rows = $this->getTitleDbKeysGroupedByNamespace( $targets ); |
| 1227 |
|
| 1228 |
$dbw = $this->lbFactory->getPrimaryDatabase(); |
| 1229 |
if ( $timestamp !== null ) { |
| 1230 |
$timestamp = $dbw->timestamp( $timestamp ); |
| 1231 |
} |
| 1232 |
$ticket = $this->lbFactory->getEmptyTransactionTicket( __METHOD__ ); |
| 1233 |
$affectedSinceWait = 0; |
| 1234 |
|
| 1235 |
|
| 1236 |
foreach ( $rows as $namespace => $namespaceTitles ) { |
| 1237 |
$rowBatches = array_chunk( $namespaceTitles, $this->updateRowsPerQuery ); |
| 1238 |
foreach ( $rowBatches as $toUpdate ) { |
| 1239 |
|
| 1240 |
$wlIds = $dbw->newSelectQueryBuilder() |
| 1241 |
->select( 'wl_id' ) |
| 1242 |
->from( 'watchlist' ) |
| 1243 |
->where( [ |
| 1244 |
'wl_user' => $user->getId(), |
| 1245 |
'wl_namespace' => $namespace, |
| 1246 |
'wl_title' => $toUpdate |
| 1247 |
] ) |
| 1248 |
->caller( __METHOD__ ) |
| 1249 |
->fetchFieldValues(); |
| 1250 |
if ( $wlIds ) { |
| 1251 |
$wlIds = array_map( 'intval', $wlIds ); |
| 1252 |
$dbw->newUpdateQueryBuilder() |
| 1253 |
->update( 'watchlist' ) |
| 1254 |
->set( [ 'wl_notificationtimestamp' => $timestamp ] ) |
| 1255 |
->where( [ 'wl_id' => $wlIds ] ) |
| 1256 |
->caller( __METHOD__ )->execute(); |
| 1257 |
|
| 1258 |
$affectedSinceWait += $dbw->affectedRows(); |
| 1259 |
|
| 1260 |
if ( $affectedSinceWait >= $this->updateRowsPerQuery ) { |
| 1261 |
$this->lbFactory->commitAndWaitForReplication( __METHOD__, $ticket ); |
| 1262 |
$affectedSinceWait = 0; |
| 1263 |
} |
| 1264 |
} |
| 1265 |
} |
| 1266 |
} |
| 1267 |
|
| 1268 |
$this->uncacheUser( $user ); |
| 1269 |
|
| 1270 |
return true; |
| 1271 |
} |
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
public function getLatestNotificationTimestamp( |
| 1280 |
$timestamp, |
| 1281 |
UserIdentity $user, |
| 1282 |
$target |
| 1283 |
) { |
| 1284 |
$timestamp = wfTimestampOrNull( TS_MW, $timestamp ); |
| 1285 |
if ( $timestamp === null ) { |
| 1286 |
return null; |
| 1287 |
} |
| 1288 |
|
| 1289 |
$seenTimestamps = $this->getPageSeenTimestamps( $user ); |
| 1290 |
if ( $seenTimestamps ) { |
| 1291 |
$seenKey = $this->getPageSeenKey( $target ); |
| 1292 |
if ( isset( $seenTimestamps[$seenKey] ) && $seenTimestamps[$seenKey] >= $timestamp ) { |
| 1293 |
|
| 1294 |
return null; |
| 1295 |
} |
| 1296 |
} |
| 1297 |
|
| 1298 |
return $timestamp; |
| 1299 |
} |
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
public function resetAllNotificationTimestampsForUser( UserIdentity $user, $timestamp = null ) { |
| 1308 |
|
| 1309 |
if ( !$user->isRegistered() ) { |
| 1310 |
return; |
| 1311 |
} |
| 1312 |
|
| 1313 |
|
| 1314 |
$job = new ClearWatchlistNotificationsJob( [ |
| 1315 |
'userId' => $user->getId(), 'timestamp' => $timestamp, 'casTime' => time() |
| 1316 |
] ); |
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
call_user_func( |
| 1321 |
$this->deferredUpdatesAddCallableUpdateCallback, |
| 1322 |
static function () use ( $job ) { |
| 1323 |
$job->run(); |
| 1324 |
} |
| 1325 |
); |
| 1326 |
} |
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
public function updateNotificationTimestamp( |
| 1337 |
UserIdentity $editor, |
| 1338 |
$target, |
| 1339 |
$timestamp |
| 1340 |
): array { |
| 1341 |
$dbw = $this->lbFactory->getPrimaryDatabase(); |
| 1342 |
$queryBuilder = $dbw->newSelectQueryBuilder() |
| 1343 |
->select( [ 'wl_id', 'wl_user' ] ) |
| 1344 |
->from( 'watchlist' ) |
| 1345 |
->where( |
| 1346 |
[ |
| 1347 |
'wl_user != ' . $editor->getId(), |
| 1348 |
'wl_namespace' => $target->getNamespace(), |
| 1349 |
'wl_title' => $target->getDBkey(), |
| 1350 |
'wl_notificationtimestamp' => null, |
| 1351 |
] |
| 1352 |
) |
| 1353 |
->caller( __METHOD__ ); |
| 1354 |
|
| 1355 |
$this->modifyQueryBuilderForExpiry( $queryBuilder, $dbw ); |
| 1356 |
|
| 1357 |
$res = $queryBuilder->fetchResultSet(); |
| 1358 |
$watchers = []; |
| 1359 |
$wlIds = []; |
| 1360 |
foreach ( $res as $row ) { |
| 1361 |
$watchers[] = (int)$row->wl_user; |
| 1362 |
$wlIds[] = (int)$row->wl_id; |
| 1363 |
} |
| 1364 |
|
| 1365 |
if ( $wlIds ) { |
| 1366 |
$fname = __METHOD__; |
| 1367 |
|
| 1368 |
|
| 1369 |
call_user_func( |
| 1370 |
$this->deferredUpdatesAddCallableUpdateCallback, |
| 1371 |
function () use ( $timestamp, $wlIds, $target, $fname ) { |
| 1372 |
$dbw = $this->lbFactory->getPrimaryDatabase(); |
| 1373 |
$ticket = $this->lbFactory->getEmptyTransactionTicket( $fname ); |
| 1374 |
|
| 1375 |
$wlIdsChunks = array_chunk( $wlIds, $this->updateRowsPerQuery ); |
| 1376 |
foreach ( $wlIdsChunks as $wlIdsChunk ) { |
| 1377 |
$dbw->newUpdateQueryBuilder() |
| 1378 |
->update( 'watchlist' ) |
| 1379 |
->set( [ 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp ) ] ) |
| 1380 |
->where( [ 'wl_id' => $wlIdsChunk ] ) |
| 1381 |
->caller( $fname )->execute(); |
| 1382 |
|
| 1383 |
if ( count( $wlIdsChunks ) > 1 ) { |
| 1384 |
$this->lbFactory->commitAndWaitForReplication( $fname, $ticket ); |
| 1385 |
} |
| 1386 |
} |
| 1387 |
$this->uncacheLinkTarget( $target ); |
| 1388 |
}, |
| 1389 |
DeferredUpdates::POSTSEND, |
| 1390 |
$dbw |
| 1391 |
); |
| 1392 |
} |
| 1393 |
|
| 1394 |
return $watchers; |
| 1395 |
} |
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
public function resetNotificationTimestamp( |
| 1406 |
UserIdentity $user, |
| 1407 |
$title, |
| 1408 |
$force = '', |
| 1409 |
$oldid = 0 |
| 1410 |
): bool { |
| 1411 |
$time = time(); |
| 1412 |
|
| 1413 |
|
| 1414 |
if ( !$user->isRegistered() || $this->readOnlyMode->isReadOnly() ) { |
| 1415 |
return false; |
| 1416 |
} |
| 1417 |
|
| 1418 |
$item = null; |
| 1419 |
if ( $force != 'force' ) { |
| 1420 |
$item = $this->getWatchedItem( $user, $title ); |
| 1421 |
if ( !$item || $item->getNotificationTimestamp() === null ) { |
| 1422 |
return false; |
| 1423 |
} |
| 1424 |
} |
| 1425 |
|
| 1426 |
|
| 1427 |
$id = $oldid; |
| 1428 |
$seenTime = null; |
| 1429 |
if ( !$id ) { |
| 1430 |
$latestRev = $this->revisionLookup->getRevisionByTitle( $title ); |
| 1431 |
if ( $latestRev ) { |
| 1432 |
$id = $latestRev->getId(); |
| 1433 |
|
| 1434 |
$seenTime = $latestRev->getTimestamp(); |
| 1435 |
} |
| 1436 |
} |
| 1437 |
if ( $seenTime === null ) { |
| 1438 |
|
| 1439 |
$seenTime = $this->revisionLookup->getTimestampFromId( $id ); |
| 1440 |
} |
| 1441 |
|
| 1442 |
|
| 1443 |
$this->stash->merge( |
| 1444 |
$this->getPageSeenTimestampsKey( $user ), |
| 1445 |
function ( $cache, $key, $current ) use ( $title, $seenTime ) { |
| 1446 |
if ( !$current ) { |
| 1447 |
$value = new MapCacheLRU( 300 ); |
| 1448 |
} elseif ( is_array( $current ) ) { |
| 1449 |
$value = MapCacheLRU::newFromArray( $current, 300 ); |
| 1450 |
} else { |
| 1451 |
|
| 1452 |
$value = $current; |
| 1453 |
} |
| 1454 |
$subKey = $this->getPageSeenKey( $title ); |
| 1455 |
|
| 1456 |
if ( $seenTime > $value->get( $subKey ) ) { |
| 1457 |
|
| 1458 |
$value->set( $subKey, $seenTime ); |
| 1459 |
|
| 1460 |
$this->latestUpdateCache->set( $key, $value->toArray(), BagOStuff::TTL_PROC_LONG ); |
| 1461 |
} elseif ( $seenTime === false ) { |
| 1462 |
|
| 1463 |
$value->set( $subKey, wfTimestamp( TS_MW ) ); |
| 1464 |
$this->latestUpdateCache->set( $key, |
| 1465 |
$value->toArray(), |
| 1466 |
BagOStuff::TTL_PROC_LONG ); |
| 1467 |
} else { |
| 1468 |
return false; |
| 1469 |
} |
| 1470 |
|
| 1471 |
return $value->toArray(); |
| 1472 |
}, |
| 1473 |
BagOStuff::TTL_HOUR |
| 1474 |
); |
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
$job = new ActivityUpdateJob( |
| 1479 |
$title, |
| 1480 |
[ |
| 1481 |
'type' => 'updateWatchlistNotification', |
| 1482 |
'userid' => $user->getId(), |
| 1483 |
'notifTime' => $this->getNotificationTimestamp( $user, $title, $item, $force, $oldid ), |
| 1484 |
'curTime' => $time |
| 1485 |
] |
| 1486 |
); |
| 1487 |
|
| 1488 |
$this->queueGroup->lazyPush( $job ); |
| 1489 |
|
| 1490 |
$this->uncache( $user, $title ); |
| 1491 |
|
| 1492 |
return true; |
| 1493 |
} |
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
private function getPageSeenTimestamps( UserIdentity $user ) { |
| 1500 |
$key = $this->getPageSeenTimestampsKey( $user ); |
| 1501 |
|
| 1502 |
$cache = $this->latestUpdateCache->getWithSetCallback( |
| 1503 |
$key, |
| 1504 |
BagOStuff::TTL_PROC_LONG, |
| 1505 |
function () use ( $key ) { |
| 1506 |
return $this->stash->get( $key ) ?: null; |
| 1507 |
} |
| 1508 |
); |
| 1509 |
|
| 1510 |
if ( $cache instanceof MapCacheLRU ) { |
| 1511 |
$cache = $cache->toArray(); |
| 1512 |
} |
| 1513 |
return $cache; |
| 1514 |
} |
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
private function getPageSeenTimestampsKey( UserIdentity $user ): string { |
| 1521 |
return $this->stash->makeGlobalKey( |
| 1522 |
'watchlist-recent-updates', |
| 1523 |
$this->lbFactory->getLocalDomainID(), |
| 1524 |
$user->getId() |
| 1525 |
); |
| 1526 |
} |
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
private function getPageSeenKey( $target ): string { |
| 1533 |
return "{$target->getNamespace()}:{$target->getDBkey()}"; |
| 1534 |
} |
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
private function getNotificationTimestamp( |
| 1545 |
UserIdentity $user, |
| 1546 |
$title, |
| 1547 |
$item, |
| 1548 |
$force, |
| 1549 |
$oldid |
| 1550 |
) { |
| 1551 |
if ( !$oldid ) { |
| 1552 |
|
| 1553 |
return null; |
| 1554 |
} |
| 1555 |
|
| 1556 |
$oldRev = $this->revisionLookup->getRevisionById( $oldid ); |
| 1557 |
if ( !$oldRev ) { |
| 1558 |
|
| 1559 |
return false; |
| 1560 |
} |
| 1561 |
|
| 1562 |
$nextRev = $this->revisionLookup->getNextRevision( $oldRev ); |
| 1563 |
if ( !$nextRev ) { |
| 1564 |
|
| 1565 |
return null; |
| 1566 |
} |
| 1567 |
|
| 1568 |
$item ??= $this->loadWatchedItem( $user, $title ); |
| 1569 |
if ( !$item ) { |
| 1570 |
|
| 1571 |
return null; |
| 1572 |
} |
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
$notificationTimestamp = $this->revisionLookup->getTimestampFromId( $oldid ); |
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
$ts = new MWTimestamp( $notificationTimestamp ); |
| 1583 |
$ts->timestamp->add( new DateInterval( 'PT1S' ) ); |
| 1584 |
$notificationTimestamp = $ts->getTimestamp( TS_MW ); |
| 1585 |
|
| 1586 |
if ( $notificationTimestamp < $item->getNotificationTimestamp() ) { |
| 1587 |
if ( $force != 'force' ) { |
| 1588 |
return false; |
| 1589 |
} else { |
| 1590 |
… |
| 1591 |
return $item->getNotificationTimestamp(); |
| 1592 |
} |
| 1593 |
} |
| 1594 |
|
| 1595 |
return $notificationTimestamp; |
| 1596 |
} |
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
public function countUnreadNotifications( UserIdentity $user, $unreadLimit = null ) { |
| 1605 |
$queryBuilder = $this->lbFactory->getReplicaDatabase()->newSelectQueryBuilder() |
| 1606 |
->select( '1' ) |
| 1607 |
->from( 'watchlist' ) |
| 1608 |
->where( [ |
| 1609 |
'wl_user' => $user->getId(), |
| 1610 |
'wl_notificationtimestamp IS NOT NULL' |
| 1611 |
] ) |
| 1612 |
->caller( __METHOD__ ); |
| 1613 |
if ( $unreadLimit !== null ) { |
| 1614 |
$unreadLimit = (int)$unreadLimit; |
| 1615 |
$queryBuilder->limit( $unreadLimit ); |
| 1616 |
} |
| 1617 |
|
| 1618 |
$rowCount = $queryBuilder->fetchRowCount(); |
| 1619 |
|
| 1620 |
if ( $unreadLimit === null ) { |
| 1621 |
return $rowCount; |
| 1622 |
} |
| 1623 |
|
| 1624 |
if ( $rowCount >= $unreadLimit ) { |
| 1625 |
return true; |
| 1626 |
} |
| 1627 |
|
| 1628 |
return $rowCount; |
| 1629 |
} |
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
public function duplicateAllAssociatedEntries( $oldTarget, $newTarget ) { |
| 1637 |
|
| 1638 |
|
| 1639 |
$this->duplicateEntry( |
| 1640 |
new TitleValue( $this->nsInfo->getSubject( $oldTarget->getNamespace() ), $oldTarget->getDBkey() ), |
| 1641 |
new TitleValue( $this->nsInfo->getSubject( $newTarget->getNamespace() ), $newTarget->getDBkey() ) |
| 1642 |
); |
| 1643 |
$this->duplicateEntry( |
| 1644 |
new TitleValue( $this->nsInfo->getTalk( $oldTarget->getNamespace() ), $oldTarget->getDBkey() ), |
| 1645 |
new TitleValue( $this->nsInfo->getTalk( $newTarget->getNamespace() ), $newTarget->getDBkey() ) |
| 1646 |
); |
| 1647 |
} |
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
public function duplicateEntry( $oldTarget, $newTarget ) { |
| 1655 |
$dbw = $this->lbFactory->getPrimaryDatabase(); |
| 1656 |
$result = $this->fetchWatchedItemsForPage( $dbw, $oldTarget ); |
| 1657 |
$newNamespace = $newTarget->getNamespace(); |
| 1658 |
$newDBkey = $newTarget->getDBkey(); |
| 1659 |
|
| 1660 |
|
| 1661 |
$values = []; |
| 1662 |
$expiries = []; |
| 1663 |
foreach ( $result as $row ) { |
| 1664 |
$values[] = [ |
| 1665 |
'wl_user' => $row->wl_user, |
| 1666 |
'wl_namespace' => $newNamespace, |
| 1667 |
'wl_title' => $newDBkey, |
| 1668 |
'wl_notificationtimestamp' => $row->wl_notificationtimestamp, |
| 1669 |
]; |
| 1670 |
|
| 1671 |
if ( $this->expiryEnabled && $row->we_expiry ) { |
| 1672 |
$expiries[$row->wl_user] = $row->we_expiry; |
| 1673 |
} |
| 1674 |
} |
| 1675 |
|
| 1676 |
if ( !$values ) { |
| 1677 |
return; |
| 1678 |
} |
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
$dbw->newReplaceQueryBuilder() |
| 1684 |
->replaceInto( 'watchlist' ) |
| 1685 |
->uniqueIndexFields( [ 'wl_user', 'wl_namespace', 'wl_title' ] ) |
| 1686 |
->rows( $values ) |
| 1687 |
->caller( __METHOD__ )->execute(); |
| 1688 |
|
| 1689 |
if ( $this->expiryEnabled ) { |
| 1690 |
$this->updateExpiriesAfterMove( $dbw, $expiries, $newNamespace, $newDBkey ); |
| 1691 |
} |
| 1692 |
} |
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
private function fetchWatchedItemsForPage( |
| 1700 |
IReadableDatabase $dbr, |
| 1701 |
$target |
| 1702 |
): IResultWrapper { |
| 1703 |
$queryBuilder = $dbr->newSelectQueryBuilder() |
| 1704 |
->select( [ 'wl_user', 'wl_notificationtimestamp' ] ) |
| 1705 |
->from( 'watchlist' ) |
| 1706 |
->where( [ |
| 1707 |
'wl_namespace' => $target->getNamespace(), |
| 1708 |
'wl_title' => $target->getDBkey(), |
| 1709 |
] ) |
| 1710 |
->caller( __METHOD__ ) |
| 1711 |
->forUpdate(); |
| 1712 |
|
| 1713 |
if ( $this->expiryEnabled ) { |
| 1714 |
$queryBuilder->leftJoin( 'watchlist_expiry', null, [ 'wl_id = we_item' ] ) |
| 1715 |
->field( 'we_expiry' ); |
| 1716 |
} |
| 1717 |
|
| 1718 |
return $queryBuilder->fetchResultSet(); |
| 1719 |
} |
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
private function updateExpiriesAfterMove( |
| 1728 |
IDatabase $dbw, |
| 1729 |
array $expiries, |
| 1730 |
int $namespace, |
| 1731 |
string $dbKey |
| 1732 |
): void { |
| 1733 |
DeferredUpdates::addCallableUpdate( |
| 1734 |
function ( $fname ) use ( $dbw, $expiries, $namespace, $dbKey ) { |
| 1735 |
|
| 1736 |
$res = $dbw->newSelectQueryBuilder() |
| 1737 |
->select( [ 'wl_user', 'wl_id' ] ) |
| 1738 |
->from( 'watchlist' ) |
| 1739 |
->where( [ |
| 1740 |
'wl_namespace' => $namespace, |
| 1741 |
'wl_title' => $dbKey, |
| 1742 |
] ) |
| 1743 |
->caller( $fname ) |
| 1744 |
->fetchResultSet(); |
| 1745 |
|
| 1746 |
|
| 1747 |
$expiryData = []; |
| 1748 |
foreach ( $res as $row ) { |
| 1749 |
if ( !empty( $expiries[$row->wl_user] ) ) { |
| 1750 |
$expiryData[] = [ |
| 1751 |
'we_item' => $row->wl_id, |
| 1752 |
'we_expiry' => $expiries[$row->wl_user], |
| 1753 |
]; |
| 1754 |
} |
| 1755 |
} |
| 1756 |
|
| 1757 |
|
| 1758 |
$batches = array_chunk( $expiryData, $this->updateRowsPerQuery ); |
| 1759 |
foreach ( $batches as $toInsert ) { |
| 1760 |
$dbw->newReplaceQueryBuilder() |
| 1761 |
->replaceInto( 'watchlist_expiry' ) |
| 1762 |
->uniqueIndexFields( [ 'we_item' ] ) |
| 1763 |
->rows( $toInsert ) |
| 1764 |
->caller( $fname ) |
| 1765 |
->execute(); |
| 1766 |
} |
| 1767 |
}, |
| 1768 |
DeferredUpdates::POSTSEND, |
| 1769 |
$dbw |
| 1770 |
); |
| 1771 |
} |
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
private function getTitleDbKeysGroupedByNamespace( array $titles ) { |
| 1778 |
$rows = []; |
| 1779 |
foreach ( $titles as $title ) { |
| 1780 |
|
| 1781 |
$rows[ $title->getNamespace() ][] = $title->getDBkey(); |
| 1782 |
} |
| 1783 |
return $rows; |
| 1784 |
} |
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
private function uncacheTitlesForUser( UserIdentity $user, array $titles ) { |
| 1791 |
foreach ( $titles as $title ) { |
| 1792 |
$this->uncache( $user, $title ); |
| 1793 |
} |
| 1794 |
} |
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
public function countExpired(): int { |
| 1800 |
$dbr = $this->lbFactory->getReplicaDatabase(); |
| 1801 |
return $dbr->newSelectQueryBuilder() |
| 1802 |
->select( '*' ) |
| 1803 |
->from( 'watchlist_expiry' ) |
| 1804 |
->where( $dbr->expr( 'we_expiry', '<=', $dbr->timestamp() ) ) |
| 1805 |
->caller( __METHOD__ ) |
| 1806 |
->fetchRowCount(); |
| 1807 |
} |
| 1808 |
|
| 1809 |
|
| 1810 |
|
| 1811 |
|
| 1812 |
public function removeExpired( int $limit, bool $deleteOrphans = false ): void { |
| 1813 |
$dbr = $this->lbFactory->getReplicaDatabase(); |
| 1814 |
$dbw = $this->lbFactory->getPrimaryDatabase(); |
| 1815 |
$ticket = $this->lbFactory->getEmptyTransactionTicket( __METHOD__ ); |
| 1816 |
|
| 1817 |
|
| 1818 |
$toDelete = $dbr->newSelectQueryBuilder() |
| 1819 |
->select( 'we_item' ) |
| 1820 |
->from( 'watchlist_expiry' ) |
| 1821 |
->where( $dbr->expr( 'we_expiry', '<=', $dbr->timestamp() ) ) |
| 1822 |
->limit( $limit ) |
| 1823 |
->caller( __METHOD__ ) |
| 1824 |
->fetchFieldValues(); |
| 1825 |
|
| 1826 |
if ( count( $toDelete ) > 0 ) { |
| 1827 |
|
| 1828 |
$dbw->newDeleteQueryBuilder() |
| 1829 |
->deleteFrom( 'watchlist' ) |
| 1830 |
->where( [ 'wl_id' => $toDelete ] ) |
| 1831 |
->caller( __METHOD__ )->execute(); |
| 1832 |
$dbw->newDeleteQueryBuilder() |
| 1833 |
->deleteFrom( 'watchlist_expiry' ) |
| 1834 |
->where( [ 'we_item' => $toDelete ] ) |
| 1835 |
->caller( __METHOD__ )->execute(); |
| 1836 |
} |
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
if ( $deleteOrphans ) { |
| 1841 |
$expiryToDelete = $dbr->newSelectQueryBuilder() |
| 1842 |
->select( 'we_item' ) |
| 1843 |
->from( 'watchlist_expiry' ) |
| 1844 |
->leftJoin( 'watchlist', null, 'wl_id = we_item' ) |
| 1845 |
->where( $dbr->makeList( |
| 1846 |
[ 'wl_id' => null, 'we_expiry' => null ], |
| 1847 |
$dbr::LIST_OR |
| 1848 |
) ) |
| 1849 |
->caller( __METHOD__ ) |
| 1850 |
->fetchFieldValues(); |
| 1851 |
if ( count( $expiryToDelete ) > 0 ) { |
| 1852 |
$dbw->newDeleteQueryBuilder() |
| 1853 |
->deleteFrom( 'watchlist_expiry' ) |
| 1854 |
->where( [ 'we_item' => $expiryToDelete ] ) |
| 1855 |
->caller( __METHOD__ )->execute(); |
| 1856 |
} |
| 1857 |
} |
| 1858 |
|
| 1859 |
$this->lbFactory->commitAndWaitForReplication( __METHOD__, $ticket ); |
| 1860 |
} |
| 1861 |
} |
| 1862 |
|
| 1863 |
class_alias( WatchedItemStore::class, 'WatchedItemStore' ); |