Skip to content

Commit

Permalink
Merge pull request #185 from wachterjohannes/enhancement/workspace-im…
Browse files Browse the repository at this point in the history
…port-uuid-behavior

Added option to control the uuid-behavior
  • Loading branch information
dbu authored Feb 12, 2018
2 parents 9f51c3e + bd16097 commit 14e1560
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 15 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ sudo: false
matrix:
include:
- php: 5.3
dist: precise
env: PACKAGE_VERSION=low

before_script:
Expand Down
33 changes: 28 additions & 5 deletions src/PHPCR/Util/Console/Command/WorkspaceImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand All @@ -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(<<<EOF
The <info>import</info> command uses the PHPCR SessionInterface::importXml method
Expand All @@ -40,6 +48,17 @@ protected function configure()
If the <info>parentpath</info> option is set, the document is imported to that
path. Otherwise the document is imported at the repository root.
The optional <info>uuid-behavior</info> option describes how UUIDs should be
handled. The following options are available:
* <info>new</info> recreate a new uuid for each imported node;
* <info>remove</info> on collision, remove the old node from the repository and
put the imported data in the tree;
* <info>replace</info> on collision, replace the existing node with the one being
imported. All children of the imported node also go to the new path;
* <info>throw</info> throw an exception on uuid collision.
EOF
)
;
Expand All @@ -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('<error>UUID-Behavior "%s" is not supported</error>', $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(
Expand Down
8 changes: 4 additions & 4 deletions src/PHPCR/Util/NodeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ public static function generateAutoNodeName($usedNames, $namespaces, $defaultNam
return self::generateWithPrefix($usedNames, '');
}

/*
* "<i>somePrefix</i>:" where <i>somePrefix</i> is a syntactically
* valid namespace prefix
*/
/*
* "<i>somePrefix</i>:" where <i>somePrefix</i> 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)
Expand Down
2 changes: 1 addition & 1 deletion src/PHPCR/Util/QOM/Sql2Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected function scan($sql2)
$token = strtok(" \n\t");
}

$regexp = '';
$regexp = array();
foreach ($tokens as $token) {
$regexp[] = preg_quote($token, '/');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function testNodeUpdate($options)
$this->setupQueryManager($options);

$args = array(
'--query-language' => null,
'--query-language' => 'jcr-sql2',
'--query' => $options['query'],
'--no-interaction' => true,
'--set-prop' => array(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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')
Expand All @@ -24,12 +24,34 @@ 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'
));

$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());
}
}
14 changes: 14 additions & 0 deletions tests/PHPCR/Tests/Util/QOM/Sql2ToQomQueryConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@

namespace PHPCR\Tests\Util\QOM;

use PHPCR\Query\QOM\QueryObjectModelFactoryInterface;
use PHPCR\Util\QOM\Sql2ToQomQueryConverter;
use PHPCR\Util\ValueConverter;

class Sql2ToQomQueryConverterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var QueryObjectModelFactoryInterface
*/
protected $qomFactory;

/**
* @var ValueConverter
*/
protected $valueConverter;

/**
* @var Sql2ToQomQueryConverter
*/
protected $converter;

public function setUp()
{
$this->qomFactory = $this->getMock('PHPCR\Query\QOM\QueryObjectModelFactoryInterface');
Expand Down

0 comments on commit 14e1560

Please sign in to comment.