/
NodeProcessor.php
357 lines (327 loc) · 13.6 KB
/
NodeProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
namespace Jackalope\NodeType;
use Jackalope\Node;
use Jackalope\Transport\AddNodeOperation;
use PHPCR\ItemExistsException;
use PHPCR\Lock\LockException;
use PHPCR\NamespaceException;
use PHPCR\NodeInterface;
use PHPCR\NodeType\ConstraintViolationException;
use PHPCR\NodeType\NodeDefinitionInterface;
use PHPCR\NodeType\NodeTypeDefinitionInterface;
use PHPCR\NodeType\PropertyDefinitionInterface;
use PHPCR\PathNotFoundException;
use PHPCR\PropertyInterface;
use PHPCR\PropertyType;
use PHPCR\RepositoryException;
use PHPCR\Util\PathHelper;
use PHPCR\Util\UUIDHelper;
use PHPCR\ValueFormatException;
use PHPCR\Version\VersionException;
/**
* This class processes according to its declared node types.
*
* - Validates properties
* - Auto-generates property values
* - Generates extra node addition operations that follow from the node definition.
*
* Adapted from the jackalope-doctrine-dbal implementation:
* https://github.com/jackalope/jackalope-doctrine-dbal/blob/31cca1d1fb7fbe56423fa34478e15ce6d93313fd/src/Jackalope/Transport/DoctrineDBAL/Client.php
*/
class NodeProcessor
{
/**
* Regex pattern to validate a URI according
* to RFC3986:.
*
* http://tools.ietf.org/html/rfc3986
*/
public const VALIDATE_URI_RFC3986 = "
/^
([a-z][a-z0-9\\*\\-\\.]*):\\/\\/
(?:
(?:(?:[\\w\\.\\-\\+!$&'\\(\\)*\\+,;=]|%[0-9a-f]{2})+:)*
(?:[\\w\\.\\-\\+%!$&'\\(\\)*\\+,;=]|%[0-9a-f]{2})+@
)?
(?:
(?:[a-z0-9\\-\\.]|%[0-9a-f]{2})+
|(?:\\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\\])
)
(?::[0-9]+)?
(?:[\\/|\\?]
(?:[\\w#!:\\.\\?\\+=&@!$'~*,;\\/\\(\\)\\[\\]\\-]|%[0-9a-f]{2})
*)?
$/xi";
/**
* Regex to assert that a string does not contain any illegal characters.
*/
public const VALIDATE_STRING = '/[^\x{9}\x{a}\x{d}\x{20}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]+/u';
/**
* ID of the connected user.
*/
private ?string $userId;
/**
* Whether the last modified property should be updated automatically.
*/
private bool $autoLastModified;
/**
* List of namespaces known to the current session. Keys are prefix, values are URI.
*/
private \ArrayObject $namespaces;
/**
* @param string|null $userId ID of the connected user, if known
* @param \ArrayObject $namespaces List of namespaces in the current session. Keys are prefix, values are URI.
* @param bool $autoLastModified Whether the last modified property should be updated automatically
*/
public function __construct(?string $userId, \ArrayObject $namespaces, bool $autoLastModified = true)
{
$this->userId = $userId;
$this->autoLastModified = $autoLastModified;
$this->namespaces = $namespaces;
}
/**
* Process the given node and return eventual extra operations determined from the node.
*
* @return AddNodeOperation[] additional operations that the client must execute for autocreated nodes
*/
public function process(NodeInterface $node): array
{
$this->validateNamespace($node->getName());
$nodeDef = $node->getPrimaryNodeType();
$nodeTypes = $node->getMixinNodeTypes();
array_unshift($nodeTypes, $nodeDef);
$additionalOperations = [];
foreach ($nodeTypes as $nodeType) {
/* @var $nodeType NodeTypeDefinitionInterface */
$additionalOperations = array_merge(
$additionalOperations,
$this->processNodeWithType($node, $nodeType)
);
}
return $additionalOperations;
}
/**
* Validate this node with the nodetype and generate not yet existing
* autogenerated properties as necessary.
*
* @return AddNodeOperation[] additional operations to handle autocreated nodes
*
* @throws \InvalidArgumentException
* @throws RepositoryException
* @throws ItemExistsException
* @throws LockException
* @throws ConstraintViolationException
* @throws PathNotFoundException
* @throws VersionException
* @throws ValueFormatException
*/
private function processNodeWithType(NodeInterface $node, NodeType $nodeTypeDefinition): array
{
$additionalOperations = [];
foreach ($nodeTypeDefinition->getDeclaredChildNodeDefinitions() as $childDef) {
/* @var $childDef NodeDefinitionInterface */
if (!$node->hasNode($childDef->getName())) {
if ('*' === $childDef->getName()) {
continue;
}
if ($childDef->isMandatory() && !$childDef->isAutoCreated()) {
throw new RepositoryException(sprintf(
'Child "%s" is mandatory, but is not present while saving "%s" at path "%s"',
$childDef->getName(),
$nodeTypeDefinition->getName(),
$node->getPath()
));
}
if ($childDef->isAutoCreated()) {
$requiredPrimaryTypeNames = $childDef->getRequiredPrimaryTypeNames();
$primaryType = count($requiredPrimaryTypeNames) ? current($requiredPrimaryTypeNames) : null;
$newNode = $node->addNode($childDef->getName(), $primaryType);
\assert($newNode instanceof Node);
$absPath = $node->getPath().'/'.$childDef->getName();
$operation = new AddNodeOperation($absPath, $newNode);
$additionalOperations[] = $operation;
}
}
}
foreach ($nodeTypeDefinition->getDeclaredPropertyDefinitions() as $propertyDef) {
/* @var $propertyDef PropertyDefinitionInterface */
if ('*' === $propertyDef->getName()) {
continue;
}
if (!$node->hasProperty($propertyDef->getName())) {
if ($propertyDef->isMandatory() && !$propertyDef->isAutoCreated()) {
throw new RepositoryException(sprintf(
'Property "%s" is mandatory, but is not present while saving "%s" at "%s"',
$propertyDef->getName(),
$nodeTypeDefinition->getName(),
$node->getPath()
));
}
if ($propertyDef->isAutoCreated()) {
switch ($propertyDef->getName()) {
case 'jcr:uuid':
$value = UUIDHelper::generateUUID();
break;
case 'jcr:createdBy':
case 'jcr:lastModifiedBy':
if (null === $this->userId) {
continue 2;
}
$value = $this->userId;
break;
case 'jcr:created':
case 'jcr:lastModified':
$value = new \DateTime();
break;
case 'jcr:etag':
// TODO: http://www.day.com/specs/jcr/2.0/3_Repository_Model.html#3.7.12.1%20mix:etag
$value = 'TODO: generate from binary properties of this node';
break;
default:
$defaultValues = $propertyDef->getDefaultValues();
if ($propertyDef->isMultiple()) {
$value = $defaultValues;
} elseif (isset($defaultValues[0])) {
$value = $defaultValues[0];
} else {
// When implementing versionable or activity, we need to handle more properties explicitly
throw new RepositoryException(sprintf(
'No default value for autocreated property "%s" at "%s"',
$propertyDef->getName(),
$node->getPath()
));
}
}
$node->setProperty(
$propertyDef->getName(),
$value,
$propertyDef->getRequiredType()
);
}
} elseif ($propertyDef->isAutoCreated()) {
$prop = $node->getProperty($propertyDef->getName());
if (!$prop->isModified() && !$prop->isNew()) {
switch ($propertyDef->getName()) {
case 'jcr:lastModified':
if ($this->autoLastModified) {
$prop->setValue(new \DateTime());
}
break;
case 'jcr:lastModifiedBy':
if ($this->autoLastModified) {
if (null === $this->userId) {
// we don't know the current user id, remove lastModifiedBy property
$node->setProperty($propertyDef->getName(), null);
} else {
$prop->setValue($this->userId);
}
}
break;
case 'jcr:etag':
// TODO: update etag if needed
break;
}
}
}
}
foreach ($nodeTypeDefinition->getDeclaredSupertypes() as $superType) {
$this->processNodeWithType($node, $superType);
}
foreach ($node->getProperties() as $property) {
$this->assertValidProperty($property);
}
return $additionalOperations;
}
/**
* Validation if all the data is correct before writing it into the database.
*
* @throws RepositoryException
* @throws ValueFormatException
*/
private function assertValidProperty(PropertyInterface $property): void
{
$type = $property->getType();
switch ($type) {
case PropertyType::NAME:
$values = $property->getValue();
if (!$property->isMultiple()) {
$values = [$values];
}
foreach ($values as $value) {
$pos = strpos($value, ':');
if (false !== $pos) {
$prefix = substr($value, 0, $pos);
if (!isset($this->namespaces[$prefix])) {
throw new ValueFormatException(sprintf(
'Invalid value for NAME property type at "%s", the namespace prefix "%s" does not exist.");',
$property->getPath(),
$prefix
));
}
}
}
break;
case PropertyType::PATH:
$values = $property->getValue();
if (!$property->isMultiple()) {
$values = [$values];
}
foreach ($values as $value) {
try {
// making the path absolute also validates the result to be a valid path
PathHelper::absolutizePath($value, $property->getPath());
} catch (RepositoryException $e) {
throw new ValueFormatException(sprintf('Value "%s" for PATH property at "%s" is invalid', $value, $property->getPath()), 0, $e);
}
}
break;
case PropertyType::URI:
$values = $property->getValue();
if (!$property->isMultiple()) {
$values = [$values];
}
foreach ($values as $value) {
if (!preg_match(self::VALIDATE_URI_RFC3986, $value)) {
throw new ValueFormatException(sprintf(
'Invalid value "%s" for URI property at "%s". Value has to comply with RFC 3986.',
$value,
$property->getPath()
));
}
}
break;
case PropertyType::DECIMAL:
case PropertyType::STRING:
$values = (array) $property->getValue();
foreach ($values as $value) {
if (0 !== preg_match(self::VALIDATE_STRING, $value)) {
throw new ValueFormatException(sprintf(
'Invalid character detected in value %s for STRING property at "%s".',
json_encode($value),
$property->getPath()
));
}
}
break;
}
}
/**
* Ensure that, if a namespace with an alias is passed, that the alias is registered.
*
* @throws NamespaceException
*/
private function validateNamespace(string $name): void
{
$aliasLength = strpos($name, ':');
if (false === $aliasLength) {
return;
}
$alias = substr($name, 0, $aliasLength);
if (!isset($this->namespaces[$alias])) {
$aliases = implode("', '", array_keys($this->namespaces->getArrayCopy()));
throw new NamespaceException(
"Namespace alias '$alias' is not known for name '$name', known namespace aliases are: '$aliases'"
);
}
}
}