Skip to content

Commit 14e1560

Browse files
authored
Merge pull request #185 from wachterjohannes/enhancement/workspace-import-uuid-behavior
Added option to control the uuid-behavior
2 parents 9f51c3e + bd16097 commit 14e1560

File tree

7 files changed

+75
-15
lines changed

7 files changed

+75
-15
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ sudo: false
1414
matrix:
1515
include:
1616
- php: 5.3
17+
dist: precise
1718
env: PACKAGE_VERSION=low
1819

1920
before_script:

src/PHPCR/Util/Console/Command/WorkspaceImportCommand.php

+28-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
*/
2020
class WorkspaceImportCommand extends BaseCommand
2121
{
22+
private static $uuidBehavior = array(
23+
'new' => ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW,
24+
'remove' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_REMOVE_EXISTING,
25+
'replace' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_REPLACE_EXISTING,
26+
'throw' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_THROW,
27+
);
28+
2229
/**
2330
* {@inheritDoc}
2431
*/
@@ -30,6 +37,7 @@ protected function configure()
3037
->setName('phpcr:workspace:import')
3138
->addArgument('filename', null, 'The xml file to import')
3239
->addOption('parentpath', 'p', InputOption::VALUE_OPTIONAL, 'Repository path to the parent where to import the file contents', '/')
40+
->addOption('uuid-behavior', null, InputOption::VALUE_REQUIRED, 'How to handle UUID collisions during the import', 'new')
3341
->setDescription('Import xml data into the repository, either in JCR system view format or arbitrary xml')
3442
->setHelp(<<<EOF
3543
The <info>import</info> command uses the PHPCR SessionInterface::importXml method
@@ -40,6 +48,17 @@ protected function configure()
4048
4149
If the <info>parentpath</info> option is set, the document is imported to that
4250
path. Otherwise the document is imported at the repository root.
51+
52+
The optional <info>uuid-behavior</info> option describes how UUIDs should be
53+
handled. The following options are available:
54+
55+
* <info>new</info> recreate a new uuid for each imported node;
56+
* <info>remove</info> on collision, remove the old node from the repository and
57+
put the imported data in the tree;
58+
* <info>replace</info> on collision, replace the existing node with the one being
59+
imported. All children of the imported node also go to the new path;
60+
* <info>throw</info> throw an exception on uuid collision.
61+
4362
EOF
4463
)
4564
;
@@ -61,11 +80,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
6180
return 1;
6281
}
6382

64-
$session->importXml(
65-
$parentPath,
66-
$filename,
67-
ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW
68-
);
83+
$uuidBehavior = $input->getOption('uuid-behavior');
84+
if (!array_key_exists($uuidBehavior, self::$uuidBehavior)) {
85+
$output->writeln(sprintf('<error>UUID-Behavior "%s" is not supported</error>', $uuidBehavior));
86+
$output->writeln(sprintf('Supported behaviors are %s', implode(', ', array_keys(self::$uuidBehavior))));
87+
88+
return 1;
89+
}
90+
91+
$session->importXML($parentPath, $filename, self::$uuidBehavior[$uuidBehavior]);
6992
$session->save();
7093

