From 45c0f44477729dbcf7cf99546c207a903fd38786 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Sat, 8 Dec 2018 23:52:11 +0100 Subject: [PATCH] Expose helper methods for XML processing The `getSchema()` and `validate()` method have both been deprecated, they were never used and remained in a very strange, partly unfinished state. There are no plans to actively use these methods in the core anyway. See #2646 --- wcfsetup/install/files/lib/util/XML.class.php | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/wcfsetup/install/files/lib/util/XML.class.php b/wcfsetup/install/files/lib/util/XML.class.php index 2557085fb4..d439508072 100644 --- a/wcfsetup/install/files/lib/util/XML.class.php +++ b/wcfsetup/install/files/lib/util/XML.class.php @@ -15,7 +15,7 @@ class XML { * DOMDocument object * @var \DOMDocument */ - protected $document = null; + protected $document; /** * document path @@ -33,7 +33,7 @@ class XML { * DOMXPath object * @var \DOMXPath */ - protected $xpath = null; + protected $xpath; /** * Prepares a new instance of DOMDocument and enables own error handler for libxml. @@ -97,6 +97,8 @@ class XML { /** * Validate the loaded document against the specified xml schema definition. + * + * @deprecated 3.2 */ public function validate() { // determine schema @@ -114,18 +116,14 @@ class XML { /** * Determines schema for given document. + * + * @deprecated 3.2 */ protected function getSchema() { - // determine schema by looking for xsi:schemaLocation - $this->schema = $this->document->documentElement->getAttributeNS($this->document->documentElement->lookupNamespaceUri('xsi'), 'schemaLocation'); - - // no valid schema found or it's lacking a valid namespace - if (strpos($this->schema, ' ') === false) { - throw new SystemException("XML document '".$this->path."' does not provide a valid schema."); - } + $tmp = $this->getSchemaLocation(); + $this->schema = "{$tmp[0]} {$tmp[1]}"; // build file path upon namespace and filename - $tmp = explode(' ', $this->schema); $this->schema = WCF_DIR.'xsd/'.mb_substr(sha1($tmp[0]), 0, 8) . '_' . basename($tmp[1]); if (!file_exists($this->schema) || !is_readable($this->schema)) { @@ -133,6 +131,28 @@ class XML { } } + /** + * Reads the schema location and returns an array containing ['namespace', 'uri']. + * + * @return string[] + * @since 3.2 + */ + public function getSchemaLocation() { + $schema = $this->document->documentElement->getAttributeNS( + $this->document->documentElement->lookupNamespaceUri('xsi'), + 'schemaLocation' + ); + + $parts = explode(' ', $schema, 2); + + // Schemas must include a namespace. + if (count($parts) !== 2) { + throw new SystemException("XML document '".$this->path."' does not provide a valid schema."); + } + + return $parts; + } + /** * Returns a DOMXPath object bound to current DOMDocument object. Default * namespace will be bound to prefix 'ns'. @@ -157,7 +177,7 @@ class XML { * @see \wcf\util\XML::__construct() * @return string[][] */ - protected function pollErrors() { + public function pollErrors() { $errors = []; $errorList = libxml_get_errors(); @@ -272,7 +292,7 @@ class XML { * @return array attributes * @since 3.2 */ - protected function getAttributes(\DOMElement $element) { + public function getAttributes(\DOMElement $element) { $attributes = []; /** @var \DOMNode $attribute */ foreach ($element->attributes as $attribute) { -- 2.20.1