Use rhumsaa/uuid instead of custom uuid implementation

This commit is contained in:
Romain Neutron
2014-03-02 23:41:41 +01:00
parent 31de811cf5
commit bb7267eafb
13 changed files with 90 additions and 287 deletions

View File

@@ -41,6 +41,7 @@
"php-xpdf/php-xpdf" : "~0.2.1",
"phpexiftool/phpexiftool" : "~0.3",
"react/zmq" : "~0.2",
"rhumsaa/uuid" : "~2.7",
"silex/silex" : "1.1.x-dev@dev",
"silex/web-profiler" : "~1.0.0@dev",
"sorien/silex-dbal-profiler" : "~1.0.0",

67
composer.lock generated
View File

@@ -3,7 +3,7 @@
"This file locks the dependencies of your project to a known state",
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file"
],
"hash": "86007d19092732cf77a4dab27e072a2b",
"hash": "0d8e038306a0c58deac6187e6a607e71",
"packages": [
{
"name": "alchemy-fr/tcpdf-clone",
@@ -3220,6 +3220,71 @@
],
"time": "2013-09-18 11:02:06"
},
{
"name": "rhumsaa/uuid",
"version": "2.7.1",
"source": {
"type": "git",
"url": "https://github.com/ramsey/uuid.git",
"reference": "8569983a11ed96087cdf94c7aa65866138681beb"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/ramsey/uuid/zipball/8569983a11ed96087cdf94c7aa65866138681beb",
"reference": "8569983a11ed96087cdf94c7aa65866138681beb",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"require-dev": {
"doctrine/dbal": ">=2.3",
"moontoast/math": "~1.1",
"phpunit/phpunit": "~3.7",
"symfony/console": "~2.3"
},
"suggest": {
"doctrine/dbal": "Allow the use of a UUID as doctrine field type.",
"moontoast/math": "Support for converting UUID to 128-bit integer (in string form).",
"symfony/console": "Support for use of the bin/uuid command line tool."
},
"bin": [
"bin/uuid"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.7.x-dev"
}
},
"autoload": {
"psr-0": {
"Rhumsaa\\Uuid": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Marijn Huizendveld",
"email": "marijn.huizendveld@gmail.com"
},
{
"name": "Ben Ramsey",
"homepage": "http://benramsey.com"
}
],
"description": "A PHP 5.3+ library for generating RFC 4122 version 1, 3, 4, and 5 universally unique identifiers (UUID).",
"homepage": "https://github.com/ramsey/uuid",
"keywords": [
"guid",
"identifier",
"uuid"
],
"time": "2014-02-22 00:08:43"
},
{
"name": "silex/silex",
"version": "1.1.x-dev",

View File

@@ -26,6 +26,7 @@ use PHPExiftool\Driver\Metadata\Metadata;
use PHPExiftool\Driver\Metadata\MetadataBag as ExiftoolMetadataBag;
use PHPExiftool\Driver\Value\Mono as MonoValue;
use PHPExiftool\Exception\ExceptionInterface as PHPExiftoolException;
use Rhumsaa\Uuid\Uuid;
/**
* Phraseanet candidate File package
@@ -112,7 +113,7 @@ class File
foreach ($availableUUIDs as $meta) {
if ($metadatas->containsKey($meta)) {
$candidate = $metadatas->get($meta)->getValue()->asString();
if (\uuid::is_valid($candidate)) {
if (Uuid::isValid($candidate)) {
$uuid = $candidate;
break;
}
@@ -120,10 +121,7 @@ class File
}
if (! $uuid && $generate) {
/**
* @todo Check if a file exists with the same checksum
*/
$uuid = \uuid::generate_v4();
$uuid = Uuid::uuid4();
}
$this->uuid = $uuid;

View File

