Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / utils / UIDGenerator.php
blobc6d1a54dd242f13a811b890a3be661d48771811e
1 <?php
2 /**
3 * This file deals with UID generation.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @author Aaron Schulz
23 use Wikimedia\Assert\Assert;
24 use MediaWiki\MediaWikiServices;
26 /**
27 * Class for getting statistically unique IDs
29 * @since 1.21
31 class UIDGenerator {
32 /** @var UIDGenerator */
33 protected static $instance = null;
35 protected $nodeIdFile; // string; local file path
36 protected $nodeId32; // string; node ID in binary (32 bits)
37 protected $nodeId48; // string; node ID in binary (48 bits)
39 protected $lockFile88; // string; local file path
40 protected $lockFile128; // string; local file path
41 protected $lockFileUUID; // string; local file path
43 /** @var array */
44 protected $fileHandles = []; // cache file handles
46 const QUICK_RAND = 1; // get randomness from fast and insecure sources
47 const QUICK_VOLATILE = 2; // use an APC like in-memory counter if available
49 protected function __construct() {
50 $this->nodeIdFile = wfTempDir() . '/mw-' . __CLASS__ . '-UID-nodeid';
51 $nodeId = '';
52 if ( is_file( $this->nodeIdFile ) ) {
53 $nodeId = file_get_contents( $this->nodeIdFile );
55 // Try to get some ID that uniquely identifies this machine (RFC 4122)...
56 if ( !preg_match( '/^[0-9a-f]{12}$/i', $nodeId ) ) {
57 MediaWiki\suppressWarnings();
58 if ( wfIsWindows() ) {
59 // https://technet.microsoft.com/en-us/library/bb490913.aspx
60 $csv = trim( wfShellExec( 'getmac /NH /FO CSV' ) );
61 $line = substr( $csv, 0, strcspn( $csv, "\n" ) );
62 $info = str_getcsv( $line );
63 $nodeId = isset( $info[0] ) ? str_replace( '-', '', $info[0] ) : '';
64 } elseif ( is_executable( '/sbin/ifconfig' ) ) { // Linux/BSD/Solaris/OS X
65 // See https://linux.die.net/man/8/ifconfig
66 $m = [];
67 preg_match( '/\s([0-9a-f]{2}(:[0-9a-f]{2}){5})\s/',
68 wfShellExec( '/sbin/ifconfig -a' ), $m );
69 $nodeId = isset( $m[1] ) ? str_replace( ':', '', $m[1] ) : '';
71 MediaWiki\restoreWarnings();
72 if ( !preg_match( '/^[0-9a-f]{12}$/i', $nodeId ) ) {
73 $nodeId = MWCryptRand::generateHex( 12, true );
74 $nodeId[1] = dechex( hexdec( $nodeId[1] ) | 0x1 ); // set multicast bit
76 file_put_contents( $this->nodeIdFile, $nodeId ); // cache
78 $this->nodeId32 = Wikimedia\base_convert( substr( sha1( $nodeId ), 0, 8 ), 16, 2, 32 );
79 $this->nodeId48 = Wikimedia\base_convert( $nodeId, 16, 2, 48 );
80 // If different processes run as different users, they may have different temp dirs.
81 // This is dealt with by initializing the clock sequence number and counters randomly.
82 $this->lockFile88 = wfTempDir() . '/mw-' . __CLASS__ . '-UID-88';
83 $this->lockFile128 = wfTempDir() . '/mw-' . __CLASS__ . '-UID-128';
84 $this->lockFileUUID = wfTempDir() . '/mw-' . __CLASS__ . '-UUID-128';
87 /**
88 * @todo: move to MW-specific factory class and inject temp dir
89 * @return UIDGenerator
91 protected static function singleton() {
92 if ( self::$instance === null ) {
93 self::$instance = new self();
96 return self::$instance;
99 /**
100 * Get a statistically unique 88-bit unsigned integer ID string.
101 * The bits of the UID are prefixed with the time (down to the millisecond).
103 * These IDs are suitable as values for the shard key of distributed data.
104 * If a column uses these as values, it should be declared UNIQUE to handle collisions.
105 * New rows almost always have higher UIDs, which makes B-TREE updates on INSERT fast.
106 * They can also be stored "DECIMAL(27) UNSIGNED" or BINARY(11) in MySQL.
108 * UID generation is serialized on each server (as the node ID is for the whole machine).
110 * @param int $base Specifies a base other than 10
111 * @return string Number
112 * @throws RuntimeException
114 public static function newTimestampedUID88( $base = 10 ) {
115 Assert::parameterType( 'integer', $base, '$base' );
116 Assert::parameter( $base <= 36, '$base', 'must be <= 36' );
117 Assert::parameter( $base >= 2, '$base', 'must be >= 2' );
119 $gen = self::singleton();
120 $info = $gen->getTimeAndDelay( 'lockFile88', 1, 1024, 1024 );
121 $info['offsetCounter'] = $info['offsetCounter'] % 1024;
122 return Wikimedia\base_convert( $gen->getTimestampedID88( $info ), 2, $base );
126 * @param array $info The result of UIDGenerator::getTimeAndDelay() or
127 * a plain (UIDGenerator::millitime(), counter, clock sequence) array.
128 * @return string 88 bits
129 * @throws RuntimeException
131 protected function getTimestampedID88( array $info ) {
132 if ( isset( $info['time'] ) ) {
133 $time = $info['time'];
134 $counter = $info['offsetCounter'];
135 } else {
136 $time = $info[0];
137 $counter = $info[1];
139 // Take the 46 LSBs of "milliseconds since epoch"
140 $id_bin = $this->millisecondsSinceEpochBinary( $time );
141 // Add a 10 bit counter resulting in 56 bits total
142 $id_bin .= str_pad( decbin( $counter ), 10, '0', STR_PAD_LEFT );
143 // Add the 32 bit node ID resulting in 88 bits total
144 $id_bin .= $this->nodeId32;
145 // Convert to a 1-27 digit integer string
146 if ( strlen( $id_bin ) !== 88 ) {
147 throw new RuntimeException( "Detected overflow for millisecond timestamp." );
150 return $id_bin;
154 * Get a statistically unique 128-bit unsigned integer ID string.
155 * The bits of the UID are prefixed with the time (down to the millisecond).
157 * These IDs are suitable as globally unique IDs, without any enforced uniqueness.
158 * New rows almost always have higher UIDs, which makes B-TREE updates on INSERT fast.
159 * They can also be stored as "DECIMAL(39) UNSIGNED" or BINARY(16) in MySQL.
161 * UID generation is serialized on each server (as the node ID is for the whole machine).
163 * @param int $base Specifies a base other than 10
164 * @return string Number
165 * @throws RuntimeException
167 public static function newTimestampedUID128( $base = 10 ) {
168 Assert::parameterType( 'integer', $base, '$base' );
169 Assert::parameter( $base <= 36, '$base', 'must be <= 36' );
170 Assert::parameter( $base >= 2, '$base', 'must be >= 2' );
172 $gen = self::singleton();
173 $info = $gen->getTimeAndDelay( 'lockFile128', 16384, 1048576, 1048576 );
174 $info['offsetCounter'] = $info['offsetCounter'] % 1048576;
176 return Wikimedia\base_convert( $gen->getTimestampedID128( $info ), 2, $base );
180 * @param array $info The result of UIDGenerator::getTimeAndDelay() or
181 * a plain (UIDGenerator::millitime(), counter, clock sequence) array.
182 * @return string 128 bits
183 * @throws RuntimeException
185 protected function getTimestampedID128( array $info ) {
186 if ( isset( $info['time'] ) ) {
187 $time = $info['time'];
188 $counter = $info['offsetCounter'];
189 $clkSeq = $info['clkSeq'];
190 } else {
191 $time = $info[0];
192 $counter = $info[1];
193 $clkSeq = $info[2];
195 // Take the 46 LSBs of "milliseconds since epoch"
196 $id_bin = $this->millisecondsSinceEpochBinary( $time );
197 // Add a 20 bit counter resulting in 66 bits total
198 $id_bin .= str_pad( decbin( $counter ), 20, '0', STR_PAD_LEFT );
199 // Add a 14 bit clock sequence number resulting in 80 bits total
200 $id_bin .= str_pad( decbin( $clkSeq ), 14, '0', STR_PAD_LEFT );
201 // Add the 48 bit node ID resulting in 128 bits total
202 $id_bin .= $this->nodeId48;
203 // Convert to a 1-39 digit integer string
204 if ( strlen( $id_bin ) !== 128 ) {
205 throw new RuntimeException( "Detected overflow for millisecond timestamp." );
208 return $id_bin;
212 * Return an RFC4122 compliant v1 UUID
214 * @return string
215 * @throws RuntimeException
216 * @since 1.27
218 public static function newUUIDv1() {
219 $gen = self::singleton();
220 // There can be up to 10000 intervals for the same millisecond timestamp.
221 // [0,4999] counter + [0,5000] offset is in [0,9999] for the offset counter.
222 // Add this onto the timestamp to allow making up to 5000 IDs per second.
223 return $gen->getUUIDv1( $gen->getTimeAndDelay( 'lockFileUUID', 16384, 5000, 5001 ) );
227 * Return an RFC4122 compliant v1 UUID
229 * @return string 32 hex characters with no hyphens
230 * @throws RuntimeException
231 * @since 1.27
233 public static function newRawUUIDv1() {
234 return str_replace( '-', '', self::newUUIDv1() );
238 * @param array $info Result of UIDGenerator::getTimeAndDelay()
239 * @return string 128 bits
241 protected function getUUIDv1( array $info ) {
242 $clkSeq_bin = Wikimedia\base_convert( $info['clkSeq'], 10, 2, 14 );
243 $time_bin = $this->intervalsSinceGregorianBinary( $info['time'], $info['offsetCounter'] );
244 // Take the 32 bits of "time low"
245 $id_bin = substr( $time_bin, 28, 32 );
246 // Add 16 bits of "time mid" resulting in 48 bits total
247 $id_bin .= substr( $time_bin, 12, 16 );
248 // Add 4 bit version resulting in 52 bits total
249 $id_bin .= '0001';
250 // Add 12 bits of "time high" resulting in 64 bits total
251 $id_bin .= substr( $time_bin, 0, 12 );
252 // Add 2 bits of "variant" resulting in 66 bits total
253 $id_bin .= '10';
254 // Add 6 bits of "clock seq high" resulting in 72 bits total
255 $id_bin .= substr( $clkSeq_bin, 0, 6 );
256 // Add 8 bits of "clock seq low" resulting in 80 bits total
257 $id_bin .= substr( $clkSeq_bin, 6, 8 );
258 // Add the 48 bit node ID resulting in 128 bits total
259 $id_bin .= $this->nodeId48;
260 // Convert to a 32 char hex string with dashes
261 if ( strlen( $id_bin ) !== 128 ) {
262 throw new RuntimeException( "Detected overflow for millisecond timestamp." );
264 $hex = Wikimedia\base_convert( $id_bin, 2, 16, 32 );
265 return sprintf( '%s-%s-%s-%s-%s',
266 // "time_low" (32 bits)
267 substr( $hex, 0, 8 ),
268 // "time_mid" (16 bits)
269 substr( $hex, 8, 4 ),
270 // "time_hi_and_version" (16 bits)
271 substr( $hex, 12, 4 ),
272 // "clk_seq_hi_res" (8 bits) and "clk_seq_low" (8 bits)
273 substr( $hex, 16, 4 ),
274 // "node" (48 bits)
275 substr( $hex, 20, 12 )
280 * Return an RFC4122 compliant v4 UUID
282 * @param int $flags Bitfield (supports UIDGenerator::QUICK_RAND)
283 * @return string
284 * @throws RuntimeException
286 public static function newUUIDv4( $flags = 0 ) {
287 $hex = ( $flags & self::QUICK_RAND )
288 ? wfRandomString( 31 )
289 : MWCryptRand::generateHex( 31 );
291 return sprintf( '%s-%s-%s-%s-%s',
292 // "time_low" (32 bits)
293 substr( $hex, 0, 8 ),
294 // "time_mid" (16 bits)
295 substr( $hex, 8, 4 ),
296 // "time_hi_and_version" (16 bits)
297 '4' . substr( $hex, 12, 3 ),
298 // "clk_seq_hi_res" (8 bits, variant is binary 10x) and "clk_seq_low" (8 bits)
299 dechex( 0x8 | ( hexdec( $hex[15] ) & 0x3 ) ) . $hex[16] . substr( $hex, 17, 2 ),
300 // "node" (48 bits)
301 substr( $hex, 19, 12 )
306 * Return an RFC4122 compliant v4 UUID
308 * @param int $flags Bitfield (supports UIDGenerator::QUICK_RAND)
309 * @return string 32 hex characters with no hyphens
310 * @throws RuntimeException
312 public static function newRawUUIDv4( $flags = 0 ) {
313 return str_replace( '-', '', self::newUUIDv4( $flags ) );
317 * Return an ID that is sequential *only* for this node and bucket
319 * These IDs are suitable for per-host sequence numbers, e.g. for some packet protocols.
320 * If UIDGenerator::QUICK_VOLATILE is used the counter might reset on server restart.
322 * @param string $bucket Arbitrary bucket name (should be ASCII)
323 * @param int $bits Bit size (<=48) of resulting numbers before wrap-around
324 * @param int $flags (supports UIDGenerator::QUICK_VOLATILE)
325 * @return float Integer value as float
326 * @since 1.23
328 public static function newSequentialPerNodeID( $bucket, $bits = 48, $flags = 0 ) {
329 return current( self::newSequentialPerNodeIDs( $bucket, $bits, 1, $flags ) );
333 * Return IDs that are sequential *only* for this node and bucket
335 * @see UIDGenerator::newSequentialPerNodeID()
336 * @param string $bucket Arbitrary bucket name (should be ASCII)
337 * @param int $bits Bit size (16 to 48) of resulting numbers before wrap-around
338 * @param int $count Number of IDs to return
339 * @param int $flags (supports UIDGenerator::QUICK_VOLATILE)
340 * @return array Ordered list of float integer values
341 * @since 1.23
343 public static function newSequentialPerNodeIDs( $bucket, $bits, $count, $flags = 0 ) {
344 $gen = self::singleton();
345 return $gen->getSequentialPerNodeIDs( $bucket, $bits, $count, $flags );
349 * Return IDs that are sequential *only* for this node and bucket
351 * @see UIDGenerator::newSequentialPerNodeID()
352 * @param string $bucket Arbitrary bucket name (should be ASCII)
353 * @param int $bits Bit size (16 to 48) of resulting numbers before wrap-around
354 * @param int $count Number of IDs to return
355 * @param int $flags (supports UIDGenerator::QUICK_VOLATILE)
356 * @return array Ordered list of float integer values
357 * @throws RuntimeException
359 protected function getSequentialPerNodeIDs( $bucket, $bits, $count, $flags ) {
360 if ( $count <= 0 ) {
361 return []; // nothing to do
362 } elseif ( $bits < 16 || $bits > 48 ) {
363 throw new RuntimeException( "Requested bit size ($bits) is out of range." );
366 $counter = null; // post-increment persistent counter value
368 // Use APC/eAccelerator/xcache if requested, available, and not in CLI mode;
369 // Counter values would not survive accross script instances in CLI mode.
370 $cache = null;
371 if ( ( $flags & self::QUICK_VOLATILE ) && PHP_SAPI !== 'cli' ) {
372 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
374 if ( $cache ) {
375 $counter = $cache->incrWithInit( $bucket, $cache::TTL_INDEFINITE, $count, $count );
376 if ( $counter === false ) {
377 throw new RuntimeException( 'Unable to set value to ' . get_class( $cache ) );
381 // Note: use of fmod() avoids "division by zero" on 32 bit machines
382 if ( $counter === null ) {
383 $path = wfTempDir() . '/mw-' . __CLASS__ . '-' . rawurlencode( $bucket ) . '-48';
384 // Get the UID lock file handle
385 if ( isset( $this->fileHandles[$path] ) ) {
386 $handle = $this->fileHandles[$path];
387 } else {
388 $handle = fopen( $path, 'cb+' );
389 $this->fileHandles[$path] = $handle ?: null; // cache
391 // Acquire the UID lock file
392 if ( $handle === false ) {
393 throw new RuntimeException( "Could not open '{$path}'." );
394 } elseif ( !flock( $handle, LOCK_EX ) ) {
395 fclose( $handle );
396 throw new RuntimeException( "Could not acquire '{$path}'." );
398 // Fetch the counter value and increment it...
399 rewind( $handle );
400 $counter = floor( trim( fgets( $handle ) ) ) + $count; // fetch as float
401 // Write back the new counter value
402 ftruncate( $handle, 0 );
403 rewind( $handle );
404 fwrite( $handle, fmod( $counter, pow( 2, 48 ) ) ); // warp-around as needed
405 fflush( $handle );
406 // Release the UID lock file
407 flock( $handle, LOCK_UN );
410 $ids = [];
411 $divisor = pow( 2, $bits );
412 $currentId = floor( $counter - $count ); // pre-increment counter value
413 for ( $i = 0; $i < $count; ++$i ) {
414 $ids[] = fmod( ++$currentId, $divisor );
417 return $ids;
421 * Get a (time,counter,clock sequence) where (time,counter) is higher
422 * than any previous (time,counter) value for the given clock sequence.
423 * This is useful for making UIDs sequential on a per-node bases.
425 * @param string $lockFile Name of a local lock file
426 * @param int $clockSeqSize The number of possible clock sequence values
427 * @param int $counterSize The number of possible counter values
428 * @param int $offsetSize The number of possible offset values
429 * @return array (result of UIDGenerator::millitime(), counter, clock sequence)
430 * @throws RuntimeException
432 protected function getTimeAndDelay( $lockFile, $clockSeqSize, $counterSize, $offsetSize ) {
433 // Get the UID lock file handle
434 if ( isset( $this->fileHandles[$lockFile] ) ) {
435 $handle = $this->fileHandles[$lockFile];
436 } else {
437 $handle = fopen( $this->$lockFile, 'cb+' );
438 $this->fileHandles[$lockFile] = $handle ?: null; // cache
440 // Acquire the UID lock file
441 if ( $handle === false ) {
442 throw new RuntimeException( "Could not open '{$this->$lockFile}'." );
443 } elseif ( !flock( $handle, LOCK_EX ) ) {
444 fclose( $handle );
445 throw new RuntimeException( "Could not acquire '{$this->$lockFile}'." );
447 // Get the current timestamp, clock sequence number, last time, and counter
448 rewind( $handle );
449 $data = explode( ' ', fgets( $handle ) ); // "<clk seq> <sec> <msec> <counter> <offset>"
450 $clockChanged = false; // clock set back significantly?
451 if ( count( $data ) == 5 ) { // last UID info already initialized
452 $clkSeq = (int)$data[0] % $clockSeqSize;
453 $prevTime = [ (int)$data[1], (int)$data[2] ];
454 $offset = (int)$data[4] % $counterSize; // random counter offset
455 $counter = 0; // counter for UIDs with the same timestamp
456 // Delay until the clock reaches the time of the last ID.
457 // This detects any microtime() drift among processes.
458 $time = $this->timeWaitUntil( $prevTime );
459 if ( !$time ) { // too long to delay?
460 $clockChanged = true; // bump clock sequence number
461 $time = self::millitime();
462 } elseif ( $time == $prevTime ) {
463 // Bump the counter if there are timestamp collisions
464 $counter = (int)$data[3] % $counterSize;
465 if ( ++$counter >= $counterSize ) { // sanity (starts at 0)
466 flock( $handle, LOCK_UN ); // abort
467 throw new RuntimeException( "Counter overflow for timestamp value." );
470 } else { // last UID info not initialized
471 $clkSeq = mt_rand( 0, $clockSeqSize - 1 );
472 $counter = 0;
473 $offset = mt_rand( 0, $offsetSize - 1 );
474 $time = self::millitime();
476 // microtime() and gettimeofday() can drift from time() at least on Windows.
477 // The drift is immediate for processes running while the system clock changes.
478 // time() does not have this problem. See https://bugs.php.net/bug.php?id=42659.
479 if ( abs( time() - $time[0] ) >= 2 ) {
480 // We don't want processes using too high or low timestamps to avoid duplicate
481 // UIDs and clock sequence number churn. This process should just be restarted.
482 flock( $handle, LOCK_UN ); // abort
483 throw new RuntimeException( "Process clock is outdated or drifted." );
485 // If microtime() is synced and a clock change was detected, then the clock went back
486 if ( $clockChanged ) {
487 // Bump the clock sequence number and also randomize the counter offset,
488 // which is useful for UIDs that do not include the clock sequence number.
489 $clkSeq = ( $clkSeq + 1 ) % $clockSeqSize;
490 $offset = mt_rand( 0, $offsetSize - 1 );
491 trigger_error( "Clock was set back; sequence number incremented." );
493 // Update the (clock sequence number, timestamp, counter)
494 ftruncate( $handle, 0 );
495 rewind( $handle );
496 fwrite( $handle, "{$clkSeq} {$time[0]} {$time[1]} {$counter} {$offset}" );
497 fflush( $handle );
498 // Release the UID lock file
499 flock( $handle, LOCK_UN );
501 return [
502 'time' => $time,
503 'counter' => $counter,
504 'clkSeq' => $clkSeq,
505 'offset' => $offset,
506 'offsetCounter' => $counter + $offset
511 * Wait till the current timestamp reaches $time and return the current
512 * timestamp. This returns false if it would have to wait more than 10ms.
514 * @param array $time Result of UIDGenerator::millitime()
515 * @return array|bool UIDGenerator::millitime() result or false
517 protected function timeWaitUntil( array $time ) {
518 do {
519 $ct = self::millitime();
520 if ( $ct >= $time ) { // https://secure.php.net/manual/en/language.operators.comparison.php
521 return $ct; // current timestamp is higher than $time
523 } while ( ( ( $time[0] - $ct[0] ) * 1000 + ( $time[1] - $ct[1] ) ) <= 10 );
525 return false;
529 * @param array $time Result of UIDGenerator::millitime()
530 * @return string 46 LSBs of "milliseconds since epoch" in binary (rolls over in 4201)
531 * @throws RuntimeException
533 protected function millisecondsSinceEpochBinary( array $time ) {
534 list( $sec, $msec ) = $time;
535 $ts = 1000 * $sec + $msec;
536 if ( $ts > pow( 2, 52 ) ) {
537 throw new RuntimeException( __METHOD__ .
538 ': sorry, this function doesn\'t work after the year 144680' );
541 return substr( Wikimedia\base_convert( $ts, 10, 2, 46 ), -46 );
545 * @param array $time Result of UIDGenerator::millitime()
546 * @param integer $delta Number of intervals to add on to the timestamp
547 * @return string 60 bits of "100ns intervals since 15 October 1582" (rolls over in 3400)
548 * @throws RuntimeException
550 protected function intervalsSinceGregorianBinary( array $time, $delta = 0 ) {
551 list( $sec, $msec ) = $time;
552 $offset = '122192928000000000';
553 if ( PHP_INT_SIZE >= 8 ) { // 64 bit integers
554 $ts = ( 1000 * $sec + $msec ) * 10000 + (int)$offset + $delta;
555 $id_bin = str_pad( decbin( $ts % pow( 2, 60 ) ), 60, '0', STR_PAD_LEFT );
556 } elseif ( extension_loaded( 'gmp' ) ) {
557 $ts = gmp_add( gmp_mul( (string)$sec, '1000' ), (string)$msec ); // ms
558 $ts = gmp_add( gmp_mul( $ts, '10000' ), $offset ); // 100ns intervals
559 $ts = gmp_add( $ts, (string)$delta );
560 $ts = gmp_mod( $ts, gmp_pow( '2', '60' ) ); // wrap around
561 $id_bin = str_pad( gmp_strval( $ts, 2 ), 60, '0', STR_PAD_LEFT );
562 } elseif ( extension_loaded( 'bcmath' ) ) {
563 $ts = bcadd( bcmul( $sec, 1000 ), $msec ); // ms
564 $ts = bcadd( bcmul( $ts, 10000 ), $offset ); // 100ns intervals
565 $ts = bcadd( $ts, $delta );
566 $ts = bcmod( $ts, bcpow( 2, 60 ) ); // wrap around
567 $id_bin = Wikimedia\base_convert( $ts, 10, 2, 60 );
568 } else {
569 throw new RuntimeException( 'bcmath or gmp extension required for 32 bit machines.' );
571 return $id_bin;
575 * @return array (current time in seconds, milliseconds since then)
577 protected static function millitime() {
578 list( $msec, $sec ) = explode( ' ', microtime() );
580 return [ (int)$sec, (int)( $msec * 1000 ) ];
584 * Delete all cache files that have been created.
586 * This is a cleanup method primarily meant to be used from unit tests to
587 * avoid poluting the local filesystem. If used outside of a unit test
588 * environment it should be used with caution as it may destroy state saved
589 * in the files.
591 * @see unitTestTearDown
592 * @since 1.23
594 protected function deleteCacheFiles() {
595 // Bug: 44850
596 foreach ( $this->fileHandles as $path => $handle ) {
597 if ( $handle !== null ) {
598 fclose( $handle );
600 if ( is_file( $path ) ) {
601 unlink( $path );
603 unset( $this->fileHandles[$path] );
605 if ( is_file( $this->nodeIdFile ) ) {
606 unlink( $this->nodeIdFile );
611 * Cleanup resources when tearing down after a unit test.
613 * This is a cleanup method primarily meant to be used from unit tests to
614 * avoid poluting the local filesystem. If used outside of a unit test
615 * environment it should be used with caution as it may destroy state saved
616 * in the files.
618 * @see deleteCacheFiles
619 * @since 1.23
621 public static function unitTestTearDown() {
622 // Bug: 44850
623 $gen = self::singleton();
624 $gen->deleteCacheFiles();
627 function __destruct() {
628 array_map( 'fclose', array_filter( $this->fileHandles ) );