Neos\ContentRepository\Command\NodeCommandControllerPlugin::fixNodesWithInconsistentIdentifier PHP Method

fixNodesWithInconsistentIdentifier() public method

Detect and fix nodes in non-live workspaces whose identifier does not match their corresponding node in the live workspace.
public fixNodesWithInconsistentIdentifier ( string $workspaceName, boolean $dryRun ) : void
$workspaceName string This argument will be ignored
$dryRun boolean Simulate?
return void
    public function fixNodesWithInconsistentIdentifier($workspaceName, $dryRun)
    {
        $this->output->outputLine('Checking for nodes with inconsistent identifier ...');
        $nodesArray = [];
        $liveWorkspaceNames = [];
        $nonLiveWorkspaceNames = [];
        foreach ($this->workspaceRepository->findAll() as $workspace) {
            /** @var Workspace $workspace */
            if ($workspace->getBaseWorkspace() !== null) {
                $nonLiveWorkspaceNames[] = $workspace->getName();
            } else {
                $liveWorkspaceNames[] = $workspace->getName();
            }
        }
        foreach ($nonLiveWorkspaceNames as $workspaceName) {
            /** @var QueryBuilder $queryBuilder */
            $queryBuilder = $this->entityManager->createQueryBuilder();
            $queryBuilder->select('nonlive.Persistence_Object_Identifier, nonlive.identifier, nonlive.path, live.identifier AS liveIdentifier')->from(NodeData::class, 'nonlive')->join(NodeData::class, 'live', 'WITH', 'live.path = nonlive.path AND live.dimensionsHash = nonlive.dimensionsHash AND live.identifier != nonlive.identifier')->where('nonlive.workspace = ?1')->andWhere($queryBuilder->expr()->in('live.workspace', $liveWorkspaceNames))->andWhere('nonlive.path != \'/\'')->setParameter(1, $workspaceName);
            foreach ($queryBuilder->getQuery()->getArrayResult() as $nodeDataArray) {
                $this->output->outputLine('Node %s in workspace %s has identifier %s but live node has identifier %s.', [$nodeDataArray['path'], $workspaceName, $nodeDataArray['identifier'], $nodeDataArray['liveIdentifier']]);
                $nodesArray[] = $nodeDataArray;
            }
        }
        if ($nodesArray === []) {
            return;
        }
        if (!$dryRun) {
            $self = $this;
            $this->output->outputLine();
            $this->output->outputLine('Nodes with inconsistent identifiers found.');
            $this->askBeforeExecutingTask(sprintf('Do you want to fix the identifiers of %s node%s now?', count($nodesArray), count($nodesArray) > 1 ? 's' : ''), function () use($self, $nodesArray) {
                foreach ($nodesArray as $nodeArray) {
                    /** @var QueryBuilder $queryBuilder */
                    $queryBuilder = $this->entityManager->createQueryBuilder();
                    $queryBuilder->update(NodeData::class, 'nonlive')->set('nonlive.identifier', $queryBuilder->expr()->literal($nodeArray['liveIdentifier']))->where('nonlive.Persistence_Object_Identifier = ?1')->setParameter(1, $nodeArray['Persistence_Object_Identifier']);
                    $result = $queryBuilder->getQuery()->getResult();
                    if ($result !== 1) {
                        $self->output->outputLine('<error>Error:</error> The update query returned an unexpected result!');
                        $self->output->outputLine('<b>Query:</b> ' . $queryBuilder->getQuery()->getSQL());
                        $self->output->outputLine('<b>Result:</b> %s', [var_export($result, true)]);
                        exit(1);
                    }
                }
                $self->output->outputLine('Fixed inconsistent identifiers.');
            });
        } else {
            $this->output->outputLine('Found %s node%s with inconsistent identifiers which need to be fixed.', array(count($nodesArray), count($nodesArray) > 1 ? 's' : ''));
        }
        $this->output->outputLine();
    }