MediaWiki REL1_37
MediaWiki\Storage\NameTableStore Class Reference
Collaboration diagram for MediaWiki\Storage\NameTableStore:

Public Member Functions

 __construct (ILoadBalancer $dbLoadBalancer, WANObjectCache $cache, LoggerInterface $logger, $table, $idField, $nameField, callable $normalizationCallback=null, $dbDomain=false, callable $insertCallback=null)
 
 acquireId (string $name)
 Acquire the id of the given name.
 
 getId (string $name)
 Get the id of the given name.
 
 getMap ()
 Get the whole table, in no particular order as a map of ids to names.
 
 getName (int $id)
 Get the name of the given id.
 
 reloadMap ( $connFlags=0)
 Reloads the name table from the primary database, and purges the WAN cache entry.
 

Private Member Functions

 getCacheKey ()
 Gets the cache key for names.
 
 getDBConnection ( $index, $flags=0)
 
 getFieldsToStore ( $name, $id=null)
 
 getTableFromCachesOrReplica ()
 
 loadTable (IDatabase $db)
 Gets the table from the db.
 
 normalizeName ( $name)
 
 retryStore (IDatabase $dbw, $name, $id)
 After the initial insertion got rolled back, this can be used to try the insertion again, and ensure a consistent state of the cache.
 
 store (string $name)
 Stores the given name in the DB, returning the ID when an insert occurs.
 

Private Attributes

WANObjectCache $cache
 
int $cacheTTL
 
bool string $domain
 
string $idField
 
null callable $insertCallback
 
ILoadBalancer $loadBalancer
 
LoggerInterface $logger
 
string $nameField
 
null callable $normalizationCallback
 
string $table
 
string[] $tableCache = null
 

Detailed Description

Author
Addshore
Since
1.31

Definition at line 36 of file NameTableStore.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\Storage\NameTableStore::__construct ( ILoadBalancer  $dbLoadBalancer,
WANObjectCache  $cache,
LoggerInterface  $logger,
  $table,
  $idField,
  $nameField,
callable  $normalizationCallback = null,
  $dbDomain = false,
callable  $insertCallback = null 
)
Parameters
ILoadBalancer$dbLoadBalancerA load balancer for acquiring database connections
WANObjectCache$cacheA cache manager for caching data. This can be the local wiki's default instance even if $dbDomain refers to a different wiki, since makeGlobalKey() is used to constructed a key that allows cached names from the same database to be re-used between wikis. For example, enwiki and frwiki will use the same cache keys for names from the wikidatawiki database, regardless of the cache's default key space.
LoggerInterface$logger
string$table
string$idField
string$nameField
callable | null$normalizationCallbackNormalization to be applied to names before being saved or queried. This should be a callback that accepts and returns a single string.
bool | string$dbDomainDatabase domain ID. Use false for the local database domain.
callable | null$insertCallbackCallback to change insert fields accordingly. This parameter was introduced in 1.32

Definition at line 85 of file NameTableStore.php.

References MediaWiki\Storage\NameTableStore\$cache, MediaWiki\Storage\NameTableStore\$idField, MediaWiki\Storage\NameTableStore\$insertCallback, MediaWiki\Storage\NameTableStore\$logger, MediaWiki\Storage\NameTableStore\$nameField, MediaWiki\Storage\NameTableStore\$normalizationCallback, and MediaWiki\Storage\NameTableStore\$table.

Member Function Documentation

◆ acquireId()

MediaWiki\Storage\NameTableStore::acquireId ( string  $name)

Acquire the id of the given name.

This creates a row in the table if it doesn't already exist.

Note
If called within an atomic section, there is a chance for the acquired ID to be lost on rollback. A best effort is made to re-insert the mapping in this case, and consistency of the cache with the database table is ensured by re-loading the map after a failed atomic section. However, there is no guarantee that an ID returned by this method is valid outside the transaction in which it was produced. This means that calling code should not retain the return value beyond the scope of a transaction, but rather call acquireId() again after the transaction is complete. In some rare cases, this may produce an ID different from the first call.
Parameters
string$name
Exceptions
NameTableAccessException
Returns
int

Definition at line 162 of file NameTableStore.php.

