wiki.techinc.nl/includes/user/TempUser/DBSerialProvider.php
Tim Starling e8dbf5f80c TempUser infrastructure and services
Add services and utilities for automatic creation of temporary user
accounts on page save, in order to avoid exposing the user's IP
address.

* Add $wgAutoCreateTempUser, for configuring the system
* Add TempUserConfig service, which interprets the config.
* Add TempUserCreator service, which creates users during page save as
  requested by EditPage. With proxy methods to TempUserConfig for
  convenience.
* Add table user_autocreate_serial. Table creation is necessary before
  the feature is enabled but is not necessary before deployment of this
  commit.

Bug: T300263
Change-Id: Ib14a352490fc42039106523118e8d021844e3dfb
2022-04-14 09:23:55 +10:00

65 lines
1.4 KiB
PHP

<?php
namespace MediaWiki\User\TempUser;
use Wikimedia\Rdbms\IDatabase;
/**
* Base class for serial acquisition code shared between core and CentralAuth.
*
* @since 1.39
*/
abstract class DBSerialProvider implements SerialProvider {
/** @var int */
private $numShards;
/**
* @param array $config
* - numShards (int, default 1): A small integer. This can be set to a
* value greater than 1 to avoid acquiring a global lock when
* allocating IDs, at the expense of making the IDs be non-monotonic.
*/
public function __construct( $config ) {
$this->numShards = $config['numShards'] ?? 1;
}
public function acquireIndex(): int {
if ( $this->numShards ) {
$shard = mt_rand( 0, $this->numShards - 1 );
} else {
$shard = 0;
}
$dbw = $this->getDB();
$table = $this->getTableName();
$dbw->startAtomic( __METHOD__ );
$dbw->upsert(
$table,
[
'uas_shard' => $shard,
'uas_value' => 1,
],
[ [ 'uas_shard' ] ],
[ 'uas_value=uas_value+1' ],
__METHOD__
);
$value = $dbw->newSelectQueryBuilder()
->select( 'uas_value' )
->from( $table )
->where( [ 'uas_shard' => $shard ] )
->caller( __METHOD__ )
->fetchField();
$dbw->endAtomic( __METHOD__ );
return $value * $this->numShards + $shard;
}
/**
* @return IDatabase
*/
abstract protected function getDB();
/**
* @return string
*/
abstract protected function getTableName();
}