/** * @test * @dataProvider sourcesAndNodeNames */ public function renderValidNodeNameWorks($source, $expectedNodeName) { $this->assertEquals($expectedNodeName, Utility::renderValidNodeName($source)); }
/** * Checks if the given $nodeType is allowed as a childNode of the given $childNodeName * (which must be auto-created in $this NodeType). * * Only allowed to be called if $childNodeName is auto-created. * * @param string $childNodeName The name of a configured childNode of this NodeType * @param NodeType $nodeType The NodeType to check constraints for. * @return boolean TRUE if the $nodeType is allowed as grandchild node, FALSE otherwise. * @throws \InvalidArgumentException If the given $childNodeName is not configured to be auto-created in $this. */ public function allowsGrandchildNodeType($childNodeName, NodeType $nodeType) { $autoCreatedChildNodes = $this->getAutoCreatedChildNodes(); if (!isset($autoCreatedChildNodes[$childNodeName])) { throw new \InvalidArgumentException('The method "allowsGrandchildNodeType" can only be used on auto-created childNodes, given $childNodeName "' . $childNodeName . '" is not auto-created.', 1403858395); } $constraints = $autoCreatedChildNodes[$childNodeName]->getConfiguration('constraints.nodeTypes') ?: array(); $childNodeConfiguration = []; foreach ($this->getConfiguration('childNodes') as $name => $configuration) { $childNodeConfiguration[Utility::renderValidNodeName($name)] = $configuration; } $childNodeConstraintConfiguration = ObjectAccess::getPropertyPath($childNodeConfiguration, $childNodeName . '.constraints.nodeTypes') ?: array(); $constraints = Arrays::arrayMergeRecursiveOverrule($constraints, $childNodeConstraintConfiguration); return $this->isNodeTypeAllowedByConstraints($nodeType, $constraints); }
/** * Build a cached array of dimension values and a hash to search for it. * * @return void */ protected function buildDimensionValues() { $dimensionValues = []; foreach ($this->dimensions as $dimension) { /** @var NodeDimension $dimension */ $dimensionValues[$dimension->getName()][] = $dimension->getValue(); } $this->dimensionsHash = Utility::sortDimensionValueArrayAndReturnDimensionsHash($dimensionValues); $this->dimensionValues = $dimensionValues; }
/** * Saves the given array as a node data entity without using the ORM. * * If the node data already exists (same dimensions, same identifier, same workspace) * it is replaced. * * @param array $nodeData node data to save as an associative array ( $column_name => $value ) * @throws ImportException * @return void */ protected function persistNodeData($nodeData) { if ($nodeData['workspace'] !== 'live') { throw new ImportException('Saving NodeData with workspace != "live" using direct SQL not supported yet. Workspace is "' . $nodeData['workspace'] . '".'); } if ($nodeData['path'] === '/') { return; } // cleanup old data /** @var Connection $connection */ $connection = $this->entityManager->getConnection(); // prepare node dimensions $dimensionValues = $nodeData['dimensionValues']; $dimensionsHash = Utility::sortDimensionValueArrayAndReturnDimensionsHash($dimensionValues); $jsonPropertiesDataTypeHandler = JsonArrayType::getType(JsonArrayType::FLOW_JSON_ARRAY); // post-process node data $nodeData['dimensionsHash'] = $dimensionsHash; $nodeData['dimensionValues'] = $jsonPropertiesDataTypeHandler->convertToDatabaseValue($dimensionValues, $connection->getDatabasePlatform()); $nodeData['properties'] = $jsonPropertiesDataTypeHandler->convertToDatabaseValue($nodeData['properties'], $connection->getDatabasePlatform()); $nodeData['accessRoles'] = $jsonPropertiesDataTypeHandler->convertToDatabaseValue($nodeData['accessRoles'], $connection->getDatabasePlatform()); $connection->executeQuery('DELETE FROM neos_contentrepository_domain_model_nodedimension' . ' WHERE nodedata IN (' . ' SELECT persistence_object_identifier FROM neos_contentrepository_domain_model_nodedata' . ' WHERE identifier = :identifier' . ' AND workspace = :workspace' . ' AND dimensionshash = :dimensionsHash' . ' )', array('identifier' => $nodeData['identifier'], 'workspace' => $nodeData['workspace'], 'dimensionsHash' => $nodeData['dimensionsHash'])); /** @var \Doctrine\ORM\QueryBuilder $queryBuilder */ $queryBuilder = $this->entityManager->createQueryBuilder(); $queryBuilder->delete()->from(NodeData::class, 'n')->where('n.identifier = :identifier')->andWhere('n.dimensionsHash = :dimensionsHash')->andWhere('n.workspace = :workspace')->setParameter('identifier', $nodeData['identifier'])->setParameter('workspace', $nodeData['workspace'])->setParameter('dimensionsHash', $nodeData['dimensionsHash']); $queryBuilder->getQuery()->execute(); // insert new data // we need to use executeUpdate to execute the INSERT -- else the data types are not taken into account. // That's why we build a DQL INSERT statement which is then executed. $queryParts = array(); $queryArguments = array(); $queryTypes = array(); foreach ($this->nodeDataPropertyNames as $propertyName => $propertyConfig) { if (isset($nodeData[$propertyName])) { $queryParts[$propertyName] = ':' . $propertyName; $queryArguments[$propertyName] = $nodeData[$propertyName]; if (isset($propertyConfig['columnType'])) { $queryTypes[$propertyName] = $propertyConfig['columnType']; } } } $connection->executeUpdate('INSERT INTO neos_contentrepository_domain_model_nodedata (' . implode(', ', array_keys($queryParts)) . ') VALUES (' . implode(', ', $queryParts) . ')', $queryArguments, $queryTypes); foreach ($dimensionValues as $dimension => $values) { foreach ($values as $value) { $nodeDimension = array('persistence_object_identifier' => Algorithms::generateUUID(), 'nodedata' => $nodeData['Persistence_Object_Identifier'], 'name' => $dimension, 'value' => $value); $connection->insert('neos_contentrepository_domain_model_nodedimension', $nodeDimension); } } }
/** * Create missing child nodes for the given node type * * @param NodeType $nodeType * @param string $workspaceName * @param boolean $dryRun * @return void */ protected function createChildNodesByNodeType(NodeType $nodeType, $workspaceName, $dryRun) { $createdNodesCount = 0; $updatedNodesCount = 0; $nodeCreationExceptions = 0; $nodeIdentifiersWhichNeedUpdate = []; $nodeTypes = $this->nodeTypeManager->getSubNodeTypes($nodeType->getName(), false); $nodeTypes[$nodeType->getName()] = $nodeType; if ($this->nodeTypeManager->hasNodeType((string) $nodeType)) { $nodeType = $this->nodeTypeManager->getNodeType((string) $nodeType); $nodeTypeNames[$nodeType->getName()] = $nodeType; } else { $this->output->outputLine('Node type "%s" does not exist', array((string) $nodeType)); exit(1); } /** @var $nodeType NodeType */ foreach ($nodeTypes as $nodeTypeName => $nodeType) { $childNodes = $nodeType->getAutoCreatedChildNodes(); foreach ($this->getNodeDataByNodeTypeAndWorkspace($nodeTypeName, $workspaceName) as $nodeData) { $context = $this->nodeFactory->createContextMatchingNodeData($nodeData); $node = $this->nodeFactory->createFromNodeData($nodeData, $context); if (!$node instanceof NodeInterface) { continue; } foreach ($childNodes as $childNodeName => $childNodeType) { try { $childNode = $node->getNode($childNodeName); $childNodeIdentifier = Utility::buildAutoCreatedChildNodeIdentifier($childNodeName, $node->getIdentifier()); if ($childNode === null) { if ($dryRun === false) { $node->createNode($childNodeName, $childNodeType, $childNodeIdentifier); $this->output->outputLine('Auto created node named "%s" in "%s"', array($childNodeName, $node->getPath())); } else { $this->output->outputLine('Missing node named "%s" in "%s"', array($childNodeName, $node->getPath())); } $createdNodesCount++; } elseif ($childNode->getIdentifier() !== $childNodeIdentifier) { $nodeIdentifiersWhichNeedUpdate[$childNode->getIdentifier()] = $childNodeIdentifier; } } catch (\Exception $exception) { $this->output->outputLine('Could not create node named "%s" in "%s" (%s)', array($childNodeName, $node->getPath(), $exception->getMessage())); $nodeCreationExceptions++; } } } } if (count($nodeIdentifiersWhichNeedUpdate) > 0) { if ($dryRun === false) { foreach ($nodeIdentifiersWhichNeedUpdate as $oldNodeIdentifier => $newNodeIdentifier) { $queryBuilder = $this->entityManager->createQueryBuilder(); $queryBuilder->update(NodeData::class, 'n')->set('n.identifier', $queryBuilder->expr()->literal($newNodeIdentifier))->where('n.identifier = ?1')->setParameter(1, $oldNodeIdentifier); $result = $queryBuilder->getQuery()->getResult(); $updatedNodesCount++; $this->output->outputLine('Updated node identifier from %s to %s because it was not a "stable" identifier', [$oldNodeIdentifier, $newNodeIdentifier]); } } else { foreach ($nodeIdentifiersWhichNeedUpdate as $oldNodeIdentifier => $newNodeIdentifier) { $this->output->outputLine('Child nodes with identifier "%s" need to change their identifier to "%s"', [$oldNodeIdentifier, $newNodeIdentifier]); $updatedNodesCount++; } } } if ($createdNodesCount !== 0 || $nodeCreationExceptions !== 0 || $updatedNodesCount !== 0) { if ($dryRun === false) { if ($createdNodesCount > 0) { $this->output->outputLine('Created %s new child nodes', array($createdNodesCount)); } if ($updatedNodesCount > 0) { $this->output->outputLine('Updated identifier of %s child nodes', array($updatedNodesCount)); } if ($nodeCreationExceptions > 0) { $this->output->outputLine('%s Errors occurred during child node creation', array($nodeCreationExceptions)); } $this->persistenceManager->persistAll(); } else { if ($createdNodesCount > 0) { $this->output->outputLine('%s missing child nodes need to be created', array($createdNodesCount)); } if ($updatedNodesCount > 0) { $this->output->outputLine('%s identifiers of child nodes need to be updated', array($updatedNodesCount)); } } } }
/** * Create a workspace * * @Flow\Validate(argumentName="title", type="\Neos\Flow\Validation\Validator\NotEmptyValidator") * @param string $title Human friendly title of the workspace, for example "Christmas Campaign" * @param Workspace $baseWorkspace Workspace the new workspace should be based on * @param string $visibility Visibility of the new workspace, must be either "internal" or "shared" * @param string $description A description explaining the purpose of the new workspace * @return void */ public function createAction($title, Workspace $baseWorkspace, $visibility, $description = '') { $workspace = $this->workspaceRepository->findOneByTitle($title); if ($workspace instanceof Workspace) { $this->addFlashMessage($this->translator->translateById('workspaces.workspaceWithThisTitleAlreadyExists', [], null, null, 'Modules', 'Neos.Neos'), '', Message::SEVERITY_WARNING); $this->redirect('new'); } $workspaceName = Utility::renderValidNodeName($title) . '-' . substr(base_convert(microtime(false), 10, 36), -5, 5); while ($this->workspaceRepository->findOneByName($workspaceName) instanceof Workspace) { $workspaceName = Utility::renderValidNodeName($title) . '-' . substr(base_convert(microtime(false), 10, 36), -5, 5); } if ($visibility === 'private') { $owner = $this->userService->getCurrentUser(); } else { $owner = null; } $workspace = new Workspace($workspaceName, $baseWorkspace, $owner); $workspace->setTitle($title); $workspace->setDescription($description); $this->workspaceRepository->add($workspace); $this->redirect('index'); }
/** * Create a recursive copy of this node below $referenceNode with $nodeName. * * $detachedCopy only has an influence if we are copying from one dimension to the other, possibly creating a new * node variant: * * - If $detachedCopy is TRUE, the whole (recursive) copy is done without connecting original and copied node, * so NOT CREATING a new node variant. * - If $detachedCopy is FALSE, and the node does not yet have a variant in the target dimension, we are CREATING * a new node variant. * * As a caller of this method, $detachedCopy should be TRUE if $this->getNodeType()->isAggregate() is TRUE, and FALSE * otherwise. * * @param NodeInterface $referenceNode * @param boolean $detachedCopy * @param string $nodeName * @return NodeInterface */ protected function createRecursiveCopy(NodeInterface $referenceNode, $nodeName, $detachedCopy) { $identifier = null; $referenceNodeDimensions = $referenceNode->getDimensions(); $referenceNodeDimensionsHash = Utility::sortDimensionValueArrayAndReturnDimensionsHash($referenceNodeDimensions); $thisDimensions = $this->getDimensions(); $thisNodeDimensionsHash = Utility::sortDimensionValueArrayAndReturnDimensionsHash($thisDimensions); if ($detachedCopy === false && $referenceNodeDimensionsHash !== $thisNodeDimensionsHash && $referenceNode->getContext()->getNodeByIdentifier($this->getIdentifier()) === null) { // If the target dimensions are different than this one, and there is no node shadowing this one in the target dimension yet, we use the same // node identifier, effectively creating a new node variant. $identifier = $this->getIdentifier(); } $copiedNode = $referenceNode->createSingleNode($nodeName, null, $identifier); $copiedNode->similarize($this, true); /** @var $childNode Node */ foreach ($this->getChildNodes() as $childNode) { // Prevent recursive copy when copying into itself if ($childNode->getIdentifier() !== $copiedNode->getIdentifier()) { $childNode->copyIntoInternal($copiedNode, $childNode->getName(), $detachedCopy); } } return $copiedNode; }