Migrate to IDatabase::newUpdateQueryBuilder

Change-Id: I0b3fd864e5227068114ca7aa9e98361046f393c1
This commit is contained in:
Umherirrender 2024-04-15 21:49:52 +02:00
parent f17f231a70
commit 3691d773d3
10 changed files with 106 additions and 76 deletions

View file

@ -108,21 +108,23 @@ class EditRevUpdater {
$logs = $this->logIds[ $key ];
if ( $logs[ 'local' ] ) {
$dbw = $this->lbFactory->getPrimaryDatabase();
$dbw->update( 'abuse_filter_log',
[ 'afl_rev_id' => $revisionRecord->getId() ],
[ 'afl_id' => $logs['local'] ],
__METHOD__
);
$dbw->newUpdateQueryBuilder()
->update( 'abuse_filter_log' )
->set( [ 'afl_rev_id' => $revisionRecord->getId() ] )
->where( [ 'afl_id' => $logs['local'] ] )
->caller( __METHOD__ )
->execute();
$ret = true;
}
if ( $logs[ 'global' ] ) {
$fdb = $this->centralDBManager->getConnection( DB_PRIMARY );
$fdb->update( 'abuse_filter_log',
[ 'afl_rev_id' => $revisionRecord->getId() ],
[ 'afl_id' => $logs['global'], 'afl_wiki' => $this->wikiID ],
__METHOD__
);
$fdb->newUpdateQueryBuilder()
->update( 'abuse_filter_log' )
->set( [ 'afl_rev_id' => $revisionRecord->getId() ] )
->where( [ 'afl_id' => $logs['global'], 'afl_wiki' => $this->wikiID ] )
->caller( __METHOD__ )
->execute();
$ret = true;
}
return $ret;

View file

@ -159,7 +159,12 @@ class FilterStore {
->execute();
$filterId = $dbw->insertId();
} else {
$dbw->update( 'abuse_filter', $rowForInsert, [ 'af_id' => $filterId ], __METHOD__ );
$dbw->newUpdateQueryBuilder()
->update( 'abuse_filter' )
->set( $rowForInsert )
->where( [ 'af_id' => $filterId ] )
->caller( __METHOD__ )
->execute();
}
$newRow['af_id'] = $filterId;

View file

@ -158,12 +158,12 @@ class HideAbuseLog extends AbuseFilterView {
return [ 'abusefilter-log-hide-no-change' ];
}
$dbw->update(
'abuse_filter_log',
[ 'afl_deleted' => $newValue ],
[ 'afl_id' => $actualIDs ],
__METHOD__
);
$dbw->newUpdateQueryBuilder()
->update( 'abuse_filter_log' )
->set( [ 'afl_deleted' => $newValue ] )
->where( [ 'afl_id' => $actualIDs ] )
->caller( __METHOD__ )
->execute();
// Log in a DeferredUpdates to avoid potential flood
DeferredUpdates::addCallableUpdate( function () use ( $fields, $actualIDs ) {

View file

@ -142,12 +142,12 @@ class EmergencyWatcher implements Watcher {
$this->lbFactory->getPrimaryDatabase(),
__METHOD__,
static function ( IDatabase $dbw, $fname ) use ( $throttleFilters ) {
$dbw->update(
'abuse_filter',
[ 'af_throttled' => 1 ],
[ 'af_id' => $throttleFilters ],
$fname
);
$dbw->newUpdateQueryBuilder()
->update( 'abuse_filter' )
->set( [ 'af_throttled' => 1 ] )
->where( [ 'af_id' => $throttleFilters ] )
->caller( $fname )
->execute();
}
)
);

View file

@ -53,11 +53,11 @@ class UpdateHitCountWatcher implements Watcher {
* @param array $loggedFilters
*/
private function updateHitCounts( IDatabase $dbw, array $loggedFilters ): void {
$dbw->update(
'abuse_filter',
[ 'af_hit_count=af_hit_count+1' ],
[ 'af_id' => $loggedFilters ],
__METHOD__
);
$dbw->newUpdateQueryBuilder()
->update( 'abuse_filter' )
->set( [ 'af_hit_count=af_hit_count+1' ] )
->where( [ 'af_id' => $loggedFilters ] )
->caller( __METHOD__ )
->execute();
}
}

View file

@ -273,16 +273,16 @@ class MigrateActorsAF extends LoggedUpdateMaintenance {
$countErrors++;
continue;
}
$dbw->update(
$table,
[
$dbw->newUpdateQueryBuilder()
->update( $table )
->set( [
$actorField => $row->actor_id,
],
array_intersect_key( (array)$row, $pkFilter ) + [
] )
->where( array_intersect_key( (array)$row, $pkFilter ) + [
$actorField => 0
],
__METHOD__
);
] )
->caller( __METHOD__ )
->execute();
$countUpdated += $dbw->affectedRows();
}

View file

@ -44,12 +44,12 @@ class PurgeOldLogIPData extends Maintenance {
);
if ( $ids ) {
$dbw->update(
'abuse_filter_log',
[ 'afl_ip' => '' ],
[ 'afl_id' => $ids ],
__METHOD__
);
$dbw->newUpdateQueryBuilder()
->update( 'abuse_filter_log' )
->set( [ 'afl_ip' => '' ] )
->where( [ 'afl_id' => $ids ] )
->caller( __METHOD__ )
->execute();
$count += $dbw->affectedRows();
$this->output( "$count\n" );

View file

@ -240,12 +240,12 @@ class UpdateVarDumps extends LoggedUpdateMaintenance {
}
$storedID = $this->varBlobStore->storeVarDump( $vars );
$this->dbw->update(
'abuse_filter_log',
[ 'afl_var_dump' => $storedID ],
[ 'afl_id' => $row->afl_id ],
__METHOD__
);
$this->dbw->newUpdateQueryBuilder()
->update( 'abuse_filter_log' )
->set( [ 'afl_var_dump' => $storedID ] )
->where( [ 'afl_id' => $row->afl_id ] )
->caller( __METHOD__ )
->execute();
}
$rebuilt = $brokenRows->numRows() - $deleted;
return [ 'rebuilt' => $rebuilt, 'deleted' => $deleted ];
@ -343,12 +343,12 @@ class UpdateVarDumps extends LoggedUpdateMaintenance {
$holder = is_array( $stored ) ? VariableHolder::newFromArray( $stored ) : $stored;
// Note: this will upgrade to the new JSON format, so we use tt:
$newDump = $this->varBlobStore->storeVarDump( $holder );
$this->dbw->update(
'abuse_filter_log',
[ 'afl_var_dump' => $newDump ],
[ 'afl_id' => $row->afl_id ],
__METHOD__
);
$this->dbw->newUpdateQueryBuilder()
->update( 'abuse_filter_log' )
->set( [ 'afl_var_dump' => $newDump ] )
->where( [ 'afl_id' => $row->afl_id ] )
->caller( __METHOD__ )
->execute();
}
}
return [ 'change' => $changeRows, 'truncated' => $truncatedDumps ];
@ -576,15 +576,15 @@ class UpdateVarDumps extends LoggedUpdateMaintenance {
$newFlags[] = 'external';
}
$this->dbw->update(
'text',
[
$this->dbw->newUpdateQueryBuilder()
->update( 'text' )
->set( [
'old_text' => $toStore,
'old_flags' => implode( ',', $newFlags )
],
[ 'old_id' => $row->old_id ],
__METHOD__
);
] )
->where( [ 'old_id' => $row->old_id ] )
->caller( __METHOD__ )
->execute();
}
if ( $this->printOrphanedFile !== null && $orphaned ) {
file_put_contents( $this->printOrphanedFile, implode( ', ', $orphaned ) . "\n", FILE_APPEND );
@ -670,21 +670,23 @@ class UpdateVarDumps extends LoggedUpdateMaintenance {
$numRows = 0;
do {
$this->maybePrintProgress( $prevID );
$args = [
'abuse_filter_log',
[ "afl_var_dump = $newIdSQL" ],
[
"afl_id > $prevID",
"afl_id <= $curID",
'afl_var_dump ' . $this->dbr->buildLike( 'stored-text:', $this->dbr->anyString() )
],
__METHOD__,
[ 'ORDER BY' => 'afl_id ASC' ]
$table = 'abuse_filter_log';
$var = "afl_var_dump = $newIdSQL";
$conds = [
"afl_id > $prevID",
"afl_id <= $curID",
'afl_var_dump ' . $this->dbr->buildLike( 'stored-text:', $this->dbr->anyString() )
];
$options = [ 'ORDER BY' => 'afl_id ASC' ];
if ( $this->dryRun ) {
$numRows += $this->dbr->selectRowCount( ...$args );
$numRows += $this->dbr->selectRowCount( $table, $var, $conds, __METHOD__, $options );
} else {
$this->dbw->update( ...$args );
$this->dbw->newUpdateQueryBuilder()
->update( $table )
->set( $var )
->where( $conds )
->caller( __METHOD__ )
->execute();
$numRows += $this->dbw->affectedRows();
$this->waitForReplication();
}

View file

@ -8,6 +8,7 @@ use MediaWikiIntegrationTestCase;
use Wikimedia\Rdbms\DBConnRef;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\LBFactory;
use Wikimedia\Rdbms\UpdateQueryBuilder;
/**
* @coversDefaultClass \MediaWiki\Extension\AbuseFilter\Watcher\UpdateHitCountWatcher
@ -29,6 +30,10 @@ class UpdateHitCountWatcherTest extends MediaWikiIntegrationTestCase {
[ 'af_hit_count=af_hit_count+1' ],
[ 'af_id' => $localFilters ]
);
$localDB->method( 'newUpdateQueryBuilder' )
->willReturnCallback( static function () use ( $localDB ) {
return new UpdateQueryBuilder( $localDB );
} );
$lb = $this->createMock( LBFactory::class );
$lb->method( 'getPrimaryDatabase' )->willReturn( $localDB );
@ -38,6 +43,10 @@ class UpdateHitCountWatcherTest extends MediaWikiIntegrationTestCase {
[ 'af_hit_count=af_hit_count+1' ],
[ 'af_id' => $globalFilters ]
);
$globalDB->method( 'newUpdateQueryBuilder' )
->willReturnCallback( static function () use ( $globalDB ) {
return new UpdateQueryBuilder( $globalDB );
} );
$centralDBManager = $this->createMock( CentralDBManager::class );
$centralDBManager->method( 'getConnection' )->willReturn( $globalDB );

View file

@ -14,6 +14,7 @@ use MediaWikiUnitTestCase;
use Wikimedia\Rdbms\DBConnRef;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\LBFactory;
use Wikimedia\Rdbms\UpdateQueryBuilder;
use WikiPage;
/**
@ -137,8 +138,16 @@ class EditRevUpdaterTest extends MediaWikiUnitTestCase {
[ $page, $rev ] = $this->getPageAndRev( $titleValue );
$localDB = $this->createMock( DBConnRef::class );
$localDB->expects( $ids['local'] ? $this->once() : $this->never() )->method( 'update' );
$localDB->expects( $ids['local'] ? $this->once() : $this->never() )->method( 'newUpdateQueryBuilder' )
->willReturnCallback( static function () use ( $localDB ) {
return new UpdateQueryBuilder( $localDB );
} );
$centralDB = $this->createMock( IDatabase::class );
$centralDB->expects( $ids['global'] ? $this->once() : $this->never() )->method( 'update' );
$centralDB->expects( $ids['global'] ? $this->once() : $this->never() )->method( 'newUpdateQueryBuilder' )
->willReturnCallback( static function () use ( $centralDB ) {
return new UpdateQueryBuilder( $centralDB );
} );
$updater = $this->getUpdater( $localDB, $centralDB );
$updater->setLastEditPage( $page );
$updater->setLogIdsForTarget( $titleValue, $ids );
@ -167,9 +176,12 @@ class EditRevUpdaterTest extends MediaWikiUnitTestCase {
$badIDs = [ 'local' => [], 'global' => [ 1, 2 ] ];
[ $page, $rev ] = $this->getPageAndRev( $goodTitleValue );
$localDB = $this->createMock( DBConnRef::class );
$localDB->expects( $this->once() )->method( 'update' );
$localDB->expects( $this->once() )->method( 'newUpdateQueryBuilder' )
->willReturnCallback( static function () use ( $localDB ) {
return new UpdateQueryBuilder( $localDB );
} );
$centralDB = $this->createMock( IDatabase::class );
$centralDB->expects( $this->never() )->method( 'update' );
$centralDB->expects( $this->never() )->method( 'newUpdateQueryBuilder' );
$updater = $this->getUpdater( $localDB, $centralDB );
$updater->setLastEditPage( $page );
$updater->setLogIdsForTarget( $goodTitleValue, $goodIDs );