Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / xmldb / classes / XMLDBFile.class.php
bloba4df766893611719b445858037fe45f9c76a07ef
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
12 // //
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. //
17 // //
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: //
22 // //
23 // http://www.gnu.org/copyleft/gpl.html //
24 // //
25 ///////////////////////////////////////////////////////////////////////////
27 /// This class represents an entire XMLDB file
29 class XMLDBFile extends XMLDBObject {
31 var $path;
32 var $schema;
33 var $dtd;
34 var $xmldb_structure;
36 /**
37 * Constructor of the XMLDBFile
39 function XMLDBFile ($path) {
40 parent::XMLDBObject($path);
41 $this->path = $path;
42 $this->xmldb_structure = NULL;
45 /**
46 * Determine if the XML file exists
48 function fileExists() {
49 if (file_exists($this->path) && is_readable($this->path)) {
50 return true;
52 return false;
55 /**
56 * Determine if the XML is writeable
58 function fileWriteable() {
59 if (is_writeable(dirname($this->path))) {
60 return true;
62 return false;
65 function &getStructure() {
66 return $this->xmldb_structure;
69 /**
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);
90 /// Check for errors
91 $errors = false;
92 if (function_exists('libxml_get_errors')) {
93 $errors = libxml_get_errors();
96 /// Prepare 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 ."),
105 $error->line);
107 /// Add structure to file
108 $this->xmldb_structure = $structure;
109 /// Check has failed
110 return false;
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;
125 /// Check has failed
126 return false;
128 /// Free parser resources
129 xml_parser_free($parser);
131 /// Arriving here, something is really wrong because nor dom not expat are present
132 else {
133 return false;
135 return true;
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()) {
145 return false;
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);
152 /// Analize results
153 if ($this->xmldb_structure->isLoaded()) {
154 $this->loaded = true;
155 return true;
156 } else {
157 return false;
160 return 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);
169 return $structure;
173 * This function sets the DTD of the XML file
175 function setDTD($path) {
176 $this->dtd = $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() {
191 $result = true;
193 $structure =& $this->getStructure();
195 $result = file_put_contents($this->path, $structure->xmlOutput());
197 return $result;