protected function removeAbstractAndUndefinedNodes($workspaceName, $dryRun)
{
$this->output->outputLine('Checking for nodes with abstract or undefined node types ...');
$abstractNodeTypes = array();
$nonAbstractNodeTypes = array();
foreach ($this->nodeTypeManager->getNodeTypes() as $nodeType) {
/** @var NodeType $nodeType */
if ($nodeType->isAbstract()) {
$abstractNodeTypes[] = $nodeType->getName();
} else {
$nonAbstractNodeTypes[] = $nodeType->getName();
}
}
/** @var \Doctrine\ORM\QueryBuilder $queryBuilder */
$queryBuilder = $this->entityManager->createQueryBuilder();
$queryBuilder->select('n')->distinct()->from(NodeData::class, 'n')->where('n.nodeType NOT IN (:excludeNodeTypes)')->setParameter('excludeNodeTypes', $nonAbstractNodeTypes)->andWhere('n.workspace = :workspace')->setParameter('workspace', $workspaceName);
$nodes = $queryBuilder->getQuery()->getArrayResult();
$removableNodesCount = count($nodes);
if ($removableNodesCount === 0) {
return;
}
foreach ($nodes as $node) {
$name = $node['path'] === '/' ? '' : substr($node['path'], strrpos($node['path'], '/') + 1);
$type = in_array($node['nodeType'], $abstractNodeTypes) ? 'abstract' : 'undefined';
$this->output->outputLine('Found node with %s node type named "%s" (%s) in "%s"', array($type, $name, $node['nodeType'], $node['path']));
}
$this->output->outputLine();
if (!$dryRun) {
$self = $this;
$this->askBeforeExecutingTask('Abstract or undefined node types found, do you want to remove them?', function () use($self, $nodes, $workspaceName, $removableNodesCount) {
foreach ($nodes as $node) {
$self->removeNode($node['identifier'], $node['dimensionsHash']);
}
$self->output->outputLine('Removed %s node%s with abstract or undefined node types.', array($removableNodesCount, $removableNodesCount > 1 ? 's' : ''));
});
} else {
$this->output->outputLine('Found %s node%s with abstract or undefined node types to be removed.', array($removableNodesCount, $removableNodesCount > 1 ? 's' : ''));
}
$this->output->outputLine();
}