156 $endId =
$dbr->selectField(
'revision',
'MAX(rev_id)',
false, __METHOD__ );
160 echo
"Finding revisions...\n";
163 $res =
$dbr->select( [
'revision',
'text' ],
164 [
'rev_id',
'rev_page',
'old_id',
'old_flags',
'old_text' ],
166 'rev_id > ' .
$dbr->addQuotes( $startId ),
167 'rev_text_id=old_id',
169 'old_flags ' .
$dbr->buildLike(
$dbr->anyString(),
'external',
$dbr->anyString() ),
173 'ORDER BY' =>
'rev_id',
174 'LIMIT' => $this->batchSize
177 if ( !
$res->numRows() ) {
182 foreach (
$res as $row ) {
183 $startId = $row->rev_id;
186 echo
"Invalid DB:// URL in rev_id {$row->rev_id}\n";
189 if ( !in_array( $info[
'cluster'], $this->clusters ) ) {
190 echo
"Invalid cluster returned in SQL query: {$info['cluster']}\n";
194 'bt_page' => $row->rev_page,
195 'bt_rev_id' => $row->rev_id,
196 'bt_text_id' => $row->old_id,
197 'bt_cluster' => $info[
'cluster'],
198 'bt_blob_id' => $info[
'id'],
199 'bt_cgz_hash' => $info[
'hash']
201 if ( $this->doBlobOrphans ) {
202 gmp_setbit( $this->trackedBlobs[$info[
'cluster']], $info[
'id'] );
205 $dbw->insert(
'blob_tracking', $insertBatch, __METHOD__ );
206 $rowsInserted += count( $insertBatch );
209 if ( $batchesDone >= $this->reportingInterval ) {
211 echo
"$startId / $endId\n";
215 echo
"Found $rowsInserted revisions\n";
224 # Wait until the blob_tracking table is available in the replica DB
227 $pos = $dbw->getMasterPos();
228 $dbr->masterPosWait( $pos, 100000 );
232 $endId =
$dbr->selectField(
'text',
'MAX(old_id)',
false, __METHOD__ );
236 echo
"Finding orphan text...\n";
238 # Scan the text table for orphan text
240 $res =
$dbr->select( [
'text',
'blob_tracking' ],
241 [
'old_id',
'old_flags',
'old_text' ],
243 'old_id>' .
$dbr->addQuotes( $startId ),
245 'old_flags ' .
$dbr->buildLike(
$dbr->anyString(),
'external',
$dbr->anyString() ),
250 'ORDER BY' =>
'old_id',
251 'LIMIT' => $this->batchSize
253 [
'blob_tracking' => [
'LEFT JOIN',
'bt_text_id=old_id' ] ]
256 foreach (
$res as $row ) {
257 $ids[] = $row->old_id;
260 if ( !
$res->numRows() ) {
265 foreach (
$res as $row ) {
266 $startId = $row->old_id;
269 echo
"Invalid DB:// URL in old_id {$row->old_id}\n";
272 if ( !in_array( $info[
'cluster'], $this->clusters ) ) {
273 echo
"Invalid cluster returned in SQL query\n";
280 'bt_text_id' => $row->old_id,
281 'bt_cluster' => $info[
'cluster'],
282 'bt_blob_id' => $info[
'id'],
283 'bt_cgz_hash' => $info[
'hash']
285 if ( $this->doBlobOrphans ) {
286 gmp_setbit( $this->trackedBlobs[$info[
'cluster']], $info[
'id'] );
289 $dbw->insert(
'blob_tracking', $insertBatch, __METHOD__ );
291 $rowsInserted += count( $insertBatch );
293 if ( $batchesDone >= $this->reportingInterval ) {
295 echo
"$startId / $endId\n";
299 echo
"Found $rowsInserted orphan text rows\n";
310 if ( !extension_loaded(
'gmp' ) ) {
311 echo
"Can't find orphan blobs, need bitfield support provided by GMP.\n";
318 foreach ( $this->clusters
as $cluster ) {
319 echo
"Searching for orphan blobs in $cluster...\n";
324 if ( strpos(
$e->error,
'Unknown database' ) !==
false ) {
325 echo
"No database on $cluster\n";
327 echo
"Error on $cluster: " .
$e->getMessage() .
"\n";
331 $table = $extDB->getLBInfo(
'blobs table' );
332 if ( is_null( $table ) ) {
335 if ( !$extDB->tableExists( $table ) ) {
336 echo
"No blobs table on cluster $cluster\n";
341 $actualBlobs = gmp_init( 0 );
342 $endId = $extDB->selectField( $table,
'MAX(blob_id)',
false, __METHOD__ );
346 $res = $extDB->select( $table,
348 [
'blob_id > ' . $extDB->addQuotes( $startId ) ],
350 [
'LIMIT' => $this->batchSize,
'ORDER BY' =>
'blob_id' ]
353 if ( !
$res->numRows() ) {
357 foreach (
$res as $row ) {
358 gmp_setbit( $actualBlobs, $row->blob_id );
360 $startId = $row->blob_id;
363 if ( $batchesDone >= $this->reportingInterval ) {
365 echo
"$startId / $endId\n";
371 $orphans = gmp_and( $actualBlobs, gmp_com( $this->trackedBlobs[$cluster] ) );
378 $id = gmp_scan1( $orphans, $id );
383 'bo_cluster' => $cluster,
386 if ( count( $insertBatch ) > $this->batchSize ) {
387 $dbw->insert(
'blob_orphans', $insertBatch, __METHOD__ );
394 if ( $insertBatch ) {
395 $dbw->insert(
'blob_orphans', $insertBatch, __METHOD__ );
397 echo
"Found $numOrphans orphan(s) in $cluster\n";