* @author Aaron Schulz
*/
use Wikimedia\Assert\Assert;
+use MediaWiki\MediaWikiServices;
/**
* Class for getting statistically unique IDs
protected $lockFileUUID; // string; local file path
/** @var array */
- protected $fileHandles = array(); // cache file handles
+ protected $fileHandles = []; // cache file handles
const QUICK_RAND = 1; // get randomness from fast and insecure sources
const QUICK_VOLATILE = 2; // use an APC like in-memory counter if available
$nodeId = isset( $info[0] ) ? str_replace( '-', '', $info[0] ) : '';
} elseif ( is_executable( '/sbin/ifconfig' ) ) { // Linux/BSD/Solaris/OS X
// See http://linux.die.net/man/8/ifconfig
- $m = array();
+ $m = [];
preg_match( '/\s([0-9a-f]{2}(:[0-9a-f]{2}){5})\s/',
wfShellExec( '/sbin/ifconfig -a' ), $m );
$nodeId = isset( $m[1] ) ? str_replace( ':', '', $m[1] ) : '';
$time = $info[0];
$counter = $info[1];
}
- // Take the 46 MSBs of "milliseconds since epoch"
+ // Take the 46 LSBs of "milliseconds since epoch"
$id_bin = $this->millisecondsSinceEpochBinary( $time );
// Add a 10 bit counter resulting in 56 bits total
$id_bin .= str_pad( decbin( $counter ), 10, '0', STR_PAD_LEFT );
$counter = $info[1];
$clkSeq = $info[2];
}
- // Take the 46 bits of "milliseconds since epoch"
+ // Take the 46 LSBs of "milliseconds since epoch"
$id_bin = $this->millisecondsSinceEpochBinary( $time );
// Add a 20 bit counter resulting in 66 bits total
$id_bin .= str_pad( decbin( $counter ), 20, '0', STR_PAD_LEFT );
* @see UIDGenerator::newSequentialPerNodeID()
* @param string $bucket Arbitrary bucket name (should be ASCII)
* @param int $bits Bit size (16 to 48) of resulting numbers before wrap-around
- * @param int $count Number of IDs to return (1 to 10000)
+ * @param int $count Number of IDs to return
* @param int $flags (supports UIDGenerator::QUICK_VOLATILE)
* @return array Ordered list of float integer values
* @since 1.23
* @see UIDGenerator::newSequentialPerNodeID()
* @param string $bucket Arbitrary bucket name (should be ASCII)
* @param int $bits Bit size (16 to 48) of resulting numbers before wrap-around
- * @param int $count Number of IDs to return (1 to 10000)
+ * @param int $count Number of IDs to return
* @param int $flags (supports UIDGenerator::QUICK_VOLATILE)
* @return array Ordered list of float integer values
* @throws RuntimeException
*/
protected function getSequentialPerNodeIDs( $bucket, $bits, $count, $flags ) {
if ( $count <= 0 ) {
- return array(); // nothing to do
- } elseif ( $count > 10000 ) {
- throw new RuntimeException( "Number of requested IDs ($count) is too high." );
+ return []; // nothing to do
} elseif ( $bits < 16 || $bits > 48 ) {
throw new RuntimeException( "Requested bit size ($bits) is out of range." );
}
// Counter values would not survive accross script instances in CLI mode.
$cache = null;
if ( ( $flags & self::QUICK_VOLATILE ) && PHP_SAPI !== 'cli' ) {
- $cache = ObjectCache::getLocalServerInstance();
+ $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
}
if ( $cache ) {
- $counter = $cache->incr( $bucket, $count );
+ $counter = $cache->incrWithInit( $bucket, $cache::TTL_INDEFINITE, $count, $count );
if ( $counter === false ) {
- if ( !$cache->add( $bucket, (int)$count ) ) {
- throw new RuntimeException( 'Unable to set value to ' . get_class( $cache ) );
- }
- $counter = $count;
+ throw new RuntimeException( 'Unable to set value to ' . get_class( $cache ) );
}
}
flock( $handle, LOCK_UN );
}
- $ids = array();
+ $ids = [];
$divisor = pow( 2, $bits );
$currentId = floor( $counter - $count ); // pre-increment counter value
for ( $i = 0; $i < $count; ++$i ) {
$clockChanged = false; // clock set back significantly?
if ( count( $data ) == 5 ) { // last UID info already initialized
$clkSeq = (int)$data[0] % $clockSeqSize;
- $prevTime = array( (int)$data[1], (int)$data[2] );
+ $prevTime = [ (int)$data[1], (int)$data[2] ];
$offset = (int)$data[4] % $counterSize; // random counter offset
$counter = 0; // counter for UIDs with the same timestamp
// Delay until the clock reaches the time of the last ID.
// Release the UID lock file
flock( $handle, LOCK_UN );
- return array(
+ return [
'time' => $time,
'counter' => $counter,
'clkSeq' => $clkSeq,
'offset' => $offset,
'offsetCounter' => $counter + $offset
- );
+ ];
}
/**
/**
* @param array $time Result of UIDGenerator::millitime()
- * @return string 46 MSBs of "milliseconds since epoch" in binary (rolls over in 4201)
+ * @return string 46 LSBs of "milliseconds since epoch" in binary (rolls over in 4201)
* @throws RuntimeException
*/
protected function millisecondsSinceEpochBinary( array $time ) {
$ts = ( 1000 * $sec + $msec ) * 10000 + (int)$offset + $delta;
$id_bin = str_pad( decbin( $ts % pow( 2, 60 ) ), 60, '0', STR_PAD_LEFT );
} elseif ( extension_loaded( 'gmp' ) ) {
- $ts = gmp_add( gmp_mul( (string) $sec, '1000' ), (string) $msec ); // ms
+ $ts = gmp_add( gmp_mul( (string)$sec, '1000' ), (string)$msec ); // ms
$ts = gmp_add( gmp_mul( $ts, '10000' ), $offset ); // 100ns intervals
- $ts = gmp_add( $ts, (string) $delta );
+ $ts = gmp_add( $ts, (string)$delta );
$ts = gmp_mod( $ts, gmp_pow( '2', '60' ) ); // wrap around
$id_bin = str_pad( gmp_strval( $ts, 2 ), 60, '0', STR_PAD_LEFT );
} elseif ( extension_loaded( 'bcmath' ) ) {
protected static function millitime() {
list( $msec, $sec ) = explode( ' ', microtime() );
- return array( (int)$sec, (int)( $msec * 1000 ) );
+ return [ (int)$sec, (int)( $msec * 1000 ) ];
}
/**