source: moodle/trunk/fuentes/admin/tool/xmldb/actions/load_xml_file/load_xml_file.class.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 4 years ago

Ported code to xenial

File size: 4.0 KB
Line 
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 */
30class 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        // Set own custom attributes
42        $this->sesskey_protected = false; // This action doesn't need sesskey protection
43
44        // Get needed strings
45        $this->loadStrings(array(
46            // 'key' => 'module',
47        ));
48    }
49
50    /**
51     * Invoke method, every class will have its own
52     * returns true/false on completion, setting both
53     * errormsg and output as necessary
54     */
55    function invoke() {
56        parent::invoke();
57
58        $result = true;
59
60        // Set own core attributes
61        $this->does_generate = ACTION_NONE;
62        //$this->does_generate = ACTION_GENERATE_HTML;
63
64        // These are always here
65        global $CFG, $XMLDB;
66
67        // Do the job, setting $result as needed
68
69        // Get the dir containing the file
70        $dirpath = required_param('dir', PARAM_PATH);
71        $dirpath = $CFG->dirroot . $dirpath;
72
73        // Get the correct dir
74        if (!empty($XMLDB->dbdirs)) {
75            $dbdir = $XMLDB->dbdirs[$dirpath];
76            if ($dbdir) {
77                // Set some defaults
78                $dbdir->xml_exists = false;
79                $dbdir->xml_writeable = false;
80                $dbdir->xml_loaded  = false;
81                // Only if the directory exists
82                if (!$dbdir->path_exists) {
83                    return false;
84                }
85                $xmldb_file = new xmldb_file($dbdir->path . '/install.xml');
86                // Set the XML DTD and schema
87                $xmldb_file->setDTD($CFG->dirroot . '/lib/xmldb/xmldb.dtd');
88                $xmldb_file->setSchema($CFG->dirroot . '/lib/xmldb/xmldb.xsd');
89                // Set dbdir as necessary
90                if ($xmldb_file->fileExists()) {
91                    $dbdir->xml_exists = true;
92                }
93                if ($xmldb_file->fileWriteable()) {
94                    $dbdir->xml_writeable = true;
95                }
96                // Load the XML contents to structure
97                $loaded = $xmldb_file->loadXMLStructure();
98                if ($loaded && $xmldb_file->isLoaded()) {
99                    $dbdir->xml_loaded = true;
100                    $dbdir->filemtime = filemtime($dbdir->path . '/install.xml');
101                }
102                $dbdir->xml_file = $xmldb_file;
103            } else {
104                $this->errormsg = 'Wrong directory (' . $dirpath . ')';
105                $result = false;
106            }
107        } else {
108            $this->errormsg = 'XMLDB structure not found';
109            $result = false;
110        }
111        // Launch postaction if exists
112        if ($this->getPostAction() && $result) {
113            return $this->launch($this->getPostAction());
114        }
115
116        return $result;
117    }
118}
119
Note: See TracBrowser for help on using the repository browser.