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