Merge "Fix order of @var parameter in PHP"
[lhc/web/wiklou.git] / includes / jobqueue / jobs / ClearUserWatchlistJob.php
index 17c4b66..01fa46c 100644 (file)
@@ -10,7 +10,17 @@ use MediaWiki\MediaWikiServices;
  * @ingroup JobQueue
  * @since 1.31
  */
-class ClearUserWatchlistJob extends Job {
+class ClearUserWatchlistJob extends Job implements GenericParameterJob {
+       /**
+        * @param array $params
+        *  - userId,         The ID for the user whose watchlist is being cleared.
+        *  - maxWatchlistId, The maximum wl_id at the time the job was first created,
+        */
+       public function __construct( array $params ) {
+               parent::__construct( 'clearUserWatchlist', $params );
+
+               $this->removeDuplicates = true;
+       }
 
        /**
         * @param User $user User to clear the watchlist for.
@@ -19,36 +29,14 @@ class ClearUserWatchlistJob extends Job {
         * @return ClearUserWatchlistJob
         */
        public static function newForUser( User $user, $maxWatchlistId ) {
-               return new self(
-                       null,
-                       [ 'userId' => $user->getId(), 'maxWatchlistId' => $maxWatchlistId ]
-               );
-       }
-
-       /**
-        * @param Title|null $title Not used by this job.
-        * @param array $params
-        *  - batchSize,      Number of watchlist entries to remove at once.
-        *  - userId,         The ID for the user whose watchlist is being cleared.
-        *  - maxWatchlistId, The maximum wl_id at the time the job was first created,
-        */
-       public function __construct( Title $title = null, array $params ) {
-               if ( !array_key_exists( 'batchSize', $params ) ) {
-                       $params['batchSize'] = 1000;
-               }
-
-               parent::__construct(
-                       'clearUserWatchlist',
-                       SpecialPage::getTitleFor( 'EditWatchlist', 'clear' ),
-                       $params
-               );
-
-               $this->removeDuplicates = true;
+               return new self( [ 'userId' => $user->getId(), 'maxWatchlistId' => $maxWatchlistId ] );
        }
 
        public function run() {
+               global $wgUpdateRowsPerQuery;
                $userId = $this->params['userId'];
                $maxWatchlistId = $this->params['maxWatchlistId'];
+               $batchSize = $wgUpdateRowsPerQuery;
 
                $loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer();
                $dbw = $loadBalancer->getConnection( DB_MASTER );
@@ -56,7 +44,7 @@ class ClearUserWatchlistJob extends Job {
 
                // Wait before lock to try to reduce time waiting in the lock.
                if ( !$loadBalancer->safeWaitForMasterPos( $dbr ) ) {
-                       $this->setLastError( 'Timed out while waiting for slave to catch up before lock' );
+                       $this->setLastError( 'Timed out waiting for replica to catch up before lock' );
                        return false;
                }
 
@@ -69,7 +57,7 @@ class ClearUserWatchlistJob extends Job {
                }
 
                if ( !$loadBalancer->safeWaitForMasterPos( $dbr ) ) {
-                       $this->setLastError( 'Timed out while waiting for slave to catch up within lock' );
+                       $this->setLastError( 'Timed out waiting for replica to catch up within lock' );
                        return false;
                }
 
@@ -86,7 +74,7 @@ class ClearUserWatchlistJob extends Job {
                        __METHOD__,
                        [
                                'ORDER BY' => 'wl_id ASC',
-                               'LIMIT' => $this->params['batchSize'],
+                               'LIMIT' => $batchSize,
                        ]
                );
 
@@ -101,8 +89,10 @@ class ClearUserWatchlistJob extends Job {
                $lbf->commitMasterChanges( __METHOD__ );
                unset( $scopedLock );
 
-               if ( count( $watchlistIds ) == $this->params['batchSize'] ) {
-                       JobQueueGroup::singleton()->push( new self( $this->getTitle(), $this->getParams() ) );
+               if ( count( $watchlistIds ) === (int)$batchSize ) {
+                       // Until we get less results than the limit, recursively push
+                       // the same job again.
+                       JobQueueGroup::singleton()->push( new self( $this->getParams() ) );
                }
 
                return true;