1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* @package tool_xmldb
|
|
|
19 |
* @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
|
|
|
20 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
21 |
*/
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* This class will load one XML file to memory if necessary
|
|
|
25 |
*
|
|
|
26 |
* @package tool_xmldb
|
|
|
27 |
* @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
*/
|
|
|
30 |
class load_xml_file extends XMLDBAction {
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Init method, every subclass will have its own
|
|
|
34 |
*/
|
|
|
35 |
function init() {
|
|
|
36 |
parent::init();
|
|
|
37 |
// Set own core attributes
|
|
|
38 |
$this->can_subaction = ACTION_NONE;
|
|
|
39 |
//$this->can_subaction = ACTION_HAVE_SUBACTIONS;
|
|
|
40 |
|
|
|
41 |
// Get needed strings
|
|
|
42 |
$this->loadStrings(array(
|
|
|
43 |
// 'key' => 'module',
|
|
|
44 |
));
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Invoke method, every class will have its own
|
|
|
49 |
* returns true/false on completion, setting both
|
|
|
50 |
* errormsg and output as necessary
|
|
|
51 |
*/
|
|
|
52 |
function invoke() {
|
|
|
53 |
parent::invoke();
|
|
|
54 |
|
|
|
55 |
$result = true;
|
|
|
56 |
|
|
|
57 |
// Set own core attributes
|
|
|
58 |
$this->does_generate = ACTION_NONE;
|
|
|
59 |
//$this->does_generate = ACTION_GENERATE_HTML;
|
|
|
60 |
|
|
|
61 |
// These are always here
|
|
|
62 |
global $CFG, $XMLDB;
|
|
|
63 |
|
|
|
64 |
// Do the job, setting $result as needed
|
|
|
65 |
|
|
|
66 |
// Get the dir containing the file
|
|
|
67 |
$dirpath = required_param('dir', PARAM_PATH);
|
|
|
68 |
$dirpath = $CFG->dirroot . $dirpath;
|
|
|
69 |
|
|
|
70 |
// Get the correct dir
|
|
|
71 |
if (!empty($XMLDB->dbdirs)) {
|
|
|
72 |
$dbdir = $XMLDB->dbdirs[$dirpath];
|
|
|
73 |
if ($dbdir) {
|
|
|
74 |
// Set some defaults
|
|
|
75 |
$dbdir->xml_exists = false;
|
|
|
76 |
$dbdir->xml_writeable = false;
|
|
|
77 |
$dbdir->xml_loaded = false;
|
|
|
78 |
// Only if the directory exists
|
|
|
79 |
if (!$dbdir->path_exists) {
|
|
|
80 |
return false;
|
|
|
81 |
}
|
|
|
82 |
$xmldb_file = new xmldb_file($dbdir->path . '/install.xml');
|
|
|
83 |
// Set the XML DTD and schema
|
|
|
84 |
$xmldb_file->setDTD($CFG->dirroot . '/lib/xmldb/xmldb.dtd');
|
|
|
85 |
$xmldb_file->setSchema($CFG->dirroot . '/lib/xmldb/xmldb.xsd');
|
|
|
86 |
// Set dbdir as necessary
|
|
|
87 |
if ($xmldb_file->fileExists()) {
|
|
|
88 |
$dbdir->xml_exists = true;
|
|
|
89 |
}
|
|
|
90 |
if ($xmldb_file->fileWriteable()) {
|
|
|
91 |
$dbdir->xml_writeable = true;
|
|
|
92 |
}
|
|
|
93 |
// Load the XML contents to structure
|
|
|
94 |
$loaded = $xmldb_file->loadXMLStructure();
|
|
|
95 |
if ($loaded && $xmldb_file->isLoaded()) {
|
|
|
96 |
$dbdir->xml_loaded = true;
|
|
|
97 |
$dbdir->filemtime = filemtime($dbdir->path . '/install.xml');
|
|
|
98 |
}
|
|
|
99 |
$dbdir->xml_file = $xmldb_file;
|
|
|
100 |
} else {
|
|
|
101 |
$this->errormsg = 'Wrong directory (' . $dirpath . ')';
|
|
|
102 |
$result = false;
|
|
|
103 |
}
|
|
|
104 |
} else {
|
|
|
105 |
$this->errormsg = 'XMLDB structure not found';
|
|
|
106 |
$result = false;
|
|
|
107 |
}
|
|
|
108 |
// Launch postaction if exists
|
|
|
109 |
if ($this->getPostAction() && $result) {
|
|
|
110 |
return $this->launch($this->getPostAction());
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
return $result;
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
|