]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - maintenance/storage/compressOld.inc
MediaWiki 1.17.0
[autoinstallsdev/mediawiki.git] / maintenance / storage / compressOld.inc
index 344a490f2875f28ea81ee58c3a79a65a516b1bf7..93be5f757900d178d7ecf86992be1fb6278f8b1d 100644 (file)
@@ -1,34 +1,28 @@
 <?php
 /**
- * @package MediaWiki
- * @subpackage Maintenance
+ * @file
+ * @ingroup Maintenance ExternalStorage
  */
 
-/** */
-require_once( 'Revision.php' );
-require_once( 'ExternalStoreDB.php' );
-
 /** @todo document */
 function compressOldPages( $start = 0, $extdb = '' ) {
        $fname = 'compressOldPages';
 
        $chunksize = 50;
        print "Starting from old_id $start...\n";
-       $dbw =& wfGetDB( DB_MASTER );
+       $dbw = wfGetDB( DB_MASTER );
        do {
-               $end = $start + $chunksize;
-               $res = $dbw->select( 'text', array( 'old_id','old_flags','old_namespace','old_title','old_text' ),
+               $res = $dbw->select( 'text', array( 'old_id','old_flags','old_text' ),
                        "old_id>=$start", $fname, array( 'ORDER BY' => 'old_id', 'LIMIT' => $chunksize, 'FOR UPDATE' ) );
                if( $dbw->numRows( $res ) == 0 ) {
                        break;
                }
                $last = $start;
-               while( $row = $dbw->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        # print "  {$row->old_id} - {$row->old_namespace}:{$row->old_title}\n";
                        compressPage( $row, $extdb );
                        $last = $row->old_id;
                }
-               $dbw->freeResult( $res );
                $start = $last + 1; # Deletion may leave long empty stretches
                print "$start...\n";
        } while( true );
@@ -41,7 +35,7 @@ function compressPage( $row, $extdb ) {
                #print "Already compressed row {$row->old_id}\n";
                return false;
        }
-       $dbw =& wfGetDB( DB_MASTER );
+       $dbw = wfGetDB( DB_MASTER );
        $flags = $row->old_flags ? "{$row->old_flags},gzip" : "gzip";
        $compress = gzdeflate( $row->old_text );
 
@@ -56,13 +50,14 @@ function compressPage( $row, $extdb ) {
        }
 
        # Update text row
-       $dbw->update( 'text', 
+       $dbw->update( 'text',
                array( /* SET */
                        'old_flags' => $flags,
                        'old_text' => $compress
                ), array( /* WHERE */
                        'old_id' => $row->old_id
-               ), $fname, 'LIMIT 1'
+               ), $fname,
+               array( 'LIMIT' => 1 )
        );
        return true;
 }
@@ -71,16 +66,25 @@ define( 'LS_INDIVIDUAL', 0 );
 define( 'LS_CHUNKED', 1 );
 
 /** @todo document */
-function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorThreshold, $beginDate, $endDate )
+function compressWithConcat( $startId, $maxChunkSize, $beginDate,
+       $endDate, $extdb="", $maxPageId = false )
 {
        $fname = 'compressWithConcat';
        $loadStyle = LS_CHUNKED;
-       
-       $dbr =& wfGetDB( DB_SLAVE );
-       $dbw =& wfGetDB( DB_MASTER );
+
+       $dbr = wfGetDB( DB_SLAVE );
+       $dbw = wfGetDB( DB_MASTER );
+
+       # Set up external storage
+       if ( $extdb != '' ) {
+               $storeObj = new ExternalStoreDB;
+       }
 
        # Get all articles by page_id
-       $maxPageId = $dbr->selectField( 'page', 'max(page_id)', '', $fname );
+       if ( !$maxPageId ) {
+               $maxPageId = $dbr->selectField( 'page', 'max(page_id)', '', $fname );
+       }
+       print "Starting from $startId of $maxPageId\n";
        $pageConds = array();
 
        /*
@@ -89,20 +93,32 @@ function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorTh
                $pageConds[] = 'page_namespace<>0';
        }
        if ( $queryExtra ) {
-                $pageConds[] = $queryExtra;
+                               $pageConds[] = $queryExtra;
        }
         */
 
        # For each article, get a list of revisions which fit the criteria
+
        # No recompression, use a condition on old_flags
+       # Don't compress object type entities, because that might produce data loss when
+       # overwriting bulk storage concat rows. Don't compress external references, because
+       # the script doesn't yet delete rows from external storage.
        $conds = array(
-               "old_flags NOT LIKE '%object%' " .
-               " AND (old_flags NOT LIKE '%external%' OR old_text NOT LIKE 'DB://%/%/%')");
-       
+               'old_flags NOT ' . $dbr->buildLike( $dbr->anyString(), 'object', $dbr->anyString() ) . ' AND old_flags NOT '
+                       . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() ) );
+
        if ( $beginDate ) {
+               if ( !preg_match( '/^\d{14}$/', $beginDate ) ) {
+                       print "Invalid begin date \"$beginDate\"\n";
+                       return false;
+               }
                $conds[] = "rev_timestamp>'" . $beginDate . "'";
-       } 
+       }
        if ( $endDate )  {
+               if ( !preg_match( '/^\d{14}$/', $endDate ) ) {
+                       print "Invalid end date \"$endDate\"\n";
+                       return false;
+               }
                $conds[] = "rev_timestamp<'" . $endDate . "'";
        }
        if ( $loadStyle == LS_CHUNKED ) {
@@ -116,11 +132,20 @@ function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorTh
                $revLoadOptions = array();
        }
 
