For the maintenance/ directory files:
[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 $dbr =& wfGetDB( DB_SLAVE );
22 $maxID = $dbr->selectField( 'text', 'MAX(old_id)', false, $fname );
23 $blockSize = 10000;
24 $numBlocks = intval( $maxID / $blockSize ) + 1;
25
26 for ( $b = 0; $b < $numBlocks; $b++ ) {
27 wfWaitForSlaves( 5 );
28
29 printf( "%5.2f%%\n", $b / $numBlocks * 100 );
30 $start = intval($maxID / $numBlocks) * $b + 1;
31 $end = intval($maxID / $numBlocks) * ($b + 1);
32
33 $res = $dbr->select( 'text', array( 'old_id', 'old_text', 'old_flags' ),
34 "old_id>=$start AND old_id<=$end " .
35 # Using a more restrictive flag set for now, until I do some more analysis -- TS
36 #"AND old_flags LIKE '%object%' AND old_flags NOT LIKE '%external%' ".
37
38 "AND old_flags='object' " .
39 "AND old_text LIKE 'O:15:\"historyblobstub\"%'", $fname );
40 while ( $row = $dbr->fetchObject( $res ) ) {
41 resolveStub( $row->old_id, $row->old_text, $row->old_flags );
42 }
43 $dbr->freeResult( $res );
44
45
46 }
47 print "100%\n";
48 }
49
50 /**
51 * Resolve a history stub
52 */
53 function resolveStub( $id, $stubText, $flags ) {
54 $fname = 'resolveStub';
55
56 $stub = unserialize( $stubText );
57 $flags = explode( ',', $flags );
58
59 $dbr =& wfGetDB( DB_SLAVE );
60 $dbw =& wfGetDB( DB_MASTER );
61
62 if ( strtolower( get_class( $stub ) ) !== 'historyblobstub' ) {
63 print "Error found object of class " . get_class( $stub ) . ", expecting historyblobstub\n";
64 return;
65 }
66
67 # Get the (maybe) external row
68 $externalRow = $dbr->selectRow( 'text', array( 'old_text' ),
69 array( 'old_id' => $stub->mOldId, "old_flags LIKE '%external%'" ),
70 $fname
71 );
72
73 if ( !$externalRow ) {
74 # Object wasn't external
75 return;
76 }
77
78 # Preserve the legacy encoding flag, but switch from object to external
79 if ( in_array( 'utf-8', $flags ) ) {
80 $newFlags = 'external,utf-8';
81 } else {
82 $newFlags = 'external';
83 }
84
85 # Update the row
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 ?>