diff --git a/src/PHPCR/Util/Console/Command/WorkspaceImportCommand.php b/src/PHPCR/Util/Console/Command/WorkspaceImportCommand.php index 1bf356d..85960bb 100644 --- a/src/PHPCR/Util/Console/Command/WorkspaceImportCommand.php +++ b/src/PHPCR/Util/Console/Command/WorkspaceImportCommand.php @@ -19,6 +19,13 @@ */ class WorkspaceImportCommand extends BaseCommand { + private static $uuidBehavior = array( + 'new' => ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW, + 'remove' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_REMOVE_EXISTING, + 'replace' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_REPLACE_EXISTING, + 'throw' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_THROW, + ); + /** * {@inheritDoc} */ @@ -30,6 +37,7 @@ protected function configure() ->setName('phpcr:workspace:import') ->addArgument('filename', null, 'The xml file to import') ->addOption('parentpath', 'p', InputOption::VALUE_OPTIONAL, 'Repository path to the parent where to import the file contents', '/') + ->addOption('uuid-behavior', null, InputOption::VALUE_REQUIRED, 'How to handle UUID collisions during the import', 'new') ->setDescription('Import xml data into the repository, either in JCR system view format or arbitrary xml') ->setHelp(<<import command uses the PHPCR SessionInterface::importXml method @@ -40,6 +48,17 @@ protected function configure() If the parentpath option is set, the document is imported to that path. Otherwise the document is imported at the repository root. + +The optional uuid-behavior option describes how UUIDs should be +handled. The following options are available: + +* new recreate a new uuid for each imported node; +* remove on collision, remove the old node from the repository and + put the imported data in the tree; +* replace on collision, replace the existing node with the one being + imported. All children of the imported node also go to the new path; +* throw throw an exception on uuid collision. + EOF ) ; @@ -61,11 +80,15 @@ protected function execute(InputInterface $input, OutputInterface $output) return 1; } - $session->importXml( - $parentPath, - $filename, - ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW - ); + $uuidBehavior = $input->getOption('uuid-behavior'); + if (!array_key_exists($uuidBehavior, self::$uuidBehavior)) { + $output->writeln(sprintf('UUID-Behavior "%s" is not supported', $uuidBehavior)); + $output->writeln(sprintf('Supported behaviors are %s', implode(', ', array_keys(self::$uuidBehavior)))); + + return 1; + } + + $session->importXML($parentPath, $filename, self::$uuidBehavior[$uuidBehavior]); $session->save(); $output->writeln(sprintf( diff --git a/src/PHPCR/Util/NodeHelper.php b/src/PHPCR/Util/NodeHelper.php index 778e647..7e6b627 100644 --- a/src/PHPCR/Util/NodeHelper.php +++ b/src/PHPCR/Util/NodeHelper.php @@ -154,10 +154,10 @@ public static function generateAutoNodeName($usedNames, $namespaces, $defaultNam return self::generateWithPrefix($usedNames, ''); } - /* - * "somePrefix:" where somePrefix is a syntactically - * valid namespace prefix - */ + /* + * "somePrefix:" where somePrefix is a syntactically + * valid namespace prefix + */ if (':' == $nameHint[strlen($nameHint)-1] && substr_count($nameHint, ':') === 1 && preg_match('#^[a-zA-Z][a-zA-Z0-9]*:$#', $nameHint) diff --git a/tests/PHPCR/Tests/Util/Console/Command/WorkspaceImportCommandTest.php b/tests/PHPCR/Tests/Util/Console/Command/WorkspaceImportCommandTest.php index d4641a5..8c5c6af 100644 --- a/tests/PHPCR/Tests/Util/Console/Command/WorkspaceImportCommandTest.php +++ b/tests/PHPCR/Tests/Util/Console/Command/WorkspaceImportCommandTest.php @@ -2,9 +2,9 @@ namespace PHPCR\Tests\Util\Console\Command; -use Symfony\Component\Console\Application; -use PHPCR\Util\Console\Command\WorkspaceImportCommand; +use PHPCR\ImportUUIDBehaviorInterface; use PHPCR\RepositoryInterface; +use PHPCR\Util\Console\Command\WorkspaceImportCommand; class WorkspaceImportCommandTest extends BaseCommandTest { @@ -14,7 +14,7 @@ public function setUp() $this->application->add(new WorkspaceImportCommand()); } - public function testNodeTypeList() + public function testImport() { $this->session->expects($this->once()) ->method('getRepository') @@ -24,7 +24,8 @@ public function testNodeTypeList() ->with(RepositoryInterface::OPTION_XML_IMPORT_SUPPORTED) ->will($this->returnValue(true)); $this->session->expects($this->once()) - ->method('importXml'); + ->method('importXml') + ->with('/', 'test_import.xml', ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW); $ct = $this->executeCommand('phpcr:workspace:import', array( 'filename' => 'test_import.xml' @@ -32,4 +33,25 @@ public function testNodeTypeList() $this->assertContains('Successfully imported', $ct->getDisplay()); } + + public function testImportUuidBehaviorThrow() + { + $this->session->expects($this->once()) + ->method('getRepository') + ->will($this->returnValue($this->repository)); + $this->repository->expects($this->once()) + ->method('getDescriptor') + ->with(RepositoryInterface::OPTION_XML_IMPORT_SUPPORTED) + ->will($this->returnValue(true)); + $this->session->expects($this->once()) + ->method('importXml') + ->with('/', 'test_import.xml', ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_THROW); + + $ct = $this->executeCommand('phpcr:workspace:import', array( + 'filename' => 'test_import.xml', + '--uuid-behavior' => 'throw', + )); + + $this->assertContains('Successfully imported', $ct->getDisplay()); + } }