3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * XML format importer class
24 * @copyright 2008 Andrei Bautu
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') ||
die();
31 * XML format importer class (uses SAX for speed and low memory footprint).
32 * Provides logic for parsing XML data and calling appropriate callbacks.
33 * Subclasses should define XML data sources.
35 abstract class xml_database_importer
extends database_importer
{
36 protected $current_table;
37 protected $current_row;
38 protected $current_field;
39 protected $current_data;
40 protected $current_data_is_null;
43 * Creates and setups a SAX parser. Subclasses should use this method to
44 * create the XML parser.
46 * @return resource XML parser resource.
48 protected function get_parser() {
49 $parser = xml_parser_create();
50 xml_set_object($parser, $this);
51 xml_set_element_handler($parser, 'tag_open', 'tag_close');
52 xml_set_character_data_handler($parser, 'cdata');
53 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING
, false);
58 * Callback function. Called by the XML parser for opening tags processing.
60 * @param resource $parser XML parser resource.
61 * @param string $tag name of opening tag
62 * @param array $attributes set of opening tag XML attributes
65 protected function tag_open($parser, $tag, $attributes) {
67 case 'moodle_database' :
68 if (empty($attributes['version']) ||
empty($attributes['timestamp'])) {
69 throw new dbtransfer_exception('malformedxmlexception');
71 $this->begin_database_import($attributes['version'], $attributes['timestamp']);
74 if (isset($this->current_table
)) {
75 throw new dbtransfer_exception('malformedxmlexception');
77 if (empty($attributes['name']) ||
empty($attributes['schemaHash'])) {
78 throw new dbtransfer_exception('malformedxmlexception');
80 $this->current_table
= $attributes['name'];
81 $this->begin_table_import($this->current_table
, $attributes['schemaHash']);
84 if (isset($this->current_row
) ||
!isset($this->current_table
)) {
85 throw new dbtransfer_exception('malformedxmlexception');
87 $this->current_row
= new stdClass();
90 if (isset($this->current_field
) ||
!isset($this->current_row
)) {
91 throw new dbtransfer_exception('malformedxmlexception');
93 $this->current_field
= $attributes['name'];
94 $this->current_data
= '';
95 if (isset($attributes['value']) and $attributes['value'] === 'null') {
96 $this->current_data_is_null
= true;
98 $this->current_data_is_null
= false;
102 throw new dbtransfer_exception('malformedxmlexception');
107 * Callback function. Called by the XML parser for closing tags processing.
109 * @param resource $parser XML parser resource.
110 * @param string $tag name of opening tag
113 protected function tag_close($parser, $tag) {
115 case 'moodle_database' :
116 $this->finish_database_import();
120 $this->finish_table_import($this->current_table
);
121 $this->current_table
= null;
125 $this->import_table_data($this->current_table
, $this->current_row
);
126 $this->current_row
= null;;
130 $field = $this->current_field
;
131 if ($this->current_data_is_null
) {
132 $this->current_row
->$field = null;
134 $this->current_row
->$field = $this->current_data
;
136 $this->current_field
= null;
137 $this->current_data
= null;
138 $this->current_data_is_null
= null;
142 throw new dbtransfer_exception('malformedxmlexception');
147 * Callback function. Called by the XML parser for character data processing.
149 * @param resource $parser XML parser resource.
150 * @param string $data character data to be processed
153 protected function cdata($parser, $cdata) {
154 if (isset($this->current_field
)) {
155 $this->current_data
.= $cdata;