Skip to content

Commit

Permalink
Merge pull request #60 from lolautruche/feature/languageCode
Browse files Browse the repository at this point in the history
Added support for language code
  • Loading branch information
gggeek authored Aug 26, 2016
2 parents 67c8cf4 + 0eb0bf0 commit caf771c
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 26 deletions.
32 changes: 32 additions & 0 deletions API/LanguageAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Kaliop\eZMigrationBundle\API;

interface LanguageAwareInterface
{
/**
* Injects language code, with xxx-YY format.
* e.g. fre-FR
*
* @param string $languageCode
*/
public function setLanguageCode($languageCode);

/**
* @return string
*/
public function getLanguageCode();

/**
* Sets default language code, with xxx-YY format.
* e.g. fre-FR
*
* @param string $languageCode
*/
public function setDefaultLanguageCode($languageCode);

/**
* @return string
*/
public function getDefaultLanguageCode();
}
7 changes: 6 additions & 1 deletion Command/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected function configure()
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
"The directory or file to load the migration definitions from"
)
->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")
Expand Down Expand Up @@ -147,7 +148,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln("<info>Processing $name</info>");

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("\n<error>Migration failed! Reason: " . $e->getMessage() . "</error>\n");
Expand Down
5 changes: 2 additions & 3 deletions Core/Executor/ContentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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());
}
}

Expand Down
20 changes: 10 additions & 10 deletions Core/Executor/ContentTypeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
);
}

Expand Down Expand Up @@ -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'],
);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
40 changes: 37 additions & 3 deletions Core/Executor/RepositoryExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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';

Expand Down Expand Up @@ -45,6 +44,18 @@ abstract class RepositoryExecutor extends AbstractExecutor
*/
protected $repository;

/**
* Language code for current step.
*
* @var string
*/
private $languageCode;

/**
* @var string
*/
private $defaultLanguageCode;

/**
* The bundle object representing the bundle the currently processed migration is in.
*
Expand Down Expand Up @@ -87,6 +98,9 @@ public function execute(MigrationStep $step)

$this->dsl = $step->dsl;
$this->context = $step->context;
if (isset($this->dsl['lang'])) {
$this->setLanguageCode($this->dsl['lang']);
}

if (method_exists($this, $action)) {

Expand Down Expand Up @@ -132,4 +146,24 @@ protected function loginUser($userId)

return $previousUser->id;
}

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->defaultLanguageCode ?: self::DEFAULT_LANGUAGE_CODE;
}
}
2 changes: 1 addition & 1 deletion Core/Executor/UserGroupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion Core/Executor/UserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
12 changes: 11 additions & 1 deletion Core/MigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down
9 changes: 4 additions & 5 deletions Core/ReferenceResolver/CustomReferenceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'");
}

Expand All @@ -46,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;
}
}
2 changes: 2 additions & 0 deletions Resources/doc/DSL/ManageContent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Resources/doc/DSL/ManageContentType.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Resources/doc/DSL/ManageUsersAndGroups.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit caf771c

Please sign in to comment.