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 );
45 $dbr = $loadBalancer->getConnectionRef(
DB_REPLICA );
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->newDeleteQueryBuilder()
81 ->deleteFrom(
'watchlist' )
82 ->where( [
'wl_id' => $watchlistIds ] )
83 ->caller( __METHOD__ )->execute();
84 if ( MediaWikiServices::getInstance()->getMainConfig()->
get( MainConfigNames::WatchlistExpiry ) ) {
85 $dbw->newDeleteQueryBuilder()
86 ->deleteFrom(
'watchlist_expiry' )
87 ->where( [
'we_item' => $watchlistIds ] )
88 ->caller( __METHOD__ )->execute();
92 $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
93 $lbf->commitPrimaryChanges( __METHOD__ );
96 if ( count( $watchlistIds ) === (
int)$batchSize ) {
99 MediaWikiServices::getInstance()->getJobQueueGroup()->push(
new self( $this->
getParams() ) );
106 $info = parent::getDeduplicationInfo();
108 unset( $info[
'namespace'] );
109 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.