Last active
February 20, 2024 19:29
-
-
Save james2001/7a70c30b7a7ed6e56e605ec8cbc02f8e to your computer and use it in GitHub Desktop.
Sulu property migration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Jackalope\Query\Row; | |
use PHPCR\Migrations\VersionInterface; | |
use PHPCR\SessionInterface; | |
use Sulu\Component\Localization\Localization; | |
use Symfony\Component\DependencyInjection\ContainerAwareInterface; | |
use Symfony\Component\DependencyInjection\ContainerAwareTrait; | |
class Version202402092204 implements VersionInterface, ContainerAwareInterface | |
{ | |
use ContainerAwareTrait; | |
private const TEMPLATE = 'project_detail'; | |
private const OLD_PROPERTY = 'client_company_logo'; | |
private const NEW_PROPERTY = 'image'; | |
public function up(SessionInterface $session) | |
{ | |
$liveSession = $this->container->get('sulu_document_manager.live_session'); | |
$localizations = $this->container->get('sulu_core.webspace.webspace_manager')->getAllLocalizations(); | |
/** @var Localization $localization */ | |
foreach ($localizations as $localization) { | |
$this->upgrade($session,$localization); | |
$this->upgrade($liveSession,$localization); | |
} | |
$session->save(); | |
$liveSession->save(); | |
} | |
public function down(SessionInterface $session) | |
{ | |
$liveSession = $this->container->get('sulu_document_manager.live_session'); | |
$localizations = $this->container->get('sulu_core.webspace.webspace_manager')->getAllLocalizations(); | |
/** @var Localization $localization */ | |
foreach ($localizations as $localization) { | |
$this->downgrade($session,$localization); | |
$this->downgrade($liveSession,$localization); | |
} | |
$session->save(); | |
$liveSession->save(); | |
} | |
/** | |
* Upgrade all nodes in given session. | |
*/ | |
private function upgrade(SessionInterface $session, Localization $localization) | |
{ | |
/** @var Row $row */ | |
foreach ($this->getRowsToMigrate($session, $localization) as $row) { | |
$node = $row->getNode(); | |
$localizedOldPropertyName = \sprintf('i18n:%s-%s', $localization->getLocale(), self::OLD_PROPERTY); | |
$localizedNewPropertyName = \sprintf('i18n:%s-%s', $localization->getLocale(), self::NEW_PROPERTY); | |
if ($node->hasProperty($localizedOldPropertyName)) { | |
$node->setProperty($localizedNewPropertyName, $node->getPropertyValue($localizedOldPropertyName)); | |
$node->setProperty($localizedOldPropertyName, null); | |
} | |
} | |
} | |
/** | |
* Downgrades all nodes in given session. | |
*/ | |
private function downgrade(SessionInterface $session, Localization $localization) | |
{ | |
/** @var Row $row */ | |
foreach ($this->getRowsToMigrate($session, $localization) as $row) { | |
$node = $row->getNode(); | |
$localizedOldPropertyName = \sprintf('i18n:%s-%s', $localization->getLocale(), self::OLD_PROPERTY); | |
$localizedNewPropertyName = \sprintf('i18n:%s-%s', $localization->getLocale(), self::NEW_PROPERTY); | |
if ($node->hasProperty($localizedNewPropertyName)) { | |
$node->setProperty($localizedOldPropertyName, $node->getPropertyValue($localizedNewPropertyName)); | |
$node->setProperty($localizedNewPropertyName, null); | |
} | |
} | |
} | |
/** | |
* Creates a generator that generates all rows that have to be migrated. | |
* | |
* @return \Generator | |
*/ | |
private function getRowsToMigrate(SessionInterface $session, Localization $localization) | |
{ | |
$queryManager = $session->getWorkspace()->getQueryManager(); | |
$pageCondition = '([jcr:mixinTypes] = "sulu:page" OR [jcr:mixinTypes] = "sulu:home")'; | |
$templateCondition = \sprintf('([i18n:%s-template] = "%s")', $localization->getLocale(), self::TEMPLATE); | |
$query = 'SELECT * FROM [nt:unstructured] WHERE (' . $templateCondition . 'AND' . $pageCondition . ')'; | |
yield from $queryManager->createQuery($query, 'JCR-SQL2')->execute(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment