Merge "Web installer: Correctly escape U+00A0 NO-BREAK SPACE"
[lhc/web/wiklou.git] / includes / Storage / NameTableStore.php
index ebce3da..3516ffe 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
@@ -68,9 +70,11 @@ class NameTableStore {
         * @param string $table
         * @param string $idField
         * @param string $nameField
-        * @param callable $normalizationCallback Normalization to be applied to names before being
+        * @param callable|null $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|null $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;
        }
 
        /**
@@ -103,8 +109,20 @@ class NameTableStore {
                return $this->loadBalancer->getConnection( $index, [], $this->wikiId, $flags );
        }
 
+       /**
+        * Gets the cache key for names.
+        *
+        * The cache key is constructed based on the wiki ID passed to the constructor, and allows
+        * sharing of name tables cached for a specific database between wikis.
+        *
+        * @return string
+        */
        private function getCacheKey() {
-               return $this->cache->makeKey( 'NameTableSqlStore', $this->table, $this->wikiId );
+               return $this->cache->makeGlobalKey(
+                       'NameTableSqlStore',
+                       $this->table,
+                       $this->loadBalancer->resolveDomainID( $this->wikiId )
+               );
        }
 
        /**
@@ -348,7 +366,7 @@ class NameTableStore {
 
                $dbw->insert(
                        $this->table,
-                       [ $this->nameField => $name ],
+                       $this->getFieldsToStore( $name ),
                        __METHOD__,
                        [ 'IGNORE' ]
                );
@@ -363,4 +381,16 @@ class NameTableStore {
                return $dbw->insertId();
        }
 
+       /**
+        * @param string $name
+        * @return array
+        */
+       private function getFieldsToStore( $name ) {
+               $fields = [ $this->nameField => $name ];
+               if ( $this->insertCallback !== null ) {
+                       $fields = call_user_func( $this->insertCallback, $fields );
+               }
+               return $fields;
+       }
+
 }