Skip to content

Commit

Permalink
Merge pull request #9 from punktDe/bugfix/handle-internal-and-private…
Browse files Browse the repository at this point in the history
…-workspaces

!!! TASK: Ignore internal and private workspaces on calculating changes
  • Loading branch information
daniellienert authored Apr 23, 2021
2 parents 2bd8ed8 + 02816e3 commit 9b6800e
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 4 deletions.
26 changes: 22 additions & 4 deletions Classes/Domain/Repository/NodeDataRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Neos\ContentRepository\Domain\Model\NodeData;
use Neos\ContentRepository\Domain\Model\Workspace;
use Neos\Flow\Persistence\Repository;
use PunktDe\EditConflictPrevention\Service\WorkspaceService;

/**
* @Flow\Scope("singleton")
Expand All @@ -32,15 +33,23 @@ class NodeDataRepository extends Repository
*/
protected $nodeTypeManager;

/**
* @Flow\Inject
* @var WorkspaceService
*/
protected $workspaceService;

/**
* First get all content collection nodes which are a direct child of the given document.
* Then get all nodes that are children of that path
*
* @param NodeInterface $documentNode
* @param Workspace $userWorkspace
* @param bool $ignoreInternalAndPrivateWorkspaces
* @return NodeData[]
* @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException
*/
public function findChangedSubNodesInOtherWorkspaces(NodeInterface $documentNode, Workspace $userWorkspace): array
public function findChangedSubNodesInOtherWorkspaces(NodeInterface $documentNode, Workspace $userWorkspace, bool $ignoreInternalAndPrivateWorkspaces = true): array
{
$contentCollectionNodeTypes = array_merge([$this->nodeTypeManager->getNodeType(self::NODETYPE_CONTENT_COLLECTION)], $this->nodeTypeManager->getSubNodeTypes(self::NODETYPE_CONTENT_COLLECTION));

Expand All @@ -60,23 +69,32 @@ public function findChangedSubNodesInOtherWorkspaces(NodeInterface $documentNode
return [];
}

$ignoredWorkspaces = ['live'];
if ($ignoreInternalAndPrivateWorkspaces) {
$ignoredWorkspaces = array_merge(
$ignoredWorkspaces,
$this->workspaceService->getInternalWorkspaceNames(),
$this->workspaceService->getPrivateWorkspaceNames()
);
}

$queryBuilder = $this->entityManager->createQueryBuilder();
$queryBuilder->select('n')
->from(NodeData::class, 'n')
->where('n.workspace != :userWorkspace')
->andWhere('n.workspace != :liveWorkspace')
->andWhere($queryBuilder->expr()->not($queryBuilder->expr()->in('n.workspace', ':ignoredWorkspaces')))
->andWhere('n.dimensionsHash = :dimensionsHash');

$queryBuilder->setParameters([
':userWorkspace' => $userWorkspace->getName(),
':liveWorkspace' => 'live',
':ignoredWorkspaces' => $ignoredWorkspaces,
':dimensionsHash' => $documentNode->getNodeData()->getDimensionsHash(),
]);

$pathCandidates = [];

foreach (array_unique($contentCollectionPaths) as $contentCollectionPath) {
$pathParameterName = ':x' . md5($contentCollectionPath);
$pathParameterName = ':ccp_' . md5($contentCollectionPath);
$pathCandidates[] = $queryBuilder->expr()->like('n.path', $pathParameterName);
$queryBuilder->setParameter($pathParameterName, $contentCollectionPath . '%');
}
Expand Down
79 changes: 79 additions & 0 deletions Classes/Service/WorkspaceService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);

namespace PunktDe\EditConflictPrevention\Service;

/*
* (c) 2021 punkt.de GmbH - Karlsruhe, Germany - http://punkt.de
* All rights reserved.
*/

use Neos\ContentRepository\Domain\Model\Workspace;
use Neos\ContentRepository\Domain\Repository\WorkspaceRepository;
use Neos\Flow\Annotations as Flow;

/**
* @Flow\Scope("singleton")
*/
class WorkspaceService
{

/**
* @Flow\Inject
* @var WorkspaceRepository
*/
protected $workspaceRepository;

/**
* @var array
*/
protected $internalWorkspaceNames = null;

/**
* @var array
*/
protected $privateWorkspaceNames = null;

public function getInternalWorkspaceNames(): array
{
if (is_array($this->internalWorkspaceNames)) {
return $this->internalWorkspaceNames;
}

$query = $this->workspaceRepository->createQuery();
$result = $query->matching(
$query->logicalAnd(
$query->logicalNot($query->equals('baseWorkspace', null)),
$query->equals('owner', null)
)
)->execute();

$this->internalWorkspaceNames = array_map(static function (Workspace $workspace) {
return $workspace->getName();
}, $result->toArray());

return $this->internalWorkspaceNames;
}

public function getPrivateWorkspaceNames(): array
{
if (is_array($this->privateWorkspaceNames)) {
return $this->privateWorkspaceNames;
}

$query = $this->workspaceRepository->createQuery();
$result = $query->matching(
$query->logicalAnd(
$query->logicalNot($query->equals('baseWorkspace', null)),
$query->logicalNot($query->equals('owner', null)),
$query->logicalNot('name', Workspace::PERSONAL_WORKSPACE_PREFIX . '%')
)
)->execute();

$this->privateWorkspaceNames = array_map(static function (Workspace $workspace) {
return $workspace->getName();
}, $result->toArray());

return $this->privateWorkspaceNames;
}
}

0 comments on commit 9b6800e

Please sign in to comment.