weekly release 2.1.7+
[moodle.git] / lib / dtl / xml_database_importer.php
blob24ca87ab9a6c8c744aa62c7511d223bf559449cc
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
19 /**
20 * XML format importer class
22 * @package core
23 * @subpackage dtl
24 * @copyright 2008 Andrei Bautu
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 /**
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;
42 /**
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);
54 return $parser;
57 /**
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
63 * @return void
65 protected function tag_open($parser, $tag, $attributes) {
66 switch ($tag) {
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']);
72 break;
73 case 'table' :
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']);
82 break;
83 case 'record' :
84 if (isset($this->current_row) || !isset($this->current_table)) {
85 throw new dbtransfer_exception('malformedxmlexception');
87 $this->current_row = new stdClass();
88 break;
89 case 'field' :
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;
97 } else {
98 $this->current_data_is_null = false;
100 break;
101 default :
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
111 * @return void
113 protected function tag_close($parser, $tag) {
114 switch ($tag) {
115 case 'moodle_database' :
116 $this->finish_database_import();
117 break;
119 case 'table' :
120 $this->finish_table_import($this->current_table);
121 $this->current_table = null;
122 break;
124 case 'record' :
125 $this->import_table_data($this->current_table, $this->current_row);
126 $this->current_row = null;;
127 break;
129 case 'field' :
130 $field = $this->current_field;
131 if ($this->current_data_is_null) {
132 $this->current_row->$field = null;
133 } else {
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;
139 break;
141 default :
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
151 * @return void
153 protected function cdata($parser, $cdata) {
154 if (isset($this->current_field)) {
155 $this->current_data .= $cdata;