function testCurrent()
  {
    $this->assertFalse(UIDGenerator :: current());

    UIDGenerator :: next();
    UIDGenerator :: next();
    UIDGenerator :: next();

    $this->assertEqual(UIDGenerator :: current(), 3);
  }
Beispiel #2
0
 public function testUUIDv4()
 {
     for ($i = 0; $i < 100; $i++) {
         $id = UIDGenerator::newUUIDv4();
         $this->assertEquals(true, preg_match('!^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$!', $id), "UID {$id} has the right format");
         $id = UIDGenerator::newRawUUIDv4();
         $this->assertEquals(true, preg_match('!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id), "UID {$id} has the right format");
         $id = UIDGenerator::newRawUUIDv4(UIDGenerator::QUICK_RAND);
         $this->assertEquals(true, preg_match('!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id), "UID {$id} has the right format");
     }
 }
 /**
  * @see ExternalStoreMedium::store()
  */
 public function store($backend, $data)
 {
     $be = FileBackendGroup::singleton()->get($backend);
     if ($be instanceof FileBackend) {
         // Get three random base 36 characters to act as shard directories
         $rand = wfBaseConvert(mt_rand(0, 46655), 10, 36, 3);
         // Make sure ID is roughly lexicographically increasing for performance
         $id = str_pad(UIDGenerator::newTimestampedUID128(32), 26, '0', STR_PAD_LEFT);
         // Segregate items by wiki ID for the sake of bookkeeping
         $wiki = isset($this->params['wiki']) ? $this->params['wiki'] : wfWikiID();
         $url = $be->getContainerStoragePath('data') . '/' . rawurlencode($wiki) . "/{$rand[0]}/{$rand[1]}/{$rand[2]}/{$id}";
         $be->prepare(array('dir' => dirname($url), 'noAccess' => 1, 'noListing' => 1));
         if ($be->create(array('dst' => $url, 'content' => $data))->isOK()) {
             return $url;
         }
     }
     return false;
 }
 /**
  * @covers UIDGenerator::newSequentialPerNodeIDs
  */
 public function testNewSequentialIDs()
 {
     $ids = UIDGenerator::newSequentialPerNodeIDs('test', 32, 5);
     $lastId = null;
     foreach ($ids as $id) {
         $this->assertType('float', $id, "ID returned as float");
         $this->assertGreaterThan(0, $id, "ID greater than 1");
         if ($lastId) {
             $this->assertGreaterThan($lastId, $id, "IDs increasing in value");
         }
         $lastId = $id;
     }
 }
Beispiel #5
0
 /**
  * @return UIDGenerator
  */
 protected static function singleton()
 {
     if (self::$instance === null) {
         self::$instance = new self();
     }
     return self::$instance;
 }
Beispiel #6
0
 /**
  * @param IJobSpecification $job
  * @return array
  */
 protected function getNewJobFields(IJobSpecification $job)
 {
     return array('type' => $job->getType(), 'namespace' => $job->getTitle()->getNamespace(), 'title' => $job->getTitle()->getDBkey(), 'params' => $job->getParams(), 'rtimestamp' => $job->getReleaseTimestamp() ?: 0, 'uuid' => UIDGenerator::newRawUUIDv4(UIDGenerator::QUICK_RAND), 'sha1' => $job->ignoreDuplicates() ? Wikimedia\base_convert(sha1(serialize($job->getDeduplicationInfo())), 16, 36, 31) : '', 'timestamp' => time());
 }
Beispiel #7
0
 /**
  * Send Hyper Text Caching Protocol (HTCP) CLR requests.
  *
  * @throws MWException
  * @param string[] $urlArr Collection of URLs to purge
  */
 private static function HTCPPurge(array $urlArr)
 {
     global $wgHTCPRouting, $wgHTCPMulticastTTL;
     // HTCP CLR operation
     $htcpOpCLR = 4;
     // @todo FIXME: PHP doesn't support these socket constants (include/linux/in.h)
     if (!defined("IPPROTO_IP")) {
         define("IPPROTO_IP", 0);
         define("IP_MULTICAST_LOOP", 34);
         define("IP_MULTICAST_TTL", 33);
     }
     // pfsockopen doesn't work because we need set_sock_opt
     $conn = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
     if (!$conn) {
         $errstr = socket_strerror(socket_last_error());
         wfDebugLog('squid', __METHOD__ . ": Error opening UDP socket: {$errstr}");
         return;
     }
     // Set socket options
     socket_set_option($conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0);
     if ($wgHTCPMulticastTTL != 1) {
         // Set multicast time to live (hop count) option on socket
         socket_set_option($conn, IPPROTO_IP, IP_MULTICAST_TTL, $wgHTCPMulticastTTL);
     }
     // Get sequential trx IDs for packet loss counting
     $ids = UIDGenerator::newSequentialPerNodeIDs('squidhtcppurge', 32, count($urlArr), UIDGenerator::QUICK_VOLATILE);
     foreach ($urlArr as $url) {
         if (!is_string($url)) {
             throw new MWException('Bad purge URL');
         }
         $url = self::expand($url);
         $conf = self::getRuleForURL($url, $wgHTCPRouting);
         if (!$conf) {
             wfDebugLog('squid', __METHOD__ . "No HTCP rule configured for URL {$url} , skipping");
             continue;
         }
         if (isset($conf['host']) && isset($conf['port'])) {
             // Normalize single entries
             $conf = array($conf);
         }
         foreach ($conf as $subconf) {
             if (!isset($subconf['host']) || !isset($subconf['port'])) {
                 throw new MWException("Invalid HTCP rule for URL {$url}\n");
             }
         }
         // Construct a minimal HTCP request diagram
         // as per RFC 2756
         // Opcode 'CLR', no response desired, no auth
         $htcpTransID = current($ids);
         next($ids);
         $htcpSpecifier = pack('na4na*na8n', 4, 'HEAD', strlen($url), $url, 8, 'HTTP/1.0', 0);
         $htcpDataLen = 8 + 2 + strlen($htcpSpecifier);
         $htcpLen = 4 + $htcpDataLen + 2;
         // Note! Squid gets the bit order of the first
         // word wrong, wrt the RFC. Apparently no other
         // implementation exists, so adapt to Squid
         $htcpPacket = pack('nxxnCxNxxa*n', $htcpLen, $htcpDataLen, $htcpOpCLR, $htcpTransID, $htcpSpecifier, 2);
         wfDebugLog('squid', __METHOD__ . "Purging URL {$url} via HTCP");
         foreach ($conf as $subconf) {
             socket_sendto($conn, $htcpPacket, $htcpLen, 0, $subconf['host'], $subconf['port']);
         }
     }
 }
Beispiel #8
0
 public static function generate()
 {
     $hex = \UIDGenerator::newTimestampedUID88(16);
     $hex = str_pad($hex, static::HEX_LEN, '0', STR_PAD_LEFT);
     return self::fromHex($hex);
 }
Beispiel #9
0
	/**
	 * @param $job Job
	 * @return array
	 */
	protected function getNewJobFields( Job $job ) {
		return array(
			// Fields that describe the nature of the job
			'type'       => $job->getType(),
			'namespace'  => $job->getTitle()->getNamespace(),
			'title'      => $job->getTitle()->getDBkey(),
			'params'     => $job->getParams(),
			// Some jobs cannot run until a "release timestamp"
			'rtimestamp' => $job->getReleaseTimestamp() ?: 0,
			// Additional job metadata
			'uuid'       => UIDGenerator::newRawUUIDv4( UIDGenerator::QUICK_RAND ),
			'sha1'       => $job->ignoreDuplicates()
				? wfBaseConvert( sha1( serialize( $job->getDeduplicationInfo() ) ), 16, 36, 31 )
				: '',
			'timestamp'  => time() // UNIX timestamp
		);
	}
 /**
  * Returns a UUID objects based on given input. Will automatically try to
  * determine the input format of the given $input or fail with an exception.
  *
  * @param mixed $input
  * @return UUID|null
  * @throws InvalidInputException
  */
 public static function create($input = false)
 {
     // Most calls to UUID::create are binary strings, check string first
     if (is_string($input) || is_int($input) || $input === false) {
         if ($input === false) {
             // new uuid in base 16 and pad to HEX_LEN with 0's
             $hexValue = str_pad(\UIDGenerator::newTimestampedUID88(16), self::HEX_LEN, '0', STR_PAD_LEFT);
             return new static($hexValue, static::INPUT_HEX);
         } else {
             $len = strlen($input);
             if ($len === self::BIN_LEN) {
                 $value = $input;
                 $type = static::INPUT_BIN;
             } elseif ($len >= self::MIN_ALNUM_LEN && $len <= self::ALNUM_LEN && ctype_alnum($input)) {
                 $value = $input;
                 $type = static::INPUT_ALNUM;
             } elseif ($len === self::HEX_LEN && ctype_xdigit($input)) {
                 $value = $input;
                 $type = static::INPUT_HEX;
             } elseif ($len === self::OLD_BIN_LEN) {
                 $value = substr($input, 0, self::BIN_LEN);
                 $type = static::INPUT_BIN;
             } elseif ($len === self::OLD_HEX_LEN && ctype_xdigit($input)) {
                 $value = substr($input, 0, self::HEX_LEN);
                 $type = static::INPUT_HEX;
             } elseif (is_numeric($input)) {
                 // convert base 10 to base 16 and pad to HEX_LEN with 0's
                 $value = wfBaseConvert($input, 10, 16, self::HEX_LEN);
                 $type = static::INPUT_HEX;
             } else {
                 throw new InvalidInputException('Unknown input to UUID class', 'invalid-input');
             }
             if (isset(self::$instances[$type][$value])) {
                 return self::$instances[$type][$value];
             } else {
                 return new static($value, $type);
             }
         }
     } else {
         if (is_object($input)) {
             if ($input instanceof UUID) {
                 return $input;
             } elseif ($input instanceof Blob) {
                 return self::create($input->fetch());
             } else {
                 throw new InvalidInputException('Unknown input of type ' . get_class($input), 'invalid-input');
             }
         } elseif ($input === null) {
             return null;
         } else {
             throw new InvalidInputException('Unknown input type to UUID class: ' . gettype($input), 'invalid-input');
         }
     }
 }
 function nextUID()
 {
   include_once(LIMB_DIR . '/core/orm/UIDGenerator.class.php');
   return UIDGenerator :: next();
 }
Beispiel #12
0
=========================================================================*/
/*
 * \author Aleš Pavel
 */
require_once 'gdcm.php';
$reader = new Reader();
$reader->SetFilename("test.dcm");
$ret = $reader->Read();
if (!$ret) {
    return 1;
}
$file = $reader->GetFile();
$ano = new Anonymizer();
$ano->SetFile($file);
$ano->RemovePrivateTags();
$ano->RemoveGroupLength();
$t = new Tag(0x10, 0x10);
$ano->Replace($t, "GDCM^PHP^Test^Hello^World");
$g = new UIDGenerator();
$ano->Replace(new Tag(0x8, 0x18), $g->Generate());
$ano->Replace(new Tag(0x20, 0xd), $g->Generate());
$ano->Replace(new Tag(0x20, 0xe), $g->Generate());
$ano->Replace(new Tag(0x20, 0x52), $g->Generate());
$writer = new Writer();
$writer->SetFileName("test2.dcm");
$writer->SetFile($ano->GetFile());
$ret = $writer->Write();
if (!$ret) {
    return 1;
}