scripts for external storage
[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 $stub = unserialize( $stub );
59 if ( get_class( $stub ) !== 'historyblobstub' ) {
60 print "Error, invalid stub object\n";
61 return;
62 }
63
64 # Get the (maybe) external row
65 $externalRow = $dbr->selectRow( 'text', array( 'old_text' ),
66 array( 'old_id' => $stub->mOldId, "old_flags LIKE '%external%'" ),
67 $fname
68 );
69
70 if ( !$externalRow ) {
71 # Object wasn't external
72 continue;
73 }
74
75 # Preserve the legacy encoding flag, but switch from object to external
76 $flags = explode( ',', $flagsArray[$id] );
77 if ( in_array( 'utf-8', $flags ) ) {
78 $newFlags = 'external,utf-8';
79 } else {
80 $newFlags = 'external';
81 }
82
83 # Update the row
84 $dbw->update( 'text',
85 array( /* SET */
86 'old_flags' => $newFlags,
87 'old_text' => $externalRow->old_text . '/' . $stub->mHash
88 ),
89 array( /* WHERE */
90 'old_id' => $id
91 ), $fname
92 );
93 }
94 }
95
96 ?>