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