]> git.mjollnir.org Git - moodle.git/commitdiff
Now XMLDB Editor performs check/validation of XML files
authorstronk7 <stronk7>
Fri, 23 Feb 2007 20:59:28 +0000 (20:59 +0000)
committerstronk7 <stronk7>
Fri, 23 Feb 2007 20:59:28 +0000 (20:59 +0000)
before loading them using the best parser available.
MDL-7726 and first part of MDL-8264

Merged from MOODLE_18_STABLE

admin/xmldb/actions/load_xml_file/load_xml_file.class.php
lib/xmldb/classes/XMLDBFile.class.php

index 0922b2b2cfb246fb79f928d3dd24e3f2e32848ae..eb56ad7d87c8545616af5db316e1835f93607b42 100644 (file)
@@ -81,6 +81,9 @@ class load_xml_file extends XMLDBAction {
                     return false;
                 }
                 $xmldb_file = new XMLDBFile($dbdir->path . '/install.xml');
+            ///Set the XML DTD and schema
+                $xmldb_file->setDTD($CFG->dirroot . '/lib/xmldb/xmldb.dtd');
+                $xmldb_file->setSchema($CFG->dirroot . '/lib/xmldb/xmldb.xsd');
             /// Set dbdir as necessary
                 if ($xmldb_file->fileExists()) {
                     $dbdir->xml_exists = true;
index 571aa99ba2736fda822f5c2a9180c33a5844e460..b39c8c7fbdadfebc98bf97a18c14ba3248734afd 100644 (file)
@@ -29,6 +29,8 @@
 class XMLDBFile extends XMLDBObject {
 
     var $path;
+    var $schema;
+    var $dtd;
     var $xmldb_structure;
 
     /**
@@ -64,11 +66,73 @@ class XMLDBFile extends XMLDBObject {
         return $this->xmldb_structure;
     }
 
+    /**
+     * This function will check/validate the XML file for correctness 
+     * Dinamically if will use the best available checker/validator
+     * (expat syntax checker or DOM schema validator
+     */
+    function validateXMLStructure() {
+
+    /// Going to perform complete DOM schema validation
+        if (extension_loaded('dom')) {
+        /// Let's capture errors
+            libxml_use_internal_errors(true);
+        /// Create and load XML file
+            $parser = new DOMDocument();
+            $parser->load($this->path);
+        /// Validate XML file against schema
+            if (!$parser->schemaValidate($this->schema)) {
+            /// Get errors
+                $errors = libxml_get_errors();
+            /// Create one structure to store errors
+                $structure = new XMLDBStructure($this->path);
+            /// Add errors to structure
+                $structure->errormsg = 'XML Error: ';
+                foreach ($errors as $error) {
+                    $structure->errormsg .= sprintf("%s at line %d. ", 
+                                                     trim($error->message, "\n\r\t ."),
+                                                     $error->line);
+                }
+            /// Add structure to file
+                $this->xmldb_structure = $structure;
+            /// Check has failed
+                return false;
+            }
+        }
+    /// Going to perform expat simple check (no validation)
+        else if (function_exists('xml_parser_create')) {
+            $parser = xml_parser_create();
+            if (!xml_parse($parser, file_get_contents($this->path))) {
+            /// Create one structure to store errors
+                $structure = new XMLDBStructure($this->path);
+            /// Add error to structure
+                $structure->errormsg = sprintf("XML Error: %s at line %d", 
+                         xml_error_string(xml_get_error_code($parser)),
+                         xml_get_current_line_number($parser));
+            /// Add structure to file
+                $this->xmldb_structure = $structure;
+            /// Check has failed
+                return false;
+            }
+        /// Free parser resources
+            xml_parser_free($parser);
+        }
+    /// Arriving here, something is really wrong because nor dom not expat are present
+        else {
+            return false;
+        }
+        return true;
+    }
+
     /**
      * Load and the XMLDB structure from file
      */
     function loadXMLStructure() {
         if ($this->fileExists()) {
+        /// Let's validate the XML file
+            if (!$this->validateXMLStructure()) {
+                return false;
+            }
         /// File exists, so let's process it
         /// Load everything to a big array
             $xmlarr = xmlize(file_get_contents($this->path));
@@ -94,6 +158,20 @@ class XMLDBFile extends XMLDBObject {
         return $structure;
     }
 
+    /**
+     * This function sets the DTD of the XML file
+     */
+    function setDTD($path) {
+        $this->dtd = $path;
+    }
+
+    /**
+     * This function sets the schema of the XML file
+     */
+    function setSchema($path) {
+        $this->schema = $path;
+    }
+
     /**
      * This function saves the whole XMLDBStructure to its file
      */