MediaWiki  REL1_22
moveToExternal.php
Go to the documentation of this file.
00001 <?php
00024 define( 'REPORTING_INTERVAL', 1 );
00025 
00026 if ( !defined( 'MEDIAWIKI' ) ) {
00027     require_once __DIR__ . '/../commandLine.inc';
00028     require_once __DIR__ . '/../../includes/externalstore/ExternalStoreDB.php';
00029     require_once 'resolveStubs.php';
00030 
00031     $fname = 'moveToExternal';
00032 
00033     if ( !isset( $args[0] ) ) {
00034         print "Usage: php moveToExternal.php [-s <startid>] [-e <endid>] <cluster>\n";
00035         exit;
00036     }
00037 
00038     $cluster = $args[0];
00039     $dbw = wfGetDB( DB_MASTER );
00040 
00041     if ( isset( $options['e'] ) ) {
00042         $maxID = $options['e'];
00043     } else {
00044         $maxID = $dbw->selectField( 'text', 'MAX(old_id)', false, $fname );
00045     }
00046     $minID = isset( $options['s'] ) ? $options['s'] : 1;
00047 
00048     moveToExternal( $cluster, $maxID, $minID );
00049 }
00050 
00051 function moveToExternal( $cluster, $maxID, $minID = 1 ) {
00052     $fname = 'moveToExternal';
00053     $dbw = wfGetDB( DB_MASTER );
00054     $dbr = wfGetDB( DB_SLAVE );
00055 
00056     $count = $maxID - $minID + 1;
00057     $blockSize = 1000;
00058     $numBlocks = ceil( $count / $blockSize );
00059     print "Moving text rows from $minID to $maxID to external storage\n";
00060     $ext = new ExternalStoreDB;
00061     $numMoved = 0;
00062 
00063     for ( $block = 0; $block < $numBlocks; $block++ ) {
00064         $blockStart = $block * $blockSize + $minID;
00065         $blockEnd = $blockStart + $blockSize - 1;
00066 
00067         if ( !( $block % REPORTING_INTERVAL ) ) {
00068             print "oldid=$blockStart, moved=$numMoved\n";
00069             wfWaitForSlaves();
00070         }
00071 
00072         $res = $dbr->select( 'text', array( 'old_id', 'old_flags', 'old_text' ),
00073             array(
00074                 "old_id BETWEEN $blockStart AND $blockEnd",
00075                 'old_flags NOT ' . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() ),
00076             ), $fname );
00077         foreach ( $res as $row ) {
00078             # Resolve stubs
00079             $text = $row->old_text;
00080             $id = $row->old_id;
00081             if ( $row->old_flags === '' ) {
00082                 $flags = 'external';
00083             } else {
00084                 $flags = "{$row->old_flags},external";
00085             }
00086 
00087             if ( strpos( $flags, 'object' ) !== false ) {
00088                 $obj = unserialize( $text );
00089                 $className = strtolower( get_class( $obj ) );
00090                 if ( $className == 'historyblobstub' ) {
00091                     # resolveStub( $id, $row->old_text, $row->old_flags );
00092                     # $numStubs++;
00093                     continue;
00094                 } elseif ( $className == 'historyblobcurstub' ) {
00095                     $text = gzdeflate( $obj->getText() );
00096                     $flags = 'utf-8,gzip,external';
00097                 } elseif ( $className == 'concatenatedgziphistoryblob' ) {
00098                     // Do nothing
00099                 } else {
00100                     print "Warning: unrecognised object class \"$className\"\n";
00101                     continue;
00102                 }
00103             } else {
00104                 $className = false;
00105             }
00106 
00107             if ( strlen( $text ) < 100 && $className === false ) {
00108                 // Don't move tiny revisions
00109                 continue;
00110             }
00111 
00112             # print "Storing "  . strlen( $text ) . " bytes to $url\n";
00113             # print "old_id=$id\n";
00114 
00115             $url = $ext->store( $cluster, $text );
00116             if ( !$url ) {
00117                 print "Error writing to external storage\n";
00118                 exit;
00119             }
00120             $dbw->update( 'text',
00121                 array( 'old_flags' => $flags, 'old_text' => $url ),
00122                 array( 'old_id' => $id ), $fname );
00123             $numMoved++;
00124         }
00125     }
00126 }