References MediaWiki\Storage\NameTableStore\$table, DB_PRIMARY, MediaWiki\Storage\NameTableStore\getCacheKey(), MediaWiki\Storage\NameTableStore\getDBConnection(), MediaWiki\Storage\NameTableStore\getTableFromCachesOrReplica(), MediaWiki\Storage\NameTableStore\normalizeName(), MediaWiki\Storage\NameTableStore\reloadMap(), and MediaWiki\Storage\NameTableStore\store().

◆ getCacheKey()

MediaWiki\Storage\NameTableStore::getCacheKey ( )
private

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.

Returns
string

Definition at line 126 of file NameTableStore.php.

Referenced by MediaWiki\Storage\NameTableStore\acquireId(), MediaWiki\Storage\NameTableStore\getName(), MediaWiki\Storage\NameTableStore\getTableFromCachesOrReplica(), and MediaWiki\Storage\NameTableStore\reloadMap().

◆ getDBConnection()

MediaWiki\Storage\NameTableStore::getDBConnection (   $index,
  $flags = 0 
)
private
Parameters
int$indexA database index, like DB_PRIMARY or DB_REPLICA
int$flagsDatabase connection flags
Returns
IDatabase

Definition at line 114 of file NameTableStore.php.

Referenced by MediaWiki\Storage\NameTableStore\acquireId(), MediaWiki\Storage\NameTableStore\getName(), MediaWiki\Storage\NameTableStore\getTableFromCachesOrReplica(), MediaWiki\Storage\NameTableStore\reloadMap(), and MediaWiki\Storage\NameTableStore\store().

◆ getFieldsToStore()

MediaWiki\Storage\NameTableStore::getFieldsToStore (   $name,
  $id = null 
)
private

◆ getId()

MediaWiki\Storage\NameTableStore::getId ( string  $name)

Get the id of the given name.

If the name doesn't exist this will throw. This should be used in cases where we believe the name already exists or want to check for existence.

Parameters
string$name
Exceptions
NameTableAccessExceptionThe name does not exist
Returns
int Id

Definition at line 251 of file NameTableStore.php.

References MediaWiki\Storage\NameTableStore\$table, MediaWiki\Storage\NameTableStore\getTableFromCachesOrReplica(), MediaWiki\Storage\NameTableAccessException\newFromDetails(), and MediaWiki\Storage\NameTableStore\normalizeName().

◆ getMap()

MediaWiki\Storage\NameTableStore::getMap ( )

Get the whole table, in no particular order as a map of ids to names.

This method could be subject to DB or cache lag.

Returns
string[] keys are the name ids, values are the names themselves Example: [ 1 => 'foo', 3 => 'bar' ]

Definition at line 333 of file NameTableStore.php.

References MediaWiki\Storage\NameTableStore\getTableFromCachesOrReplica().

◆ getName()

MediaWiki\Storage\NameTableStore::getName ( int  $id)

Get the name of the given id.

If the id doesn't exist this will throw. This should be used in cases where we believe the id already exists.

Note: Calls to this method will result in a primary DB select for non existing IDs.

Parameters
int$id
Exceptions
NameTableAccessExceptionThe id does not exist
Returns
string name

Definition at line 275 of file NameTableStore.php.

References $source, MediaWiki\Storage\NameTableStore\$table, DB_PRIMARY, DB_REPLICA, MediaWiki\Storage\NameTableStore\getCacheKey(), MediaWiki\Storage\NameTableStore\getDBConnection(), MediaWiki\Storage\NameTableStore\getTableFromCachesOrReplica(), MediaWiki\Storage\NameTableStore\loadTable(), and MediaWiki\Storage\NameTableAccessException\newFromDetails().

◆ getTableFromCachesOrReplica()

◆ loadTable()

MediaWiki\Storage\NameTableStore::loadTable ( IDatabase  $db)
private

Gets the table from the db.

Parameters
IDatabase$db
Returns
string[]

Definition at line 367 of file NameTableStore.php.

References Wikimedia\Rdbms\IDatabase\select().

Referenced by MediaWiki\Storage\NameTableStore\getName(), MediaWiki\Storage\NameTableStore\getTableFromCachesOrReplica(), and MediaWiki\Storage\NameTableStore\reloadMap().

◆ normalizeName()

