From 20c9b6eb5929c5d57bb3562ec0f86ac7190de463 Mon Sep 17 00:00:00 2001 From: Amir Sarabadani Date: Mon, 11 Jun 2018 12:52:31 +0200 Subject: [PATCH] Introduce argument for insert callback in NameTableStore This will be useful in cases we need to define more data when inserting a new row Bug: T193868 Change-Id: Ib5c2da2ef951e0bc782847ff8bd4606681ee2196 --- includes/Storage/NameTableStore.php | 15 +++++++++++-- .../includes/Storage/NameTableStoreTest.php | 21 +++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/includes/Storage/NameTableStore.php b/includes/Storage/NameTableStore.php index ebce3da965..505ab4c321 100644 --- a/includes/Storage/NameTableStore.php +++ b/includes/Storage/NameTableStore.php @@ -60,6 +60,8 @@ class NameTableStore { private $nameField; /** @var null|callable */ private $normalizationCallback = null; + /** @var null|callable */ + private $insertCallback = null; /** * @param LoadBalancer $dbLoadBalancer A load balancer for acquiring database connections @@ -71,6 +73,8 @@ class NameTableStore { * @param callable $normalizationCallback Normalization to be applied to names before being * saved or queried. This should be a callback that accepts and returns a single string. * @param bool|string $wikiId The ID of the target wiki database. Use false for the local wiki. + * @param callable $insertCallback Callback to change insert fields accordingly. + * This parameter was introduced in 1.32 */ public function __construct( LoadBalancer $dbLoadBalancer, @@ -80,7 +84,8 @@ class NameTableStore { $idField, $nameField, callable $normalizationCallback = null, - $wikiId = false + $wikiId = false, + callable $insertCallback = null ) { $this->loadBalancer = $dbLoadBalancer; $this->cache = $cache; @@ -91,6 +96,7 @@ class NameTableStore { $this->normalizationCallback = $normalizationCallback; $this->wikiId = $wikiId; $this->cacheTTL = IExpiringStore::TTL_MONTH; + $this->insertCallback = $insertCallback; } /** @@ -346,9 +352,14 @@ class NameTableStore { $dbw = $this->getDBConnection( DB_MASTER ); + $insertFields = [ $this->nameField => $name ]; + if ( $this->insertCallback !== null ) { + $insertFields = call_user_func( $this->insertCallback, $insertFields ); + } + $dbw->insert( $this->table, - [ $this->nameField => $name ], + $insertFields, __METHOD__, [ 'IGNORE' ] ); diff --git a/tests/phpunit/includes/Storage/NameTableStoreTest.php b/tests/phpunit/includes/Storage/NameTableStoreTest.php index 0cd164b76f..e5104d8b28 100644 --- a/tests/phpunit/includes/Storage/NameTableStoreTest.php +++ b/tests/phpunit/includes/Storage/NameTableStoreTest.php @@ -83,14 +83,17 @@ class NameTableStoreTest extends MediaWikiTestCase { BagOStuff $cacheBag, $insertCalls, $selectCalls, - $normalizationCallback = null + $normalizationCallback = null, + $insertCallback = null ) { return new NameTableStore( $this->getMockLoadBalancer( $this->getCallCheckingDb( $insertCalls, $selectCalls ) ), $this->getHashWANObjectCache( $cacheBag ), new NullLogger(), 'slot_roles', 'role_id', 'role_name', - $normalizationCallback + $normalizationCallback, + false, + $insertCallback ); } @@ -295,4 +298,18 @@ class NameTableStoreTest extends MediaWikiTestCase { $this->assertSame( $barId, $store3->acquireId( 'bar' ) ); } + public function testGetAndAcquireIdInsertCallback() { + $store = $this->getNameTableSqlStore( + new EmptyBagOStuff(), + 1, + 1, + null, + function ( $insertFields ) { + $insertFields['role_id'] = 7251; + return $insertFields; + } + ); + $this->assertSame( 7251, $store->acquireId( 'A' ) ); + } + } -- 2.20.1