Merge "Add support for PHP7 random_bytes in favor of mcrypt_create_iv"
[lhc/web/wiklou.git] / includes / jobqueue / jobs / RefreshLinksJob.php
index a337da4..f9284a5 100644 (file)
@@ -29,7 +29,7 @@ use MediaWiki\MediaWikiServices;
  *   - a) Recursive jobs to update links for backlink pages for a given title.
  *        These jobs have (recursive:true,table:<table>) set.
  *   - b) Jobs to update links for a set of pages (the job title is ignored).
- *           These jobs have (pages:(<page ID>:(<namespace>,<title>),...) set.
+ *        These jobs have (pages:(<page ID>:(<namespace>,<title>),...) set.
  *   - c) Jobs to update links for a single page (the job title)
  *        These jobs need no extra fields set.
  *
@@ -88,7 +88,8 @@ class RefreshLinksJob extends Job {
                        // enqueued will be reflected in backlink page parses when the leaf jobs run.
                        if ( !isset( $params['range'] ) ) {
                                try {
-                                       wfGetLBFactory()->waitForReplication( [
+                                       $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+                                       $lbFactory->waitForReplication( [
                                                'wiki'    => wfWikiID(),
                                                'timeout' => self::LAG_WAIT_TIMEOUT
                                        ] );
@@ -128,13 +129,18 @@ class RefreshLinksJob extends Job {
         * @return bool
         */
        protected function runForTitle( Title $title ) {
-               $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
+               $services = MediaWikiServices::getInstance();
+               $stats = $services->getStatsdDataFactory();
+               $lbFactory = $services->getDBLoadBalancerFactory();
+               $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
 
                $page = WikiPage::factory( $title );
                $page->loadPageData( WikiPage::READ_LATEST );
 
                // Serialize links updates by page ID so they see each others' changes
-               $scopedLock = LinksUpdate::acquirePageLock( wfGetDB( DB_MASTER ), $page->getId(), 'job' );
+               $dbw = $lbFactory->getMainLB()->getConnection( DB_MASTER );
+               /** @noinspection PhpUnusedLocalVariableInspection */
+               $scopedLock = LinksUpdate::acquirePageLock( $dbw, $page->getId(), 'job' );
                // Get the latest ID *after* acquirePageLock() flushed the transaction.
                // This is used to detect edits/moves after loadPageData() but before the scope lock.
                // The works around the chicken/egg problem of determining the scope lock key.
@@ -241,10 +247,12 @@ class RefreshLinksJob extends Job {
                        $parserOutput
                );
 
-               $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
-               $ticket = $factory->getEmptyTransactionTicket( __METHOD__ );
+               // For legacy hook handlers doing updates via LinksUpdateConstructed, make sure
+               // any pending writes they made get flushed before the doUpdate() calls below.
+               // This avoids snapshot-clearing errors in LinksUpdate::acquirePageLock().
+               $lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
+
                foreach ( $updates as $key => $update ) {
-                       $update->setTransactionTicket( $ticket );
                        // FIXME: This code probably shouldn't be here?
                        // Needed by things like Echo notifications which need
                        // to know which user caused the links update
@@ -264,6 +272,7 @@ class RefreshLinksJob extends Job {
                }
 
                foreach ( $updates as $update ) {
+                       $update->setTransactionTicket( $ticket );
                        $update->doUpdate();
                }