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->newSelectQueryBuilder()
72 ->where( [
'wl_user' => $userId ] )
73 ->andWhere(
$dbr->buildComparison(
'<=', [
'wl_id' => $maxWatchlistId ] ) )
75 ->caller( __METHOD__ )->fetchFieldValues();
76 if ( count( $watchlistIds ) == 0 ) {
80 $dbw->delete(
'watchlist', [
'wl_id' => $watchlistIds ], __METHOD__ );
81 if ( MediaWikiServices::getInstance()->getMainConfig()->
get(
82 MainConfigNames::WatchlistExpiry ) ) {
83 $dbw->delete(
'watchlist_expiry', [
'we_item' => $watchlistIds ], __METHOD__ );
87 $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
88 $lbf->commitPrimaryChanges( __METHOD__ );
91 if ( count( $watchlistIds ) === (
int)$batchSize ) {
94 MediaWikiServices::getInstance()->getJobQueueGroup()->push(
new self( $this->
getParams() ) );
101 $info = parent::getDeduplicationInfo();
103 unset( $info[
'namespace'] );
104 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.