Woops, fix bug with last commit when the number of rows <= 2.
[lhc/web/wiklou.git] / includes / LinksUpdate.php
index bd09502..9bcd9d6 100644 (file)
@@ -1,12 +1,8 @@
 <?php
 /**
- * See deferred.txt
- * @package MediaWiki
- */
-
-/**
- * @todo document
- * @package MediaWiki
+ * See docs/deferred.txt
+ * 
+ * @todo document (e.g. one-sentence top-level class description).
  */
 class LinksUpdate {
 
@@ -28,10 +24,10 @@ class LinksUpdate {
 
        /**
         * Constructor
-        * Initialize private variables
-        * @param $title Integer: FIXME
-        * @param $parserOutput FIXME
-        * @param $recursive Boolean: FIXME, default 'true'.
+        *
+        * @param Title $title Title of the page we're updating
+        * @param ParserOutput $parserOutput Output from a full parse of this page
+        * @param bool $recursive Queue jobs for recursive updates?
         */
        function LinksUpdate( $title, $parserOutput, $recursive = true ) {
                global $wgAntiLockFlags;
@@ -41,10 +37,10 @@ class LinksUpdate {
                } else {
                        $this->mOptions = array( 'FOR UPDATE' );
                }
-               $this->mDb =& wfGetDB( DB_MASTER );
+               $this->mDb = wfGetDB( DB_MASTER );
 
                if ( !is_object( $title ) ) {
-                       wfDebugDieBacktrace( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
+                       throw new MWException( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
                                "Please see Article::editUpdates() for an invocation example.\n" );
                }
                $this->mTitle = $title;
@@ -68,6 +64,8 @@ class LinksUpdate {
                }
 
                $this->mRecursive = $recursive;
+               
+               wfRunHooks( 'LinksUpdateConstructed', array( &$this ) );
        }
 
        /**
@@ -85,7 +83,7 @@ class LinksUpdate {
        function doIncrementalUpdate() {
                $fname = 'LinksUpdate::doIncrementalUpdate';
                wfProfileIn( $fname );
-
+               
                # Page links
                $existing = $this->getExistingLinks();
                $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
@@ -115,15 +113,6 @@ class LinksUpdate {
                $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
                        $this->getTemplateInsertions( $existing ) );
 
-               # Refresh links of all pages including this page
-               if ( $this->mRecursive ) {
-                       $tlto = $this->mTitle->getTemplateLinksTo();
-                       if ( count( $tlto ) ) {
-                               require_once( 'JobQueue.php' );
-                               Job::queueLinksJobs( $tlto );
-                       }
-               }
-
                # Category links
                $existing = $this->getExistingCategories();
                $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
@@ -133,6 +122,12 @@ class LinksUpdate {
                $categoryUpdates = array_diff_assoc( $existing, $this->mCategories ) + array_diff_assoc( $this->mCategories, $existing );
                $this->invalidateCategories( $categoryUpdates );
 
+               # Refresh links of all pages including this page
+               # This will be in a separate transaction
+               if ( $this->mRecursive ) {
+                       $this->queueRecursiveJobs();
+               }
+               
                wfProfileOut( $fname );
        }
 
@@ -151,15 +146,6 @@ class LinksUpdate {
                $existing = $this->getExistingImages();
                $imageUpdates = array_diff_key( $existing, $this->mImages ) + array_diff_key( $this->mImages, $existing );
 
-               # Refresh links of all pages including this page
-               if ( $this->mRecursive ) {
-                       $tlto = $this->mTitle->getTemplateLinksTo();
-                       if ( count( $tlto ) ) {
-                               require_once( 'JobQueue.php' );
-                               Job::queueLinksJobs( $tlto );
-                       }
-               }
-
                $this->dumbTableUpdate( 'pagelinks',     $this->getLinkInsertions(),     'pl_from' );
                $this->dumbTableUpdate( 'imagelinks',    $this->getImageInsertions(),    'il_from' );
                $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
@@ -171,8 +157,46 @@ class LinksUpdate {
                $this->invalidateCategories( $categoryUpdates );
                $this->invalidateImageDescriptions( $imageUpdates );
 
+               # Refresh links of all pages including this page
+               # This will be in a separate transaction
+               if ( $this->mRecursive ) {
+                       $this->queueRecursiveJobs();
+               }
+
                wfProfileOut( $fname );
        }
+
+       function queueRecursiveJobs() {
+               wfProfileIn( __METHOD__ );
+               
+               $batchSize = 100;
+               $dbr = wfGetDB( DB_SLAVE );
+               $res = $dbr->select( array( 'templatelinks', 'page' ), 
+                       array( 'page_namespace', 'page_title' ),
+                       array( 
+                               'page_id=tl_from', 
+                               'tl_namespace' => $this->mTitle->getNamespace(),
+                               'tl_title' => $this->mTitle->getDBkey()
+                       ), __METHOD__
+               );
+
+               $done = false;
+               while ( !$done ) {
+                       $jobs = array();
+                       for ( $i = 0; $i < $batchSize; $i++ ) {
+                               $row = $dbr->fetchObject( $res );
+                               if ( !$row ) {
+                                       $done = true;
+                                       break;
+                               }
+                               $title = Title::makeTitle( $row->page_namespace, $row->page_title );
+                               $jobs[] = new RefreshLinksJob( $title, '' );
+                       }
+                       Job::batchInsert( $jobs );
+               }
+               $dbr->freeResult( $res );
+               wfProfileOut( __METHOD__ );
+       }
        
        /**
         * Invalidate the cache of a list of pages from a single namespace
@@ -572,4 +596,4 @@ class LinksUpdate {
                return $arr;
        }
 }
-?>
+