3 * Move revision's text to external storage
6 * @ingroup Maintenance ExternalStorage
9 define( 'REPORTING_INTERVAL', 1 );
11 if ( !defined( 'MEDIAWIKI' ) ) {
12 require_once( dirname( __FILE__
) . '/../commandLine.inc' );
13 require_once( dirname( __FILE__
) . '/../../includes/ExternalStoreDB.php' );
14 require_once( 'resolveStubs.php' );
16 $fname = 'moveToExternal';
18 if ( !isset( $args[0] ) ) {
19 print "Usage: php moveToExternal.php [-s <startid>] [-e <endid>] <cluster>\n";
24 $dbw = wfGetDB( DB_MASTER
);
26 if ( isset( $options['e'] ) ) {
27 $maxID = $options['e'];
29 $maxID = $dbw->selectField( 'text', 'MAX(old_id)', false, $fname );
31 $minID = isset( $options['s'] ) ?
$options['s'] : 1;
33 moveToExternal( $cluster, $maxID, $minID );
36 function moveToExternal( $cluster, $maxID, $minID = 1 ) {
37 $fname = 'moveToExternal';
38 $dbw = wfGetDB( DB_MASTER
);
39 $dbr = wfGetDB( DB_SLAVE
);
41 $count = $maxID - $minID +
1;
43 $numBlocks = ceil( $count / $blockSize );
44 print "Moving text rows from $minID to $maxID to external storage\n";
45 $ext = new ExternalStoreDB
;
48 for ( $block = 0; $block < $numBlocks; $block++
) {
49 $blockStart = $block * $blockSize +
$minID;
50 $blockEnd = $blockStart +
$blockSize - 1;
52 if ( !( $block % REPORTING_INTERVAL
) ) {
53 print "oldid=$blockStart, moved=$numMoved\n";
57 $res = $dbr->select( 'text', array( 'old_id', 'old_flags', 'old_text' ),
59 "old_id BETWEEN $blockStart AND $blockEnd",
60 'old_flags NOT ' . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() ),
62 foreach ( $res as $row ) {
64 $text = $row->old_text
;
66 if ( $row->old_flags
=== '' ) {
69 $flags = "{$row->old_flags},external";
72 if ( strpos( $flags, 'object' ) !== false ) {
73 $obj = unserialize( $text );
74 $className = strtolower( get_class( $obj ) );
75 if ( $className == 'historyblobstub' ) {
76 # resolveStub( $id, $row->old_text, $row->old_flags );
79 } elseif ( $className == 'historyblobcurstub' ) {
80 $text = gzdeflate( $obj->getText() );
81 $flags = 'utf-8,gzip,external';
82 } elseif ( $className == 'concatenatedgziphistoryblob' ) {
85 print "Warning: unrecognised object class \"$className\"\n";
92 if ( strlen( $text ) < 100 && $className === false ) {
93 // Don't move tiny revisions
97 # print "Storing " . strlen( $text ) . " bytes to $url\n";
98 # print "old_id=$id\n";
100 $url = $ext->store( $cluster, $text );
102 print "Error writing to external storage\n";
105 $dbw->update( 'text',
106 array( 'old_flags' => $flags, 'old_text' => $url ),
107 array( 'old_id' => $id ), $fname );