3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
13 // This program is free software; you can redistribute it and/or modify //
14 // it under the terms of the GNU General Public License as published by //
15 // the Free Software Foundation; either version 2 of the License, or //
16 // (at your option) any later version. //
18 // This program is distributed in the hope that it will be useful, //
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21 // GNU General Public License for more details: //
23 // http://www.gnu.org/copyleft/gpl.html //
25 ///////////////////////////////////////////////////////////////////////////
27 /// This class represents an entire XMLDB file
29 class XMLDBFile
extends XMLDBObject
{
37 * Constructor of the XMLDBFile
39 function XMLDBFile ($path) {
40 parent
::XMLDBObject($path);
42 $this->xmldb_structure
= NULL;
46 * Determine if the XML file exists
48 function fileExists() {
49 if (file_exists($this->path
) && is_readable($this->path
)) {
56 * Determine if the XML is writeable
58 function fileWriteable() {
59 if (is_writeable(dirname($this->path
))) {
65 function &getStructure() {
66 return $this->xmldb_structure
;
70 * This function will check/validate the XML file for correctness
71 * Dinamically if will use the best available checker/validator
72 * (expat syntax checker or DOM schema validator
74 function validateXMLStructure() {
76 /// Going to perform complete DOM schema validation
77 if (extension_loaded('dom')) {
78 /// Let's capture errors
79 if (function_exists('libxml_use_internal_errors')) {
80 libxml_use_internal_errors(true); // This function is PHP5 only (MDL-8730)
83 /// Create and load XML file
84 $parser = new DOMDocument();
85 $parser->load($this->path
);
86 /// Only validate if we have a schema
87 if (!empty($this->schema
) && file_exists($this->schema
)) {
88 $parser->schemaValidate($this->schema
);
92 if (function_exists('libxml_get_errors')) {
93 $errors = libxml_get_errors();
97 if (!empty($errors)) {
98 /// Create one structure to store errors
99 $structure = new XMLDBStructure($this->path
);
100 /// Add errors to structure
101 $structure->errormsg
= 'XML Error: ';
102 foreach ($errors as $error) {
103 $structure->errormsg
.= sprintf("%s at line %d. ",
104 trim($error->message
, "\n\r\t ."),
107 /// Add structure to file
108 $this->xmldb_structure
= $structure;
113 /// Going to perform expat simple check (no validation)
114 else if (function_exists('xml_parser_create')) {
115 $parser = xml_parser_create();
116 if (!xml_parse($parser, file_get_contents($this->path
))) {
117 /// Create one structure to store errors
118 $structure = new XMLDBStructure($this->path
);
119 /// Add error to structure
120 $structure->errormsg
= sprintf("XML Error: %s at line %d",
121 xml_error_string(xml_get_error_code($parser)),
122 xml_get_current_line_number($parser));
123 /// Add structure to file
124 $this->xmldb_structure
= $structure;
128 /// Free parser resources
129 xml_parser_free($parser);
131 /// Arriving here, something is really wrong because nor dom not expat are present
139 * Load and the XMLDB structure from file
141 function loadXMLStructure() {
142 if ($this->fileExists()) {
143 /// Let's validate the XML file
144 if (!$this->validateXMLStructure()) {
147 /// File exists, so let's process it
148 /// Load everything to a big array
149 $xmlarr = xmlize(file_get_contents($this->path
));
150 /// Convert array to xmldb structure
151 $this->xmldb_structure
= $this->arr2XMLDBStructure($xmlarr);
153 if ($this->xmldb_structure
->isLoaded()) {
154 $this->loaded
= true;
164 * This function takes an xmlized array and put it into one XMLDBStructure
166 function arr2XMLDBStructure ($xmlarr) {
167 $structure = new XMLDBStructure($this->path
);
168 $structure->arr2XMLDBStructure($xmlarr);
173 * This function sets the DTD of the XML file
175 function setDTD($path) {
180 * This function sets the schema of the XML file
182 function setSchema($path) {
183 $this->schema
= $path;
187 * This function saves the whole XMLDBStructure to its file
189 function saveXMLFile() {
193 $structure =& $this->getStructure();
195 $result = file_put_contents($this->path
, $structure->xmlOutput());