Introduce argument for insert callback in NameTableStore
authorAmir Sarabadani <ladsgroup@gmail.com>
Mon, 11 Jun 2018 10:52:31 +0000 (12:52 +0200)
committerAddshore <addshorewiki@gmail.com>
Thu, 14 Jun 2018 15:51:16 +0000 (15:51 +0000)
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
tests/phpunit/includes/Storage/NameTableStoreTest.php

index ebce3da..505ab4c 100644 (file)
@@ -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' ]
                );
index 0cd164b..e5104d8 100644 (file)
@@ -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' ) );
+       }
+
 }