Merge "add IGNORE INDEX option to mysql database handler"
[lhc/web/wiklou.git] / includes / jobqueue / jobs / CategoryMembershipChangeJob.php
index 5dc2acb..1828dd7 100644 (file)
@@ -19,6 +19,7 @@
  *
  * @file
  */
+use MediaWiki\MediaWikiServices;
 
 /**
  * Job to add recent change entries mentioning category membership changes
@@ -48,7 +49,8 @@ class CategoryMembershipChangeJob extends Job {
                        return false; // deleted?
                }
 
-               $dbw = wfGetDB( DB_MASTER );
+               $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
+               $dbw = $lb->getConnection( DB_MASTER );
                // Use a named lock so that jobs for this page see each others' changes
                $lockKey = "CategoryMembershipUpdates:{$page->getId()}";
                $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__, 10 );
@@ -57,14 +59,14 @@ class CategoryMembershipChangeJob extends Job {
                        return false;
                }
 
-               $dbr = wfGetDB( DB_SLAVE, array( 'recentchanges' ) );
-               // Wait till the slave is caught up so that jobs for this page see each others' changes
-               if ( !wfGetLB()->safeWaitForMasterPos( $dbr ) ) {
-                       $this->setLastError( "Timed out while waiting for slave to catch up" );
+               $dbr = wfGetDB( DB_REPLICA, [ 'recentchanges' ] );
+               // Wait till the replica DB is caught up so that jobs for this page see each others' changes
+               if ( !$lb->safeWaitForMasterPos( $dbr ) ) {
+                       $this->setLastError( "Timed out while waiting for replica DB to catch up" );
                        return false;
                }
                // Clear any stale REPEATABLE-READ snapshot
-               $dbr->commit( __METHOD__, 'flush' );
+               $dbr->flushSnapshot( __METHOD__ );
 
                $cutoffUnix = wfTimestamp( TS_UNIX, $this->params['revTimestamp'] );
                // Using ENQUEUE_FUDGE_SEC handles jobs inserted out of revision order due to the delay
@@ -73,26 +75,26 @@ class CategoryMembershipChangeJob extends Job {
 
                // Get the newest revision that has a SRC_CATEGORIZE row...
                $row = $dbr->selectRow(
-                       array( 'revision', 'recentchanges' ),
-                       array( 'rev_timestamp', 'rev_id' ),
-                       array(
+                       [ 'revision', 'recentchanges' ],
+                       [ 'rev_timestamp', 'rev_id' ],
+                       [
                                'rev_page' => $page->getId(),
                                'rev_timestamp >= ' . $dbr->addQuotes( $dbr->timestamp( $cutoffUnix ) )
-                       ),
+                       ],
                        __METHOD__,
-                       array( 'ORDER BY' => 'rev_timestamp DESC, rev_id DESC' ),
-                       array(
-                               'recentchanges' => array(
+                       [ 'ORDER BY' => 'rev_timestamp DESC, rev_id DESC' ],
+                       [
+                               'recentchanges' => [
                                        'INNER JOIN',
-                                       array(
+                                       [
                                                'rc_this_oldid = rev_id',
                                                'rc_source' => RecentChange::SRC_CATEGORIZE,
                                                // Allow rc_cur_id or rc_timestamp index usage
                                                'rc_cur_id = rev_page',
                                                'rc_timestamp >= rev_timestamp'
-                                       )
-                               )
-                       )
+                                       ]
+                               ]
+                       ]
                );
                // Only consider revisions newer than any such revision
                if ( $row ) {
@@ -108,29 +110,33 @@ class CategoryMembershipChangeJob extends Job {
                $res = $dbr->select(
                        'revision',
                        Revision::selectFields(),
-                       array(
+                       [
                                'rev_page' => $page->getId(),
                                "rev_timestamp > $encCutoff" .
                                        " OR (rev_timestamp = $encCutoff AND rev_id > $lastRevId)"
-                       ),
+                       ],
                        __METHOD__,
-                       array( 'ORDER BY' => 'rev_timestamp ASC, rev_id ASC' )
+                       [ 'ORDER BY' => 'rev_timestamp ASC, rev_id ASC' ]
                );
 
                // Apply all category updates in revision timestamp order
+               $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
                foreach ( $res as $row ) {
-                       $this->notifyUpdatesForRevision( $page, Revision::newFromRow( $row ) );
+                       $this->notifyUpdatesForRevision( $lbFactory, $page, Revision::newFromRow( $row ) );
                }
 
                return true;
        }
 
        /**
+        * @param LBFactory $lbFactory
         * @param WikiPage $page
         * @param Revision $newRev
         * @throws MWException
         */
-       protected function notifyUpdatesForRevision( WikiPage $page, Revision $newRev ) {
+       protected function notifyUpdatesForRevision(
+               LBFactory $lbFactory, WikiPage $page, Revision $newRev
+       ) {
                $config = RequestContext::getMain()->getConfig();
                $title = $page->getTitle();
 
@@ -156,7 +162,8 @@ class CategoryMembershipChangeJob extends Job {
                        return; // nothing to do
                }
 
-               $dbw = wfGetDB( DB_MASTER );
+               $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
+
                $catMembChange = new CategoryMembershipChange( $title, $newRev );
                $catMembChange->checkTemplateLinks();
 
@@ -167,8 +174,7 @@ class CategoryMembershipChangeJob extends Job {
                        $categoryTitle = Title::makeTitle( NS_CATEGORY, $categoryName );
                        $catMembChange->triggerCategoryAddedNotification( $categoryTitle );
                        if ( $insertCount++ && ( $insertCount % $batchSize ) == 0 ) {
-                               $dbw->commit( __METHOD__, 'flush' );
-                               wfGetLBFactory()->waitForReplication();
+                               $lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
                        }
                }
 
@@ -176,8 +182,7 @@ class CategoryMembershipChangeJob extends Job {
                        $categoryTitle = Title::makeTitle( NS_CATEGORY, $categoryName );
                        $catMembChange->triggerCategoryRemovedNotification( $categoryTitle );
                        if ( $insertCount++ && ( $insertCount++ % $batchSize ) == 0 ) {
-                               $dbw->commit( __METHOD__, 'flush' );
-                               wfGetLBFactory()->waitForReplication();
+                               $lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
                        }
                }
        }
@@ -194,14 +199,14 @@ class CategoryMembershipChangeJob extends Job {
                // up to date, neither of which are true.
                $oldCategories = $oldRev
                        ? $this->getCategoriesAtRev( $title, $oldRev, $parseTimestamp )
-                       : array();
+                       : [];
                // Parse the new revision and get the categories
                $newCategories = $this->getCategoriesAtRev( $title, $newRev, $parseTimestamp );
 
                $categoryInserts = array_values( array_diff( $newCategories, $oldCategories ) );
                $categoryDeletes = array_values( array_diff( $oldCategories, $newCategories ) );
 
-               return array( $categoryInserts, $categoryDeletes );
+               return [ $categoryInserts, $categoryDeletes ];
        }
 
        /**