Skip to content

Instantly share code, notes, and snippets.

@lorenzulrich
Last active January 18, 2024 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lorenzulrich/737dda4949d33f4fb8225ddbc739bfaa to your computer and use it in GitHub Desktop.
Save lorenzulrich/737dda4949d33f4fb8225ddbc739bfaa to your computer and use it in GitHub Desktop.
<?php
namespace FoobarCom\Site\Command;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Eel\FlowQuery\FlowQuery;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Phlu\Corporate\Factory\ContextFactory;
/**
* This command controller helps performing maintenance tasks
*
* @Flow\Scope("singleton")
*/
class MaintenanceCommandController extends CommandController
{
/**
* @Flow\Inject
* @var ContextFactory
*/
protected $contextFactory;
/**
* Remove a node and all its children
*
* @param string $nodeIdentifier
*/
public function removeNodeCommand(string $nodeIdentifier): void
{
$rootNode = $this->contextFactory->getRootNode();
$flowQuery = new FlowQuery([$rootNode]);
/** @var NodeInterface $nodeToRemove */
$nodeToRemove = $flowQuery->find('#' . $nodeIdentifier)->first()->get(0);
if (!$nodeToRemove instanceof NodeInterface) {
$this->outputFormatted('Node with identifier %s not found.', [$nodeIdentifier]);
}
$this->outputFormatted(
'Removing node %s (%s) and its children. This may take some time.',
[
$nodeToRemove->getProperty('title'),
$nodeIdentifier
]
);
$nodeToRemove->remove();
$this->outputFormatted('<success>Node moved successfully.</success>');
}
/**
* Moves a node to another parent node
*
* @param string $nodeIdentifier
* @param string $targetParentNodeIdentifier
*/
public function moveNodeCommand(string $nodeIdentifier, string $targetParentNodeIdentifier): void
{
$rootNode = $this->contextFactory->getRootNode();
$flowQuery = new FlowQuery([$rootNode]);
/** @var NodeInterface $nodeToMove */
$nodeToMove = $flowQuery->find('#' . $nodeIdentifier)->first()->get(0);
if (!$nodeToMove instanceof NodeInterface) {
$this->outputFormatted('Node with identifier %s not found.', [$nodeIdentifier]);
}
$targetParentNode = $flowQuery->find('#' . $targetParentNodeIdentifier)->first()->get(0);
if (!$targetParentNode instanceof NodeInterface) {
$this->outputFormatted('Target parent node with identifier %s not found.', [$targetParentNodeIdentifier]);
}
$this->outputFormatted(
'Moving node %s (%s) into node %s (%s). This may take some time.',
[
$nodeToMove->getProperty('title'),
$nodeIdentifier,
$targetParentNode->getProperty('title'),
$targetParentNodeIdentifier
]
);
$nodeToMove->moveInto($targetParentNode);
$this->outputFormatted('<success>Node moved successfully.</success>');
}
/**
* Moves all nodes of a given type to another page
*
* @param string $sourceParentNodeIdentifier
* @param string $targetParentNodeIdentifier
*/
public function moveNodesCommand(string $sourceParentNodeIdentifier, string $nodeTypeName, string $targetParentNodeIdentifier): void
{
$flowQuery = new FlowQuery([$this->contextFactory->create()->getRootNode()]);
/** @var $sourceParentNode */
$sourceParentNode = $flowQuery->find('#' . $sourceParentNodeIdentifier)->first()->get(0);
if (!$sourceParentNode instanceof NodeInterface) {
$this->outputFormatted('Node with identifier %s not found.', [$sourceParentNodeIdentifier]);
}
$targetParentNode = $flowQuery->find('#' . $targetParentNodeIdentifier)->first()->get(0);
if (!$targetParentNode instanceof NodeInterface) {
$this->outputFormatted('Target parent node with identifier %s not found.', [$targetParentNodeIdentifier]);
}
$this->outputFormatted(
'Moving nodes of type %s from node %s (%s) into node %s (%s).',
[
$nodeTypeName,
$sourceParentNode->getProperty('title'),
$sourceParentNodeIdentifier,
$targetParentNode->getProperty('title'),
$targetParentNodeIdentifier
]
);
$sourceNodesFlowQuery = new FlowQuery([$sourceParentNode]);
$sourceNodes = $sourceNodesFlowQuery->find(sprintf('[instanceof %s]', $nodeTypeName))->get();
/** @var NodeInterface $node */
foreach ($sourceNodes as $node) {
$this->outputFormatted(
'Moving node %s (%s) into node %s (%s).',
[
$node->getProperty('title'),
$node->getIdentifier(),
$targetParentNode->getProperty('title'),
$targetParentNodeIdentifier
]
);
$node->moveInto($targetParentNode);
$this->outputFormatted('<success>Node moved successfully.</success>');
}
$this->outputFormatted('<success>All nodes moved successfully.</success>');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment