Skip to content

Instantly share code, notes, and snippets.

@alexander-schranz
Created October 19, 2023 14:20
Show Gist options
  • Save alexander-schranz/fd4a0b57205dfbbe4079e943d1752111 to your computer and use it in GitHub Desktop.
Save alexander-schranz/fd4a0b57205dfbbe4079e943d1752111 to your computer and use it in GitHub Desktop.
A command to check if default workspace is not longer in sync with live workspace in Sulu CMS
<?php
declare(strict_types=1);
namespace App\Command;
use PHPCR\NodeInterface;
use PHPCR\Query\RowInterface;
use PHPCR\SessionInterface;
use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'app:php-cr:fixer',
description: 'Add a short description for your command',
)]
/*
Requires:
# config/packages/services.yaml
services:
_defaults:
bind:
'$defaultSession': '@sulu_document_manager.default_session'
'$liveSession': '@sulu_document_manager.live_session'
*/
class PhpCrFixerCommand extends Command
{
public function __construct(
private readonly WebspaceManagerInterface $webspaceManager,
private readonly SessionInterface $defaultSession,
private readonly SessionInterface $liveSession,
) {
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$ui = new SymfonyStyle($input, $output);
$locales = $this->webspaceManager->getAllLocales();
$notFoundDefaultNodes = [];
$unsyncDefaultNodes = [];
/** @var RowInterface $row */
foreach ($this->loadLiveNodes() as $row) {
/** @var NodeInterface $liveNode */
$liveNode = $row->getNode();
$uuid = $liveNode->getIdentifier();
if (\str_starts_with($uuid, '/')) {
continue; // sometimes none uuid are returned this currently ignore
}
$defaultNode = $this->loadDefaultNode($uuid);
if (!$defaultNode) {
$notFoundDefaultNodes[] = [
'uuid' => $liveNode->getIdentifier(),
'path' => $liveNode->getPath(),
];
// TODO this is more critical we maybe need to copy this nodes from live to default
continue;
}
foreach ($locales as $locale) {
$changedPropertyName = 'i18n:' . $locale . '-changed';
$liveChanged = 0;
if ($liveNode->hasProperty($changedPropertyName)) {
$liveChangedDateTime = $liveNode->getProperty($changedPropertyName)->getValue();
if ($liveChangedDateTime) {
\assert($liveChangedDateTime instanceof \DateTimeInterface);
$liveChanged = $liveChangedDateTime->getTimestamp();
}
}
$defaultChanged = 0;
if ($defaultNode->hasProperty($changedPropertyName)) {
$defaultChangedDateTime = $defaultNode->getProperty($changedPropertyName)->getValue();
if ($defaultChangedDateTime) {
\assert($defaultChangedDateTime instanceof \DateTimeInterface);
$defaultChanged = $defaultChangedDateTime->getTimestamp();
}
}
if ($liveChanged > $defaultChanged) {
$unsyncDefaultNodes[] = [
'uuid' => $liveNode->getIdentifier(),
'path' => $liveNode->getPath(),
];
// TODO revertDraft via document manager? make sure to flush and clear after it
}
}
}
$ui->title('Not found default nodes');
$ui->table(['uuid', 'path'], $notFoundDefaultNodes);
$ui->title('Unsync default nodes');
$ui->table(['uuid', 'path'], $unsyncDefaultNodes);
if (\count($notFoundDefaultNodes) || \count($unsyncDefaultNodes)) {
$ui->warning('Live and default seems not be in sync');
} else {
$ui->success('No unsync things found.');
}
return Command::SUCCESS;
}
private function loadLiveNodes(): \Generator
{
$queryManager = $this->liveSession->getWorkspace()->getQueryManager();
$query = 'SELECT * FROM [nt:unstructured]';
yield from $queryManager->createQuery($query, 'JCR-SQL2')->execute();
}
private function loadDefaultNode(string $identifier): ?NodeInterface
{
try {
$defaultNode = $this->defaultSession->getNodeByIdentifier($identifier);
} catch (\Exception $e) {
return null;
}
return $defaultNode;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment