\n to eof
[lhc/web/wiklou.git] / maintenance / storage / fixBug20757.php
1 <?php
2
3 require_once( dirname( __FILE__ ) . '/../Maintenance.php' );
4
5 class FixBug20757 extends Maintenance {
6 var $batchSize = 10000;
7 var $mapCache = array();
8 var $mapCacheSize = 0;
9 var $maxMapCacheSize = 1000000;
10
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 );
16 }
17
18 function execute() {
19 $dbr = wfGetDB( DB_SLAVE );
20 $dbw = wfGetDB( DB_MASTER );
21
22 $dryRun = $this->getOption( 'dry-run' );
23 if ( $dryRun ) {
24 print "Dry run only.\n";
25 }
26
27 $startId = $this->getOption( 'start', 0 );
28 $numGood = 0;
29 $numFixed = 0;
30 $numBad = 0;
31
32 $totalRevs = $dbr->selectField( 'text', 'MAX(old_id)', false, __METHOD__ );
33
34 if ( $dbr->getType() == 'mysql'
35 && version_compare( $dbr->getServerVersion(), '4.1.0', '>=' ) )
36 {
37 // In MySQL 4.1+, the binary field old_text has a non-working LOWER() function
38 $lowerLeft = 'LOWER(CONVERT(LEFT(old_text,22) USING latin1))';
39 } else {
40 // No CONVERT() in MySQL 4.0
41 $lowerLeft = 'LOWER(LEFT(old_text,22))';
42 }
43
44 while ( true ) {
45 print "ID: $startId / $totalRevs\r";
46
47 $res = $dbr->select(
48 'text',
49 array( 'old_id', 'old_flags', 'old_text' ),
50 array(
51 'old_id > ' . intval( $startId ),
52 'old_flags LIKE \'%object%\' AND old_flags NOT LIKE \'%external%\'',
53 "$lowerLeft = 'o:15:\"historyblobstub\"'",
54 ),
55 __METHOD__,
56 array(
57 'ORDER BY' => 'old_id',
58 'LIMIT' => $this->batchSize,
59 )
60 );
61
62 if ( !$res->numRows() ) {
63 break;
64 }
65
66 $secondaryIds = array();
67 $stubs = array();
68
69 foreach ( $res as $row ) {
70 $startId = $row->old_id;
71
72 // Basic sanity checks
73 $obj = unserialize( $row->old_text );
74 if ( $obj === false ) {
75 print "{$row->old_id}: unrecoverable: cannot unserialize\n";
76 ++$numBad;
77 continue;
78 }
79
80 if ( !is_object( $obj ) ) {
81 print "{$row->old_id}: unrecoverable: unserialized to type " .
82 gettype( $obj ) . ", possible double-serialization\n";
83 ++$numBad;
84 continue;
85 }
86
87 if ( strtolower( get_class( $obj ) ) !== 'historyblobstub' ) {
88 print "{$row->old_id}: unrecoverable: unexpected object class " .
89 get_class( $obj ) . "\n";
90 ++$numBad;
91 continue;
92 }
93
94 // Process flags
95 $flags = explode( ',', $row->old_flags );
96 if ( in_array( 'utf-8', $flags ) || in_array( 'utf8', $flags ) ) {
97 $legacyEncoding = false;
98 } else {
99 $legacyEncoding = true;
100 }
101
102 // Queue the stub for future batch processing
103 $id = intval( $obj->mOldId );
104 $secondaryIds[] = $id;
105 $stubs[$row->old_id] = array(
106 'legacyEncoding' => $legacyEncoding,
107 'secondaryId' => $id,
108 'hash' => $obj->mHash,
109 );
110 }
111
112 $secondaryIds = array_unique( $secondaryIds );
113
114 if ( !count( $secondaryIds ) ) {
115 continue;
116 }
117
118 // Run the batch query on blob_tracking
119 $res = $dbr->select(
120 'blob_tracking',
121 '*',
122 array(
123 'bt_text_id' => $secondaryIds,
124 ),
125 __METHOD__
126 );
127 $trackedBlobs = array();
128 foreach ( $res as $row ) {
129 $trackedBlobs[$row->bt_text_id] = $row;
130 }
131
132 // Process the stubs
133 foreach ( $stubs as $primaryId => $stub ) {
134 $secondaryId = $stub['secondaryId'];
135 if ( !isset( $trackedBlobs[$secondaryId] ) ) {
136 // No tracked blob. Work out what went wrong
137 $secondaryRow = $dbr->selectRow(
138 'text',
139 array( 'old_flags', 'old_text' ),
140 array( 'old_id' => $secondaryId ),
141 __METHOD__
142 );
143 if ( !$secondaryRow ) {
144 print "$primaryId: unrecoverable: secondary row is missing\n";
145 ++$numBad;
146 } elseif ( $this->isUnbrokenStub( $stub, $secondaryRow ) ) {
147 // Not broken yet, and not in the tracked clusters so it won't get
148 // broken by the current RCT run.
149 ++$numGood;
150 } elseif ( strpos( $secondaryRow->old_flags, 'external' ) !== false ) {
151 print "$primaryId: unrecoverable: secondary gone to {$secondaryRow->old_text}\n";
152 ++$numBad;
153 } else {
154 print "$primaryId: unrecoverable: miscellaneous corruption of secondary row\n";
155 ++$numBad;
156 }
157 unset( $stubs[$primaryId] );
158 continue;
159 }
160 $trackRow = $trackedBlobs[$secondaryId];
161
162 // Check that the specified text really is available in the tracked source row
163 $url = "DB://{$trackRow->bt_cluster}/{$trackRow->bt_blob_id}/{$stub['hash']}";
164 $text = ExternalStore::fetchFromURL( $url );
165 if ( $text === false ) {
166 print "$primaryId: unrecoverable: source text missing\n";
167 ++$numBad;
168 unset( $stubs[$primaryId] );
169 continue;
170 }
171 if ( md5( $text ) !== $stub['hash'] ) {
172 print "$primaryId: unrecoverable: content hashes do not match\n";
173 ++$numBad;
174 unset( $stubs[$primaryId] );
175 continue;
176 }
177
178 // Find the page_id and rev_id
179 // The page is probably the same as the page of the secondary row
180 $pageId = intval( $trackRow->bt_page );
181 if ( !$pageId ) {
182 $revId = $pageId = 0;
183 } else {
184 $revId = $this->findTextIdInPage( $pageId, $primaryId );
185 if ( !$revId ) {
186 // Actually an orphan
187 $pageId = $revId = 0;
188 }
189 }
190
191 $newFlags = $stub['legacyEncoding'] ? 'external' : 'external,utf-8';
192
193 if ( !$dryRun ) {
194 // Reset the text row to point to the original copy
195 $dbw->begin();
196 $dbw->update(
197 'text',
198 // SET
199 array(
200 'old_flags' => $newFlags,
201 'old_text' => $url
202 ),
203 // WHERE
204 array( 'old_id' => $primaryId ),
205 __METHOD__
206 );
207
208 // Add a blob_tracking row so that the new reference can be recompressed
209 // without needing to run trackBlobs.php again
210 $dbw->insert( 'blob_tracking',
211 array(
212 'bt_page' => $pageId,
213 'bt_rev_id' => $revId,
214 'bt_text_id' => $primaryId,
215 'bt_cluster' => $trackRow->bt_cluster,
216 'bt_blob_id' => $trackRow->bt_blob_id,
217 'bt_cgz_hash' => $stub['hash'],
218 'bt_new_url' => null,
219 'bt_moved' => 0,
220 ),
221 __METHOD__
222 );
223 $dbw->commit();
224 $this->waitForSlaves();
225 }
226
227 print "$primaryId: resolved to $url\n";
228 ++$numFixed;
229 }
230 }
231
232 print "\n";
233 print "Fixed: $numFixed\n";
234 print "Unrecoverable: $numBad\n";
235 print "Good stubs: $numGood\n";
236 }
237
238 function waitForSlaves() {
239 static $iteration = 0;
240 ++$iteration;
241 if ( ++$iteration > 50 == 0 ) {
242 wfWaitForSlaves( 5 );
243 $iteration = 0;
244 }
245 }
246
247 function findTextIdInPage( $pageId, $textId ) {
248 $ids = $this->getRevTextMap( $pageId );
249 if ( !isset( $ids[$textId] ) ) {
250 return null;
251 } else {
252 return $ids[$textId];
253 }
254 }
255
256 function getRevTextMap( $pageId ) {
257 if ( !isset( $this->mapCache[$pageId] ) ) {
258 // Limit cache size
259 while ( $this->mapCacheSize > $this->maxMapCacheSize ) {
260 $key = key( $this->mapCache );
261 $this->mapCacheSize -= count( $this->mapCache[$key] );
262 unset( $this->mapCache[$key] );
263 }
264
265 $dbr = wfGetDB( DB_SLAVE );
266 $map = array();
267 $res = $dbr->select( 'revision',
268 array( 'rev_id', 'rev_text_id' ),
269 array( 'rev_page' => $pageId ),
270 __METHOD__
271 );
272 foreach ( $res as $row ) {
273 $map[$row->rev_text_id] = $row->rev_id;
274 }
275 $this->mapCache[$pageId] = $map;
276 $this->mapCacheSize += count( $map );
277 }
278 return $this->mapCache[$pageId];
279 }
280
281 /**
282 * This is based on part of HistoryBlobStub::getText().
283 * Determine if the text can be retrieved from the row in the normal way.
284 */
285 function isUnbrokenStub( $stub, $secondaryRow ) {
286 $flags = explode( ',', $secondaryRow->old_flags );
287 $text = $secondaryRow->old_text;
288 if ( in_array( 'external', $flags ) ) {
289 $url = $text;
290 @list( /* $proto */ , $path ) = explode( '://', $url, 2 );
291 if ( $path == "" ) {
292 return false;
293 }
294 $text = ExternalStore::fetchFromUrl( $url );
295 }
296 if ( !in_array( 'object', $flags ) ) {
297 return false;
298 }
299
300 if ( in_array( 'gzip', $flags ) ) {
301 $obj = unserialize( gzinflate( $text ) );
302 } else {
303 $obj = unserialize( $text );
304 }
305
306 if ( !is_object( $obj ) ) {
307 // Correct for old double-serialization bug.
308 $obj = unserialize( $obj );
309 }
310
311 if ( !is_object( $obj ) ) {
312 return false;
313 }
314
315 $obj->uncompress();
316 $text = $obj->getItem( $stub['hash'] );
317 return $text !== false;
318 }
319 }
320
321 $maintClass = 'FixBug20757';
322 require_once( DO_MAINTENANCE );
323