7 define( 'CONCAT_HEADER', 'O:27:"concatenatedgziphistoryblob"' );
9 if ( !defined( 'MEDIAWIKI' ) ) {
10 require_once( dirname(__FILE__
) . '/../commandLine.inc' );
11 require_once('ExternalStore.php');
12 require_once( 'ExternalStoreDB.php' );
18 //----------------------------------------------------------------------------------
20 function checkError( $msg, $ids ) {
22 if ( is_array( $ids ) && count( $ids ) == 1 ) {
25 if ( is_array( $ids ) ) {
27 foreach ( $ids as $id ) {
28 $revIds = array_merge( $revIds, array_keys( $oldIdMap, $id ) );
30 print "$msg in text rows " . implode( ', ', $ids ) .
31 ", revisions " . implode( ', ', $revIds ) . "\n";
34 $revIds = array_keys( $oldIdMap, $id );
35 if ( count( $revIds ) == 1 ) {
36 print "$msg in old_id $id, rev_id {$revIds[0]}\n";
38 print "$msg in old_id $id, revisions " . implode( ', ', $revIds ) . "\n";
43 function checkStorage() {
46 $fname = 'checkStorage';
47 $dbr =& wfGetDB( DB_SLAVE
);
48 $maxRevId = $dbr->selectField( 'revision', 'MAX(rev_id)', false, $fname );
51 $objectStats = array();
52 $knownFlags = array( 'external', 'gzip', 'object', 'utf-8' );
55 for ( $chunkStart = 1 ; $chunkStart < $maxRevId; $chunkStart +
= $chunkSize ) {
56 $chunkEnd = $chunkStart +
$chunkSize - 1;
57 //print "$chunkStart to $chunkEnd of $maxRevId\n";
59 // Fetch revision rows
61 $res = $dbr->select( 'revision', array( 'rev_id', 'rev_text_id' ),
62 array( "rev_id BETWEEN $chunkStart AND $chunkEnd" ), $fname );
63 while ( $row = $dbr->fetchObject( $res ) ) {
64 $oldIdMap[$row->rev_id
] = $row->rev_text_id
;
66 $dbr->freeResult( $res );
69 $missingTextRows = array_flip( $oldIdMap );
70 $externalRevs = array();
71 $objectRevs = array();
72 $flagsFields = array();
73 $res = $dbr->select( 'text', array( 'old_id', 'old_flags' ),
74 'old_id IN (' . implode( ',', $oldIdMap ) . ')', $fname );
75 while ( $row = $dbr->fetchObject( $res ) ) {
76 $flags = $row->old_flags
;
79 // Create flagStats row if it doesn't exist
80 $flagStats = $flagStats +
array( $flags => 0 );
85 unset( $missingTextRows[$row->old_id
] );
87 // Check for external or object
91 $flagArray = explode( ',', $flags );
93 if ( in_array( 'external', $flagArray ) ) {
94 $flagsFields[$id] = $flags; // is this needed?
95 $externalRevs[] = $id;
96 } elseif ( in_array( 'object', $flagArray ) ) {
97 $flagsFields[$id] = $flags; // is this needed?
101 // Check for unrecognised flags
102 if ( count( array_diff( $flagArray, $knownFlags ) ) ) {
103 print_r( array_diff( $flagArray, $knownFlags ) );
104 checkError( "Warning: invalid flags field \"$flags\"", $id );
107 $dbr->freeResult( $res );
109 // Output errors for any missing text rows
110 foreach ( $missingTextRows as $oldId => $revId ) {
111 print "Error: missing text row $oldId for revision $revId\n";
114 // Verify external revisions
115 $externalConcatBlobs = array();
116 $externalNormalBlobs = array();
117 if ( count( $externalRevs ) ) {
118 $res = $dbr->select( 'text', array( 'old_id', 'old_flags', 'old_text' ),
119 array( 'old_id IN (' . implode( ',', $externalRevs ) . ')' ), $fname );
120 while ( $row = $dbr->fetchObject( $res ) ) {
121 $urlParts = explode( '://', $row->old_text
, 2 );
122 if ( count( $urlParts ) !== 2 ||
$urlParts[1] == '' ) {
123 checkError( "Error: invalid URL \"{$row->old_text}\"", $row->old_id
);
126 list( $proto, $path ) = $urlParts;
127 if ( $proto != 'DB' ) {
128 checkError( "Error: invalid external protocol \"$proto\"", $row->old_id
);
131 $path = explode( '/', $row->old_text
);
134 if ( isset( $path[4] ) ) {
135 $externalConcatBlobs[$cluster][$id][] = $row->old_id
;
137 $externalNormalBlobs[$cluster][$id][] = $row->old_id
;
140 $dbr->freeResult( $res );
143 // Check external concat blobs for the right header
144 checkExternalConcatBlobs( $externalConcatBlobs );
147 // Check external normal blobs for existence
148 if ( count( $externalNormalBlobs ) ) {
149 if ( is_null( $dbStore ) ) {
150 $dbStore = new ExternalStoreDB
;
152 foreach ( $externalConcatBlobs as $cluster => $xBlobIds ) {
153 $blobIds = array_keys( $xBlobIds );
154 $extDb =& $dbStore->getSlave( $cluster );
155 $blobsTable = $dbStore->getTable( $extDb );
156 $res = $extDb->select( $blobsTable,
158 array( 'blob_id IN( ' . implode( ',', $blobIds ) . ')' ), $fname );
159 while ( $row = $extDb->fetchObject( $res ) ) {
160 unset( $xBlobIds[$row->blob_id
] );
162 $extDb->freeResult( $res );
163 // Print errors for missing blobs rows
164 foreach ( $xBlobIds as $blobId => $oldId ) {
165 checkError( "Error: missing target $blobId for one-part ES URL", $oldId );
170 // Check local objects
172 $concatBlobs = array();
174 if ( count( $objectRevs ) ) {
176 $res = $dbr->select( 'text', array( 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ),
177 array( 'old_id IN (' . implode( ',', $objectRevs ) . ')' ), $fname );
178 while ( $row = $dbr->fetchObject( $res ) ) {
179 $oldId = $row->old_id
;
180 if ( !preg_match( '/^O:(\d+):"(\w+)"/', $row->header
, $matches ) ) {
181 checkError( "Error: invalid object header", $oldId );
185 $className = $matches[2];
186 if ( strlen( $className ) != $matches[1] ) {
187 checkError( "Error: invalid object header, wrong class name length", $oldId );
191 $objectStats = $objectStats +
array( $className => 0 );
192 $objectStats[$className]++
;
194 switch ( $className ) {
195 case 'concatenatedgziphistoryblob':
198 case 'historyblobstub':
199 case 'historyblobcurstub':
200 if ( strlen( $row->header
) == $headerLength ) {
201 checkError( "Error: overlong stub header", $oldId );
204 $stubObj = unserialize( $row->header
);
205 if ( !is_object( $stubObj ) ) {
206 checkError( "Error: unable to unserialize stub object", $oldId );
209 if ( $className == 'historyblobstub' ) {
210 $concatBlobs[$stubObj->mOldId
][] = $oldId;
212 $curIds[$stubObj->mCurId
][] = $oldId;
216 checkError( "Error: unrecognised object class \"$className\"", $oldId );
219 $dbr->freeResult( $res );
222 // Check local concat blob validity
223 $externalConcatBlobs = array();
224 if ( count( $concatBlobs ) ) {
226 $res = $dbr->select( 'text', array( 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ),
227 array( 'old_id IN (' . implode( ',', array_keys( $concatBlobs ) ) . ')' ), $fname );
228 while ( $row = $dbr->fetchObject( $res ) ) {
229 $flags = explode( ',', $row->old_flags
);
230 if ( in_array( 'external', $flags ) ) {
231 // Concat blob is in external storage?
232 if ( in_array( 'object', $flags ) ) {
233 $urlParts = explode( '/', $row->header
);
234 if ( $urlParts[0] != 'DB:' ) {
235 checkError( "Error: unrecognised external storage type \"{$urlParts[0]}", $row->old_id
);
237 $cluster = $urlParts[2];
239 if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {
240 $externalConcatBlobs[$cluster][$id] = array();
242 $externalConcatBlobs[$cluster][$id] = array_merge(
243 $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id
]
247 checkError( "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",
248 $concatBlobs[$row->old_id
] );
250 } elseif ( substr( $row->header
, 0, strlen( CONCAT_HEADER
) ) != CONCAT_HEADER
) {
251 checkError( "Error: Incorrect object header for concat bulk row {$row->old_id}",
252 $concatBlobs[$row->old_id
] );
255 unset( $concatBlobs[$row->old_id
] );
257 $dbr->freeResult( $res );
260 // Check targets of unresolved stubs
261 checkExternalConcatBlobs( $externalConcatBlobs );
267 print "\n\nFlag statistics:\n";
268 $total = array_sum( $flagStats );
269 foreach ( $flagStats as $flag => $count ) {
270 printf( "%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );
272 print "\nObject statistics:\n";
273 $total = array_sum( $objectStats );
274 foreach ( $objectStats as $className => $count ) {
275 printf( "%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );
280 function checkExternalConcatBlobs( $externalConcatBlobs ) {
281 static $dbStore = null;
282 $fname = 'checkExternalConcatBlobs';
283 if ( !count( $externalConcatBlobs ) ) {
287 if ( is_null( $dbStore ) ) {
288 $dbStore = new ExternalStoreDB
;
291 foreach ( $externalConcatBlobs as $cluster => $oldIds ) {
292 $blobIds = array_keys( $oldIds );
293 $extDb =& $dbStore->getSlave( $cluster );
294 $blobsTable = $dbStore->getTable( $extDb );
295 $headerLength = strlen( CONCAT_HEADER
);
296 $res = $extDb->select( $blobsTable,
297 array( 'blob_id', "LEFT(blob_text, $headerLength) AS header" ),
298 array( 'blob_id IN( ' . implode( ',', $blobIds ) . ')' ), $fname );
299 while ( $row = $extDb->fetchObject( $res ) ) {
300 unset( $oldIds[$row->blob_id
] );
301 if ( $row->header
!= CONCAT_HEADER
) {
302 checkError( "Error: invalid header on target of two-part ES URL",
303 $oldIds[$row->blob_id
] );
306 $extDb->freeResult( $res );
308 // Print errors for missing blobs rows
309 foreach ( $oldIds as $blobId => $oldIds ) {
310 checkError( "Error: missing target $blobId for two-part ES URL", $oldIds );