3 * Move revision's text to external storage
6 * @ingroup Maintenance ExternalStorage
9 define( 'REPORTING_INTERVAL', 1 );
11 if ( !defined( 'MEDIAWIKI' ) ) {
12 $optionsWithArgs = array( 'e', 's' );
14 require_once( dirname( __FILE__
) . '/../commandLine.inc' );
15 require_once( 'ExternalStoreDB.php' );
16 require_once( 'resolveStubs.php' );
18 $fname = 'moveToExternal';
20 if ( !isset( $args[0] ) ) {
21 print "Usage: php moveToExternal.php [-s <startid>] [-e <endid>] <cluster>\n";
26 $dbw = wfGetDB( DB_MASTER
);
28 if ( isset( $options['e'] ) ) {
29 $maxID = $options['e'];
31 $maxID = $dbw->selectField( 'text', 'MAX(old_id)', false, $fname );
33 $minID = isset( $options['s'] ) ?
$options['s'] : 1;
35 moveToExternal( $cluster, $maxID, $minID );
40 function moveToExternal( $cluster, $maxID, $minID = 1 ) {
41 $fname = 'moveToExternal';
42 $dbw = wfGetDB( DB_MASTER
);
43 $dbr = wfGetDB( DB_SLAVE
);
45 $count = $maxID - $minID +
1;
47 $numBlocks = ceil( $count / $blockSize );
48 print "Moving text rows from $minID to $maxID to external storage\n";
49 $ext = new ExternalStoreDB
;
52 for ( $block = 0; $block < $numBlocks; $block++
) {
53 $blockStart = $block * $blockSize +
$minID;
54 $blockEnd = $blockStart +
$blockSize - 1;
56 if ( !( $block % REPORTING_INTERVAL
) ) {
57 print "oldid=$blockStart, moved=$numMoved\n";
61 $res = $dbr->select( 'text', array( 'old_id', 'old_flags', 'old_text' ),
63 "old_id BETWEEN $blockStart AND $blockEnd",
64 'old_flags NOT ' . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() ),
66 while ( $row = $dbr->fetchObject( $res ) ) {
68 $text = $row->old_text
;
70 if ( $row->old_flags
=== '' ) {
73 $flags = "{$row->old_flags},external";
76 if ( strpos( $flags, 'object' ) !== false ) {
77 $obj = unserialize( $text );
78 $className = strtolower( get_class( $obj ) );
79 if ( $className == 'historyblobstub' ) {
80 # resolveStub( $id, $row->old_text, $row->old_flags );
83 } elseif ( $className == 'historyblobcurstub' ) {
84 $text = gzdeflate( $obj->getText() );
85 $flags = 'utf-8,gzip,external';
86 } elseif ( $className == 'concatenatedgziphistoryblob' ) {
89 print "Warning: unrecognised object class \"$className\"\n";
96 if ( strlen( $text ) < 100 && $className === false ) {
97 // Don't move tiny revisions
101 # print "Storing " . strlen( $text ) . " bytes to $url\n";
102 # print "old_id=$id\n";
104 $url = $ext->store( $cluster, $text );
106 print "Error writing to external storage\n";
109 $dbw->update( 'text',
110 array( 'old_flags' => $flags, 'old_text' => $url ),
111 array( 'old_id' => $id ), $fname );
114 $dbr->freeResult( $res );