7194
$output->writeln(sprintf(

src/PHPCR/Util/NodeHelper.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ public static function generateAutoNodeName($usedNames, $namespaces, $defaultNam
154154
return self::generateWithPrefix($usedNames, '');
155155
}
156156

157-
/*
158-
* "<i>somePrefix</i>:" where <i>somePrefix</i> is a syntactically
159-
* valid namespace prefix
160-
*/
157+
/*
158+
* "<i>somePrefix</i>:" where <i>somePrefix</i> is a syntactically
159+
* valid namespace prefix
160+
*/
161161
if (':' == $nameHint[strlen($nameHint)-1]
162162
&& substr_count($nameHint, ':') === 1
163163
&& preg_match('#^[a-zA-Z][a-zA-Z0-9]*:$#', $nameHint)

src/PHPCR/Util/QOM/Sql2Scanner.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ protected function scan($sql2)
159159
$token = strtok(" \n\t");
160160
}
161161

162-
$regexp = '';
162+
$regexp = array();
163163
foreach ($tokens as $token) {
164164
$regexp[] = preg_quote($token, '/');
165165
}

tests/PHPCR/Tests/Util/Console/Command/NodesUpdateCommandTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function testNodeUpdate($options)
8888
$this->setupQueryManager($options);
8989

9090
$args = array(
91-
'--query-language' => null,
91+
'--query-language' => 'jcr-sql2',
9292
'--query' => $options['query'],
9393
'--no-interaction' => true,
9494
'--set-prop' => array(),

tests/PHPCR/Tests/Util/Console/Command/WorkspaceImportCommandTest.php

+26-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace PHPCR\Tests\Util\Console\Command;
44

5-
use Symfony\Component\Console\Application;
6-
use PHPCR\Util\Console\Command\WorkspaceImportCommand;
5+
use PHPCR\ImportUUIDBehaviorInterface;
76
use PHPCR\RepositoryInterface;
7+
use PHPCR\Util\Console\Command\WorkspaceImportCommand;
88

99
class WorkspaceImportCommandTest extends BaseCommandTest
1010
{
@@ -14,7 +14,7 @@ public function setUp()
1414
$this->application->add(new WorkspaceImportCommand());
1515
}
1616

17-
public function testNodeTypeList()
17+
public function testImport()
1818
{
1919
$this->session->expects($this->once())
2020
->method('getRepository')
@@ -24,12 +24,34 @@ public function testNodeTypeList()
2424
->with(RepositoryInterface::OPTION_XML_IMPORT_SUPPORTED)
2525
->will($this->returnValue(true));
2626
$this->session->expects($this->once())
27-
->method('importXml');
27+
->method('importXml')
28+
->with('/', 'test_import.xml', ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW);
2829

2930
$ct = $this->executeCommand('phpcr:workspace:import', array(
3031
'filename' => 'test_import.xml'
3132
));
3233

3334
$this->assertContains('Successfully imported', $ct->getDisplay());
3435
}
36+
37+
public function testImportUuidBehaviorThrow()
38+
{
39+
$this->session->expects($this->once())
40+
->method('getRepository')
41+
->will($this->returnValue($this->repository));
42+
$this->repository->expects($this->once())
43+
->method('getDescriptor')
44+
->with(RepositoryInterface::OPTION_XML_IMPORT_SUPPORTED)
45+
->will($this->returnValue(true));
46+
$this->session->expects($this->once())
47+
->method('importXml')
48+
->with('/', 'test_import.xml', ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_THROW);
49+
50+
$ct = $this->executeCommand('phpcr:workspace:import', array(
51+
'filename' => 'test_import.xml',
52+
'--uuid-behavior' => 'throw',
53+
));
54+
55+
$this->assertContains('Successfully imported', $ct->getDisplay());
56+
}
3557
}

tests/PHPCR/Tests/Util/QOM/Sql2ToQomQueryConverterTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@
22

33
namespace PHPCR\Tests\Util\QOM;
44

5+
use PHPCR\Query\QOM\QueryObjectModelFactoryInterface;
56
use PHPCR\Util\QOM\Sql2ToQomQueryConverter;
7+
use PHPCR\Util\ValueConverter;
68

79
class Sql2ToQomQueryConverterTest extends \PHPUnit_Framework_TestCase
810
{
11+
/**
12+
* @var QueryObjectModelFactoryInterface
13+
*/
914
protected $qomFactory;
15+
16+
/**
17+
* @var ValueConverter
18+
*/
1019
protected $valueConverter;
1120

21+
/**
22+
* @var Sql2ToQomQueryConverter
23+
*/
24+
protected $converter;
25+
1226
public function setUp()
1327
{
1428
$this->qomFactory = $this->getMock('PHPCR\Query\QOM\QueryObjectModelFactoryInterface');

0 commit comments

Comments
 (0)