private $nameField;
/** @var null|callable */
private $normalizationCallback = null;
+ /** @var null|callable */
+ private $insertCallback = null;
/**
* @param LoadBalancer $dbLoadBalancer A load balancer for acquiring database connections
* @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,
$idField,
$nameField,
callable $normalizationCallback = null,
- $wikiId = false
+ $wikiId = false,
+ callable $insertCallback = null
) {
$this->loadBalancer = $dbLoadBalancer;
$this->cache = $cache;
$this->normalizationCallback = $normalizationCallback;
$this->wikiId = $wikiId;
$this->cacheTTL = IExpiringStore::TTL_MONTH;
+ $this->insertCallback = $insertCallback;
}
/**
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 )
+ );
}
/**
$dbw->insert(
$this->table,
- [ $this->nameField => $name ],
+ $this->getFieldsToStore( $name ),
__METHOD__,
[ 'IGNORE' ]
);
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;
+ }
+
}