| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace Wikimedia\UUID; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
use RuntimeException; |
| 25 |
use Wikimedia\Assert\Assert; |
| 26 |
use Wikimedia\AtEase\AtEase; |
| 27 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class GlobalIdGenerator { |
| 35 |
|
| 36 |
protected $shellCallback; |
| 37 |
|
| 38 |
|
| 39 |
protected $tmpDir; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
protected $uniqueFilePrefix; |
| 45 |
|
| 46 |
protected $nodeIdFile; |
| 47 |
|
| 48 |
protected $nodeId32; |
| 49 |
|
| 50 |
protected $nodeId48; |
| 51 |
|
| 52 |
|
| 53 |
protected $loaded = false; |
| 54 |
|
| 55 |
protected $lockFile88; |
| 56 |
|
| 57 |
protected $lockFile128; |
| 58 |
|
| 59 |
protected $lockFileUUID; |
| 60 |
|
| 61 |
|
| 62 |
protected $fileHandles = []; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
private const FILE_PREFIX = 'mw-GlobalIdGenerator'; |
| 69 |
|
| 70 |
|
| 71 |
private const CLOCK_TIME = 'time'; |
| 72 |
|
| 73 |
private const CLOCK_COUNTER = 'counter'; |
| 74 |
|
| 75 |
private const CLOCK_SEQUENCE = 'clkSeq'; |
| 76 |
|
| 77 |
private const CLOCK_OFFSET = 'offset'; |
| 78 |
|
| 79 |
private const CLOCK_OFFSET_COUNTER = 'offsetCounter'; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function __construct( $tempDirectory, $shellCallback ) { |
| 86 |
if ( func_num_args() >= 3 && !is_callable( $shellCallback ) ) { |
| 87 |
trigger_error( |
| 88 |
__CLASS__ . ' with a BagOStuff instance was deprecated in MediaWiki 1.37.', |
| 89 |
E_USER_DEPRECATED |
| 90 |
); |
| 91 |
$shellCallback = func_get_arg( 2 ); |
| 92 |
} |
| 93 |
if ( !strlen( $tempDirectory ) ) { |
| 94 |
throw new InvalidArgumentException( "No temp directory provided" ); |
| 95 |
} |
| 96 |
$this->tmpDir = $tempDirectory; |
| 97 |
|
| 98 |
if ( function_exists( 'posix_geteuid' ) ) { |
| 99 |
$fileSuffix = posix_geteuid(); |
| 100 |
} elseif ( function_exists( 'getmyuid' ) ) { |
| 101 |
$fileSuffix = getmyuid(); |
| 102 |
} else { |
| 103 |
$fileSuffix = ''; |
| 104 |
} |
| 105 |
$this->uniqueFilePrefix = self::FILE_PREFIX . $fileSuffix; |
| 106 |
$this->nodeIdFile = $tempDirectory . '/' . $this->uniqueFilePrefix . '-UID-nodeid'; |
| 107 |
|
| 108 |
|
| 109 |
$this->lockFile88 = $tempDirectory . '/' . $this->uniqueFilePrefix . '-UID-88'; |
| 110 |
$this->lockFile128 = $tempDirectory . '/' . $this->uniqueFilePrefix . '-UID-128'; |
| 111 |
$this->lockFileUUID = $tempDirectory . '/' . $this->uniqueFilePrefix . '-UUID-128'; |
| 112 |
|
| 113 |
$this->shellCallback = $shellCallback; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function newTimestampedUID88( int $base = 10 ) { |
| 132 |
Assert::parameter( $base <= 36, '$base', 'must be <= 36' ); |
| 133 |
Assert::parameter( $base >= 2, '$base', 'must be >= 2' ); |
| 134 |
|
| 135 |
$info = $this->getTimeAndDelay( 'lockFile88', 1, 1024, 1024 ); |
| 136 |
$info[self::CLOCK_OFFSET_COUNTER] %= 1024; |
| 137 |
|
| 138 |
return \Wikimedia\base_convert( $this->getTimestampedID88( $info ), 2, $base ); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
protected function getTimestampedID88( array $info ) { |
| 147 |
$time = $info[self::CLOCK_TIME]; |
| 148 |
$counter = $info[self::CLOCK_OFFSET_COUNTER]; |
| 149 |
|
| 150 |
$id_bin = $this->millisecondsSinceEpochBinary( $time ); |
| 151 |
|
| 152 |
$id_bin .= str_pad( decbin( $counter ), 10, '0', STR_PAD_LEFT ); |
| 153 |
|
| 154 |
$id_bin .= $this->getNodeId32(); |
| 155 |
|
| 156 |
if ( strlen( $id_bin ) !== 88 ) { |
| 157 |
throw new RuntimeException( "Detected overflow for millisecond timestamp." ); |
| 158 |
} |
| 159 |
|
| 160 |
return $id_bin; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public function newTimestampedUID128( int $base = 10 ) { |
| 178 |
Assert::parameter( $base <= 36, '$base', 'must be <= 36' ); |
| 179 |
Assert::parameter( $base >= 2, '$base', 'must be >= 2' ); |
| 180 |
|
| 181 |
$info = $this->getTimeAndDelay( 'lockFile128', 16384, 1_048_576, 1_048_576 ); |
| 182 |
$info[self::CLOCK_OFFSET_COUNTER] %= 1_048_576; |
| 183 |
|
| 184 |
return \Wikimedia\base_convert( $this->getTimestampedID128( $info ), 2, $base ); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
protected function getTimestampedID128( array $info ) { |
| 193 |
$time = $info[self::CLOCK_TIME]; |
| 194 |
$counter = $info[self::CLOCK_OFFSET_COUNTER]; |
| 195 |
$clkSeq = $info[self::CLOCK_SEQUENCE]; |
| 196 |
|
| 197 |
$id_bin = $this->millisecondsSinceEpochBinary( $time ); |
| 198 |
|
| 199 |
$id_bin .= str_pad( decbin( $counter ), 20, '0', STR_PAD_LEFT ); |
| 200 |
|
| 201 |
$id_bin .= str_pad( decbin( $clkSeq ), 14, '0', STR_PAD_LEFT ); |
| 202 |
|
| 203 |
$id_bin .= $this->getNodeId48(); |
| 204 |
|
| 205 |
if ( strlen( $id_bin ) !== 128 ) { |
| 206 |
throw new RuntimeException( "Detected overflow for millisecond timestamp." ); |
| 207 |
} |
| 208 |
|
| 209 |
return $id_bin; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public function newUUIDv1() { |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
return $this->getUUIDv1( $this->getTimeAndDelay( 'lockFileUUID', 16384, 5000, 5001 ) ); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
protected function getUUIDv1( array $info ) { |
| 230 |
$clkSeq_bin = \Wikimedia\base_convert( $info[self::CLOCK_SEQUENCE], 10, 2, 14 ); |
| 231 |
$time_bin = $this->intervalsSinceGregorianBinary( |
| 232 |
$info[self::CLOCK_TIME], |
| 233 |
$info[self::CLOCK_OFFSET_COUNTER] |
| 234 |
); |
| 235 |
|
| 236 |
$id_bin = substr( $time_bin, 28, 32 ); |
| 237 |
|
| 238 |
$id_bin .= substr( $time_bin, 12, 16 ); |
| 239 |
|
| 240 |
$id_bin .= '0001'; |
| 241 |
|
| 242 |
$id_bin .= substr( $time_bin, 0, 12 ); |
| 243 |
|
| 244 |
$id_bin .= '10'; |
| 245 |
|
| 246 |
$id_bin .= substr( $clkSeq_bin, 0, 6 ); |
| 247 |
|
| 248 |
$id_bin .= substr( $clkSeq_bin, 6, 8 ); |
| 249 |
|
| 250 |
$id_bin .= $this->getNodeId48(); |
| 251 |
|
| 252 |
if ( strlen( $id_bin ) !== 128 ) { |
| 253 |
throw new RuntimeException( "Detected overflow for millisecond timestamp." ); |
| 254 |
} |
| 255 |
$hex = \Wikimedia\base_convert( $id_bin, 2, 16, 32 ); |
| 256 |
return sprintf( '%s-%s-%s-%s-%s', |
| 257 |
|
| 258 |
substr( $hex, 0, 8 ), |
| 259 |
|
| 260 |
substr( $hex, 8, 4 ), |
| 261 |
|
| 262 |
substr( $hex, 12, 4 ), |
| 263 |
|
| 264 |
substr( $hex, 16, 4 ), |
| 265 |
|
| 266 |
substr( $hex, 20, 12 ) |
| 267 |
); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public function newRawUUIDv1() { |
| 277 |
return str_replace( '-', '', $this->newUUIDv1() ); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
public function newUUIDv4() { |
| 287 |
$hex = bin2hex( random_bytes( 32 / 2 ) ); |
| 288 |
|
| 289 |
return sprintf( '%s-%s-%s-%s-%s', |
| 290 |
|
| 291 |
substr( $hex, 0, 8 ), |
| 292 |
|
| 293 |
substr( $hex, 8, 4 ), |
| 294 |
|
| 295 |
'4' . substr( $hex, 12, 3 ), |
| 296 |
|
| 297 |
dechex( 0x8 | ( hexdec( $hex[15] ) & 0x3 ) ) . $hex[16] . substr( $hex, 17, 2 ), |
| 298 |
|
| 299 |
substr( $hex, 19, 12 ) |
| 300 |
); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
public function newRawUUIDv4() { |
| 310 |
return str_replace( '-', '', $this->newUUIDv4() ); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public function newSequentialPerNodeID( $bucket, $bits = 48 ) { |
| 323 |
return current( $this->newSequentialPerNodeIDs( $bucket, $bits, 1 ) ); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
public function newSequentialPerNodeIDs( $bucket, $bits, $count ) { |
| 336 |
return $this->getSequentialPerNodeIDs( $bucket, $bits, $count ); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
public function getTimestampFromUUIDv1( string $uuid, int $format = TS_MW ) { |
| 347 |
$components = []; |
| 348 |
if ( !preg_match( |
| 349 |
'/^([0-9a-f]{8})-([0-9a-f]{4})-(1[0-9a-f]{3})-([89ab][0-9a-f]{3})-([0-9a-f]{12})$/', |
| 350 |
$uuid, |
| 351 |
$components |
| 352 |
) ) { |
| 353 |
throw new InvalidArgumentException( "Invalid UUIDv1 {$uuid}" ); |
| 354 |
} |
| 355 |
|
| 356 |
$timestamp = hexdec( substr( $components[3], 1 ) . $components[2] . $components[1] ); |
| 357 |
|
| 358 |
|
| 359 |
$unixTime = ( $timestamp - 0x01b21dd213814000 ) / 1e7; |
| 360 |
|
| 361 |
return ConvertibleTimestamp::convert( $format, $unixTime ); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
protected function getSequentialPerNodeIDs( $bucket, $bits, $count ) { |
| 375 |
if ( $count <= 0 ) { |
| 376 |
return []; |
| 377 |
} |
| 378 |
if ( $bits < 16 || $bits > 48 ) { |
| 379 |
throw new RuntimeException( "Requested bit size ($bits) is out of range." ); |
| 380 |
} |
| 381 |
|
| 382 |
$path = $this->tmpDir . '/' . $this->uniqueFilePrefix . '-' . rawurlencode( $bucket ) . '-48'; |
| 383 |
|
| 384 |
if ( isset( $this->fileHandles[$path] ) ) { |
| 385 |
$handle = $this->fileHandles[$path]; |
| 386 |
} else { |
| 387 |
$handle = fopen( $path, 'cb+' ); |
| 388 |
$this->fileHandles[$path] = $handle ?: null; |
| 389 |
} |
| 390 |
|
| 391 |
if ( $handle === false ) { |
| 392 |
throw new RuntimeException( "Could not open '{$path}'." ); |
| 393 |
} |
| 394 |
if ( !flock( $handle, LOCK_EX ) ) { |
| 395 |
fclose( $handle ); |
| 396 |
throw new RuntimeException( "Could not acquire '{$path}'." ); |
| 397 |
} |
| 398 |
|
| 399 |
rewind( $handle ); |
| 400 |
|
| 401 |
|
| 402 |
$counter = floor( (float)trim( fgets( $handle ) ) ) + $count; |
| 403 |
|
| 404 |
|
| 405 |
ftruncate( $handle, 0 ); |
| 406 |
rewind( $handle ); |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
fwrite( $handle, (string)fmod( $counter, 2 ** 48 ) ); |
| 411 |
fflush( $handle ); |
| 412 |
|
| 413 |
|
| 414 |
flock( $handle, LOCK_UN ); |
| 415 |
|
| 416 |
$ids = []; |
| 417 |
$divisor = 2 ** $bits; |
| 418 |
|
| 419 |
|
| 420 |
$currentId = floor( $counter - $count ); |
| 421 |
for ( $i = 0; $i < $count; ++$i ) { |
| 422 |
|
| 423 |
$ids[] = fmod( ++$currentId, $divisor ); |
| 424 |
} |
| 425 |
|
| 426 |
return $ids; |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
protected function getTimeAndDelay( $lockFile, $clockSeqSize, $counterSize, $offsetSize ) { |
| 447 |
|
| 448 |
if ( isset( $this->fileHandles[$this->$lockFile] ) ) { |
| 449 |
$handle = $this->fileHandles[$this->$lockFile]; |
| 450 |
} else { |
| 451 |
$handle = fopen( $this->$lockFile, 'cb+' ); |
| 452 |
$this->fileHandles[$this->$lockFile] = $handle ?: null; |
| 453 |
} |
| 454 |
|
| 455 |
if ( $handle === false ) { |
| 456 |
throw new RuntimeException( "Could not open '{$this->$lockFile}'." ); |
| 457 |
} |
| 458 |
if ( !flock( $handle, LOCK_EX ) ) { |
| 459 |
fclose( $handle ); |
| 460 |
throw new RuntimeException( "Could not acquire '{$this->$lockFile}'." ); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
$msecCounterSize = $counterSize * 1000; |
| 506 |
|
| 507 |
rewind( $handle ); |
| 508 |
|
| 509 |
|
| 510 |
$data = explode( ' ', fgets( $handle ) ); |
| 511 |
|
| 512 |
if ( count( $data ) === 4 ) { |
| 513 |
|
| 514 |
$clkSeq = (int)$data[0] % $clockSeqSize; |
| 515 |
$prevSec = (int)$data[1]; |
| 516 |
$prevMsecCounter = (int)$data[2] % $msecCounterSize; |
| 517 |
$randOffset = (int)$data[3] % $counterSize; |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
$sec = $this->timeWaitUntil( $prevSec ); |
| 522 |
if ( $sec === false ) { |
| 523 |
|
| 524 |
|
| 525 |
$clkSeq = ( $clkSeq + 1 ) % $clockSeqSize; |
| 526 |
$sec = time(); |
| 527 |
$msecCounter = 0; |
| 528 |
$randOffset = random_int( 0, $offsetSize - 1 ); |
| 529 |
trigger_error( "Clock was set back; sequence number incremented." ); |
| 530 |
} elseif ( $sec === $prevSec ) { |
| 531 |
|
| 532 |
$msecCounter = $prevMsecCounter + 1; |
| 533 |
if ( $msecCounter >= $msecCounterSize ) { |
| 534 |
|
| 535 |
flock( $handle, LOCK_UN ); |
| 536 |
throw new RuntimeException( "Counter overflow for timestamp value." ); |
| 537 |
} |
| 538 |
} else { |
| 539 |
|
| 540 |
$msecCounter = 0; |
| 541 |
} |
| 542 |
} else { |
| 543 |
|
| 544 |
$clkSeq = random_int( 0, $clockSeqSize - 1 ); |
| 545 |
$sec = time(); |
| 546 |
$msecCounter = 0; |
| 547 |
$randOffset = random_int( 0, $offsetSize - 1 ); |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
ftruncate( $handle, 0 ); |
| 552 |
rewind( $handle ); |
| 553 |
fwrite( $handle, "{$clkSeq} {$sec} {$msecCounter} {$randOffset}" ); |
| 554 |
fflush( $handle ); |
| 555 |
flock( $handle, LOCK_UN ); |
| 556 |
|
| 557 |
|
| 558 |
$msec = (int)( $msecCounter / 1000 ); |
| 559 |
$counter = $msecCounter % 1000; |
| 560 |
|
| 561 |
return [ |
| 562 |
self::CLOCK_TIME => [ $sec, $msec ], |
| 563 |
self::CLOCK_COUNTER => $counter, |
| 564 |
self::CLOCK_SEQUENCE => $clkSeq, |
| 565 |
self::CLOCK_OFFSET => $randOffset, |
| 566 |
self::CLOCK_OFFSET_COUNTER => $counter + $randOffset, |
| 567 |
]; |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
protected function timeWaitUntil( $time ) { |
| 578 |
$start = microtime( true ); |
| 579 |
do { |
| 580 |
$ct = time(); |
| 581 |
|
| 582 |
if ( $ct >= $time ) { |
| 583 |
|
| 584 |
return $ct; |
| 585 |
} |
| 586 |
|
| 587 |
} while ( ( microtime( true ) - $start ) <= 0.010 ); |
| 588 |
|
| 589 |
return false; |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
protected function millisecondsSinceEpochBinary( array $time ) { |
| 598 |
[ $sec, $msec ] = $time; |
| 599 |
$ts = 1000 * $sec + $msec; |
| 600 |
if ( $ts > 2 ** 52 ) { |
| 601 |
throw new RuntimeException( __METHOD__ . |
| 602 |
': sorry, this function doesn\'t work after the year 144680' ); |
| 603 |
} |
| 604 |
|
| 605 |
return substr( \Wikimedia\base_convert( (string)$ts, 10, 2, 46 ), -46 ); |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
protected function intervalsSinceGregorianBinary( array $time, $delta = 0 ) { |
| 615 |
[ $sec, $msec ] = $time; |
| 616 |
$offset = '122192928000000000'; |
| 617 |
|
| 618 |
|
| 619 |
if ( PHP_INT_SIZE >= 8 ) { |
| 620 |
$ts = ( 1000 * $sec + $msec ) * 10000 + (int)$offset + $delta; |
| 621 |
$id_bin = str_pad( decbin( $ts % ( 2 ** 60 ) ), 60, '0', STR_PAD_LEFT ); |
| 622 |
} elseif ( extension_loaded( 'gmp' ) ) { |
| 623 |
|
| 624 |
$ts = gmp_add( gmp_mul( (string)$sec, '1000' ), (string)$msec ); |
| 625 |
|
| 626 |
$ts = gmp_add( gmp_mul( $ts, '10000' ), $offset ); |
| 627 |
$ts = gmp_add( $ts, (string)$delta ); |
| 628 |
|
| 629 |
$ts = gmp_mod( $ts, gmp_pow( '2', 60 ) ); |
| 630 |
$id_bin = str_pad( gmp_strval( $ts, 2 ), 60, '0', STR_PAD_LEFT ); |
| 631 |
} elseif ( extension_loaded( 'bcmath' ) ) { |
| 632 |
|
| 633 |
$ts = bcadd( bcmul( $sec, '1000' ), $msec ); |
| 634 |
|
| 635 |
$ts = bcadd( bcmul( $ts, '10000' ), $offset ); |
| 636 |
$ts = bcadd( $ts, (string)$delta ); |
| 637 |
|
| 638 |
$ts = bcmod( $ts, bcpow( '2', '60' ) ); |
| 639 |
$id_bin = \Wikimedia\base_convert( $ts, 10, 2, 60 ); |
| 640 |
} else { |
| 641 |
throw new RuntimeException( 'bcmath or gmp extension required for 32 bit machines.' ); |
| 642 |
} |
| 643 |
return $id_bin; |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
private function load() { |
| 650 |
if ( $this->loaded ) { |
| 651 |
return; |
| 652 |
} |
| 653 |
|
| 654 |
$this->loaded = true; |
| 655 |
|
| 656 |
|
| 657 |
$nodeId = @file_get_contents( $this->nodeIdFile ) ?: ''; |
| 658 |
|
| 659 |
if ( !preg_match( '/^[0-9a-f]{12}$/i', $nodeId ) ) { |
| 660 |
AtEase::suppressWarnings(); |
| 661 |
if ( PHP_OS_FAMILY === 'Windows' ) { |
| 662 |
|
| 663 |
$csv = trim( ( $this->shellCallback )( 'getmac /NH /FO CSV' ) ); |
| 664 |
$line = substr( $csv, 0, strcspn( $csv, "\n" ) ); |
| 665 |
$info = str_getcsv( $line, ",", "\"", "\\" ); |
| 666 |
|
| 667 |
$nodeId = isset( $info[0] ) ? str_replace( '-', '', $info[0] ) : ''; |
| 668 |
} elseif ( is_executable( '/sbin/ifconfig' ) ) { |
| 669 |
|
| 670 |
|
| 671 |
$m = []; |
| 672 |
preg_match( '/\s([0-9a-f]{2}(?::[0-9a-f]{2}){5})\s/', |
| 673 |
( $this->shellCallback )( '/sbin/ifconfig -a' ), $m ); |
| 674 |
$nodeId = isset( $m[1] ) ? str_replace( ':', '', $m[1] ) : ''; |
| 675 |
} |
| 676 |
AtEase::restoreWarnings(); |
| 677 |
if ( !preg_match( '/^[0-9a-f]{12}$/i', $nodeId ) ) { |
| 678 |
$nodeId = bin2hex( random_bytes( 12 / 2 ) ); |
| 679 |
|
| 680 |
$nodeId[1] = dechex( hexdec( $nodeId[1] ) | 0x1 ); |
| 681 |
} |
| 682 |
file_put_contents( $this->nodeIdFile, $nodeId ); |
| 683 |
} |
| 684 |
$this->nodeId32 = \Wikimedia\base_convert( substr( sha1( $nodeId ), 0, 8 ), 16, 2, 32 ); |
| 685 |
$this->nodeId48 = \Wikimedia\base_convert( $nodeId, 16, 2, 48 ); |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
private function getNodeId32() { |
| 692 |
$this->load(); |
| 693 |
|
| 694 |
return $this->nodeId32; |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
private function getNodeId48() { |
| 701 |
$this->load(); |
| 702 |
|
| 703 |
return $this->nodeId48; |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
private function deleteCacheFiles() { |
| 718 |
foreach ( $this->fileHandles as $path => $handle ) { |
| 719 |
if ( $handle !== null ) { |
| 720 |
fclose( $handle ); |
| 721 |
} |
| 722 |
if ( is_file( $path ) ) { |
| 723 |
unlink( $path ); |
| 724 |
} |
| 725 |
unset( $this->fileHandles[$path] ); |
| 726 |
} |
| 727 |
if ( is_file( $this->nodeIdFile ) ) { |
| 728 |
unlink( $this->nodeIdFile ); |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
public function unitTestTearDown() { |
| 745 |
$this->deleteCacheFiles(); |
| 746 |
} |
| 747 |
|
| 748 |
public function __destruct() { |
| 749 |
|
| 750 |
array_map( 'fclose', array_filter( $this->fileHandles ) ); |
| 751 |
} |
| 752 |
} |