Live fixes: lots of scary magic runes; php 5 fixes; reporting; etc
[lhc/web/wiklou.git] / maintenance / storage / moveToExternal.php
1 <?php
2
3 define( 'REPORTING_INTERVAL', 100 );
4
5 if ( !defined( 'MEDIAWIKI' ) ) {
6 $optionsWithArgs = array( 'm' );
7
8 require_once( '../commandLine.inc' );
9 require_once( 'ExternalStoreDB.php' );
10 require_once( 'resolveStubs.php' );
11
12 $fname = 'moveToExternal';
13
14 if ( !isset( $args[0] ) ) {
15 print "Usage: php moveToExternal.php [-m <maxid>] <cluster>\n";
16 exit;
17 }
18
19 $cluster = $args[0];
20 $dbw =& wfGetDB( DB_MASTER );
21
22 if ( isset( $options['m'] ) ) {
23 $maxID = $options['m'];
24 } else {
25 $maxID = $dbw->selectField( 'text', 'MAX(old_id)', false, $fname );
26 }
27
28 moveToExternal( $cluster, $maxID );
29 }
30
31
32
33 function moveToExternal( $cluster, $maxID ) {
34 $fname = 'moveToExternal';
35 $dbw =& wfGetDB( DB_MASTER );
36
37 print "Moving $maxID text rows to external storage\n";
38 $ext = new ExternalStoreDB;
39 for ( $id = 1; $id <= $maxID; $id++ ) {
40 if ( !($id % REPORTING_INTERVAL) ) {
41 print "$id\n";
42 wfWaitForSlaves( 5 );
43 }
44 $row = $dbw->selectRow( 'text', array( 'old_flags', 'old_text' ),
45 array(
46 'old_id' => $id,
47 "old_flags NOT LIKE '%external%'",
48 ), $fname );
49 if ( !$row ) {
50 # Non-existent or already done
51 continue;
52 }
53
54 # Resolve stubs
55 $text = $row->old_text;
56 if ( $row->old_flags === '' ) {
57 $flags = 'external';
58 } else {
59 $flags = "{$row->old_flags},external";
60 }
61
62 if ( strpos( $flags, 'object' ) !== false ) {
63 $obj = unserialize( $text );
64 $className = strtolower( get_class( $obj ) );
65 if ( $className == 'historyblobstub' ) {
66 resolveStub( $id, $row->old_text, $row->old_flags );
67 continue;
68 } elseif ( $className == 'historyblobcurstub' ) {
69 $text = gzdeflate( $obj->getText() );
70 $flags = 'utf-8,gzip,external';
71 } elseif ( $className == 'concatenatedgziphistoryblob' ) {
72 // Do nothing
73 } else {
74 print "Warning: unrecognised object class \"$className\"\n";
75 continue;
76 }
77 }
78
79 if ( strlen( $text ) < 100 ) {
80 // Don't move tiny revisions
81 continue;
82 }
83
84 #print "Storing " . strlen( $text ) . " bytes to $url\n";
85
86 $url = $ext->store( $cluster, $text );
87 if ( !$url ) {
88 print "Error writing to external storage\n";
89 exit;
90 }
91 $dbw->update( 'text',
92 array( 'old_flags' => $flags, 'old_text' => $url ),
93 array( 'old_id' => $id ), $fname );
94 }
95 }
96
97 ?>