]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blobdiff - includes/HTMLCacheUpdate.php
MediaWiki 1.15.4-scripts
[autoinstalls/mediawiki.git] / includes / HTMLCacheUpdate.php
index 260439b23344376c79e9a8a5ed9ee7af7801d398..bd63c072ded9fa7293b955577d0a0cd8bc198285 100644 (file)
@@ -5,21 +5,23 @@
  * Small numbers of links will be done immediately, large numbers are pushed onto
  * the job queue.
  *
- * This class is designed to work efficiently with small numbers of links, and 
+ * This class is designed to work efficiently with small numbers of links, and
  * to work reasonably well with up to ~10^5 links. Above ~10^6 links, the memory
  * and time requirements of loading all backlinked IDs in doUpdate() might become
  * prohibitive. The requirements measured at Wikimedia are approximately:
- * 
+ *
  *   memory: 48 bytes per row
  *   time: 16us per row for the query plus processing
  *
  * The reason this query is done is to support partitioning of the job
- * by backlinked ID. The memory issue could be allieviated by doing this query in 
+ * by backlinked ID. The memory issue could be allieviated by doing this query in
  * batches, but of course LIMIT with an offset is inefficient on the DB side.
  *
- * The class is nevertheless a vast improvement on the previous method of using 
+ * The class is nevertheless a vast improvement on the previous method of using
  * Image::getLinksTo() and Title::touchArray(), which uses about 2KB of memory per
  * link.
+ *
+ * @ingroup Cache
  */
 class HTMLCacheUpdate
 {
@@ -33,159 +35,86 @@ class HTMLCacheUpdate
                $this->mTable = $table;
                $this->mRowsPerJob = $wgUpdateRowsPerJob;
                $this->mRowsPerQuery = $wgUpdateRowsPerQuery;
+               $this->mCache = $this->mTitle->getBacklinkCache();
        }
 
