54 $this->nodeIdFile =
wfTempDir() .
'/mw-' . __CLASS__ .
'-UID-nodeid';
56 if (
is_file( $this->nodeIdFile ) ) {
60 if ( !
preg_match(
'/^[0-9a-f]{12}$/i', $nodeId ) ) {
71 preg_match(
'/\s([0-9a-f]{2}(:[0-9a-f]{2}){5})\s/',
76 if ( !
preg_match(
'/^[0-9a-f]{12}$/i', $nodeId ) ) {
86 $this->lockFile88 =
wfTempDir() .
'/mw-' . __CLASS__ .
'-UID-88';
87 $this->lockFile128 =
wfTempDir() .
'/mw-' . __CLASS__ .
'-UID-128';
88 $this->lockFileUUID =
wfTempDir() .
'/mw-' . __CLASS__ .
'-UUID-128';
96 if ( self::$instance ===
null ) {
97 self::$instance =
new self();
119 Assert::parameterType(
'integer',
$base,
'$base' );
120 Assert::parameter(
$base <= 36,
'$base',
'must be <= 36' );
121 Assert::parameter(
$base >= 2,
'$base',
'must be >= 2' );
124 $info = $gen->getTimeAndDelay(
'lockFile88', 1, 1024, 1024 );
125 $info[
'offsetCounter'] = $info[
'offsetCounter'] % 1024;
136 if (
isset( $info[
'time'] ) ) {
137 $time = $info[
'time'];
138 $counter = $info[
'offsetCounter'];
145 $id_bin .=
str_pad(
decbin( $counter ), 10,
'0', STR_PAD_LEFT );
149 if (
strlen( $id_bin ) !== 88 ) {
150 throw new RuntimeException(
"Detected overflow for millisecond timestamp." );
171 Assert::parameterType(
'integer',
$base,
'$base' );
172 Assert::parameter(
$base <= 36,
'$base',
'must be <= 36' );
173 Assert::parameter(
$base >= 2,
'$base',
'must be >= 2' );
176 $info = $gen->getTimeAndDelay(
'lockFile128', 16384, 1048576, 1048576 );
177 $info[
'offsetCounter'] = $info[
'offsetCounter'] % 1048576;
189 if (
isset( $info[
'time'] ) ) {
190 $time = $info[
'time'];
191 $counter = $info[
'offsetCounter'];
192 $clkSeq = $info[
'clkSeq'];
194 list(
$time, $counter, $clkSeq ) = $info;
199 $id_bin .=
str_pad(
decbin( $counter ), 20,
'0', STR_PAD_LEFT );
201 $id_bin .=
str_pad(
decbin( $clkSeq ), 14,
'0', STR_PAD_LEFT );
205 if (
strlen( $id_bin ) !== 128 ) {
206 throw new RuntimeException(
"Detected overflow for millisecond timestamp." );
224 return $gen->getUUIDv1( $gen->getTimeAndDelay(
'lockFileUUID', 16384, 5000, 5001 ) );
246 $id_bin =
substr( $time_bin, 28, 32 );
248 $id_bin .=
substr( $time_bin, 12, 16 );
252 $id_bin .=
substr( $time_bin, 0, 12 );
256 $id_bin .=
substr( $clkSeq_bin, 0, 6 );
258 $id_bin .=
substr( $clkSeq_bin, 6, 8 );
262 if (
strlen( $id_bin ) !== 128 ) {
263 throw new RuntimeException(
"Detected overflow for millisecond timestamp." );
266 return sprintf(
'%s-%s-%s-%s-%s',
292 return sprintf(
'%s-%s-%s-%s-%s',
298 '4' .
substr( $hex, 12, 3 ),
314 return str_replace(
'-',
'', self::newUUIDv4( $flags ) );
330 return current( self::newSequentialPerNodeIDs( $bucket, $bits, 1, $flags ) );
346 return $gen->getSequentialPerNodeIDs( $bucket, $bits, $count, $flags );
364 if ( $bits < 16 || $bits > 48 ) {
365 throw new RuntimeException(
"Requested bit size ($bits) is out of range." );
373 if ( ( $flags & self::QUICK_VOLATILE ) && !
wfIsCLI() ) {
374 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
377 $counter =
$cache->incrWithInit( $bucket, $cache::TTL_INDEFINITE, $count, $count );
378 if ( $counter ===
false ) {
379 throw new RuntimeException(
'Unable to set value to ' .
get_class(
$cache ) );
384 if ( $counter ===
null ) {
387 if (
isset( $this->fileHandles[$path] ) ) {
388 $handle = $this->fileHandles[
$path];
390 $handle =
fopen( $path,
'cb+' );
391 $this->fileHandles[
$path] = $handle ?:
null;
394 if ( $handle ===
false ) {
395 throw new RuntimeException(
"Could not open '{$path}'." );
397 if ( !
flock( $handle, LOCK_EX ) ) {
399 throw new RuntimeException(
"Could not acquire '{$path}'." );
410 flock( $handle, LOCK_UN );
414 $divisor = 2 ** $bits;
415 $currentId =
floor( $counter - $count );
416 for ( $i = 0; $i < $count; ++
$i ) {
417 $ids[] =
fmod( ++$currentId, $divisor );
440 protected function getTimeAndDelay( $lockFile, $clockSeqSize, $counterSize, $offsetSize ) {
442 if (
isset( $this->fileHandles[$lockFile] ) ) {
443 $handle = $this->fileHandles[$lockFile];
445 $handle =
fopen( $this->$lockFile,
'cb+' );
446 $this->fileHandles[$lockFile] = $handle ?:
null;
449 if ( $handle ===
false ) {
450 throw new RuntimeException(
"Could not open '{$this->$lockFile}'." );
452 if ( !
flock( $handle, LOCK_EX ) ) {
454 throw new RuntimeException(
"Could not acquire '{$this->$lockFile}'." );
499 $msecCounterSize = $counterSize * 1000;
506 if ( count(
$data ) === 4 ) {
508 $clkSeq = (
int)
$data[0] % $clockSeqSize;
512 $randOffset = (
int)
$data[3] % $counterSize;
519 if ( $sec ===
false ) {
523 $clkSeq = ( $clkSeq + 1 ) % $clockSeqSize;
525 $randOffset =
mt_rand( 0, $offsetSize - 1 );
526 trigger_error(
"Clock was set back; sequence number incremented." );
527 }
elseif ( $sec === $prevSec ) {
530 $msecCounter = (
int)
$data[2] % $msecCounterSize;
532 if ( ++$msecCounter >= $msecCounterSize ) {
534 flock( $handle, LOCK_UN );
535 throw new RuntimeException(
"Counter overflow for timestamp value." );
540 $clkSeq =
mt_rand( 0, $clockSeqSize - 1 );
543 $randOffset =
mt_rand( 0, $offsetSize - 1 );
549 fwrite( $handle,
"{$clkSeq} {$sec} {$msecCounter} {$randOffset}" );
551 flock( $handle, LOCK_UN );
554 $msec = (
int)( $msecCounter / 1000 );
555 $counter = $msecCounter % 1000;
559 'counter' => $counter,
578 if ( $ct >=
$time ) {
582 }
while ( (
microtime(
true ) - $start ) <= 0.010 );
594 $ts = 1000 * $sec +
$msec;
595 if ( $ts > 2 ** 52 ) {
596 throw new RuntimeException( __METHOD__ .
597 ': sorry, this function doesn\'t work after the year 144680' );
611 $offset =
'122192928000000000';
612 if ( PHP_INT_SIZE >= 8 ) {
613 $ts = ( 1000 * $sec +
$msec ) * 10000 + (
int)$offset +
$delta;
614 $id_bin =
str_pad(
decbin( $ts % ( 2 ** 60 ) ), 60,
'0', STR_PAD_LEFT );
618 $ts =
gmp_add( $ts, (
string)$delta );
624 $ts =
bcadd( $ts, $delta );
628 throw new RuntimeException(
'bcmath or gmp extension required for 32 bit machines.' );
647 foreach ( $this->fileHandles as $path => $handle ) {
648 if ( $handle !==
null ) {
654 unset( $this->fileHandles[$path] );
656 if (
is_file( $this->nodeIdFile ) ) {
657 unlink( $this->nodeIdFile );
677 $gen->deleteCacheFiles();
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
wfTempDir()
Tries to get the system directory for temporary files.
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
wfShellExec( $cmd, &$retval=null, $environ=[], $limits=[], $options=[])
Execute a shell command, with time and memory limits mirrored from the PHP configuration if supported...
wfIsWindows()
Check if the operating system is Windows.
wfIsCLI()
Check if we are running from the commandline.
static generateHex( $chars)
Generate a run of cryptographically random data and return it in hexadecimal string format.
Class for getting statistically unique IDs.
static newRawUUIDv1()
Return an RFC4122 compliant v1 UUID.
string $nodeIdFile
Local file path.
deleteCacheFiles()
Delete all cache files that have been created.
static newSequentialPerNodeID( $bucket, $bits=48, $flags=0)
Return an ID that is sequential only for this node and bucket.
array $fileHandles
Cached file handles.
millisecondsSinceEpochBinary(array $time)
getTimestampedID88(array $info)
string $lockFile128
Local file path.
static newSequentialPerNodeIDs( $bucket, $bits, $count, $flags=0)
Return IDs that are sequential only for this node and bucket.
string $nodeId32
Node ID in binary (32 bits)
getTimeAndDelay( $lockFile, $clockSeqSize, $counterSize, $offsetSize)
Get a (time,counter,clock sequence) where (time,counter) is higher than any previous (time,...
string $nodeId48
Node ID in binary (48 bits)
string $lockFile88
Local file path.
static UIDGenerator $instance
static newRawUUIDv4( $flags=0)
Return an RFC4122 compliant v4 UUID.
getSequentialPerNodeIDs( $bucket, $bits, $count, $flags)
Return IDs that are sequential only for this node and bucket.
timeWaitUntil( $time)
Wait till the current timestamp reaches $time and return the current timestamp.
getTimestampedID128(array $info)
intervalsSinceGregorianBinary(array $time, $delta=0)
static newUUIDv4( $flags=0)
Return an RFC4122 compliant v4 UUID.
string $lockFileUUID
Local file path.
static newUUIDv1()
Return an RFC4122 compliant v1 UUID.
static newTimestampedUID128( $base=10)
Get a statistically unique 128-bit unsigned integer ID string.
static unitTestTearDown()
Cleanup resources when tearing down after a unit test.
static newTimestampedUID88( $base=10)
Get a statistically unique 88-bit unsigned integer ID string.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
see documentation in includes Linker php for Linker::makeImageLink & $time
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))