-       $oldReadsSinceLastSlaveWait = 0;        #check slave lag periodically
-       $totalMatchingRevisions = 0;
-       $masterPos = false;
+       # Don't work with current revisions
+       # Don't lock the page table for update either -- TS 2006-04-04
+       #$tables[] = 'page';
+       #$conds[] = 'page_id=rev_page AND rev_id != page_latest';
+
        for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
-               $pageRes = $dbr->select( 'page', array('page_id', 'page_namespace', 'page_title'), 
+               wfWaitForSlaves( 5 );
+
+               # Wake up
+               $dbr->ping();
+
+               # Get the page row
+               $pageRes = $dbr->select( 'page',
+                       array('page_id', 'page_namespace', 'page_title','page_latest'),
                        $pageConds + array('page_id' => $pageId), $fname );
                if ( $dbr->numRows( $pageRes ) == 0 ) {
                        continue;
@@ -133,15 +158,21 @@ function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorTh
 
                # Load revisions
                $revRes = $dbw->select( $tables, $fields,
-                       array( 'rev_page' => $pageRow->page_id ) + $conds, 
+                       array_merge( array(
+                               'rev_page' => $pageRow->page_id,
+                               # Don't operate on the current revision
+                               # Use < instead of <> in case the current revision has changed
+                               # since the page select, which wasn't locking
+                               'rev_id < ' . $pageRow->page_latest
+                       ), $conds ),
                        $fname,
                        $revLoadOptions
                );
                $revs = array();
-               while ( $revRow = $dbw->fetchObject( $revRes ) ) {
+               foreach ( $revRes as $revRow ) {
                        $revs[] = $revRow;
                }
-               
+
                if ( count( $revs ) < 2) {
                        # No revisions matching, no further processing
                        print "\n";
@@ -162,14 +193,14 @@ function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorTh
                        $dbw->begin();
                        $usedChunk = false;
                        $primaryOldid = $revs[$i]->rev_text_id;
-                       
+
                        # Get the text of each revision and add it to the object
-                       for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy( $maxChunkFactor, $factorThreshold ); $j++ ) {
+                       for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy(); $j++ ) {
                                $oldid = $revs[$i + $j]->rev_text_id;
-                               
+
                                # Get text
                                if ( $loadStyle == LS_INDIVIDUAL ) {
-                                       $textRow = $dbw->selectRow( 'text', 
+                                       $textRow = $dbw->selectRow( 'text',
                                                array( 'old_flags', 'old_text' ),
                                                array( 'old_id' => $oldid ),
                                                $fname,
@@ -185,7 +216,7 @@ function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorTh
                                        #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
                                }
 
-                               if ( $j == 0 ) {
+                               if ( $extdb == "" && $j == 0 ) {
                                        $chunk->setText( $text );
                                        print '.';
                                } else {
@@ -195,10 +226,9 @@ function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorTh
                                                $stub = false;
                                                print 'x';
                                        } else {
-                                               $stub = $chunk->addItem( $text );
+                                               $stub = new HistoryBlobStub( $chunk->addItem( $text ) );
                                                $stub->setLocation( $primaryOldid );
-                                               $hash = $stub->getHash();
-                                               $stub = serialize( $stub );
+                                               $stub->setReferrer( $oldid );
                                                print '.';
                                                $usedChunk = true;
                                        }
@@ -206,41 +236,65 @@ function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorTh
                                }
                        }
                        $thisChunkSize = $j;
-                       
+
                        # If we couldn't actually use any stubs because the pages were too small, do nothing
                        if ( $usedChunk ) {
-                               # Store the main object
-                               $dbw->update( 'text',
-                                       array( /* SET */
-                                               'old_text' => serialize( $chunk ),
-                                               'old_flags' => 'object,utf-8',
-                                       ), array( /* WHERE */
-                                               'old_id' => $primaryOldid
-                                       )
-                               );
-
-                               # Store the stub objects
-                               for ( $j = 1; $j < $thisChunkSize; $j++ ) {
-                                       # Skip if not compressing
-                                       if ( $stubs[$j] !== false ) {
+                               if ( $extdb != "" ) {
+                                       # Move blob objects to External Storage
+                                       $stored = $storeObj->store( $extdb, serialize( $chunk ));
+                                       if ($stored === false) {
+                                               print "Unable to store object\n";
+                                               return false;
+                                       }
+                                       # Store External Storage URLs instead of Stub placeholders
+                                       foreach ($stubs as $stub) {
+                                               if ($stub===false)
+                                                       continue;
+                                               # $stored should provide base path to a BLOB
+                                               $url = $stored."/".$stub->getHash();
                                                $dbw->update( 'text',
                                                        array( /* SET */
-                                                               'old_text' => $stubs[$j],
-                                                               'old_flags' => 'object,utf-8',
-                                                       ), array( /* WHERE */
-                                                               'old_id' => $revs[$i + $j]->rev_text_id
+                                                               'old_text' => $url,
+                                                               'old_flags' => 'external,utf-8',
+                                                       ), array ( /* WHERE */
+                                                               'old_id' => $stub->getReferrer(),
                                                        )
                                                );
                                        }
+                               } else {
+                                       # Store the main object locally
+                                       $dbw->update( 'text',
+                                               array( /* SET */
+                                                       'old_text' => serialize( $chunk ),
+                                                       'old_flags' => 'object,utf-8',
+                                               ), array( /* WHERE */
+                                                       'old_id' => $primaryOldid
+                                               )
+                                       );
+
+                                       # Store the stub objects
+                                       for ( $j = 1; $j < $thisChunkSize; $j++ ) {
+                                               # Skip if not compressing and don't overwrite the first revision
+                                               if ( $stubs[$j] !== false && $revs[$i + $j]->rev_text_id != $primaryOldid ) {
+                                                       $dbw->update( 'text',
+                                                               array( /* SET */
+                                                                       'old_text' => serialize($stubs[$j]),
+                                                                       'old_flags' => 'object,utf-8',
+                                                               ), array( /* WHERE */
+                                                                       'old_id' => $revs[$i + $j]->rev_text_id
+                                                               )
+                                                       );
+                                               }
+                                       }
                                }
                        }
                        # Done, next
                        print "/";
                        $dbw->commit();
                        $i += $thisChunkSize;
+                       wfWaitForSlaves( 5 );
                }
                print "\n";
        }
        return true;
 }
-?>