Redo RefreshLinksJob2 commit with trivial notice fix.
authorAaron Schulz <aaron@users.mediawiki.org>
Fri, 12 Sep 2008 09:37:31 +0000 (09:37 +0000)
committerAaron Schulz <aaron@users.mediawiki.org>
Fri, 12 Sep 2008 09:37:31 +0000 (09:37 +0000)
includes/AutoLoader.php
includes/DefaultSettings.php
includes/LinksUpdate.php
includes/RefreshLinksJob.php

index 30ce952..19d349e 100644 (file)
@@ -146,6 +146,7 @@ $wgAutoloadLocalClasses = array(
        'RdfMetaData' => 'includes/Metadata.php',
        'RecentChange' => 'includes/RecentChange.php',
        'RefreshLinksJob' => 'includes/RefreshLinksJob.php',
+       'RefreshLinksJob2' => 'includes/RefreshLinksJob.php',
        'RegexlikeReplacer' => 'includes/StringUtils.php',
        'ReplacementArray' => 'includes/StringUtils.php',
        'Replacer' => 'includes/StringUtils.php',
index eda1a6f..d149d9f 100644 (file)
@@ -1621,6 +1621,7 @@ $wgAllowSlowParserFunctions = false;
  */
 $wgJobClasses = array(
        'refreshLinks' => 'RefreshLinksJob',
+       'refreshLinks2' => 'RefreshLinksJob2',
        'htmlCacheUpdate' => 'HTMLCacheUpdateJob',
        'html_cache_update' => 'HTMLCacheUpdateJob', // backwards-compatible
        'sendMail' => 'EmaillingJob',
index 696ca8e..8b03947 100644 (file)
@@ -193,34 +193,49 @@ class LinksUpdate {
        }
 
        function queueRecursiveJobs() {
+               global $wgUpdateRowsPerJob;
                wfProfileIn( __METHOD__ );
 
-               $batchSize = 100;
                $dbr = wfGetDB( DB_SLAVE );
-               $res = $dbr->select( array( 'templatelinks', 'page' ),
-                       array( 'page_namespace', 'page_title' ),
-                       array(
-                               'page_id=tl_from',
+               $res = $dbr->select( 'templatelinks',
+                       array( 'tl_from' ),
+                       array( 
                                '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;
+               $numRows = $res->numRows();
+               if( !$numRows ) {
+                       return; // nothing to do
+               }
+               $numBatches = ceil( $numRows / $wgUpdateRowsPerJob );
+               $realBatchSize = $numRows / $numBatches;
+               $start = false;
+               $jobs = array();
+               do {
+                       for( $i = 0; $i <= $realBatchSize - 1; $i++ ) {
+                               $row = $res->fetchRow();
+                               if( $row ) {
+                                       $id = $row[0];
+                               } else {
+                                       $id = false;
                                        break;
                                }
-                               $title = Title::makeTitle( $row->page_namespace, $row->page_title );
-                               $jobs[] = new RefreshLinksJob( $title, '' );
                        }
-                       Job::batchInsert( $jobs );
-               }
+                       $params = array(
+                               'start' => $start,
+                               'end' => ( $id !== false ? $id - 1 : false ),
+                       );
+                       $jobs[] = new RefreshLinksJob2( $this->mTitle, $params );
+
+                       $start = $id;
+               } while ( $start );
+
                $dbr->freeResult( $res );
+
+               Job::batchInsert( $jobs );
+
                wfProfileOut( __METHOD__ );
        }
 
index f95e5a5..1c119a8 100644 (file)
@@ -47,3 +47,87 @@ class RefreshLinksJob extends Job {
                return true;
        }
 }
+
+/**
+ * Background job to update links for a given title.
+ * Newer version for high use templates.
+ *
+ * @ingroup JobQueue
+ */
+class RefreshLinksJob2 extends Job {
+
+       function __construct( $title, $params, $id = 0 ) {
+               parent::__construct( 'refreshLinks2', $title, $params, $id );
+       }
+
+       /**
+        * Run a refreshLinks2 job
+        * @return boolean success
+        */
+       function run() {
+               global $wgParser;
+
+               wfProfileIn( __METHOD__ );
+
+               $linkCache = LinkCache::singleton();
+               $linkCache->clear();
+
+               if( is_null( $this->title ) ) {
+                       $this->error = "refreshLinks2: Invalid title";
+                       wfProfileOut( __METHOD__ );
+                       return false;
+               }
+               if( !isset($this->params['start']) || !isset($this->params['end']) ) {
+                       $this->error = "refreshLinks2: Invalid params";
+                       wfProfileOut( __METHOD__ );
+                       return false;
+               }
+               $start = intval($this->params['start']);
+               $end = intval($this->params['end']);
+               
+               $dbr = wfGetDB( DB_SLAVE );
+               $res = $dbr->select( array( 'templatelinks', 'page' ),
+                       array( 'page_namespace', 'page_title' ),
+                       array(
+                               'page_id=tl_from',
+                               "tl_from >= '$start'",
+                               "tl_from <= '$end'", 
+                               'tl_namespace' => $this->title->getNamespace(),
+                               'tl_title' => $this->title->getDBkey()
+                       ), __METHOD__
+               );
+               
+               # Not suitable for page load triggered job running!
+               # Gracefully switch to refreshLinks jobs if this happens.
+               if( php_sapi_name() != 'cli' ) {
+                       $jobs = array();
+                       while( $row = $dbr->fetchObject( $res ) ) {
+                               $title = Title::makeTitle( $row->page_namespace, $row->page_title );
+                               $jobs[] = new RefreshLinksJob( $title, '' );
+                       }
+                       Job::batchInsert( $jobs );
+                       return true;
+               }
+               # Re-parse each page that transcludes this page and update their tracking links...
+               while( $row = $dbr->fetchObject( $res ) ) {
+                       $title = Title::makeTitle( $row->page_namespace, $row->page_title );
+                       $revision = Revision::newFromTitle( $title );
+                       if ( !$revision ) {
+                               $this->error = 'refreshLinks: Article not found "' . $title->getPrefixedDBkey() . '"';
+                               wfProfileOut( __METHOD__ );
+                               return false;
+                       }
+                       wfProfileIn( __METHOD__.'-parse' );
+                       $options = new ParserOptions;
+                       $parserOutput = $wgParser->parse( $revision->getText(), $title, $options, true, true, $revision->getId() );
+                       wfProfileOut( __METHOD__.'-parse' );
+                       wfProfileIn( __METHOD__.'-update' );
+                       $update = new LinksUpdate( $title, $parserOutput, false );
+                       $update->doUpdate();
+                       wfProfileOut( __METHOD__.'-update' );
+                       wfProfileOut( __METHOD__ );
+               }
+
+               return true;
+       }
+}