Skip to content

Commit

Permalink
When copying nodes remap any references within the copied set
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed Jan 5, 2015
1 parent 5dbd7f2 commit f4afc1e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 94 deletions.
36 changes: 33 additions & 3 deletions src/Jackalope/Transport/DoctrineDBAL/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,15 +571,23 @@ public function copyNode($srcAbsPath, $dstAbsPath, $srcWorkspace = null)

$query = 'SELECT * FROM phpcr_nodes WHERE path LIKE ? AND workspace_name = ?';
$stmt = $this->conn->executeQuery($query, array($srcAbsPath . '%', $srcWorkspace));
$rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);

$uuidMap = array();
$resultSetUuids = array();

// first iterate and build up an array of all the UUIDs in the result set
foreach ($rows as $row) {
$resultSetUuids[$row['identifier']] = $row['path'];
}

foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) {
$referenceElsToRemap = array();
foreach ($rows as $row) {
$newPath = str_replace($srcAbsPath, $dstAbsPath, $row['path']);

$stringDom = new \DOMDocument('1.0', 'UTF-8');
$stringDom->loadXML($row['props']);

$numericalDom = null;

if ($row['numerical_props']) {
$numericalDom = new \DOMDocument('1.0', 'UTF-8');
$numericalDom->loadXML($row['numerical_props']);
Expand All @@ -589,9 +597,24 @@ public function copyNode($srcAbsPath, $dstAbsPath, $srcWorkspace = null)
'stringDom' => $stringDom,
'numericalDom' => $numericalDom
);

$xpath = new \DOMXpath($stringDom);
$referenceEls = $xpath->query('.//sv:property[@sv:type="reference"]');

foreach ($referenceEls as $referenceEl) {
if (array_key_exists($referenceEl->nodeValue, $resultSetUuids)) {
$referenceElsToRemap[] = array($referenceEl, $newPath, $row['type'], $propsData);
}
}

$originalUuid = $row['identifier'];

// when copying a node, the copy is always a new node. set $isNewNode to true
$newNodeId = $this->syncNode(null, $newPath, $row['type'], true, array(), $propsData);

$newUuid = $this->nodeIdentifiers[$newPath];
$uuidMap[$originalUuid] = $newUuid;

$query = 'INSERT INTO phpcr_binarydata (node_id, property_name, workspace_name, idx, data)'.
' SELECT ?, b.property_name, ?, b.idx, b.data FROM phpcr_binarydata b WHERE b.node_id = ?';

Expand All @@ -601,6 +624,13 @@ public function copyNode($srcAbsPath, $dstAbsPath, $srcWorkspace = null)
throw new RepositoryException("Unexpected exception while copying node from $srcAbsPath to $dstAbsPath", $e->getCode(), $e);
}
}

foreach ($referenceElsToRemap as $data) {
list($referenceEl, $newPath, $type, $propsData) = $data;
$referenceEl->nodeValue = $uuidMap[$referenceEl->nodeValue];

$this->syncNode($this->nodeIdentifiers[$newPath], $newPath, $type, false, array(), $propsData);
}
}

/**
Expand Down
8 changes: 0 additions & 8 deletions tests/benchmark.back/queries.txt

This file was deleted.

19 changes: 0 additions & 19 deletions tests/benchmark.back/query_order_by.sh

This file was deleted.

32 changes: 0 additions & 32 deletions tests/benchmark.back/resmaster.txt

This file was deleted.

32 changes: 0 additions & 32 deletions tests/benchmark.back/resnumerical.txt

This file was deleted.

0 comments on commit f4afc1e

Please sign in to comment.