@@ -13,6 +13,7 @@ namespace Alchemy\Phrasea\Command\Upgrade;
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Border\File;
use Rhumsaa\Uuid\Uuid;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
@@ -121,7 +122,7 @@ class Step31 implements DatasUpgraderInterface
{
$pathfile = \p4string::addEndSlash($record['path']) . $record['file'];
$uuid = \uuid::generate_v4();
$uuid = Uuid::uuid4();
try {
$media = $this->app['mediavorus']->guess($pathfile);
$collection = \collection::get_from_coll_id($app, $databox, (int) $record['coll_id']);

View File

@@ -9,6 +9,7 @@
* file that was distributed with this source code.
*/
use Rhumsaa\Uuid\Uuid;
use Symfony\Component\HttpFoundation\Request;
class Bridge_Api_Youtube extends Bridge_Api_Abstract implements Bridge_Api_Interface
@@ -829,7 +830,7 @@ class Bridge_Api_Youtube extends Bridge_Api_Abstract implements Bridge_Api_Inter
$this->_api = new Zend_Gdata_YouTube(
$http_client,
uuid::generate_v4(),
Uuid::uuid4(),
$this->conf->get(['main', 'bridge', 'youtube', 'client_id']),
$this->conf->get(['main', 'bridge', 'youtube', 'developer_key']));
$this->_api->setMajorProtocolVersion(2);

View File

@@ -19,6 +19,7 @@ use Alchemy\Phrasea\SearchEngine\SearchEngineInterface;
use Alchemy\Phrasea\SearchEngine\SearchEngineOptions;
use Doctrine\ORM\EntityManager;
use MediaVorus\MediaVorus;
use Rhumsaa\Uuid\Uuid;
use Symfony\Component\HttpFoundation\File\File as SymfoFile;
class record_adapter implements record_Interface, cache_cacheableInterface
@@ -1190,7 +1191,7 @@ class record_adapter implements record_Interface, cache_cacheableInterface
':parent_record_id' => 1,
':type' => 'unknown',
':sha256' => null,
':uuid' => \uuid::generate_v4(),
':uuid' => Uuid::uuid4(),
':originalname' => null,
':mime' => null,
]);

View File

@@ -1,154 +0,0 @@
<?php
/*
* @author Andrew Moore
*
* Originally found at: http://www.php.net/manual/en/function.uniqid.php#94959
*/
class uuid
{
/**
* @desc UUID named based version that use MD5(hash)
* @param string $namespace
* @param string $name
* @return string
*/
public static function generate_v3($namespace, $name)
{
if ( ! self::is_valid($namespace)) {
return false;
}
// Get hexadecimal components of namespace
$nhex = str_replace(['-', '{', '}'], '', $namespace);
// Binary Value
$nstr = '';
// Convert Namespace UUID to bits
$length = strlen($nhex);
for ($i = 0; $i < $length; $i+=2) {
$nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1]));
}
// Calculate hash value
$hash = md5($nstr . $name);
return sprintf('%08s-%04s-%04x-%04x-%12s',
// 32 bits for "time_low"
substr($hash, 0, 8),
// 16 bits for "time_mid"
substr($hash, 8, 4),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 3
(hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
(hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
// 48 bits for "node"
substr($hash, 20, 12)
);
}
/**
* @desc UUID randomly generated version
* @return string
*/
public static function generate_v4()
{
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
// 16 bits for "time_mid"
mt_rand(0, 0xffff),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand(0, 0x0fff) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand(0, 0x3fff) | 0x8000,
// 48 bits for "node"
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
/**
* @desc UUID named version that use SHA-1 hashing
* @param string $namespace
* @param string $name
* @return string
*/
public static function generate_v5($namespace, $name)
{
if ( ! self::is_valid($namespace)) {
return false;
}
// Get hexadecimal components of namespace
$nhex = str_replace(['-', '{', '}'], '', $namespace);
// Binary Value
$nstr = '';
// Convert Namespace UUID to bits
$length = strlen($nhex);
for ($i = 0; $i < $length; $i+=2) {
$nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1]));
}
// Calculate hash value
$hash = sha1($nstr . $name);
return sprintf('%08s-%04s-%04x-%04x-%12s',
// 32 bits for "time_low"
substr($hash, 0, 8),
// 16 bits for "time_mid"
substr($hash, 8, 4),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 5
(hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
(hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
// 48 bits for "node"
substr($hash, 20, 12)
);
}
/**
* @desc check if an uuid is a valid one
* @param string $uuid
* @return boolean
*/
public static function is_valid($uuid)
{
return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' .
'[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1;
}
/**
* @desc Compare two UUID's "lexically"
* @param stiring $uuid1
* @param string $uuid2
* @return int
* -1 uuid1<uuid2 0 uuid1==uuid2 +1 uuid1>uuid2
*/
public static function compare($uuid1, $uuid2)
{
return (strcmp($uuid1, $uuid2) === 0);
}
/**
* @desc Check wheter an UUID is the NULL UUID 00000000-0000-0000-0000-000000000000
* @return string
*/
public static function is_null($uuid)
{
return (0 === strcmp($uuid, '00000000-0000-0000-0000-000000000000'));
}
}

