Big commit: kill almost every freeResult() call as useless
[lhc/web/wiklou.git] / maintenance / storage / moveToExternal.php
1 <?php
2 /**
3 * Move revision's text to external storage
4 *
5 * @file
6 * @ingroup Maintenance ExternalStorage
7 */
8
9 define( 'REPORTING_INTERVAL', 1 );
10
11 if ( !defined( 'MEDIAWIKI' ) ) {
12 $optionsWithArgs = array( 'e', 's' );
13
14 require_once( dirname( __FILE__ ) . '/../commandLine.inc' );
15 require_once( 'ExternalStoreDB.php' );
16 require_once( 'resolveStubs.php' );
17
18 $fname = 'moveToExternal';
19
20 if ( !isset( $args[0] ) ) {
21 print "Usage: php moveToExternal.php [-s <startid>] [-e <endid>] <cluster>\n";
22 exit;
23 }
24
25 $cluster = $args[0];
26 $dbw = wfGetDB( DB_MASTER );
27
28 if ( isset( $options['e'] ) ) {
29 $maxID = $options['e'];
30 } else {
31 $maxID = $dbw->selectField( 'text', 'MAX(old_id)', false, $fname );
32 }
33 $minID = isset( $options['s'] ) ? $options['s'] : 1;
34
35 moveToExternal( $cluster, $maxID, $minID );
36 }
37
38
39
40 function moveToExternal( $cluster, $maxID, $minID = 1 ) {
41 $fname = 'moveToExternal';
42 $dbw = wfGetDB( DB_MASTER );
43 $dbr = wfGetDB( DB_SLAVE );
44
45 $count = $maxID - $minID + 1;
46 $blockSize = 1000;
47 $numBlocks = ceil( $count / $blockSize );
48 print "Moving text rows from $minID to $maxID to external storage\n";
49 $ext = new ExternalStoreDB;
50 $numMoved = 0;
51
52 for ( $block = 0; $block < $numBlocks; $block++ ) {
53 $blockStart = $block * $blockSize + $minID;
54 $blockEnd = $blockStart + $blockSize - 1;
55
56 if ( !( $block % REPORTING_INTERVAL ) ) {
57 print "oldid=$blockStart, moved=$numMoved\n";
58 wfWaitForSlaves( 2 );
59 }
60
61 $res = $dbr->select( 'text', array( 'old_id', 'old_flags', 'old_text' ),
62 array(
63 "old_id BETWEEN $blockStart AND $blockEnd",
64 'old_flags NOT ' . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() ),
65 ), $fname );
66 while ( $row = $dbr->fetchObject( $res ) ) {
67 # Resolve stubs
68 $text = $row->old_text;
69 $id = $row->old_id;
70 if ( $row->old_flags === '' ) {
71 $flags = 'external';
72 } else {
73 $flags = "{$row->old_flags},external";
74 }
75
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 );
81 # $numStubs++;
82 continue;
83 } elseif ( $className == 'historyblobcurstub' ) {
84 $text = gzdeflate( $obj->getText() );
85 $flags = 'utf-8,gzip,external';
86 } elseif ( $className == 'concatenatedgziphistoryblob' ) {
87 // Do nothing
88 } else {
89 print "Warning: unrecognised object class \"$className\"\n";
90 continue;
91 }
92 } else {
93 $className = false;
94 }
95
96 if ( strlen( $text ) < 100 && $className === false ) {
97 // Don't move tiny revisions
98 continue;
99 }
100
101 # print "Storing " . strlen( $text ) . " bytes to $url\n";
102 # print "old_id=$id\n";
103
104 $url = $ext->store( $cluster, $text );
105 if ( !$url ) {
106 print "Error writing to external storage\n";
107 exit;
108 }
109 $dbw->update( 'text',
110 array( 'old_flags' => $flags, 'old_text' => $url ),
111 array( 'old_id' => $id ), $fname );
112 $numMoved++;
113 }
114 }
115 }
116
117