From 65590e5e9cb6bd2eb4a65d843aa972e127fba381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Je=CC=81ro=CC=82me=20Vieilledent?= Date: Thu, 25 Aug 2016 17:06:35 +0200 Subject: [PATCH 1/5] Add support for language in repository migration steps --- Core/Executor/ContentManager.php | 5 ++--- Core/Executor/ContentTypeManager.php | 20 ++++++++++---------- Core/Executor/RepositoryExecutor.php | 18 ++++++++++++++++++ Core/Executor/UserGroupManager.php | 2 +- Core/Executor/UserManager.php | 2 +- 5 files changed, 32 insertions(+), 15 deletions(-) diff --git a/Core/Executor/ContentManager.php b/Core/Executor/ContentManager.php index 7db1ba6d..062dce0b 100644 --- a/Core/Executor/ContentManager.php +++ b/Core/Executor/ContentManager.php @@ -85,8 +85,7 @@ protected function create() } $contentType = $contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier); - // FIXME: Defaulting in language code for now - $contentCreateStruct = $contentService->newContentCreateStruct($contentType, self::DEFAULT_LANGUAGE_CODE); + $contentCreateStruct = $contentService->newContentCreateStruct($contentType, $this->getLanguageCode()); $this->setFields($contentCreateStruct, $this->dsl['attributes'], $contentType); if (array_key_exists('remote_id', $this->dsl)) { @@ -246,7 +245,7 @@ protected function setFields(&$createOrUpdateStruct, array $fields, ContentType $fieldValue = $this->getSingleFieldValue($field[$fieldIdentifier], $fieldType, $this->context); } - $createOrUpdateStruct->setField($fieldIdentifier, $fieldValue, self::DEFAULT_LANGUAGE_CODE); + $createOrUpdateStruct->setField($fieldIdentifier, $fieldValue, $this->getLanguageCode()); } } diff --git a/Core/Executor/ContentTypeManager.php b/Core/Executor/ContentTypeManager.php index c9e417bf..2daaf4e1 100644 --- a/Core/Executor/ContentTypeManager.php +++ b/Core/Executor/ContentTypeManager.php @@ -36,20 +36,20 @@ protected function create() $contentTypeGroup = $contentTypeService->loadContentTypeGroup($this->dsl['content_type_group']); $contentTypeCreateStruct = $contentTypeService->newContentTypeCreateStruct($this->dsl['identifier']); - $contentTypeCreateStruct->mainLanguageCode = self::DEFAULT_LANGUAGE_CODE; + $contentTypeCreateStruct->mainLanguageCode = $this->getLanguageCode(); // Object Name pattern $contentTypeCreateStruct->nameSchema = $this->dsl['name_pattern']; // set names for the content type $contentTypeCreateStruct->names = array( - self::DEFAULT_LANGUAGE_CODE => $this->dsl['name'], + $this->getLanguageCode() => $this->dsl['name'], ); if (array_key_exists('description', $this->dsl)) { // set description for the content type $contentTypeCreateStruct->descriptions = array( - self::DEFAULT_LANGUAGE_CODE => $this->dsl['description'], + $this->getLanguageCode() => $this->dsl['description'], ); } @@ -97,19 +97,19 @@ protected function update() $contentTypeDraft = $contentTypeService->createContentTypeDraft($contentType); $contentTypeUpdateStruct = $contentTypeService->newContentTypeUpdateStruct(); - $contentTypeUpdateStruct->mainLanguageCode = self::DEFAULT_LANGUAGE_CODE; + $contentTypeUpdateStruct->mainLanguageCode = $this->getLanguageCode(); if (array_key_exists('new_identifier', $this->dsl)) { $contentTypeUpdateStruct->identifier = $this->dsl['new_identifier']; } if (array_key_exists('name', $this->dsl)) { - $contentTypeUpdateStruct->names = array(self::DEFAULT_LANGUAGE_CODE => $this->dsl['name']); + $contentTypeUpdateStruct->names = array($this->getLanguageCode() => $this->dsl['name']); } if (array_key_exists('description', $this->dsl)) { $contentTypeUpdateStruct->descriptions = array( - self::DEFAULT_LANGUAGE_CODE => $this->dsl['description'], + $this->getLanguageCode() => $this->dsl['description'], ); } @@ -249,10 +249,10 @@ private function createFieldDefinition(ContentTypeService $contentTypeService, a if (!in_array($key, array('identifier', 'type'))) { switch ($key) { case 'name': - $fieldDefinition->names = array(self::DEFAULT_LANGUAGE_CODE => $value); + $fieldDefinition->names = array($this->getLanguageCode() => $value); break; case 'description': - $fieldDefinition->descriptions = array(self::DEFAULT_LANGUAGE_CODE => $value); + $fieldDefinition->descriptions = array($this->getLanguageCode() => $value); break; case 'required': $fieldDefinition->isRequired = $value; @@ -304,10 +304,10 @@ private function updateFieldDefinition(ContentTypeService $contentTypeService, a $fieldDefinitionUpdateStruct->identifier = $value; break; case 'name': - $fieldDefinitionUpdateStruct->names = array(self::DEFAULT_LANGUAGE_CODE => $value); + $fieldDefinitionUpdateStruct->names = array($this->getLanguageCode() => $value); break; case 'description': - $fieldDefinitionUpdateStruct->descriptions = array(self::DEFAULT_LANGUAGE_CODE => $value); + $fieldDefinitionUpdateStruct->descriptions = array($this->getLanguageCode() => $value); break; case 'required': $fieldDefinitionUpdateStruct->isRequired = $value; diff --git a/Core/Executor/RepositoryExecutor.php b/Core/Executor/RepositoryExecutor.php index 67224feb..fa205e6a 100644 --- a/Core/Executor/RepositoryExecutor.php +++ b/Core/Executor/RepositoryExecutor.php @@ -45,6 +45,13 @@ abstract class RepositoryExecutor extends AbstractExecutor */ protected $repository; + /** + * Language code for current step. + * + * @var string + */ + private $languageCode; + /** * The bundle object representing the bundle the currently processed migration is in. * @@ -87,6 +94,7 @@ public function execute(MigrationStep $step) $this->dsl = $step->dsl; $this->context = $step->context; + $this->languageCode = isset($this->dsl['lang']) ? $this->dsl['lang'] : self::DEFAULT_LANGUAGE_CODE; if (method_exists($this, $action)) { @@ -132,4 +140,14 @@ protected function loginUser($userId) return $previousUser->id; } + + /** + * Returns selected language code. + * + * @return string + */ + protected function getLanguageCode() + { + return $this->languageCode; + } } diff --git a/Core/Executor/UserGroupManager.php b/Core/Executor/UserGroupManager.php index cd573c05..48e0f550 100644 --- a/Core/Executor/UserGroupManager.php +++ b/Core/Executor/UserGroupManager.php @@ -27,7 +27,7 @@ protected function create() $contentType = $this->repository->getContentTypeService()->loadContentTypeByIdentifier("user_group"); - $userGroupCreateStruct = $userService->newUserGroupCreateStruct(self::DEFAULT_LANGUAGE_CODE, $contentType); + $userGroupCreateStruct = $userService->newUserGroupCreateStruct($this->getLanguageCode(), $contentType); $userGroupCreateStruct->setField('name', $this->dsl['name']); if (array_key_exists('description', $this->dsl)) { diff --git a/Core/Executor/UserManager.php b/Core/Executor/UserManager.php index 004f0492..2b2f30d4 100644 --- a/Core/Executor/UserManager.php +++ b/Core/Executor/UserManager.php @@ -49,7 +49,7 @@ protected function create() $this->dsl['username'], $this->dsl['email'], $this->dsl['password'], - self::DEFAULT_LANGUAGE_CODE, + $this->getLanguageCode(), $userContentType ); $userCreateStruct->setField('first_name', $this->dsl['first_name']); From 988c895698a02879fb055a97818718b4fdb4661c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Je=CC=81ro=CC=82me=20Vieilledent?= Date: Fri, 26 Aug 2016 11:02:47 +0200 Subject: [PATCH 2/5] Removed var_dump() leftover --- Core/ReferenceResolver/CustomReferenceResolver.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Core/ReferenceResolver/CustomReferenceResolver.php b/Core/ReferenceResolver/CustomReferenceResolver.php index c36676ae..94846e8e 100644 --- a/Core/ReferenceResolver/CustomReferenceResolver.php +++ b/Core/ReferenceResolver/CustomReferenceResolver.php @@ -30,7 +30,6 @@ public function getReferenceValue($identifier) { $identifier = $this->getReferenceIdentifier($identifier); if (!array_key_exists($identifier, $this->references)) { -var_dump($this->references); throw new \Exception("No reference set with identifier '$identifier'"); } From 65ac3886011c37bdab336638319d30c9af398996 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Je=CC=81ro=CC=82me=20Vieilledent?= Date: Fri, 26 Aug 2016 11:02:57 +0200 Subject: [PATCH 3/5] Fixed indentation --- Core/ReferenceResolver/CustomReferenceResolver.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Core/ReferenceResolver/CustomReferenceResolver.php b/Core/ReferenceResolver/CustomReferenceResolver.php index 94846e8e..477b17b5 100644 --- a/Core/ReferenceResolver/CustomReferenceResolver.php +++ b/Core/ReferenceResolver/CustomReferenceResolver.php @@ -45,10 +45,10 @@ public function getReferenceValue($identifier) */ public function addReference($identifier, $value) { - if (array_key_exists($identifier, $this->references)) { - throw new \Exception("A reference with identifier '$identifier' already exists"); - } + if (array_key_exists($identifier, $this->references)) { + throw new \Exception("A reference with identifier '$identifier' already exists"); + } - $this->references[$identifier] = $value; + $this->references[$identifier] = $value; } } From 33de6d8776d6b5d6c7b89e1ffe40c57ad1e9eec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Je=CC=81ro=CC=82me=20Vieilledent?= Date: Fri, 26 Aug 2016 11:37:56 +0200 Subject: [PATCH 4/5] Added support for default language code as console command option --- API/LanguageAwareInterface.php | 32 +++++++++++++++++++++++ Command/MigrateCommand.php | 7 ++++- Core/Executor/RepositoryExecutor.php | 38 ++++++++++++++++++++-------- Core/MigrationService.php | 12 ++++++++- 4 files changed, 76 insertions(+), 13 deletions(-) create mode 100644 API/LanguageAwareInterface.php diff --git a/API/LanguageAwareInterface.php b/API/LanguageAwareInterface.php new file mode 100644 index 00000000..c9b6bec8 --- /dev/null +++ b/API/LanguageAwareInterface.php @@ -0,0 +1,32 @@ +addOption('default-language', null, InputOption::VALUE_REQUIRED, "Default language code that will be used if no language is provided in migration steps") ->addOption('ignore-failures', null, InputOption::VALUE_NONE, "Keep executing migrations even if one fails") ->addOption('clear-cache', null, InputOption::VALUE_NONE, "Clear the cache after the command finishes") ->addOption('no-interaction', 'n', InputOption::VALUE_NONE, "Do not ask any interactive question") @@ -147,7 +148,11 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln("Processing $name"); try { - $migrationsService->executeMigration($migrationDefinition, !$input->getOption('no-transactions')); + $migrationsService->executeMigration( + $migrationDefinition, + !$input->getOption('no-transactions'), + $input->getOption('default-language') + ); } catch(\Exception $e) { if ($input->getOption('ignore-failures')) { $output->writeln("\nMigration failed! Reason: " . $e->getMessage() . "\n"); diff --git a/Core/Executor/RepositoryExecutor.php b/Core/Executor/RepositoryExecutor.php index fa205e6a..3e312a19 100644 --- a/Core/Executor/RepositoryExecutor.php +++ b/Core/Executor/RepositoryExecutor.php @@ -2,6 +2,7 @@ namespace Kaliop\eZMigrationBundle\Core\Executor; +use Kaliop\eZMigrationBundle\API\LanguageAwareInterface; use Kaliop\eZMigrationBundle\API\ReferenceResolverInterface; use Symfony\Component\HttpKernel\Bundle\BundleInterface; use Kaliop\eZMigrationBundle\API\Value\MigrationStep; @@ -11,12 +12,10 @@ /** * The core manager class that all migration action managers inherit from. */ -abstract class RepositoryExecutor extends AbstractExecutor +abstract class RepositoryExecutor extends AbstractExecutor implements LanguageAwareInterface { /** * Constant defining the default language code - * - * @todo inject via config parameter */ const DEFAULT_LANGUAGE_CODE = 'eng-GB'; @@ -52,6 +51,11 @@ abstract class RepositoryExecutor extends AbstractExecutor */ private $languageCode; + /** + * @var string + */ + private $defaultLanguageCode; + /** * The bundle object representing the bundle the currently processed migration is in. * @@ -94,7 +98,9 @@ public function execute(MigrationStep $step) $this->dsl = $step->dsl; $this->context = $step->context; - $this->languageCode = isset($this->dsl['lang']) ? $this->dsl['lang'] : self::DEFAULT_LANGUAGE_CODE; + if (isset($this->dsl['lang'])) { + $this->setLanguageCode($this->dsl['lang']); + } if (method_exists($this, $action)) { @@ -141,13 +147,23 @@ protected function loginUser($userId) return $previousUser->id; } - /** - * Returns selected language code. - * - * @return string - */ - protected function getLanguageCode() + public function setLanguageCode($languageCode) + { + $this->languageCode = $languageCode; + } + + public function getLanguageCode() + { + return $this->languageCode ?: $this->getDefaultLanguageCode(); + } + + public function setDefaultLanguageCode($languageCode) + { + $this->defaultLanguageCode = $languageCode; + } + + public function getDefaultLanguageCode() { - return $this->languageCode; + return $this->defaultLanguageCode ?: self::DEFAULT_LANGUAGE_CODE; } } diff --git a/Core/MigrationService.php b/Core/MigrationService.php index 2b5c7a62..09a751d4 100644 --- a/Core/MigrationService.php +++ b/Core/MigrationService.php @@ -3,6 +3,7 @@ namespace Kaliop\eZMigrationBundle\Core; use Kaliop\eZMigrationBundle\API\Collection\MigrationDefinitionCollection; +use Kaliop\eZMigrationBundle\API\LanguageAwareInterface; use Kaliop\eZMigrationBundle\API\StorageHandlerInterface; use Kaliop\eZMigrationBundle\API\LoaderInterface; use Kaliop\eZMigrationBundle\API\DefinitionParserInterface; @@ -155,7 +156,7 @@ public function parseMigrationDefinition(MigrationDefinition $migrationDefinitio * * @todo add support for skipped migrations, partially executed migrations */ - public function executeMigration(MigrationDefinition $migrationDefinition, $useTransaction=true) + public function executeMigration(MigrationDefinition $migrationDefinition, $useTransaction = true, $defaultLanguageCode = null) { if ($migrationDefinition->status == MigrationDefinition::STATUS_TO_PARSE) { $migrationDefinition = $this->parseMigrationDefinition($migrationDefinition); @@ -165,6 +166,15 @@ public function executeMigration(MigrationDefinition $migrationDefinition, $useT throw new \Exception("Can not execute migration '{$migrationDefinition->name}': {$migrationDefinition->parsingError}"); } + // Inject default language code in executors that support it. + if ($defaultLanguageCode) { + foreach ($this->executors as $executor) { + if ($executor instanceof LanguageAwareInterface) { + $executor->setDefaultLanguageCode($defaultLanguageCode); + } + } + } + // set migration as begun - has to be in own db transaction $migration = $this->storageHandler->startMigration($migrationDefinition); From 0eb0bf0148412de0755122cc80bc2ef75fe87727 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Je=CC=81ro=CC=82me=20Vieilledent?= Date: Fri, 26 Aug 2016 11:46:45 +0200 Subject: [PATCH 5/5] Updated DSL doc for language support --- Resources/doc/DSL/ManageContent.yml | 2 ++ Resources/doc/DSL/ManageContentType.yml | 2 ++ Resources/doc/DSL/ManageUsersAndGroups.yml | 3 ++- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Resources/doc/DSL/ManageContent.yml b/Resources/doc/DSL/ManageContent.yml index e9d0c3c5..6e7fbf11 100644 --- a/Resources/doc/DSL/ManageContent.yml +++ b/Resources/doc/DSL/ManageContent.yml @@ -6,6 +6,7 @@ priority: 0 # Set the priority of the main location other_locations: [x, y, z] # Optional, node ids of other parent location remote_id: custom_remote_id # Optional, will set an object remote id as the remote ID and a location remote ID with a _location suffix + lang: xxx-YY # Optional, will fallback to default language if not provided (--default-language option or 'eng-GB' by default) attributes: - attribute1: value1 - attribute2: value2 @@ -56,6 +57,7 @@ - parent_location_id: x # int|int[] - parent_location_remote_id: xxx # string|string[] - content_type: yyy # string|string[] a content type identifier + lang: xxx-YY # Optional, will fallback to default language if not provided (--default-language option or 'eng-GB' by default) attributes: # the list of attribute identifier value pairs - attribute1: value1 - attribute2: value2 diff --git a/Resources/doc/DSL/ManageContentType.yml b/Resources/doc/DSL/ManageContentType.yml index 1355accc..b1f128a6 100644 --- a/Resources/doc/DSL/ManageContentType.yml +++ b/Resources/doc/DSL/ManageContentType.yml @@ -8,6 +8,7 @@ description: xyz # Optional url_name_pattern: pattern # Optional is_container: true|false # Optional, defaults to false + lang: xxx-YY # Optional, will fallback to default language if not provided (--default-language option or 'eng-GB' by default) attributes: - type: xyz # Attribute type (See list https://confluence.ez.no/display/EZP/FieldTypes) @@ -40,6 +41,7 @@ name_pattern: xyz # Optional, will be updated if set url_name_pattern: xyz # Optional, will be updated if set is_container: true|false # Optional, will be updated if set + lang: xxx-YY # Optional, will fallback to default language if not provided (--default-language option or 'eng-GB' by default) attributes: # Optional, if set will update existing ones or add new ones. - identifier: xyz # Identifier of the attribute to update or identifier of the new attribute to add diff --git a/Resources/doc/DSL/ManageUsersAndGroups.yml b/Resources/doc/DSL/ManageUsersAndGroups.yml index 9cc21321..9b86bcf7 100644 --- a/Resources/doc/DSL/ManageUsersAndGroups.yml +++ b/Resources/doc/DSL/ManageUsersAndGroups.yml @@ -6,7 +6,7 @@ username: xyz email: xyz password: xyz - main_language: xyz # Optional, default to eng-GB + lang: xxx-YY # Optional, will fallback to default language if not provided (--default-language option or 'eng-GB' by default) groups: [x, y, z] # The user group ID or IDs (Object IDs) to put the user into # The list in references tells the manager to store specific values for later use # by other steps in the current migration. @@ -64,6 +64,7 @@ name: xyz # Optional description: xyz # Optional parent_group_id: x # Optional, the new parent user group ID + lang: xxx-YY # Optional, will fallback to default language if not provided (--default-language option or 'eng-GB' by default) # The list in references tells the manager to store specific values for later use # by other steps in the current migration. references: #Optional