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