don't use unspecific paths in require_once()'s ... they break with default include...
[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( dirname(__FILE__) . '/../commandLine.inc' );
9
10 resolveStubs();
11 }
12
13 /**
14 * Convert history stubs that point to an external row to direct
15 * external pointers
16 */
17 function resolveStubs() {
18 $fname = 'resolveStubs';
19
20 $dbr = wfGetDB( DB_SLAVE );
21 $maxID = $dbr->selectField( 'text', 'MAX(old_id)', false, $fname );
22 $blockSize = 10000;
23 $numBlocks = intval( $maxID / $blockSize ) + 1;
24
25 for ( $b = 0; $b < $numBlocks; $b++ ) {
26 wfWaitForSlaves( 2 );
27
28 printf( "%5.2f%%\n", $b / $numBlocks * 100 );
29 $start = intval($maxID / $numBlocks) * $b + 1;
30 $end = intval($maxID / $numBlocks) * ($b + 1);
31
32 $res = $dbr->select( 'text', array( 'old_id', 'old_text', 'old_flags' ),
33 "old_id>=$start AND old_id<=$end " .
34 # Using a more restrictive flag set for now, until I do some more analysis -- TS
35 #"AND old_flags LIKE '%object%' AND old_flags NOT LIKE '%external%' ".
36
37 "AND old_flags='object' " .
38 "AND LOWER(LEFT(old_text,22)) = 'O:15:\"historyblobstub\"'", $fname );
39 while ( $row = $dbr->fetchObject( $res ) ) {
40 resolveStub( $row->old_id, $row->old_text, $row->old_flags );
41 }
42 $dbr->freeResult( $res );
43
44
45 }
46 print "100%\n";
47 }
48
49 /**
50 * Resolve a history stub
51 */
52 function resolveStub( $id, $stubText, $flags ) {
53 $fname = 'resolveStub';
54
55 $stub = unserialize( $stubText );
56 $flags = explode( ',', $flags );
57
58 $dbr = wfGetDB( DB_SLAVE );
59 $dbw = wfGetDB( DB_MASTER );
60
61 if ( strtolower( get_class( $stub ) ) !== 'historyblobstub' ) {
62 print "Error found object of class " . get_class( $stub ) . ", expecting historyblobstub\n";
63 return;
64 }
65
66 # Get the (maybe) external row
67 $externalRow = $dbr->selectRow( 'text', array( 'old_text' ),
68 array( 'old_id' => $stub->mOldId, "old_flags LIKE '%external%'" ),
69 $fname
70 );
71
72 if ( !$externalRow ) {
73 # Object wasn't external
74 return;
75 }
76
77 # Preserve the legacy encoding flag, but switch from object to external
78 if ( in_array( 'utf-8', $flags ) ) {
79 $newFlags = 'external,utf-8';
80 } else {
81 $newFlags = 'external';
82 }
83
84 # Update the row
85 #print "oldid=$id\n";
86 $dbw->update( 'text',
87 array( /* SET */
88 'old_flags' => $newFlags,
89 'old_text' => $externalRow->old_text . '/' . $stub->mHash
90 ),
91 array( /* WHERE */
92 'old_id' => $id
93 ), $fname
94 );
95 }
96