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 if ( function_exists(
'posix_geteuid' ) ) {
102 $fileSuffix = posix_geteuid();
103 } elseif ( function_exists(
'getmyuid' ) ) {
104 $fileSuffix = getmyuid();
108 $this->uniqueFilePrefix = self::FILE_PREFIX . $fileSuffix;
109 $this->nodeIdFile = $tempDirectory .
'/' . $this->uniqueFilePrefix .
'-UID-nodeid';
112 $this->lockFile88 = $tempDirectory .
'/' . $this->uniqueFilePrefix .
'-UID-88';
113 $this->lockFile128 = $tempDirectory .
'/' . $this->uniqueFilePrefix .
'-UID-128';
114 $this->lockFileUUID = $tempDirectory .
'/' . $this->uniqueFilePrefix .
'-UUID-128';
135 Assert::parameter( $base <= 36,
'$base',
'must be <= 36' );
136 Assert::parameter( $base >= 2,
'$base',
'must be >= 2' );
139 $info[self::CLOCK_OFFSET_COUNTER] %= 1024;
150 $time = $info[self::CLOCK_TIME];
151 $counter = $info[self::CLOCK_OFFSET_COUNTER];
155 $id_bin .= str_pad( decbin( $counter ), 10,
'0', STR_PAD_LEFT );
157 $id_bin .= $this->getNodeId32();
159 if ( strlen( $id_bin ) !== 88 ) {
160 throw new RuntimeException(
"Detected overflow for millisecond timestamp." );
181 Assert::parameter( $base <= 36,
'$base',
'must be <= 36' );
182 Assert::parameter( $base >= 2,
'$base',
'must be >= 2' );
184 $info = $this->
getTimeAndDelay(
'lockFile128', 16384, 1_048_576, 1_048_576 );
185 $info[self::CLOCK_OFFSET_COUNTER] %= 1_048_576;
196 $time = $info[self::CLOCK_TIME];
197 $counter = $info[self::CLOCK_OFFSET_COUNTER];
198 $clkSeq = $info[self::CLOCK_SEQUENCE];
202 $id_bin .= str_pad( decbin( $counter ), 20,
'0', STR_PAD_LEFT );
204 $id_bin .= str_pad( decbin( $clkSeq ), 14,
'0', STR_PAD_LEFT );
206 $id_bin .= $this->getNodeId48();
208 if ( strlen( $id_bin ) !== 128 ) {
209 throw new RuntimeException(
"Detected overflow for millisecond timestamp." );
233 $clkSeq_bin = \Wikimedia\base_convert( $info[self::CLOCK_SEQUENCE], 10, 2, 14 );
235 $info[self::CLOCK_TIME],
236 $info[self::CLOCK_OFFSET_COUNTER]
239 $id_bin = substr( $time_bin, 28, 32 );
241 $id_bin .= substr( $time_bin, 12, 16 );
245 $id_bin .= substr( $time_bin, 0, 12 );
249 $id_bin .= substr( $clkSeq_bin, 0, 6 );
251 $id_bin .= substr( $clkSeq_bin, 6, 8 );
253 $id_bin .= $this->getNodeId48();
255 if ( strlen( $id_bin ) !== 128 ) {
256 throw new RuntimeException(
"Detected overflow for millisecond timestamp." );
258 $hex = \Wikimedia\base_convert( $id_bin, 2, 16, 32 );
259 return sprintf(
'%s-%s-%s-%s-%s',
261 substr( $hex, 0, 8 ),
263 substr( $hex, 8, 4 ),
265 substr( $hex, 12, 4 ),
267 substr( $hex, 16, 4 ),
269 substr( $hex, 20, 12 )
280 return str_replace(
'-',
'', $this->
newUUIDv1() );
290 $hex = bin2hex( random_bytes( 32 / 2 ) );
292 return sprintf(
'%s-%s-%s-%s-%s',
294 substr( $hex, 0, 8 ),
296 substr( $hex, 8, 4 ),
298 '4' . substr( $hex, 12, 3 ),
300 dechex( 0x8 | ( hexdec( $hex[15] ) & 0x3 ) ) . $hex[16] . substr( $hex, 17, 2 ),
302 substr( $hex, 19, 12 )
313 return str_replace(
'-',
'', $this->
newUUIDv4() );
356 '/^([0-9a-f]{8})-([0-9a-f]{4})-(1[0-9a-f]{3})-([89ab][0-9a-f]{3})-([0-9a-f]{12})$/',
360 throw new InvalidArgumentException(
"Invalid UUIDv1 {$uuid}" );
363 $timestamp = hexdec( substr( $components[3], 1 ) . $components[2] . $components[1] );
366 $unixTime = ( $timestamp - 0x01b21dd213814000 ) / 1e7;
368 return ConvertibleTimestamp::convert( $format, $unixTime );
386 if ( $bits < 16 || $bits > 48 ) {
387 throw new RuntimeException(
"Requested bit size ($bits) is out of range." );
390 $path = $this->tmpDir .
'/' . $this->uniqueFilePrefix .
'-' . rawurlencode( $bucket ) .
'-48';
392 if ( isset( $this->fileHandles[
$path] ) ) {
393 $handle = $this->fileHandles[
$path];
395 $handle = fopen(
$path,
'cb+' );
396 $this->fileHandles[
$path] = $handle ?:
null;
399 if ( $handle ===
false ) {
400 throw new RuntimeException(
"Could not open '{$path}'." );
402 if ( !flock( $handle, LOCK_EX ) ) {
404 throw new RuntimeException(
"Could not acquire '{$path}'." );
410 $counter = floor( (
float)trim( fgets( $handle ) ) ) + $count;
413 ftruncate( $handle, 0 );
418 fwrite( $handle, (
string)fmod( $counter, 2 ** 48 ) );
422 flock( $handle, LOCK_UN );
425 $divisor = 2 ** $bits;
428 $currentId = floor( $counter - $count );
429 for ( $i = 0; $i < $count; ++$i ) {
431 $ids[] = fmod( ++$currentId, $divisor );
454 protected function getTimeAndDelay( $lockFile, $clockSeqSize, $counterSize, $offsetSize ) {
456 if ( isset( $this->fileHandles[$this->$lockFile] ) ) {
457 $handle = $this->fileHandles[$this->$lockFile];
459 $handle = fopen( $this->$lockFile,
'cb+' );
460 $this->fileHandles[$this->$lockFile] = $handle ?:
null;
463 if ( $handle ===
false ) {
464 throw new RuntimeException(
"Could not open '{$this->$lockFile}'." );
466 if ( !flock( $handle, LOCK_EX ) ) {
468 throw new RuntimeException(
"Could not acquire '{$this->$lockFile}'." );
513 $msecCounterSize = $counterSize * 1000;
518 $data = explode(
' ', fgets( $handle ) );
520 if ( count( $data ) === 4 ) {
522 $clkSeq = (int)$data[0] % $clockSeqSize;
523 $prevSec = (int)$data[1];
526 $randOffset = (int)$data[3] % $counterSize;
533 if ( $sec ===
false ) {
537 $clkSeq = ( $clkSeq + 1 ) % $clockSeqSize;
539 $randOffset = mt_rand( 0, $offsetSize - 1 );
540 trigger_error(
"Clock was set back; sequence number incremented." );
541 } elseif ( $sec === $prevSec ) {
544 $msecCounter = (int)$data[2] % $msecCounterSize;
546 if ( ++$msecCounter >= $msecCounterSize ) {
548 flock( $handle, LOCK_UN );
549 throw new RuntimeException(
"Counter overflow for timestamp value." );
554 $clkSeq = mt_rand( 0, $clockSeqSize - 1 );
557 $randOffset = mt_rand( 0, $offsetSize - 1 );
561 ftruncate( $handle, 0 );
563 fwrite( $handle,
"{$clkSeq} {$sec} {$msecCounter} {$randOffset}" );
565 flock( $handle, LOCK_UN );
568 $msec = (int)( $msecCounter / 1000 );
569 $counter = $msecCounter % 1000;
572 self::CLOCK_TIME => [ $sec, $msec ],
573 self::CLOCK_COUNTER => $counter,
574 self::CLOCK_SEQUENCE => $clkSeq,
575 self::CLOCK_OFFSET => $randOffset,
576 self::CLOCK_OFFSET_COUNTER => $counter + $randOffset,
588 $start = microtime(
true );
592 if ( $ct >= $time ) {
597 }
while ( ( microtime(
true ) - $start ) <= 0.010 );
608 [ $sec, $msec ] = $time;
609 $ts = 1000 * $sec + $msec;
610 if ( $ts > 2 ** 52 ) {
611 throw new RuntimeException( __METHOD__ .
612 ': sorry, this function doesn\'t work after the year 144680' );
615 return substr( \
Wikimedia\base_convert( (
string)$ts, 10, 2, 46 ), -46 );
625 [ $sec, $msec ] = $time;
626 $offset =
'122192928000000000';
629 if ( PHP_INT_SIZE >= 8 ) {
630 $ts = ( 1000 * $sec + $msec ) * 10000 + (
int)$offset + $delta;
631 $id_bin = str_pad( decbin( $ts % ( 2 ** 60 ) ), 60,
'0', STR_PAD_LEFT );
632 } elseif ( extension_loaded(
'gmp' ) ) {
634 $ts = gmp_add( gmp_mul( (
string)$sec,
'1000' ), (
string)$msec );
636 $ts = gmp_add( gmp_mul( $ts,
'10000' ), $offset );
637 $ts = gmp_add( $ts, (
string)$delta );
639 $ts = gmp_mod( $ts, gmp_pow(
'2', 60 ) );
640 $id_bin = str_pad( gmp_strval( $ts, 2 ), 60,
'0', STR_PAD_LEFT );
641 } elseif ( extension_loaded(
'bcmath' ) ) {
643 $ts = bcadd( bcmul( $sec,
'1000' ), $msec );
645 $ts = bcadd( bcmul( $ts,
'10000' ), $offset );
646 $ts = bcadd( $ts, (
string)$delta );
648 $ts = bcmod( $ts, bcpow(
'2',
'60' ) );
649 $id_bin = \Wikimedia\base_convert( $ts, 10, 2, 60 );
651 throw new RuntimeException(
'bcmath or gmp extension required for 32 bit machines.' );
659 private function load() {
660 if ( $this->loaded ) {
664 $this->loaded =
true;
667 $nodeId = @file_get_contents( $this->nodeIdFile ) ?:
'';
669 if ( !preg_match(
'/^[0-9a-f]{12}$/i', $nodeId ) ) {
670 AtEase::suppressWarnings();
671 if ( PHP_OS_FAMILY ===
'Windows' ) {
673 $csv = trim( ( $this->shellCallback )(
'getmac /NH /FO CSV' ) );
674 $line = substr( $csv, 0, strcspn( $csv,
"\n" ) );
675 $info = str_getcsv( $line );
677 $nodeId = isset( $info[0] ) ? str_replace(
'-',
'', $info[0] ) :
'';
678 } elseif ( is_executable(
'/sbin/ifconfig' ) ) {
682 preg_match(
'/\s([0-9a-f]{2}(?::[0-9a-f]{2}){5})\s/',
683 ( $this->shellCallback )(
'/sbin/ifconfig -a' ), $m );
684 $nodeId = isset( $m[1] ) ? str_replace(
':',
'', $m[1] ) :
'';
686 AtEase::restoreWarnings();
687 if ( !preg_match(
'/^[0-9a-f]{12}$/i', $nodeId ) ) {
688 $nodeId = bin2hex( random_bytes( 12 / 2 ) );
690 $nodeId[1] = dechex( hexdec( $nodeId[1] ) | 0x1 );
692 file_put_contents( $this->nodeIdFile, $nodeId );
694 $this->nodeId32 = \Wikimedia\base_convert( substr( sha1( $nodeId ), 0, 8 ), 16, 2, 32 );
695 $this->nodeId48 = \Wikimedia\base_convert( $nodeId, 16, 2, 48 );
701 private function getNodeId32() {
710 private function getNodeId48() {
727 private function deleteCacheFiles() {
728 foreach ( $this->fileHandles as
$path => $handle ) {
729 if ( $handle !==
null ) {
732 if ( is_file(
$path ) ) {
735 unset( $this->fileHandles[
$path] );
737 if ( is_file( $this->nodeIdFile ) ) {
738 unlink( $this->nodeIdFile );
755 $this->deleteCacheFiles();
760 array_map(
'fclose', array_filter( $this->fileHandles ) );