-       function doUpdate() {
+       public function doUpdate() {
                # Fetch the IDs
-               $cond = $this->getToCondition();
-               $dbr = wfGetDB( DB_SLAVE );
-               $res = $dbr->select( $this->mTable, $this->getFromField(), $cond, __METHOD__ );
-               $resWrap = new ResultWrapper( $dbr, $res );
-               if ( $dbr->numRows( $res ) != 0 ) {
-                       if ( $dbr->numRows( $res ) > $this->mRowsPerJob ) {
-                               $this->insertJobs( $resWrap );
+               $numRows = $this->mCache->getNumLinks( $this->mTable );
+
+               if ( $numRows != 0 ) {
+                       if ( $numRows > $this->mRowsPerJob ) {
+                               $this->insertJobs();
                        } else {
-                               $this->invalidateIDs( $resWrap );
+                               $this->invalidate();
                        }
                }
-               $dbr->freeResult( $res );
+               wfRunHooks( 'HTMLCacheUpdate::doUpdate', array($this->mTitle) );
        }
 
-       function insertJobs( ResultWrapper $res ) {
-               $numRows = $res->numRows();
-               $numBatches = ceil( $numRows / $this->mRowsPerJob );
-               $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;
-                               }
-                       }
-                       
+       protected function insertJobs() {
+               $batches = $this->mCache->partition( $this->mTable, $this->mRowsPerJob );
+               if ( !$batches ) {
+                       return;
+               }
+               foreach ( $batches as $batch ) {
                        $params = array(
                                'table' => $this->mTable,
-                               'start' => $start,
-                               'end' => ( $id !== false ? $id - 1 : false ),
+                               'start' => $batch[0],
+                               'end' => $batch[1],
                        );
                        $jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
-
-                       $start = $id;
-               } while ( $start );
-
+               }
                Job::batchInsert( $jobs );
        }
 
-       function getPrefix() {
-               static $prefixes = array(
-                       'pagelinks' => 'pl',
-                       'imagelinks' => 'il',
-                       'categorylinks' => 'cl',
-                       'templatelinks' => 'tl',
-                       
-                       # Not needed
-                       # 'externallinks' => 'el',
-                       # 'langlinks' => 'll'
-               );
-
-               if ( is_null( $this->mPrefix ) ) {
-                       $this->mPrefix = $prefixes[$this->mTable];
-                       if ( is_null( $this->mPrefix ) ) {
-                               throw new MWException( "Invalid table type \"{$this->mTable}\" in " . __CLASS__ );
-                       }
-               }
-               return $this->mPrefix;
-       }
-       
-       function getFromField() {
-               return $this->getPrefix() . '_from';
-       }
-
-       function getToCondition() {
-               switch ( $this->mTable ) {
-                       case 'pagelinks':
-                               return array( 
-                                       'pl_namespace' => $this->mTitle->getNamespace(),
-                                       'pl_title' => $this->mTitle->getDBkey()
-                               );
-                       case 'templatelinks':
-                               return array(
-                                       'tl_namespace' => $this->mTitle->getNamespace(),
-                                       'tl_title' => $this->mTitle->getDBkey()
-                               );
-                       case 'imagelinks':
-                               return array( 'il_to' => $this->mTitle->getDBkey() );
-                       case 'categorylinks':
-                               return array( 'cl_to' => $this->mTitle->getDBkey() );
-               }
-               throw new MWException( 'Invalid table type in ' . __CLASS__ );
-       }
 
        /**
-        * Invalidate a set of IDs, right now
+        * Invalidate a set of pages, right now
         */
-       function invalidateIDs( ResultWrapper $res ) {
+       public function invalidate( $startId = false, $endId = false ) {
                global $wgUseFileCache, $wgUseSquid;
 
-               if ( $res->numRows() == 0 ) {
+               $titleArray = $this->mCache->getLinks( $this->mTable, $startId, $endId );
+               if ( $titleArray->count() == 0 ) {
                        return;
                }
 
                $dbw = wfGetDB( DB_MASTER );
                $timestamp = $dbw->timestamp();
-               $done = false;
-               
-               while ( !$done ) {
-                       # Get all IDs in this query into an array
-                       $ids = array();
-                       for ( $i = 0; $i < $this->mRowsPerQuery; $i++ ) {
-                               $row = $res->fetchRow();
-                               if ( $row ) {
-                                       $ids[] = $row[0];
-                               } else {
-                                       $done = true;
-                                       break;
-                               }
-                       }
 
-                       if ( !count( $ids ) ) {
-                               break;
-                       }
-                       
-                       # Update page_touched
-                       $dbw->update( 'page', 
-                               array( 'page_touched' => $timestamp ), 
-                               array( 'page_id IN (' . $dbw->makeList( $ids ) . ')' ),
-                               __METHOD__
-                       );
+               # Get all IDs in this query into an array
+               $ids = array();
+               foreach ( $titleArray as $title ) {
+                       $ids[] = $title->getArticleID();
+               }
+               # Update page_touched
+               $dbw->update( 'page',
+                       array( 'page_touched' => $timestamp ),
+                       array( 'page_id IN (' . $dbw->makeList( $ids ) . ')' ),
+                       __METHOD__
+               );
+
+               # Update squid
+               if ( $wgUseSquid ) {
+                       $u = SquidUpdate::newFromTitles( $titleArray );
+                       $u->doUpdate();
+               }
 
-                       # Update squid
-                       if ( $wgUseSquid || $wgUseFileCache ) {
-                               $titles = Title::newFromIDs( $ids );
-                               if ( $wgUseSquid ) {
-                                       $u = SquidUpdate::newFromTitles( $titles );
-                                       $u->doUpdate();
-                               }
-
-                               # Update file cache
-                               if  ( $wgUseFileCache ) {
-                                       foreach ( $titles as $title ) {
-                                               $cm = new HTMLFileCache($title);
-                                               @unlink($cm->fileCacheName());
-                                       }
-                               }
+               # Update file cache
+               if  ( $wgUseFileCache ) {
+                       foreach ( $titleArray as $title ) {
+                               HTMLFileCache::clearFileCache( $title );
                        }
                }
        }
 }
 
 /**
- * @todo document (e.g. one-sentence top-level class description).
+ * Job wrapper for HTMLCacheUpdate. Gets run whenever a related
+ * job gets called from the queue.
+ * 
+ * @ingroup JobQueue
  */
 class HTMLCacheUpdateJob extends Job {
        var $table, $start, $end;
@@ -203,24 +132,9 @@ class HTMLCacheUpdateJob extends Job {
                $this->end = $params['end'];
        }
 
-       function run() {
+       public function run() {
                $update = new HTMLCacheUpdate( $this->title, $this->table );
-
-               $fromField = $update->getFromField();
-               $conds = $update->getToCondition();
-               if ( $this->start ) {
-                       $conds[] = "$fromField >= {$this->start}";
-               }
-               if ( $this->end ) {
-                       $conds[] = "$fromField <= {$this->end}";
-               }
-
-               $dbr = wfGetDB( DB_SLAVE );
-               $res = $dbr->select( $this->table, $fromField, $conds, __METHOD__ );
-               $update->invalidateIDs( new ResultWrapper( $dbr, $res ) );
-               $dbr->freeResult( $res );
-
+               $update->invalidate( $this->start, $this->end );
                return true;
        }
 }
-