2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * XML format importer class
21 * @copyright 2008 Andrei Bautu
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 * XML format importer class (uses SAX for speed and low memory footprint).
29 * Provides logic for parsing XML data and calling appropriate callbacks.
30 * Subclasses should define XML data sources.
32 abstract class xml_database_importer
extends database_importer
{
33 protected $current_table;
34 protected $current_row;
35 protected $current_field;
36 protected $current_data;
37 protected $current_data_is_null;
40 * Creates and setups a SAX parser. Subclasses should use this method to
41 * create the XML parser.
43 * @return resource XML parser resource.
45 protected function get_parser() {
46 $parser = xml_parser_create();
47 xml_set_object($parser, $this);
48 xml_set_element_handler($parser, 'tag_open', 'tag_close');
49 xml_set_character_data_handler($parser, 'cdata');
50 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING
, false);
55 * Callback function. Called by the XML parser for opening tags processing.
57 * @param resource $parser XML parser resource.
58 * @param string $tag name of opening tag
59 * @param array $attributes set of opening tag XML attributes
62 protected function tag_open($parser, $tag, $attributes) {
64 case 'moodle_database' :
65 if (empty($attributes['version']) ||
empty($attributes['timestamp'])) {
66 throw new dbtransfer_exception('malformedxmlexception');
68 $this->begin_database_import($attributes['version'], $attributes['timestamp']);
71 if (isset($this->current_table
)) {
72 throw new dbtransfer_exception('malformedxmlexception');
74 if (empty($attributes['name']) ||
empty($attributes['schemaHash'])) {
75 throw new dbtransfer_exception('malformedxmlexception');
77 $this->current_table
= $attributes['name'];
78 $this->begin_table_import($this->current_table
, $attributes['schemaHash']);
81 if (isset($this->current_row
) ||
!isset($this->current_table
)) {
82 throw new dbtransfer_exception('malformedxmlexception');
84 $this->current_row
= new stdClass();
87 if (isset($this->current_field
) ||
!isset($this->current_row
)) {
88 throw new dbtransfer_exception('malformedxmlexception');
90 $this->current_field
= $attributes['name'];
91 $this->current_data
= '';
92 if (isset($attributes['value']) and $attributes['value'] === 'null') {
93 $this->current_data_is_null
= true;
95 $this->current_data_is_null
= false;
99 throw new dbtransfer_exception('malformedxmlexception');
104 * Callback function. Called by the XML parser for closing tags processing.
106 * @param resource $parser XML parser resource.
107 * @param string $tag name of opening tag
110 protected function tag_close($parser, $tag) {
112 case 'moodle_database' :
113 $this->finish_database_import();
117 $this->finish_table_import($this->current_table
);
118 $this->current_table
= null;
122 $this->import_table_data($this->current_table
, $this->current_row
);
123 $this->current_row
= null;
127 $field = $this->current_field
;
128 if ($this->current_data_is_null
) {
129 $this->current_row
->$field = null;
131 $this->current_row
->$field = $this->current_data
;
133 $this->current_field
= null;
134 $this->current_data
= null;
135 $this->current_data_is_null
= null;
139 throw new dbtransfer_exception('malformedxmlexception');
144 * Callback function. Called by the XML parser for character data processing.
146 * @param resource $parser XML parser resource.
147 * @param string $data character data to be processed
150 protected function cdata($parser, $data) {
151 if (isset($this->current_field
)) {
152 $this->current_data
.= $data;