External storage handling
[lhc/web/wiklou.git] / maintenance / storage / resolveStubs.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( 'includes/ExternalStoreDB.php' );
10
11 resolveStubs();
12 }
13
14 /**
15 * Convert history stubs that point to an external row to direct
16 * external pointers
17 */
18 function resolveStubs() {
19 $fname = 'resolveStubs';
20
21 print "Retrieving stub rows...\n";
22 $dbr =& wfGetDB( DB_SLAVE );
23 $maxID = $dbr->selectField( 'text', 'MAX(old_id)', false, $fname );
24 $stubs = array();
25 $flagsArray = array();
26
27 # Do it in 100 blocks
28 for ( $b = 0; $b < 100; $b++ ) {
29 print "$b%\r";
30 $start = intval($maxID / 100) * $b + 1;
31 $end = intval($maxID / 100) * ($b + 1);
32
33 $res = $dbr->select( 'text', array( 'old_id', 'old_text', 'old_flags' ),
34 "old_id>=$start AND old_id<=$end AND old_flags like '%object%' ".
35 "AND old_text LIKE 'O:15:\"historyblobstub\"%'", $fname );
36 while ( $row = $dbr->fetchObject( $res ) ) {
37 $stubs[$row->old_id] = $row->old_text;
38 $flagsArray[$row->old_id] = $row->old_flags;
39 }
40 $dbr->freeResult( $res );
41 }
42 print "100%\n";
43
44 print "\nConverting " . count( $stubs ) . " rows ...\n";
45
46 # Get master database, no transactions
47 $dbw =& wfGetDB( DB_MASTER );
48 $dbw->clearFlag( DBO_TRX );
49 $dbw->immediateCommit();
50
51 $i = 0;
52 foreach( $stubs as $id => $stub ) {
53 if ( !(++$i % REPORTING_INTERVAL) ) {
54 print "$i\n";
55 wfWaitForSlaves( 5 );
56 }
57
58 resolveStub( $id, $stub, $flagsArray[$id] );
59 }
60 }
61
62 /**
63 * Resolve a history stub
64 */
65 function resolveStub( $id, $stubText, $flags ) {
66 $fname = 'resolveStub';
67
68 $stub = unserialize( $stubText );
69 $flags = explode( ',', $flags );
70
71 $dbr =& wfGetDB( DB_SLAVE );
72 $dbw =& wfGetDB( DB_MASTER );
73
74 if ( get_class( $stub ) !== 'historyblobstub' ) {
75 print "Error, invalid stub object\n";
76 return;
77 }
78
79 # Get the (maybe) external row
80 $externalRow = $dbr->selectRow( 'text', array( 'old_text' ),
81 array( 'old_id' => $stub->mOldId, "old_flags LIKE '%external%'" ),
82 $fname
83 );
84
85 if ( !$externalRow ) {
86 # Object wasn't external
87 continue;
88 }
89
90 # Preserve the legacy encoding flag, but switch from object to external
91 if ( in_array( 'utf-8', $flags ) ) {
92 $newFlags = 'external,utf-8';
93 } else {
94 $newFlags = 'external';
95 }
96
97 # Update the row
98 $dbw->update( 'text',
99 array( /* SET */
100 'old_flags' => $newFlags,
101 'old_text' => $externalRow->old_text . '/' . $stub->mHash
102 ),
103 array( /* WHERE */
104 'old_id' => $id
105 ), $fname
106 );
107 }
108 ?>