depage-xmldb v2.3.1
XML documents in a relational database
Loading...
Searching...
No Matches
PatchApplier.php
Go to the documentation of this file.
1<?php
2
12
14
17
27{
28 protected $pdo;
29
39 public function __construct(
40 $pdo,
41 protected string $table,
42 protected $docId,
43 protected Positions $positions,
44 protected DocumentWriter $writer
45 ) {
46 $this->pdo = $pdo;
47 }
48
54 public function apply(array $plan): void
55 {
56 if (!empty($plan['deletes'])) {
57 $delete = $this->pdo->prepare(
58 "DELETE FROM {$this->table}
59 WHERE id_doc = :doc_id AND id = :id",
60 );
61
62 foreach ($plan['deletes'] as $nodeId) {
63 $delete->execute([
64 'doc_id' => $this->docId,
65 'id' => $nodeId,
66 ]);
67 }
68 }
69
70 if (!empty($plan['updates'])) {
71 $update = $this->pdo->prepare(
72 "UPDATE {$this->table}
73 SET name = :name, value = :value
74 WHERE id_doc = :doc_id AND id = :id",
75 );
76
77 foreach ($plan['updates'] as $node) {
78 $update->execute([
79 'name' => $node['name'],
80 'value' => $node['value'],
81 'doc_id' => $this->docId,
82 'id' => $node['id'],
83 ]);
84 }
85 }
86
87 // new subtrees are appended to the child list of their parent,
88 // the reordering below puts them in place
89 $insertedIds = [];
90 foreach ($plan['inserts'] as $index => $insert) {
91 $insertedIds[$index] = $this->writer->saveNodeIn($insert['node'], $insert['parent_id'], -1, true);
92 }
93
94 foreach ($plan['renumber'] as $parentId => $layout) {
95 $orderedIds = [];
96 foreach ($layout as $slot) {
97 $orderedIds[] = $slot['id'] ?? $insertedIds[$slot['insert']];
98 }
99
100 // skip the reordering when the children are already in the
101 // right order (the appends did not change it)
102 $current = $this->positions->siblings($parentId);
103 $reorder = count($current) != count($orderedIds);
104 foreach ($current as $pos => $sibling) {
105 if (!$reorder && (int) $sibling->id != $orderedIds[$pos]) {
106 $reorder = true;
107 }
108 }
109
110 if ($reorder) {
111 $this->positions->rebalance($parentId, $orderedIds);
112 }
113 }
114 }
115}
116
117/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
__construct( $pdo, protected string $table, protected $docId, protected Positions $positions, protected DocumentWriter $writer)
apply(array $plan)
executes the change plan of PatchPlanner\plan()