follow-up to r74742: fix typos
[lhc/web/wiklou.git] / maintenance / storage / compressOld.inc
index 3c42684..69964ff 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 );
 
@@ -62,7 +56,8 @@ function compressPage( $row, $extdb ) {
                        'old_text' => $compress
                ), array( /* WHERE */
                        'old_id' => $row->old_id
-               ), $fname, 'LIMIT 1'
+               ), $fname,
+               array( 'LIMIT' => 1 )
        );
        return true;
 }
@@ -71,14 +66,14 @@ define( 'LS_INDIVIDUAL', 0 );
 define( 'LS_CHUNKED', 1 );
 
 /** @todo document */
-function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorThreshold, $beginDate,
+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 != '' ) {
@@ -109,12 +104,21 @@ function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorTh
        # 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%'");
+               '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 ) {
@@ -133,9 +137,6 @@ function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorTh
        #$tables[] = 'page';
        #$conds[] = 'page_id=rev_page AND rev_id != page_latest';
 
-       $oldReadsSinceLastSlaveWait = 0;        #check slave lag periodically
-       $totalMatchingRevisions = 0;
-       $masterPos = false;
        for ( $pageId = $startId; $pageId <= $maxPageId; $pageId++ ) {
                wfWaitForSlaves( 5 );
 
@@ -155,31 +156,20 @@ function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorTh
                $titleObj = Title::makeTitle( $pageRow->page_namespace, $pageRow->page_title );
                print "$pageId\t" . $titleObj->getPrefixedDBkey() . " ";
 
-               print_r(
-                       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
-               );
-               exit;
-
                # Load revisions
                $revRes = $dbw->select( $tables, $fields,
-                       array( 
+                       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,
+                       ), $conds ),
                        $fname,
                        $revLoadOptions
                );
                $revs = array();
-               while ( $revRow = $dbw->fetchObject( $revRes ) ) {
+               foreach ( $revRes as $revRow ) {
                        $revs[] = $revRow;
                }
 
@@ -205,7 +195,7 @@ function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorTh
                        $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
@@ -236,7 +226,7 @@ function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorTh
                                                $stub = false;
                                                print 'x';
                                        } else {
-                                               $stub = $chunk->addItem( $text );
+                                               $stub = new HistoryBlobStub( $chunk->addItem( $text ) );
                                                $stub->setLocation( $primaryOldid );
                                                $stub->setReferrer( $oldid );
                                                print '.';
@@ -284,8 +274,8 @@ function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorTh
 
                                        # Store the stub objects
                                        for ( $j = 1; $j < $thisChunkSize; $j++ ) {
-                                               # Skip if not compressing
-                                               if ( $stubs[$j] !== false ) {
+                                               # 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]),
@@ -308,4 +298,3 @@ function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorTh
        }
        return true;
 }
-?>