3 require_once( dirname( __FILE__
) . '/../Maintenance.php' );
5 class FixBug20757
extends Maintenance
{
6 var $batchSize = 10000;
7 var $mapCache = array();
9 var $maxMapCacheSize = 1000000;
11 function __construct() {
12 parent
::__construct();
13 $this->mDescription
= 'Script to fix bug 20757 assuming that blob_tracking is intact';
14 $this->addOption( 'dry-run', 'Report only' );
15 $this->addOption( 'start', 'old_id to start at', false, true );
19 $dbr = wfGetDB( DB_SLAVE
);
20 $dbw = wfGetDB( DB_MASTER
);
22 $dryRun = $this->getOption( 'dry-run' );
24 print "Dry run only.\n";
27 $startId = $this->getOption( 'start', 0 );
32 $totalRevs = $dbr->selectField( 'text', 'MAX(old_id)', false, __METHOD__
);
35 print "ID: $startId / $totalRevs\r";
39 array( 'old_id', 'old_flags', 'old_text' ),
41 'old_id > ' . intval( $startId ),
42 'old_flags ' . $dbr->buildLike( $dbr->anyString(), 'object', $dbr->anyString
)
46 'ORDER BY' => 'old_id',
47 'LIMIT' => $this->batchSize
,
51 if ( !$res->numRows() ) {
55 $secondaryIds = array();
58 foreach ( $res as $row ) {
59 $startId = $row->old_id
;
61 // Basic sanity checks
62 $obj = unserialize( $row->old_text
);
63 if ( $obj === false ) {
64 print "{$row->old_id}: unrecoverable: cannot unserialize\n";
69 if ( !is_object( $obj ) ) {
70 print "{$row->old_id}: unrecoverable: unserialized to type " .
71 gettype( $obj ) . ", possible double-serialization\n";
76 if ( strtolower( get_class( $obj ) ) !== 'historyblobstub' ) {
77 print "{$row->old_id}: unrecoverable: unexpected object class " .
78 get_class( $obj ) . "\n";
83 // Queue the stub for future batch processing
84 $id = intval( $obj->mOldId
);
85 $secondaryIds[] = $id;
86 $stubs[$row->old_id
] = array(
88 'hash' => $obj->mHash
,
92 $secondaryIds = array_unique( $secondaryIds );
94 if ( !count( $secondaryIds ) ) {
98 // Run the batch query on blob_tracking
103 'bt_text_id' => $secondaryIds,
108 $trackedBlobs = array();
109 foreach ( $res as $row ) {
110 $trackedBlobs[$row->bt_text_id
] = $row;
114 $stubsToFix = array();
115 foreach ( $stubs as $primaryId => $stub ) {
116 $secondaryId = $stub['secondaryId'];
117 if ( !isset( $trackedBlobs[$secondaryId] ) ) {
118 // No tracked blob. Work out what went wrong
119 $secondaryRow = $dbr->selectRow(
121 array( 'old_flags', 'old_text' ),
122 array( 'old_id' => $secondaryId ),
125 if ( !$secondaryRow ) {
126 print "$primaryId: unrecoverable: secondary row is missing\n";
128 } elseif ( $this->isUnbrokenStub( $stub, $secondaryRow ) ) {
129 // Not broken yet, and not in the tracked clusters so it won't get
130 // broken by the current RCT run.
132 } elseif ( strpos( $secondaryRow->old_flags
, 'external' ) !== false ) {
133 print "$primaryId: unrecoverable: secondary gone to {$secondaryRow->old_text}\n";
136 print "$primaryId: unrecoverable: miscellaneous corruption of secondary row\n";
139 unset( $stubs[$primaryId] );
142 $trackRow = $trackedBlobs[$secondaryId];
144 // Check that the specified text really is available in the tracked source row
145 $url = "DB://{$trackRow->bt_cluster}/{$trackRow->bt_blob_id}/{$stub['hash']}";
146 $text = ExternalStore
::fetchFromURL( $url );
147 if ( $text === false ) {
148 print "$primaryId: unrecoverable: source text missing\n";
150 unset( $stubs[$primaryId] );
153 if ( md5( $text ) !== $stub['hash'] ) {
154 print "$primaryId: unrecoverable: content hashes do not match\n";
156 unset( $stubs[$primaryId] );
160 // Find the page_id and rev_id
161 // The page is probably the same as the page of the secondary row
162 $pageId = intval( $trackRow->bt_page
);
164 $revId = $pageId = 0;
166 $revId = $this->findTextIdInPage( $pageId, $primaryId );
168 // Actually an orphan
169 $pageId = $revId = 0;
174 // Reset the text row to point to the original copy
180 'old_flags' => 'external', // use legacy encoding
184 array( 'old_id' => $primaryId ),
188 // Add a blob_tracking row so that the new reference can be recompressed
189 // without needing to run trackBlobs.php again
190 $dbw->insert( 'blob_tracking',
192 'bt_page' => $pageId,
193 'bt_rev_id' => $revId,
194 'bt_text_id' => $primaryId,
195 'bt_cluster' => $trackRow->bt_cluster
,
196 'bt_blob_id' => $trackRow->bt_blob_id
,
197 'bt_cgz_hash' => $stub['hash'],
198 'bt_new_url' => null,
204 $this->waitForSlaves();
207 print "$primaryId: resolved to $url\n";
213 print "Fixed: $numFixed\n";
214 print "Unrecoverable: $numBad\n";
215 print "Good stubs: $numGood\n";
218 function waitForSlaves() {
219 static $iteration = 0;
221 if ( ++
$iteration > 50 == 0 ) {
222 wfWaitForSlaves( 5 );
227 function findTextIdInPage( $pageId, $textId ) {
228 $ids = $this->getRevTextMap( $pageId );
229 if ( !isset( $ids[$textId] ) ) {
232 return $ids[$textId];
236 function getRevTextMap( $pageId ) {
237 if ( !isset( $this->mapCache
[$pageId] ) ) {
239 while ( $this->mapCacheSize
> $this->maxMapCacheSize
) {
240 $key = key( $this->mapCache
);
241 $this->mapCacheSize
-= count( $this->mapCache
[$key] );
242 unset( $this->mapCache
[$key] );
245 $dbr = wfGetDB( DB_SLAVE
);
247 $res = $dbr->select( 'revision',
248 array( 'rev_id', 'rev_text_id' ),
249 array( 'rev_page' => $pageId ),
252 foreach ( $res as $row ) {
253 $map[$row->rev_text_id
] = $row->rev_id
;
255 $this->mapCache
[$pageId] = $map;
256 $this->mapCacheSize +
= count( $map );
258 return $this->mapCache
[$pageId];
262 * This is based on part of HistoryBlobStub::getText().
263 * Determine if the text can be retrieved from the row in the normal way.
265 function isUnbrokenStub( $stub, $secondaryRow ) {
266 $flags = explode( ',', $secondaryRow->old_flags
);
267 if( in_array( 'external', $flags ) ) {
268 $url = $secondaryRow->old_text
;
269 @list
( /* $proto */ , $path ) = explode( '://', $url, 2 );
273 $secondaryRow->old_text
= ExternalStore
::fetchFromUrl( $url );
275 if( !in_array( 'object', $flags ) ) {
279 if( in_array( 'gzip', $flags ) ) {
280 $obj = unserialize( gzinflate( $secondaryRow->old_text
) );
282 $obj = unserialize( $secondaryRow->old_text
);
285 if( !is_object( $obj ) ) {
286 // Correct for old double-serialization bug.
287 $obj = unserialize( $obj );
290 if ( !is_object( $obj ) ) {
295 $text = $obj->getItem( $stub['hash'] );
296 return $text !== false;
300 $maintClass = 'FixBug20757';
301 require_once( DO_MAINTENANCE
);