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

removeUndefinedProperties() public method

Performs checks for orphan nodes removes them if found.
public removeUndefinedProperties ( string $workspaceName, boolean $dryRun, NodeType $nodeType = null ) : void
$workspaceName string
$dryRun boolean Simulate?
$nodeType Neos\ContentRepository\Domain\Model\NodeType Only for this node type, if specified
return void
    public function removeUndefinedProperties($workspaceName, $dryRun, NodeType $nodeType = null)
    {
        $this->output->outputLine('Checking for undefined properties ...');
        /** @var \Neos\ContentRepository\Domain\Model\Workspace $workspace */
        $workspace = $this->workspaceRepository->findByIdentifier($workspaceName);
        $nodesWithUndefinedPropertiesNodes = array();
        $undefinedPropertiesCount = 0;
        $nodes = $nodeType !== null ? $this->getNodeDataByNodeTypeAndWorkspace($nodeType, $workspaceName) : $this->nodeDataRepository->findByWorkspace($workspace);
        foreach ($nodes as $nodeData) {
            try {
                /** @var NodeData $nodeData */
                if ($nodeData->getNodeType()->getName() === 'unstructured') {
                    continue;
                }
                $context = $this->nodeFactory->createContextMatchingNodeData($nodeData);
                $node = $this->nodeFactory->createFromNodeData($nodeData, $context);
                if (!$node instanceof NodeInterface) {
                    continue;
                }
                $nodeType = $node->getNodeType();
                $undefinedProperties = array_diff(array_keys($node->getProperties()), array_keys($nodeType->getProperties()));
                if ($undefinedProperties !== array()) {
                    $nodesWithUndefinedPropertiesNodes[$node->getIdentifier()] = array('node' => $node, 'undefinedProperties' => $undefinedProperties);
                    foreach ($undefinedProperties as $undefinedProperty) {
                        $undefinedPropertiesCount++;
                        $this->output->outputLine('Found undefined property named "%s" in "%s" (%s)', array($undefinedProperty, $node->getPath(), $node->getNodeType()->getName()));
                    }
                }
            } catch (NodeTypeNotFoundException $exception) {
                $this->output->outputLine('Skipped undefined node type in "%s"', array($nodeData->getPath()));
            }
        }
        if ($undefinedPropertiesCount > 0) {
            $this->output->outputLine();
            if (!$dryRun) {
                $self = $this;
                $this->askBeforeExecutingTask('Do you want to remove undefined node properties?', function () use($self, $nodesWithUndefinedPropertiesNodes, $undefinedPropertiesCount, $workspaceName, $dryRun) {
                    foreach ($nodesWithUndefinedPropertiesNodes as $nodesWithUndefinedPropertiesNode) {
                        /** @var NodeInterface $node */
                        $node = $nodesWithUndefinedPropertiesNode['node'];
                        foreach ($nodesWithUndefinedPropertiesNode['undefinedProperties'] as $undefinedProperty) {
                            if ($node->hasProperty($undefinedProperty)) {
                                $node->removeProperty($undefinedProperty);
                            }
                        }
                    }
                    $self->output->outputLine('Removed %s undefined propert%s.', array($undefinedPropertiesCount, $undefinedPropertiesCount > 1 ? 'ies' : 'y'));
                });
            } else {
                $this->output->outputLine('Found %s undefined propert%s to be removed.', array($undefinedPropertiesCount, $undefinedPropertiesCount > 1 ? 'ies' : 'y'));
            }
            $this->output->outputLine();
        }
        $this->persistenceManager->persistAll();
    }