MediaWiki master
DBSerialProvider.php
Go to the documentation of this file.
1<?php
2
4
7
13abstract class DBSerialProvider implements SerialProvider {
14 private readonly int $numShards;
15
22 public function __construct( array $config ) {
23 $this->numShards = $config['numShards'] ?? 1;
24 }
25
26 public function acquireIndex( int $year = 0 ): int {
27 if ( $this->numShards ) {
28 $shard = mt_rand( 0, $this->numShards - 1 );
29 } else {
30 $shard = 0;
31 }
32
33 $dbw = $this->getDB();
34 $table = $this->getTableName();
35 $dbw->startAtomic( __METHOD__ );
36 $dbw->newInsertQueryBuilder()
37 ->insertInto( $table )
38 ->row( [
39 'uas_shard' => $shard,
40 'uas_year' => $year,
41 'uas_value' => 1
42 ] )
43 ->onDuplicateKeyUpdate()
44 ->uniqueIndexFields( [ 'uas_shard', 'uas_year' ] )
45 ->set( [ 'uas_value' => new RawSQLValue( 'uas_value+1' ) ] )
46 ->caller( __METHOD__ )->execute();
47 $value = $dbw->newSelectQueryBuilder()
48 ->select( 'uas_value' )
49 ->from( $table )
50 ->where( [ 'uas_shard' => $shard ] )
51 ->andWhere( [ 'uas_year' => $year ] )
52 ->caller( __METHOD__ )
53 ->fetchField();
54 $dbw->endAtomic( __METHOD__ );
55 return $value * $this->numShards + $shard;
56 }
57
61 abstract protected function getDB();
62
66 abstract protected function getTableName();
67}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:71
Base class for serial acquisition code shared between core and CentralAuth.
acquireIndex(int $year=0)
Acquire an integer such that it is unlikely to be used again, and return it.
Raw SQL value to be used in query builders.
Interface for serial providers for temporary users.
Interface to a relational database.
Definition IDatabase.php:31