View File

@@ -15,6 +15,7 @@ use PHPExiftool\Driver\Tag\IPTC\Keywords;
use PHPExiftool\Driver\Value\Multi;
use PHPExiftool\Driver\Metadata\Metadata as PHPExiftoolMetadata;
use Alchemy\Phrasea\Border\Attribute\Metadata;
use Rhumsaa\Uuid\Uuid;
class FileTest extends \PhraseanetTestCase
{
@@ -75,20 +76,20 @@ class FileTest extends \PhraseanetTestCase
$borderFile = new File(self::$DI['app'], self::$DI['app']['mediavorus']->guess($file), self::$DI['collection']);
$uuid = $borderFile->getUUID(true, false);
$this->assertTrue(\uuid::is_valid($uuid));
$this->assertTrue(Uuid::isValid($uuid));
$this->assertEquals($uuid, $borderFile->getUUID());
$borderFile = new File(self::$DI['app'], self::$DI['app']['mediavorus']->guess($file), self::$DI['collection']);
$newuuid = $borderFile->getUUID(true, true);
$this->assertTrue(\uuid::is_valid($newuuid));
$this->assertTrue(Uuid::isValid($newuuid));
$this->assertNotEquals($uuid, $newuuid);
$this->assertEquals($newuuid, $borderFile->getUUID());
$borderFile = new File(self::$DI['app'], self::$DI['app']['mediavorus']->guess($file), self::$DI['collection']);
$uuid = $borderFile->getUUID();
$this->assertTrue(\uuid::is_valid($uuid));
$this->assertTrue(Uuid::isValid($uuid));
$this->assertEquals($uuid, $newuuid);
$this->assertEquals($uuid, $borderFile->getUUID());

View File

@@ -5,6 +5,7 @@ namespace Alchemy\Tests\Phrasea\Controller\Admin;
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Authentication\ACLProvider;
use Alchemy\Phrasea\Border\File;
use Rhumsaa\Uuid\Uuid;
class AdminCollectionTest extends \PhraseanetAuthenticatedWebTestCase
{
@@ -540,7 +541,7 @@ class AdminCollectionTest extends \PhraseanetAuthenticatedWebTestCase
':parent_record_id' => 0,
':type' => 'unknown',
':sha256' => null,
':uuid' => \uuid::generate_v4(),
':uuid' => Uuid::uuid4(),
':originalname' => null,
':mime' => null,
]);

View File

@@ -4,6 +4,7 @@ namespace Alchemy\Tests\Phrasea\Controller\Admin;
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Border\File;
use Rhumsaa\Uuid\Uuid;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class DataboxTest extends \PhraseanetAuthenticatedWebTestCase
@@ -658,7 +659,7 @@ class DataboxTest extends \PhraseanetAuthenticatedWebTestCase
':parent_record_id' => 0,
':type' => 'unknown',
':sha256' => null,
':uuid' => \uuid::generate_v4(),
':uuid' => Uuid::uuid4(),
':originalname' => null,
':mime' => null,
]);

View File

