-
Notifications
You must be signed in to change notification settings - Fork 32
/
Utils.php
530 lines (468 loc) · 18.3 KB
/
Utils.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2013-2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
* @author Jérôme Bogaerts <jerome@taotesting.com>
* @license GPLv2
*/
namespace qtism\data\storage\xml;
use DOMDocument;
use DOMElement;
use InvalidArgumentException;
use LibXMLError;
use qtism\common\enums\Enumeration;
use SimpleXMLElement;
use SplStack;
/**
* A class providing XML utility methods.
*/
class Utils
{
/**
* Get the location of an XML Schema Definition file from a given namespace.
*
* This utility method enables you to know what is the location of an XML Schema Definition
* file to be used to validate a $document for a given target namespace.
*
* @param DOMDocument $document A DOMDocument object.
* @param string $namespaceUri A Namespace URI you want to know the related XSD file location.
* @return bool|string False if no location can be found for $namespaceUri, otherwise the location of the XSD file.
*/
public static function getXsdLocation(DOMDocument $document, $namespaceUri)
{
$root = $document->documentElement;
$location = false;
if ($root !== null) {
$schemaLocation = trim(
$root->getAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation')
);
if ($schemaLocation !== '') {
$parts = preg_split('/\s+/', $schemaLocation);
// Look at pairs...
$partsCount = count($parts);
for ($i = 0; $i < $partsCount; $i += 2) {
if (isset($parts[$i + 1]) && $parts[$i] === $namespaceUri) {
$location = $parts[$i + 1];
break;
}
}
}
}
return $location;
}
/**
* Change the name of $element into $name.
*
* @param DOMElement $element A DOMElement object you want to change the name.
* @param string $name The new name of $element.
*
* @return DOMElement
*/
public static function changeElementName(DOMElement $element, $name): DOMElement
{
$newElement = $element->ownerDocument->createElement($name);
foreach ($element->childNodes as $child) {
$child = $element->ownerDocument->importNode($child, true);
$newElement->appendChild($child);
}
foreach ($element->attributes as $attrName => $attrNode) {
if ($attrNode->namespaceURI === null) {
$newElement->setAttribute($attrName, $attrNode->value);
} else {
$newElement->setAttributeNS($attrNode->namespaceURI, $attrNode->prefix . ':' . $attrName, $attrNode->value);
}
}
$newElement->ownerDocument->replaceChild($newElement, $element);
return $newElement;
}
/**
* Anonimize a given DOMElement. By 'anonimize', we mean remove
* all namespace membership of an element and its child nodes.
*
* For instance, <m:math display="inline"><m:mi>x</m:mi></m:math> becomes
* <math display="inline"><mi>x</mi></math>.
*
* @param DOMElement $element The DOMElement to be anonimized.
* @return DOMElement The anonimized DOMElement copy of $element.
*/
public static function anonimizeElement(DOMElement $element): DOMElement
{
$stack = new SplStack();
$traversed = [];
$children = [];
$stack->push($element);
while ($stack->count() > 0) {
$node = $stack->pop();
if ($node->nodeType === XML_ELEMENT_NODE && $node->childNodes->length > 0 && in_array($node, $traversed, true) === false) {
$traversed[] = $node;
$stack->push($node);
for ($i = 0; $i < $node->childNodes->length; $i++) {
$stack->push($node->childNodes->item($i));
}
} elseif ($node->nodeType === XML_ELEMENT_NODE && $node->childNodes->length > 0 && in_array($node, $traversed, true)) {
// Build hierarchical node copy from the current node. All the attributes
// of $node must be copied into $newNode.
$newNode = $node->ownerDocument->createElement($node->localName);
// Copy all attributes.
foreach ($node->attributes as $attr) {
$newNode->setAttribute($attr->localName, $attr->value);
}
for ($i = 0; $i < $node->childNodes->length; $i++) {
$newNode->appendChild(array_pop($children));
}
$children[] = $newNode;
} else {
$children[] = $node->cloneNode();
}
}
return $children[0];
}
/**
* Import all the child nodes of DOMElement $from to DOMElement $into.
*
* @param DOMElement $from The source DOMElement.
* @param DOMElement $into The target DOMElement.
* @param bool $deep Whether or not to import the whole node hierarchy.
*/
public static function importChildNodes(DOMElement $from, DOMElement $into, $deep = true): void
{
for ($i = 0; $i < $from->childNodes->length; $i++) {
$node = $into->ownerDocument->importNode($from->childNodes->item($i), $deep);
$into->appendChild($node);
}
}
/**
* Import (gracefully i.e. by respecting namespaces) the attributes of DOMElement $from to
* DOMElement $into.
*
* @param DOMElement $from The source DOMElement.
* @param DOMElement $into The target DOMElement.
*/
public static function importAttributes(DOMElement $from, DOMElement $into): void
{
for ($i = 0; $i < $from->attributes->length; $i++) {
$attr = $from->attributes->item($i);
if ($attr->localName !== 'schemaLocation') {
if (empty($attr->namespaceURI) === false) {
$into->setAttributeNS($attr->namespaceURI, $attr->prefix . ':' . $attr->localName, $attr->value);
} else {
$into->setAttribute($attr->localName, $attr->value);
}
}
}
}
/**
* Escape XML Special characters from a given string.
*
* The list below describe each escaped character and its replacement.
*
* * " --> "
* * ' --> '
* * < --> <
* * > --> $gt;
* * & --> &
*
* @param string $string An input string.
* @param bool $isAttribute Whether or not to escape ', >, < which do not have to be escaped in attributes.
* @return string An escaped string.
*/
public static function escapeXmlSpecialChars($string, $isAttribute = false): string
{
if ($isAttribute !== false) {
return str_replace(['&', '"'], ['&', '"'], $string);
}
$fullSearch = ['&', '"', "'", '<', '>'];
$fullReplace = ['&', '"', ''', '<', '>'];
return str_replace($fullSearch, $fullReplace, $string);
}
/**
* Web Component friendly version of a QTI name (without qti-).
*
* This method returns the Web Component friendly version of a QTI attribute name.
*
* Example: "minChoices" becomes "min-choices".
*
* @param string $qtiName
* @return string
*/
public static function webComponentFriendlyAttributeName($qtiName): string
{
return strtolower(preg_replace('/([A-Z])/', '-$1', $qtiName));
}
/**
* Web Component friendly version of a QTI name (with qti-).
*
* This method returns the Web Component friendly version of a QTI class name.
*
* Example: "choiceInteraction" becomes "qti-choice-interaction".
*
* @param string $qtiName
* @return string
*/
public static function webComponentFriendlyClassName($qtiName): string
{
return 'qti-' . self::webComponentFriendlyAttributeName($qtiName);
}
/**
* QTI friendly name of a Web Component friendly name.
*
* This method returns the QTI friendly name of a Web Component friendly name.
*
* Example: "qti-choice-interaction" becomes "choiceInteraction".
*
* @param string $wcName
* @return string
*/
public static function qtiFriendlyName($wcName): string
{
$qtiName = strtolower($wcName);
$qtiName = preg_replace('/^qti-/', '', $qtiName);
return lcfirst(str_replace('-', '', ucwords($qtiName, '-')));
}
/**
* Get the attribute value of a given DOMElement object, cast in a given datatype.
*
* @param DOMElement $element The element the attribute you want to retrieve the value is bound to.
* @param string $attribute The attribute name.
* @param string $datatype The returned datatype. Accepted values are 'string', 'integer', 'float', 'double' and 'boolean'.
* @return mixed The attribute value with the provided $datatype, or null if the attribute does not exist in $element.
* @throws InvalidArgumentException If $datatype is not in the range of possible values.
*/
#[\ReturnTypeWillChange]
public static function getDOMElementAttributeAs(DOMElement $element, string $attribute, $datatype = 'string')
{
$attr = $element->getAttribute($attribute);
if ($attr === '') {
return null;
}
switch ($datatype) {
case 'string':
return $attr;
case 'integer':
return (int)$attr;
case 'double':
case 'float':
return (float)$attr;
case 'boolean':
return $attr === 'true';
}
if (in_array(Enumeration::class, class_implements($datatype), true)) {
/** @var Enumeration $datatype */
if ($attr !== null) {
$constant = $datatype::getConstantByName($attr);
// Returns the original value when it's unknown in the enumeration.
if ($constant === false) {
return $attr;
}
$attr = $constant;
}
return $attr;
}
throw new InvalidArgumentException("Unknown datatype '{$datatype}'.");
}
/**
* Set the attribute value of a given DOMElement object. Boolean values will be transformed
*
* @param DOMElement $element A DOMElement object.
* @param string $attribute An XML attribute name.
* @param mixed $value A given value.
*/
public static function setDOMElementAttribute(DOMElement $element, string $attribute, $value): void
{
$element->setAttribute($attribute, self::valueAsString($value, false));
}
/**
* Set the node value of a given DOMElement object. Boolean values will be transformed as 'true'|'false'.
*
* @param DOMElement $element A DOMElement object.
* @param mixed $value A given value.
*/
public static function setDOMElementValue(DOMElement $element, $value): void
{
$element->nodeValue = self::valueAsString($value);
}
/**
* Converts value to an XML insertable string.
* Boolean is converted to either 'true' or 'false' string.
* Other variable types are optionally using string conversion.
*
* @param mixed $value
* @param bool $encode
* @return string
*/
public static function valueAsString($value, $encode = true): string
{
if (is_bool($value)) {
return $value === true ? 'true' : 'false';
}
if ($encode) {
return htmlspecialchars((string)$value, ENT_XML1, 'UTF-8');
}
return (string)$value;
}
/**
* Get the child elements of a given element by tag name. This method does
* not behave like DOMElement::getElementsByTagName. It only returns the direct
* child elements that matches $tagName but does not go recursive.
*
* @param DOMElement $element A DOMElement object.
* @param mixed $tagName The name of the tags you would like to retrieve or an array of tags to match.
* @param bool $exclude (optional) Whether the $tagName parameter must be considered as a blacklist.
* @param bool $withText (optional) Whether text nodes must be returned or not.
* @return array An array of DOMElement objects.
*/
public static function getChildElementsByTagName($element, $tagName, $exclude = false, $withText = false): array
{
if (!is_array($tagName)) {
$tagName = [$tagName];
}
$rawElts = self::getChildElements($element, $withText);
$returnValue = [];
foreach ($rawElts as $elt) {
if (in_array($elt->localName, $tagName) === !$exclude) {
$returnValue[] = $elt;
}
}
return $returnValue;
}
/**
* Get the children DOM Nodes with nodeType attribute equals to XML_ELEMENT_NODE.
*
* @param DOMElement $element A DOMElement object.
* @param bool $withText Whether text nodes must be returned or not.
* @return array An array of DOMNode objects.
*/
public static function getChildElements($element, $withText = false): array
{
$children = $element->childNodes;
$returnValue = [];
for ($i = 0; $i < $children->length; $i++) {
if ($children->item($i)->nodeType === XML_ELEMENT_NODE || ($withText === true && ($children->item($i)->nodeType === XML_TEXT_NODE || $children->item($i)->nodeType === XML_CDATA_SECTION_NODE))) {
$returnValue[] = $children->item($i);
}
}
return $returnValue;
}
/**
* Removes namespaces defined on non-root element when they are already
* defined on the root element.
*
* @param string $subject
* @param array $redundantNamespaces
* @return string
*/
public static function cleanRedundantNamespaces(string $subject, array $redundantNamespaces): string
{
foreach ($redundantNamespaces as $prefix => $namespace) {
$subject = self::removeAllButFirstOccurrence($subject, ' xmlns:' . $prefix . '="' . $namespace . '"');
}
return $subject;
}
/**
* Removes all but first occurrences of a string within a string.
*
* @param string $subject
* @param string $toRemove
* @return string
*/
public static function removeAllButFirstOccurrence(string $subject, string $toRemove): string
{
$firstPosition = strpos($subject, $toRemove);
if ($firstPosition !== false) {
$begin = substr($subject, 0, $firstPosition + strlen($toRemove));
$end = substr($subject, $firstPosition + strlen($toRemove));
$subject = $begin . str_replace($toRemove, '', $end);
}
return $subject;
}
/**
* Finds all the custom namespaces defined in the xml payload.
*
* @param string $xml
* @return array
*/
public static function findExternalNamespaces(string $xml): array
{
$doc = new SimpleXMLElement($xml);
return array_filter(
$doc->getDocNamespaces(),
static function ($key) {
return $key !== '' && $key !== 'xsi';
},
ARRAY_FILTER_USE_KEY
);
}
/**
* @param callable $command
* @param string $exceptionMessage
* @param int $exceptionCode
* @throws XmlStorageException
*/
public static function executeSafeXmlCommand(
callable $command,
string $exceptionMessage,
int $exceptionCode
): void {
// Disable xml warnings and errors and fetch error information as needed.
$oldErrorConfig = libxml_use_internal_errors(true);
$command();
$libXmlErrors = libxml_get_errors();
libxml_clear_errors();
libxml_use_internal_errors($oldErrorConfig);
if (count($libXmlErrors)) {
// Formats the xml errors and filters out the warning for duplicate schema inclusion.
$formattedErrors = self::formatLibXmlErrors($libXmlErrors);
if ($formattedErrors !== '') {
throw new XmlStorageException(
"{$exceptionMessage}:\n{$formattedErrors}",
$exceptionCode,
null,
new LibXmlErrorCollection($libXmlErrors)
);
}
}
}
/**
* Format some $libXmlErrors into an array of strings instead of an array of arrays.
*
* @param LibXMLError[] $libXmlErrors
* @return string
*/
protected static function formatLibXmlErrors(array $libXmlErrors): string
{
$formattedErrors = [];
foreach ($libXmlErrors as $error) {
switch ($error->level) {
case LIBXML_ERR_WARNING:
// Since QTI 2.2, some schemas are imported multiple times.
// Xerces does not produce errors, but libxml does...
if (preg_match('/Skipping import of schema located/ui', $error->message) === 0) {
$formattedErrors[] = 'Warning: ' . trim($error->message) . ' at ' . $error->line . ':' . $error->column . '.';
}
break;
case LIBXML_ERR_ERROR:
$formattedErrors[] = 'Error: ' . trim($error->message) . ' at ' . $error->line . ':' . $error->column . '.';
break;
case LIBXML_ERR_FATAL:
$formattedErrors[] = 'Fatal Error: ' . trim($error->message) . ' at ' . $error->line . ':' . $error->column . '.';
break;
}
}
$formattedErrors = implode("\n", $formattedErrors);
return $formattedErrors;
}
}