Merge "Clean up database access in non-deployed code"

This commit is contained in:
jenkins-bot 2023-03-08 16:50:17 +00:00 committed by Gerrit Code Review
commit 182e801a25
7 changed files with 31 additions and 34 deletions

View file

@ -13,7 +13,6 @@ require_once "$IP/maintenance/Maintenance.php";
use LoggedUpdateMaintenance;
use ManualLogEntry;
use MediaWiki\Extension\AbuseFilter\Special\SpecialAbuseFilter;
use MediaWiki\MediaWikiServices;
use User;
/**
@ -44,7 +43,7 @@ class AddMissingLoggingEntries extends LoggedUpdateMaintenance {
$dryRun = $this->hasOption( 'dry-run' );
$logParams = [];
$afhRows = [];
$db = wfGetDB( DB_REPLICA, 'vslow' );
$db = $this->getDB( DB_REPLICA, 'vslow' );
$logParamsConcat = $db->buildConcat( [ 'afh_id', $db->addQuotes( "\n" ) ] );
$legacyParamsLike = $db->buildLike( $logParamsConcat, $db->anyString() );
@ -79,16 +78,16 @@ class AddMissingLoggingEntries extends LoggedUpdateMaintenance {
return !$dryRun;
}
$logResult = wfGetDB( DB_REPLICA )->select(
$logResult = $this->getDB( DB_REPLICA )->selectFieldValues(
'logging',
[ 'log_params' ],
'log_params',
[ 'log_type' => 'abusefilter', 'log_params' => $logParams ],
__METHOD__
);
foreach ( $logResult as $row ) {
foreach ( $logResult as $params ) {
// id . "\n" . filter
$afhId = explode( "\n", $row->log_params, 2 )[0];
$afhId = explode( "\n", $params, 2 )[0];
// Forget this row had any issues - it just has a different timestamp in the log
unset( $afhRows[$afhId] );
}
@ -107,13 +106,12 @@ class AddMissingLoggingEntries extends LoggedUpdateMaintenance {
return false;
}
$dbw = wfGetDB( DB_PRIMARY );
$factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
$dbw = $this->getDB( DB_PRIMARY );
$count = 0;
foreach ( $afhRows as $row ) {
if ( $count % 100 === 0 ) {
$factory->waitForReplication();
$this->waitForReplication();
}
$user = User::newFromAnyId( $row->afh_user, $row->afh_user_text, null );

View file

@ -62,8 +62,8 @@ class FixOldLogEntries extends LoggedUpdateMaintenance {
* @return int[] The IDs of the affected rows
*/
private function deleteDuplicatedRows() {
$dbr = wfGetDB( DB_REPLICA, 'vslow' );
$dbw = wfGetDB( DB_PRIMARY );
$dbr = $this->getDB( DB_REPLICA, 'vslow' );
$dbw = $this->getDB( DB_PRIMARY );
$newFormatLike = $dbr->buildLike( $dbr->anyString(), 'historyId', $dbr->anyString() );
$batchSize = $this->getBatchSize();
$prevID = 0;
@ -136,8 +136,8 @@ class FixOldLogEntries extends LoggedUpdateMaintenance {
* @return int[] Affected log_id's
*/
private function changeNewlineType( array $deleted ) {
$dbr = wfGetDB( DB_REPLICA, 'vslow' );
$dbw = wfGetDB( DB_PRIMARY );
$dbr = $this->getDB( DB_REPLICA, 'vslow' );
$dbw = $this->getDB( DB_PRIMARY );
$batchSize = $this->getBatchSize();
$prevID = 1;
$curID = $batchSize;
@ -201,8 +201,8 @@ class FixOldLogEntries extends LoggedUpdateMaintenance {
* @return int[]
*/
private function updateLoggingFields( array $deleted ) {
$dbr = wfGetDB( DB_REPLICA, 'vslow' );
$dbw = wfGetDB( DB_PRIMARY );
$dbr = $this->getDB( DB_REPLICA, 'vslow' );
$dbw = $this->getDB( DB_PRIMARY );
$batchSize = $this->getBatchSize();
$prevID = 1;
$curID = $batchSize;
@ -252,7 +252,7 @@ class FixOldLogEntries extends LoggedUpdateMaintenance {
*/
public function doDBUpdates() {
$this->dryRun = $this->hasOption( 'dry-run' );
$this->loggingRowsCount = (int)wfGetDB( DB_REPLICA )->selectField(
$this->loggingRowsCount = (int)$this->getDB( DB_REPLICA )->selectField(
'logging',
'MAX(log_id)',
[],

View file

@ -4,6 +4,7 @@ namespace MediaWiki\Extension\AbuseFilter\Maintenance;
use LoggedUpdateMaintenance;
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
use Wikimedia\Rdbms\IMaintainableDatabase;
// @codeCoverageIgnoreStart
$IP = getenv( 'MW_INSTALL_PATH' );
@ -50,7 +51,7 @@ class NormalizeThrottleParameters extends LoggedUpdateMaintenance {
return 'NormalizeThrottleParameters';
}
/** @var \Wikimedia\Rdbms\Database The primary database */
/** @var IMaintainableDatabase The primary database */
private $dbw;
/**
@ -481,7 +482,7 @@ class NormalizeThrottleParameters extends LoggedUpdateMaintenance {
*/
public function doDBUpdates() {
$dryRun = $this->hasOption( 'dry-run' );
$this->dbw = wfGetDB( DB_PRIMARY );
$this->dbw = $this->getDB( DB_PRIMARY );
$this->beginTransaction( $this->dbw, __METHOD__ );
$normalized = $this->normalizeParameters();

View file

@ -11,7 +11,6 @@ require_once "$IP/maintenance/Maintenance.php";
// @codeCoverageIgnoreEnd
use Maintenance;
use MediaWiki\MediaWikiServices;
use MWTimestamp;
class PurgeOldLogIPData extends Maintenance {
@ -28,8 +27,7 @@ class PurgeOldLogIPData extends Maintenance {
*/
public function execute() {
$this->output( "Purging old data from abuse_filter_log...\n" );
$dbw = wfGetDB( DB_PRIMARY );
$factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
$dbw = $this->getDB( DB_PRIMARY );
$cutoffUnix = (int)MWTimestamp::now( TS_UNIX ) - $this->getConfig()->get( 'AbuseFilterLogIPMaxAge' );
$count = 0;
@ -55,7 +53,7 @@ class PurgeOldLogIPData extends Maintenance {
$count += $dbw->affectedRows();
$this->output( "$count\n" );
$factory->waitForReplication();
$this->waitForReplication();
}
} while ( count( $ids ) >= $this->getBatchSize() );

View file

@ -55,13 +55,13 @@ class SearchFilters extends Maintenance {
* @param string|false $dbname Name of database, or false if the wiki is not part of a wikifarm
*/
public function getMatchingFilters( $dbname = false ) {
$dbr = wfGetDB( DB_REPLICA, [], $dbname );
$dbr = $this->getDB( DB_REPLICA, [], $dbname );
$pattern = $dbr->addQuotes( $this->getOption( 'pattern' ) );
if ( $dbr->tableExists( 'abuse_filter' ) ) {
$rows = $dbr->select(
'abuse_filter',
'DATABASE() AS dbname, af_id',
[ 'dbname' => 'DATABASE()', 'af_id' ],
[
"af_pattern RLIKE $pattern"
]

View file

@ -12,7 +12,7 @@ use MediaWiki\Extension\AbuseFilter\Variables\VariablesBlobStore;
use MediaWiki\MediaWikiServices;
use Title;
use UnexpectedValueException;
use Wikimedia\Rdbms\Database;
use Wikimedia\Rdbms\IMaintainableDatabase;
use Wikimedia\Rdbms\IResultWrapper;
// @codeCoverageIgnoreStart
@ -34,9 +34,9 @@ require_once "$IP/maintenance/Maintenance.php";
* with serialized classes, which quickly becomes unsustainable.
*/
class UpdateVarDumps extends LoggedUpdateMaintenance {
/** @var Database A connection to replica */
/** @var IMaintainableDatabase A connection to replica */
private $dbr;
/** @var Database A connection to the primary database */
/** @var IMaintainableDatabase A connection to the primary database */
private $dbw;
/** @var bool Whether we're performing a dry run */
private $dryRun = false;
@ -98,8 +98,8 @@ class UpdateVarDumps extends LoggedUpdateMaintenance {
$this->varBlobStore = AbuseFilterServices::getVariablesBlobStore();
// Faulty rows aren't inserted anymore, hence we can query the replica and update the primary database.
$this->dbr = wfGetDB( DB_REPLICA );
$this->dbw = wfGetDB( DB_PRIMARY );
$this->dbr = $this->getDB( DB_REPLICA );
$this->dbw = $this->getDB( DB_PRIMARY );
// Control batching with the primary key to keep the queries performant and allow gaps
$this->allRowsCount = (int)$this->dbr->selectField(
@ -162,7 +162,7 @@ class UpdateVarDumps extends LoggedUpdateMaintenance {
$res = $this->doFixMissingDumps( $brokenRows );
$deleted += $res['deleted'];
$rebuilt += $res['rebuilt'];
MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
$this->waitForReplication();
$this->maybeSleep();
} while ( $prevID <= $this->allRowsCount );
@ -289,7 +289,7 @@ class UpdateVarDumps extends LoggedUpdateMaintenance {
$result = $this->doMoveToText( $res );
$changeRows += $result['change'];
$truncatedDumps += $result['truncated'];
MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
$this->waitForReplication();
$this->maybeSleep();
} while ( $prevID <= $this->allRowsCount );
@ -504,7 +504,7 @@ class UpdateVarDumps extends LoggedUpdateMaintenance {
if ( !$this->dryRun ) {
$this->doUpdateText( $res, $esAccess );
MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
$this->waitForReplication();
}
$this->maybeSleep();
} while ( $prevID <= $this->allRowsCount );
@ -683,7 +683,7 @@ class UpdateVarDumps extends LoggedUpdateMaintenance {
} else {
$this->dbw->update( ...$args );
$numRows += $this->dbw->affectedRows();
MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
$this->waitForReplication();
}
$prevID = $curID;

View file

@ -48,7 +48,7 @@ class FilterStoreTest extends MediaWikiIntegrationTestCase {
$filter = $this->getFilterFromSpecs( [ 'id' => $id ] + $row );
// Use some black magic to bypass checks
$filterStore = TestingAccessWrapper::newFromObject( AbuseFilterServices::getFilterStore() );
wfGetDB( DB_PRIMARY )->insert(
$this->db->insert(
'abuse_filter',
$filterStore->filterToDatabaseRow( $filter ),
__METHOD__