23use InvalidArgumentException;
25use Wikimedia\Assert\Assert;
26use Wikimedia\AtEase\AtEase;
27use Wikimedia\Timestamp\ConvertibleTimestamp;
65 public const QUICK_VOLATILE = 1;
71 private const FILE_PREFIX =
'mw-GlobalIdGenerator';
74 private const CLOCK_TIME =
'time';
76 private const CLOCK_COUNTER =
'counter';
78 private const CLOCK_SEQUENCE =
'clkSeq';
80 private const CLOCK_OFFSET =
'offset';
82 private const CLOCK_OFFSET_COUNTER =
'offsetCounter';
91 __CLASS__ .
' with a BagOStuff instance was deprecated in MediaWiki 1.37.',
96 if ( !strlen( $tempDirectory ) ) {
97 throw new InvalidArgumentException(
"No temp directory provided" );
99 $this->tmpDir = $tempDirectory;
101 $fileSuffix = function_exists(
'getmyuid' ) ? getmyuid() :
'';
102 $this->uniqueFilePrefix = self::FILE_PREFIX . $fileSuffix;
103 $this->nodeIdFile = $tempDirectory .
'/' . $this->uniqueFilePrefix .
'-UID-nodeid';
106 $this->lockFile88 = $tempDirectory .
'/' . $this->uniqueFilePrefix .
'-UID-88';
107 $this->lockFile128 = $tempDirectory .
'/' . $this->uniqueFilePrefix .
'-UID-128';
108 $this->lockFileUUID = $tempDirectory .
'/' . $this->uniqueFilePrefix .
'-UUID-128';
129 Assert::parameter(
$base <= 36,
'$base',
'must be <= 36' );
130 Assert::parameter(
$base >= 2,
'$base',
'must be >= 2' );
133 $info[self::CLOCK_OFFSET_COUNTER] %= 1024;
144 $time = $info[self::CLOCK_TIME];
145 $counter = $info[self::CLOCK_OFFSET_COUNTER];
149 $id_bin .= str_pad( decbin( $counter ), 10,
'0', STR_PAD_LEFT );
151 $id_bin .= $this->getNodeId32();
153 if ( strlen( $id_bin ) !== 88 ) {
154 throw new RuntimeException(
"Detected overflow for millisecond timestamp." );
175 Assert::parameter(
$base <= 36,
'$base',
'must be <= 36' );
176 Assert::parameter(
$base >= 2,
'$base',
'must be >= 2' );
178 $info = $this->
getTimeAndDelay(
'lockFile128', 16384, 1048576, 1048576 );
179 $info[self::CLOCK_OFFSET_COUNTER] %= 1048576;
190 $time = $info[self::CLOCK_TIME];
191 $counter = $info[self::CLOCK_OFFSET_COUNTER];
192 $clkSeq = $info[self::CLOCK_SEQUENCE];
196 $id_bin .= str_pad( decbin( $counter ), 20,
'0', STR_PAD_LEFT );
198 $id_bin .= str_pad( decbin( $clkSeq ), 14,
'0', STR_PAD_LEFT );
200 $id_bin .= $this->getNodeId48();
202 if ( strlen( $id_bin ) !== 128 ) {
203 throw new RuntimeException(
"Detected overflow for millisecond timestamp." );
227 $clkSeq_bin = \Wikimedia\base_convert( $info[self::CLOCK_SEQUENCE], 10, 2, 14 );
229 $info[self::CLOCK_TIME],
230 $info[self::CLOCK_OFFSET_COUNTER]
233 $id_bin = substr( $time_bin, 28, 32 );
235 $id_bin .= substr( $time_bin, 12, 16 );
239 $id_bin .= substr( $time_bin, 0, 12 );
243 $id_bin .= substr( $clkSeq_bin, 0, 6 );
245 $id_bin .= substr( $clkSeq_bin, 6, 8 );
247 $id_bin .= $this->getNodeId48();
249 if ( strlen( $id_bin ) !== 128 ) {
250 throw new RuntimeException(
"Detected overflow for millisecond timestamp." );
252 $hex = \Wikimedia\base_convert( $id_bin, 2, 16, 32 );
253 return sprintf(
'%s-%s-%s-%s-%s',
255 substr( $hex, 0, 8 ),
257 substr( $hex, 8, 4 ),
259 substr( $hex, 12, 4 ),
261 substr( $hex, 16, 4 ),
263 substr( $hex, 20, 12 )
274 return str_replace(
'-',
'', $this->
newUUIDv1() );
284 $hex = bin2hex( random_bytes( 32 / 2 ) );
286 return sprintf(
'%s-%s-%s-%s-%s',
288 substr( $hex, 0, 8 ),
290 substr( $hex, 8, 4 ),
292 '4' . substr( $hex, 12, 3 ),
294 dechex( 0x8 | ( hexdec( $hex[15] ) & 0x3 ) ) . $hex[16] . substr( $hex, 17, 2 ),
296 substr( $hex, 19, 12 )
307 return str_replace(
'-',
'', $this->
newUUIDv4() );
350 '/^([0-9a-f]{8})-([0-9a-f]{4})-(1[0-9a-f]{3})-([89ab][0-9a-f]{3})-([0-9a-f]{12})$/',
354 throw new InvalidArgumentException(
"Invalid UUIDv1 {$uuid}" );
357 $timestamp = hexdec( substr( $components[3], 1 ) . $components[2] . $components[1] );
360 $unixTime = ( $timestamp - 0x01b21dd213814000 ) / 1e7;
362 return ConvertibleTimestamp::convert( $format, $unixTime );
380 if ( $bits < 16 || $bits > 48 ) {
381 throw new RuntimeException(
"Requested bit size ($bits) is out of range." );
384 $path = $this->tmpDir .
'/' . $this->uniqueFilePrefix .
'-' . rawurlencode( $bucket ) .
'-48';
386 if ( isset( $this->fileHandles[
$path] ) ) {
387 $handle = $this->fileHandles[
$path];
389 $handle = fopen(
$path,
'cb+' );
390 $this->fileHandles[
$path] = $handle ?:
null;
393 if ( $handle ===
false ) {
394 throw new RuntimeException(
"Could not open '{$path}'." );
396 if ( !flock( $handle, LOCK_EX ) ) {
398 throw new RuntimeException(
"Could not acquire '{$path}'." );
404 $counter = floor( (
float)trim( fgets( $handle ) ) ) + $count;
407 ftruncate( $handle, 0 );
412 fwrite( $handle, (
string)fmod( $counter, 2 ** 48 ) );
416 flock( $handle, LOCK_UN );
419 $divisor = 2 ** $bits;
422 $currentId = floor( $counter - $count );
423 for ( $i = 0; $i < $count; ++$i ) {
425 $ids[] = fmod( ++$currentId, $divisor );
448 protected function getTimeAndDelay( $lockFile, $clockSeqSize, $counterSize, $offsetSize ) {
450 if ( isset( $this->fileHandles[$this->$lockFile] ) ) {
451 $handle = $this->fileHandles[$this->$lockFile];
453 $handle = fopen( $this->$lockFile,
'cb+' );
454 $this->fileHandles[$this->$lockFile] = $handle ?:
null;
457 if ( $handle ===
false ) {
458 throw new RuntimeException(
"Could not open '{$this->$lockFile}'." );
460 if ( !flock( $handle, LOCK_EX ) ) {
462 throw new RuntimeException(
"Could not acquire '{$this->$lockFile}'." );
507 $msecCounterSize = $counterSize * 1000;
512 $data = explode(
' ', fgets( $handle ) );
514 if ( count( $data ) === 4 ) {
516 $clkSeq = (int)$data[0] % $clockSeqSize;
517 $prevSec = (int)$data[1];
520 $randOffset = (int)$data[3] % $counterSize;
527 if ( $sec ===
false ) {
531 $clkSeq = ( $clkSeq + 1 ) % $clockSeqSize;
533 $randOffset = mt_rand( 0, $offsetSize - 1 );
534 trigger_error(
"Clock was set back; sequence number incremented." );
535 } elseif ( $sec === $prevSec ) {
538 $msecCounter = (int)$data[2] % $msecCounterSize;
540 if ( ++$msecCounter >= $msecCounterSize ) {
542 flock( $handle, LOCK_UN );
543 throw new RuntimeException(
"Counter overflow for timestamp value." );
548 $clkSeq = mt_rand( 0, $clockSeqSize - 1 );
551 $randOffset = mt_rand( 0, $offsetSize - 1 );
555 ftruncate( $handle, 0 );
557 fwrite( $handle,
"{$clkSeq} {$sec} {$msecCounter} {$randOffset}" );
559 flock( $handle, LOCK_UN );
562 $msec = (int)( $msecCounter / 1000 );
563 $counter = $msecCounter % 1000;
566 self::CLOCK_TIME => [ $sec, $msec ],
567 self::CLOCK_COUNTER => $counter,
568 self::CLOCK_SEQUENCE => $clkSeq,
569 self::CLOCK_OFFSET => $randOffset,
570 self::CLOCK_OFFSET_COUNTER => $counter + $randOffset,
582 $start = microtime(
true );
586 if ( $ct >= $time ) {
591 }
while ( ( microtime(
true ) - $start ) <= 0.010 );
602 [ $sec, $msec ] = $time;
603 $ts = 1000 * $sec + $msec;
604 if ( $ts > 2 ** 52 ) {
605 throw new RuntimeException( __METHOD__ .
606 ': sorry, this function doesn\'t work after the year 144680' );
609 return substr( \
Wikimedia\base_convert( (
string)$ts, 10, 2, 46 ), -46 );
619 [ $sec, $msec ] = $time;
620 $offset =
'122192928000000000';
623 if ( PHP_INT_SIZE >= 8 ) {
624 $ts = ( 1000 * $sec + $msec ) * 10000 + (
int)$offset + $delta;
625 $id_bin = str_pad( decbin( $ts % ( 2 ** 60 ) ), 60,
'0', STR_PAD_LEFT );
626 } elseif ( extension_loaded(
'gmp' ) ) {
628 $ts = gmp_add( gmp_mul( (
string)$sec,
'1000' ), (
string)$msec );
630 $ts = gmp_add( gmp_mul( $ts,
'10000' ), $offset );
631 $ts = gmp_add( $ts, (
string)$delta );
633 $ts = gmp_mod( $ts, gmp_pow(
'2', 60 ) );
634 $id_bin = str_pad( gmp_strval( $ts, 2 ), 60,
'0', STR_PAD_LEFT );
635 } elseif ( extension_loaded(
'bcmath' ) ) {
637 $ts = bcadd( bcmul( $sec,
'1000' ), $msec );
639 $ts = bcadd( bcmul( $ts,
'10000' ), $offset );
640 $ts = bcadd( $ts, (
string)$delta );
642 $ts = bcmod( $ts, bcpow(
'2',
'60' ) );
643 $id_bin = \Wikimedia\base_convert( $ts, 10, 2, 60 );
645 throw new RuntimeException(
'bcmath or gmp extension required for 32 bit machines.' );
653 private function load() {
654 if ( $this->loaded ) {
658 $this->loaded =
true;
661 if ( is_file( $this->nodeIdFile ) ) {
662 $nodeId = file_get_contents( $this->nodeIdFile );
665 if ( !preg_match(
'/^[0-9a-f]{12}$/i', $nodeId ) ) {
666 AtEase::suppressWarnings();
667 if ( PHP_OS_FAMILY ===
'Windows' ) {
669 $csv = trim( ( $this->shellCallback )(
'getmac /NH /FO CSV' ) );
670 $line = substr( $csv, 0, strcspn( $csv,
"\n" ) );
671 $info = str_getcsv( $line );
673 $nodeId = isset( $info[0] ) ? str_replace(
'-',
'', $info[0] ) :
'';
674 } elseif ( is_executable(
'/sbin/ifconfig' ) ) {
678 preg_match(
'/\s([0-9a-f]{2}(?::[0-9a-f]{2}){5})\s/',
679 ( $this->shellCallback )(
'/sbin/ifconfig -a' ), $m );
680 $nodeId = isset( $m[1] ) ? str_replace(
':',
'', $m[1] ) :
'';
682 AtEase::restoreWarnings();
683 if ( !preg_match(
'/^[0-9a-f]{12}$/i', $nodeId ) ) {
684 $nodeId = bin2hex( random_bytes( 12 / 2 ) );
686 $nodeId[1] = dechex( hexdec( $nodeId[1] ) | 0x1 );
688 file_put_contents( $this->nodeIdFile, $nodeId );
690 $this->nodeId32 = \Wikimedia\base_convert( substr( sha1( $nodeId ), 0, 8 ), 16, 2, 32 );
691 $this->nodeId48 = \Wikimedia\base_convert( $nodeId, 16, 2, 48 );
697 private function getNodeId32() {
706 private function getNodeId48() {
723 private function deleteCacheFiles() {
724 foreach ( $this->fileHandles as
$path => $handle ) {
725 if ( $handle !==
null ) {
728 if ( is_file(
$path ) ) {
731 unset( $this->fileHandles[
$path] );
733 if ( is_file( $this->nodeIdFile ) ) {
734 unlink( $this->nodeIdFile );
751 $this->deleteCacheFiles();
756 array_map(
'fclose', array_filter( $this->fileHandles ) );