MediaWiki
REL1_23
|
00001 <?php 00029 class UIDGenerator { 00031 protected static $instance = null; 00032 00033 protected $nodeIdFile; // string; local file path 00034 protected $nodeId32; // string; node ID in binary (32 bits) 00035 protected $nodeId48; // string; node ID in binary (48 bits) 00036 00037 protected $lockFile88; // string; local file path 00038 protected $lockFile128; // string; local file path 00039 00041 protected $fileHandles = array(); // cache file handles 00042 00043 const QUICK_RAND = 1; // get randomness from fast and insecure sources 00044 const QUICK_VOLATILE = 2; // use an APC like in-memory counter if available 00045 00046 protected function __construct() { 00047 $this->nodeIdFile = wfTempDir() . '/mw-' . __CLASS__ . '-UID-nodeid'; 00048 $nodeId = ''; 00049 if ( is_file( $this->nodeIdFile ) ) { 00050 $nodeId = file_get_contents( $this->nodeIdFile ); 00051 } 00052 // Try to get some ID that uniquely identifies this machine (RFC 4122)... 00053 if ( !preg_match( '/^[0-9a-f]{12}$/i', $nodeId ) ) { 00054 wfSuppressWarnings(); 00055 if ( wfIsWindows() ) { 00056 // http://technet.microsoft.com/en-us/library/bb490913.aspx 00057 $csv = trim( wfShellExec( 'getmac /NH /FO CSV' ) ); 00058 $line = substr( $csv, 0, strcspn( $csv, "\n" ) ); 00059 $info = str_getcsv( $line ); 00060 $nodeId = isset( $info[0] ) ? str_replace( '-', '', $info[0] ) : ''; 00061 } elseif ( is_executable( '/sbin/ifconfig' ) ) { // Linux/BSD/Solaris/OS X 00062 // See http://linux.die.net/man/8/ifconfig 00063 $m = array(); 00064 preg_match( '/\s([0-9a-f]{2}(:[0-9a-f]{2}){5})\s/', 00065 wfShellExec( '/sbin/ifconfig -a' ), $m ); 00066 $nodeId = isset( $m[1] ) ? str_replace( ':', '', $m[1] ) : ''; 00067 } 00068 wfRestoreWarnings(); 00069 if ( !preg_match( '/^[0-9a-f]{12}$/i', $nodeId ) ) { 00070 $nodeId = MWCryptRand::generateHex( 12, true ); 00071 $nodeId[1] = dechex( hexdec( $nodeId[1] ) | 0x1 ); // set multicast bit 00072 } 00073 file_put_contents( $this->nodeIdFile, $nodeId ); // cache 00074 } 00075 $this->nodeId32 = wfBaseConvert( substr( sha1( $nodeId ), 0, 8 ), 16, 2, 32 ); 00076 $this->nodeId48 = wfBaseConvert( $nodeId, 16, 2, 48 ); 00077 // If different processes run as different users, they may have different temp dirs. 00078 // This is dealt with by initializing the clock sequence number and counters randomly. 00079 $this->lockFile88 = wfTempDir() . '/mw-' . __CLASS__ . '-UID-88'; 00080 $this->lockFile128 = wfTempDir() . '/mw-' . __CLASS__ . '-UID-128'; 00081 } 00082 00086 protected static function singleton() { 00087 if ( self::$instance === null ) { 00088 self::$instance = new self(); 00089 } 00090 00091 return self::$instance; 00092 } 00093 00109 public static function newTimestampedUID88( $base = 10 ) { 00110 if ( !is_integer( $base ) || $base > 36 || $base < 2 ) { 00111 throw new MWException( "Base must an integer be between 2 and 36" ); 00112 } 00113 $gen = self::singleton(); 00114 $time = $gen->getTimestampAndDelay( 'lockFile88', 1, 1024 ); 00115 00116 return wfBaseConvert( $gen->getTimestampedID88( $time ), 2, $base ); 00117 } 00118 00123 protected function getTimestampedID88( array $info ) { 00124 list( $time, $counter ) = $info; 00125 // Take the 46 MSBs of "milliseconds since epoch" 00126 $id_bin = $this->millisecondsSinceEpochBinary( $time ); 00127 // Add a 10 bit counter resulting in 56 bits total 00128 $id_bin .= str_pad( decbin( $counter ), 10, '0', STR_PAD_LEFT ); 00129 // Add the 32 bit node ID resulting in 88 bits total 00130 $id_bin .= $this->nodeId32; 00131 // Convert to a 1-27 digit integer string 00132 if ( strlen( $id_bin ) !== 88 ) { 00133 throw new MWException( "Detected overflow for millisecond timestamp." ); 00134 } 00135 00136 return $id_bin; 00137 } 00138 00153 public static function newTimestampedUID128( $base = 10 ) { 00154 if ( !is_integer( $base ) || $base > 36 || $base < 2 ) { 00155 throw new MWException( "Base must be an integer between 2 and 36" ); 00156 } 00157 $gen = self::singleton(); 00158 $time = $gen->getTimestampAndDelay( 'lockFile128', 16384, 1048576 ); 00159 00160 return wfBaseConvert( $gen->getTimestampedID128( $time ), 2, $base ); 00161 } 00162 00167 protected function getTimestampedID128( array $info ) { 00168 list( $time, $counter, $clkSeq ) = $info; 00169 // Take the 46 MSBs of "milliseconds since epoch" 00170 $id_bin = $this->millisecondsSinceEpochBinary( $time ); 00171 // Add a 20 bit counter resulting in 66 bits total 00172 $id_bin .= str_pad( decbin( $counter ), 20, '0', STR_PAD_LEFT ); 00173 // Add a 14 bit clock sequence number resulting in 80 bits total 00174 $id_bin .= str_pad( decbin( $clkSeq ), 14, '0', STR_PAD_LEFT ); 00175 // Add the 48 bit node ID resulting in 128 bits total 00176 $id_bin .= $this->nodeId48; 00177 // Convert to a 1-39 digit integer string 00178 if ( strlen( $id_bin ) !== 128 ) { 00179 throw new MWException( "Detected overflow for millisecond timestamp." ); 00180 } 00181 00182 return $id_bin; 00183 } 00184 00192 public static function newUUIDv4( $flags = 0 ) { 00193 $hex = ( $flags & self::QUICK_RAND ) 00194 ? wfRandomString( 31 ) 00195 : MWCryptRand::generateHex( 31 ); 00196 00197 return sprintf( '%s-%s-%s-%s-%s', 00198 // "time_low" (32 bits) 00199 substr( $hex, 0, 8 ), 00200 // "time_mid" (16 bits) 00201 substr( $hex, 8, 4 ), 00202 // "time_hi_and_version" (16 bits) 00203 '4' . substr( $hex, 12, 3 ), 00204 // "clk_seq_hi_res (8 bits, variant is binary 10x) and "clk_seq_low" (8 bits) 00205 dechex( 0x8 | ( hexdec( $hex[15] ) & 0x3 ) ) . $hex[16] . substr( $hex, 17, 2 ), 00206 // "node" (48 bits) 00207 substr( $hex, 19, 12 ) 00208 ); 00209 } 00210 00218 public static function newRawUUIDv4( $flags = 0 ) { 00219 return str_replace( '-', '', self::newUUIDv4( $flags ) ); 00220 } 00221 00234 public static function newSequentialPerNodeID( $bucket, $bits = 48, $flags = 0 ) { 00235 return current( self::newSequentialPerNodeIDs( $bucket, $bits, 1, $flags ) ); 00236 } 00237 00249 public static function newSequentialPerNodeIDs( $bucket, $bits, $count, $flags = 0 ) { 00250 $gen = self::singleton(); 00251 return $gen->getSequentialPerNodeIDs( $bucket, $bits, $count, $flags ); 00252 } 00253 00264 protected function getSequentialPerNodeIDs( $bucket, $bits, $count, $flags ) { 00265 if ( $count <= 0 ) { 00266 return array(); // nothing to do 00267 } elseif ( $count > 10000 ) { 00268 throw new MWException( "Number of requested IDs ($count) is too high." ); 00269 } elseif ( $bits < 16 || $bits > 48 ) { 00270 throw new MWException( "Requested bit size ($bits) is out of range." ); 00271 } 00272 00273 $counter = null; // post-increment persistent counter value 00274 00275 // Use APC/eAccelerator/xcache if requested, available, and not in CLI mode; 00276 // Counter values would not survive accross script instances in CLI mode. 00277 $cache = null; 00278 if ( ( $flags & self::QUICK_VOLATILE ) && PHP_SAPI !== 'cli' ) { 00279 try { 00280 $cache = ObjectCache::newAccelerator( array() ); 00281 } catch ( MWException $e ) {} // not supported 00282 } 00283 if ( $cache ) { 00284 $counter = $cache->incr( $bucket, $count ); 00285 if ( $counter === false ) { 00286 if ( !$cache->add( $bucket, $count ) ) { 00287 throw new MWException( 'Unable to set value to ' . get_class( $cache ) ); 00288 } 00289 $counter = $count; 00290 } 00291 } 00292 00293 // Note: use of fmod() avoids "division by zero" on 32 bit machines 00294 if ( $counter === null ) { 00295 $path = wfTempDir() . '/mw-' . __CLASS__ . '-' . rawurlencode( $bucket ) . '-48'; 00296 // Get the UID lock file handle 00297 if ( isset( $this->fileHandles[$path] ) ) { 00298 $handle = $this->fileHandles[$path]; 00299 } else { 00300 $handle = fopen( $path, 'cb+' ); 00301 $this->fileHandles[$path] = $handle ?: null; // cache 00302 } 00303 // Acquire the UID lock file 00304 if ( $handle === false ) { 00305 throw new MWException( "Could not open '{$path}'." ); 00306 } elseif ( !flock( $handle, LOCK_EX ) ) { 00307 fclose( $handle ); 00308 throw new MWException( "Could not acquire '{$path}'." ); 00309 } 00310 // Fetch the counter value and increment it... 00311 rewind( $handle ); 00312 $counter = floor( trim( fgets( $handle ) ) ) + $count; // fetch as float 00313 // Write back the new counter value 00314 ftruncate( $handle, 0 ); 00315 rewind( $handle ); 00316 fwrite( $handle, fmod( $counter, pow( 2, 48 ) ) ); // warp-around as needed 00317 fflush( $handle ); 00318 // Release the UID lock file 00319 flock( $handle, LOCK_UN ); 00320 } 00321 00322 $ids = array(); 00323 $divisor = pow( 2, $bits ); 00324 $currentId = floor( $counter - $count ); // pre-increment counter value 00325 for ( $i = 0; $i < $count; ++$i ) { 00326 $ids[] = fmod( ++$currentId, $divisor ); 00327 } 00328 00329 return $ids; 00330 } 00331 00343 protected function getTimestampAndDelay( $lockFile, $clockSeqSize, $counterSize ) { 00344 // Get the UID lock file handle 00345 $path = $this->$lockFile; 00346 if ( isset( $this->fileHandles[$path] ) ) { 00347 $handle = $this->fileHandles[$path]; 00348 } else { 00349 $handle = fopen( $path, 'cb+' ); 00350 $this->fileHandles[$path] = $handle ?: null; // cache 00351 } 00352 // Acquire the UID lock file 00353 if ( $handle === false ) { 00354 throw new MWException( "Could not open '{$this->$lockFile}'." ); 00355 } elseif ( !flock( $handle, LOCK_EX ) ) { 00356 fclose( $handle ); 00357 throw new MWException( "Could not acquire '{$this->$lockFile}'." ); 00358 } 00359 // Get the current timestamp, clock sequence number, last time, and counter 00360 rewind( $handle ); 00361 $data = explode( ' ', fgets( $handle ) ); // "<clk seq> <sec> <msec> <counter> <offset>" 00362 $clockChanged = false; // clock set back significantly? 00363 if ( count( $data ) == 5 ) { // last UID info already initialized 00364 $clkSeq = (int)$data[0] % $clockSeqSize; 00365 $prevTime = array( (int)$data[1], (int)$data[2] ); 00366 $offset = (int)$data[4] % $counterSize; // random counter offset 00367 $counter = 0; // counter for UIDs with the same timestamp 00368 // Delay until the clock reaches the time of the last ID. 00369 // This detects any microtime() drift among processes. 00370 $time = $this->timeWaitUntil( $prevTime ); 00371 if ( !$time ) { // too long to delay? 00372 $clockChanged = true; // bump clock sequence number 00373 $time = self::millitime(); 00374 } elseif ( $time == $prevTime ) { 00375 // Bump the counter if there are timestamp collisions 00376 $counter = (int)$data[3] % $counterSize; 00377 if ( ++$counter >= $counterSize ) { // sanity (starts at 0) 00378 flock( $handle, LOCK_UN ); // abort 00379 throw new MWException( "Counter overflow for timestamp value." ); 00380 } 00381 } 00382 } else { // last UID info not initialized 00383 $clkSeq = mt_rand( 0, $clockSeqSize - 1 ); 00384 $counter = 0; 00385 $offset = mt_rand( 0, $counterSize - 1 ); 00386 $time = self::millitime(); 00387 } 00388 // microtime() and gettimeofday() can drift from time() at least on Windows. 00389 // The drift is immediate for processes running while the system clock changes. 00390 // time() does not have this problem. See https://bugs.php.net/bug.php?id=42659. 00391 if ( abs( time() - $time[0] ) >= 2 ) { 00392 // We don't want processes using too high or low timestamps to avoid duplicate 00393 // UIDs and clock sequence number churn. This process should just be restarted. 00394 flock( $handle, LOCK_UN ); // abort 00395 throw new MWException( "Process clock is outdated or drifted." ); 00396 } 00397 // If microtime() is synced and a clock change was detected, then the clock went back 00398 if ( $clockChanged ) { 00399 // Bump the clock sequence number and also randomize the counter offset, 00400 // which is useful for UIDs that do not include the clock sequence number. 00401 $clkSeq = ( $clkSeq + 1 ) % $clockSeqSize; 00402 $offset = mt_rand( 0, $counterSize - 1 ); 00403 trigger_error( "Clock was set back; sequence number incremented." ); 00404 } 00405 // Update the (clock sequence number, timestamp, counter) 00406 ftruncate( $handle, 0 ); 00407 rewind( $handle ); 00408 fwrite( $handle, "{$clkSeq} {$time[0]} {$time[1]} {$counter} {$offset}" ); 00409 fflush( $handle ); 00410 // Release the UID lock file 00411 flock( $handle, LOCK_UN ); 00412 00413 return array( $time, ( $counter + $offset ) % $counterSize, $clkSeq ); 00414 } 00415 00423 protected function timeWaitUntil( array $time ) { 00424 do { 00425 $ct = self::millitime(); 00426 if ( $ct >= $time ) { // http://php.net/manual/en/language.operators.comparison.php 00427 return $ct; // current timestamp is higher than $time 00428 } 00429 } while ( ( ( $time[0] - $ct[0] ) * 1000 + ( $time[1] - $ct[1] ) ) <= 10 ); 00430 00431 return false; 00432 } 00433 00438 protected function millisecondsSinceEpochBinary( array $time ) { 00439 list( $sec, $msec ) = $time; 00440 $ts = 1000 * $sec + $msec; 00441 if ( $ts > pow( 2, 52 ) ) { 00442 throw new MWException( __METHOD__ . 00443 ': sorry, this function doesn\'t work after the year 144680' ); 00444 } 00445 00446 return substr( wfBaseConvert( $ts, 10, 2, 46 ), -46 ); 00447 } 00448 00452 protected static function millitime() { 00453 list( $msec, $sec ) = explode( ' ', microtime() ); 00454 00455 return array( (int)$sec, (int)( $msec * 1000 ) ); 00456 } 00457 00469 protected function deleteCacheFiles() { 00470 // Bug: 44850 00471 foreach ( $this->fileHandles as $path => $handle ) { 00472 if ( $handle !== null ) { 00473 fclose( $handle ); 00474 } 00475 if ( is_file( $path ) ) { 00476 unlink( $path ); 00477 } 00478 unset( $this->fileHandles[$path] ); 00479 } 00480 if ( is_file( $this->nodeIdFile ) ) { 00481 unlink( $this->nodeIdFile ); 00482 } 00483 } 00484 00496 public static function unitTestTearDown() { 00497 // Bug: 44850 00498 $gen = self::singleton(); 00499 $gen->deleteCacheFiles(); 00500 } 00501 00502 function __destruct() { 00503 array_map( 'fclose', array_filter( $this->fileHandles ) ); 00504 } 00505 }