Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy references #236

Merged
merged 2 commits into from
Jan 8, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (isset($resultSetUuids[$referenceEl->nodeValue])) {
$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