@@ -11,6 +11,7 @@ use Alchemy\Phrasea\Model\Entities\Task;
use Alchemy\Phrasea\Model\Entities\LazaretSession;
use Doctrine\Common\Collections\ArrayCollection;
use Guzzle\Common\Exception\GuzzleException;
use Rhumsaa\Uuid\Uuid;
use Symfony\Component\HttpKernel\Client;
use Symfony\Component\HttpFoundation\Response;
@@ -2175,7 +2176,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase
$this->assertTrue(is_string($record['phrasea_type']));
$this->assertTrue(in_array($record['phrasea_type'], ['audio', 'document', 'image', 'video', 'flash', 'unknown']));
$this->assertArrayHasKey('uuid', $record);
$this->assertTrue(\uuid::is_valid($record['uuid']));
$this->assertTrue(Uuid::isValid($record['uuid']));
if (!is_null($record['thumbnail'])) {
$this->assertTrue(is_array($record['thumbnail']));
@@ -2228,7 +2229,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase
$this->assertArrayHasKey('uuid', $story);
$this->assertArrayHasKey('@entity@', $story);
$this->assertEquals(V1::OBJECT_TYPE_STORY, $story['@entity@']);
$this->assertTrue(\uuid::is_valid($story['uuid']));
$this->assertTrue(Uuid::isValid($story['uuid']));
if ( ! is_null($story['thumbnail'])) {
$this->assertTrue(is_array($story['thumbnail']));

View File

@@ -1,5 +1,7 @@
<?php
use Rhumsaa\Uuid\Uuid;
class record_adapterTest extends \PhraseanetAuthenticatedTestCase
{
private static $thumbtitled = false;
@@ -65,7 +67,7 @@ class record_adapterTest extends \PhraseanetAuthenticatedTestCase
public function testGet_uuid()
{
$this->assertTrue(uuid::is_valid(self::$DI['record_1']->get_uuid()));
$this->assertTrue(Uuid::isValid(self::$DI['record_1']->get_uuid()));
}
public function testGet_modification_date()

View File

@@ -1,116 +0,0 @@
<?php
class uuidTest extends \PhraseanetTestCase
{
/**
* @var uuid
*/
protected $object;
public function setUp()
{
parent::setUp();
$this->object = new uuid();
}
public function testGenerate_v3()
{
$datas = [];
for ($i = 0; $i < 1000; $i ++) {
$uuid = uuid::generate_v4();
if ( ! uuid::is_valid($uuid))
$this->fail('Generation d\'un uuid v4 invalide');
$uuid = uuid::generate_v3($uuid, self::$DI['app']['random.low']->generateString(8));
if ( ! uuid::is_valid($uuid))
$this->fail('Generation d\'un uuid v5 invalide');
$datas[] = $uuid;
}
$datas = array_unique($datas);
if (count($datas) !== 1000)
$this->fail('Generation de deux uuid identiques en v3' . count($datas));
unset($datas);
}
public function testGenerate_v4()
{
$datas = [];
for ($i = 0; $i < 1000; $i ++) {
$uuid = uuid::generate_v4();
if ( ! uuid::is_valid($uuid))
$this->fail('Generation d\'un uuid v4 invalide');
$datas[] = $uuid;
}
$datas = array_unique($datas);
if (count($datas) !== 1000)
$this->fail('Generation de deux uuid identiques en v4' . count($datas));
unset($datas);
}
public function testGenerate_v5()
{
$datas = [];
for ($i = 0; $i < 1000; $i ++) {
$uuid = uuid::generate_v4();
if ( ! uuid::is_valid($uuid))
$this->fail('Generation d\'un uuid v4 invalide');
$uuid = uuid::generate_v5($uuid, self::$DI['app']['random.low']->generateString(8));
if ( ! uuid::is_valid($uuid))
$this->fail('Generation d\'un uuid v5 invalide');
$datas[] = $uuid;
}
$datas = array_unique($datas);
if (count($datas) !== 1000)
$this->fail('Generation de deux uuid identiques en v5' . count($datas));
unset($datas);
}
public function testIs_valid()
{
for ($i = 0; $i < 1000; $i ++) {
$uuid = uuid::generate_v4();
if ( ! uuid::is_valid($uuid))
$this->fail('Generation d\'un uuid v4 invalide');
$uuid = uuid::generate_v5($uuid, self::$DI['app']['random.low']->generateString(8));
if ( ! uuid::is_valid($uuid))
$this->fail('Generation d\'un uuid v5 invalide');
$uuid = uuid::generate_v3($uuid, self::$DI['app']['random.low']->generateString(8));
if ( ! uuid::is_valid($uuid))
$this->fail('Generation d\'un uuid v3 invalide');
unset($uuid);
}
}
public function testCompare()
{
for ($i = 0; $i < 1000; $i ++) {
$uuid1 = uuid::generate_v4();
$uuid2 = uuid::generate_v4();
$this->assertFalse(uuid::compare($uuid1, $uuid2));
}
}
public function testIs_null()
{
$this->assertTrue(uuid::is_null('00000000-0000-0000-0000-000000000000'));
}
}