21 parent::__construct(
'clearUserWatchlist',
$params );
23 $this->removeDuplicates =
true;
33 return new self( [
'userId' => $user->
getId(),
'maxWatchlistId' => $maxWatchlistId ] );
36 public function run() {
37 $updateRowsPerQuery = MediaWikiServices::getInstance()->getMainConfig()->get(
38 MainConfigNames::UpdateRowsPerQuery );
39 $userId = $this->params[
'userId'];
40 $maxWatchlistId = $this->params[
'maxWatchlistId'];
41 $batchSize = $updateRowsPerQuery;
43 $loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer();
44 $dbw = $loadBalancer->getConnectionRef(
DB_PRIMARY );
48 if ( !$loadBalancer->waitForPrimaryPos(
$dbr ) ) {
49 $this->
setLastError(
'Timed out waiting for replica to catch up before lock' );
54 $lockKey =
"{{$dbw->getDomainID()}}:ClearUserWatchlist:$userId";
55 $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__, 10 );
57 $this->
setLastError(
"Could not acquire lock '$lockKey'" );
61 if ( !$loadBalancer->waitForPrimaryPos(
$dbr ) ) {
62 $this->
setLastError(
'Timed out waiting for replica to catch up within lock' );
67 $dbr->flushSnapshot( __METHOD__ );
69 $watchlistIds =
$dbr->selectFieldValues(
74 'wl_id <= ' . $maxWatchlistId
78 'LIMIT' => $batchSize,
82 if ( count( $watchlistIds ) == 0 ) {
86 $dbw->delete(
'watchlist', [
'wl_id' => $watchlistIds ], __METHOD__ );
87 if ( MediaWikiServices::getInstance()->getMainConfig()->
get(
88 MainConfigNames::WatchlistExpiry ) ) {
89 $dbw->delete(
'watchlist_expiry', [
'we_item' => $watchlistIds ], __METHOD__ );
93 $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
94 $lbf->commitPrimaryChanges( __METHOD__ );
97 if ( count( $watchlistIds ) === (
int)$batchSize ) {
100 MediaWikiServices::getInstance()->getJobQueueGroup()->push(
new self( $this->
getParams() ) );
107 $info = parent::getDeduplicationInfo();
109 unset( $info[
'namespace'] );
110 unset( $info[
'title'] );
Job to clear a users watchlist in batches.
static newForUser(UserIdentity $user, $maxWatchlistId)
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
__construct(array $params)
Class to both describe a background job and handle jobs.
getParams()
array Parameters that specify sources, targets, and options for execution
array $params
Array of job parameters.
A class containing constants representing the names of configuration variables.
Interface for generic jobs only uses the parameters field and are JSON serializable.