MediaWiki\Storage\NameTableStore::normalizeName (   $name)
private
Parameters
string$name
Returns
string

Definition at line 138 of file NameTableStore.php.

Referenced by MediaWiki\Storage\NameTableStore\acquireId(), and MediaWiki\Storage\NameTableStore\getId().

◆ reloadMap()

MediaWiki\Storage\NameTableStore::reloadMap (   $connFlags = 0)

Reloads the name table from the primary database, and purges the WAN cache entry.

Note
This should only be called in situations where the local cache has been detected to be out of sync with the database. There should be no reason to call this method from outside the NameTableStore during normal operation. This method may however be useful in unit tests.
Parameters
int$connFlagsILoadBalancer::CONN_XXX flags. Optional.
Returns
string[] The freshly reloaded name map

Definition at line 225 of file NameTableStore.php.

References MediaWiki\Storage\NameTableStore\$tableCache, DB_PRIMARY, MediaWiki\Storage\NameTableStore\getCacheKey(), MediaWiki\Storage\NameTableStore\getDBConnection(), and MediaWiki\Storage\NameTableStore\loadTable().

Referenced by MediaWiki\Storage\NameTableStore\acquireId(), and MediaWiki\Storage\NameTableStore\retryStore().

◆ retryStore()

MediaWiki\Storage\NameTableStore::retryStore ( IDatabase  $dbw,
  $name,
  $id 
)
private

After the initial insertion got rolled back, this can be used to try the insertion again, and ensure a consistent state of the cache.

Parameters
IDatabase$dbw
string$name
int$id

Definition at line 446 of file NameTableStore.php.

References Wikimedia\Rdbms\IDatabase\doAtomicSection(), MediaWiki\Storage\NameTableStore\getFieldsToStore(), Wikimedia\Rdbms\IDatabase\insert(), Wikimedia\Rdbms\IDatabase\onAtomicSectionCancel(), and MediaWiki\Storage\NameTableStore\reloadMap().

Referenced by MediaWiki\Storage\NameTableStore\store().

◆ store()

MediaWiki\Storage\NameTableStore::store ( string  $name)
private

Stores the given name in the DB, returning the ID when an insert occurs.

Parameters
string$name
Returns
int|null int if we know the ID, null if we don't

Definition at line 393 of file NameTableStore.php.

References DB_PRIMARY, MediaWiki\Storage\NameTableStore\getDBConnection(), MediaWiki\Storage\NameTableStore\getFieldsToStore(), and MediaWiki\Storage\NameTableStore\retryStore().

Referenced by MediaWiki\Storage\NameTableStore\acquireId().

Member Data Documentation

◆ $cache

WANObjectCache MediaWiki\Storage\NameTableStore::$cache
private

Definition at line 42 of file NameTableStore.php.

Referenced by MediaWiki\Storage\NameTableStore\__construct().

◆ $cacheTTL

int MediaWiki\Storage\NameTableStore::$cacheTTL
private

Definition at line 54 of file NameTableStore.php.

◆ $domain

bool string MediaWiki\Storage\NameTableStore::$domain
private

Definition at line 51 of file NameTableStore.php.

◆ $idField

string MediaWiki\Storage\NameTableStore::$idField
private

◆ $insertCallback

null callable MediaWiki\Storage\NameTableStore::$insertCallback
private

Definition at line 65 of file NameTableStore.php.

Referenced by MediaWiki\Storage\NameTableStore\__construct().

◆ $loadBalancer

ILoadBalancer MediaWiki\Storage\NameTableStore::$loadBalancer
private

Definition at line 39 of file NameTableStore.php.

◆ $logger

LoggerInterface MediaWiki\Storage\NameTableStore::$logger
private

Definition at line 45 of file NameTableStore.php.

Referenced by MediaWiki\Storage\NameTableStore\__construct().

◆ $nameField

string MediaWiki\Storage\NameTableStore::$nameField
private

◆ $normalizationCallback

null callable MediaWiki\Storage\NameTableStore::$normalizationCallback
private

Definition at line 63 of file NameTableStore.php.

Referenced by MediaWiki\Storage\NameTableStore\__construct().

◆ $table

◆ $tableCache

string [] MediaWiki\Storage\NameTableStore::$tableCache = null
private

The documentation for this class was generated from the following file: