|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00018 00028 defined('MOODLE_INTERNAL') || die(); 00029 00035 abstract class xml_database_importer extends database_importer { 00036 protected $current_table; 00037 protected $current_row; 00038 protected $current_field; 00039 protected $current_data; 00040 protected $current_data_is_null; 00041 00048 protected function get_parser() { 00049 $parser = xml_parser_create(); 00050 xml_set_object($parser, $this); 00051 xml_set_element_handler($parser, 'tag_open', 'tag_close'); 00052 xml_set_character_data_handler($parser, 'cdata'); 00053 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false); 00054 return $parser; 00055 } 00056 00065 protected function tag_open($parser, $tag, $attributes) { 00066 switch ($tag) { 00067 case 'moodle_database' : 00068 if (empty($attributes['version']) || empty($attributes['timestamp'])) { 00069 throw new dbtransfer_exception('malformedxmlexception'); 00070 } 00071 $this->begin_database_import($attributes['version'], $attributes['timestamp']); 00072 break; 00073 case 'table' : 00074 if (isset($this->current_table)) { 00075 throw new dbtransfer_exception('malformedxmlexception'); 00076 } 00077 if (empty($attributes['name']) || empty($attributes['schemaHash'])) { 00078 throw new dbtransfer_exception('malformedxmlexception'); 00079 } 00080 $this->current_table = $attributes['name']; 00081 $this->begin_table_import($this->current_table, $attributes['schemaHash']); 00082 break; 00083 case 'record' : 00084 if (isset($this->current_row) || !isset($this->current_table)) { 00085 throw new dbtransfer_exception('malformedxmlexception'); 00086 } 00087 $this->current_row = new stdClass(); 00088 break; 00089 case 'field' : 00090 if (isset($this->current_field) || !isset($this->current_row)) { 00091 throw new dbtransfer_exception('malformedxmlexception'); 00092 } 00093 $this->current_field = $attributes['name']; 00094 $this->current_data = ''; 00095 if (isset($attributes['value']) and $attributes['value'] === 'null') { 00096 $this->current_data_is_null = true; 00097 } else { 00098 $this->current_data_is_null = false; 00099 } 00100 break; 00101 default : 00102 throw new dbtransfer_exception('malformedxmlexception'); 00103 } 00104 } 00105 00113 protected function tag_close($parser, $tag) { 00114 switch ($tag) { 00115 case 'moodle_database' : 00116 $this->finish_database_import(); 00117 break; 00118 00119 case 'table' : 00120 $this->finish_table_import($this->current_table); 00121 $this->current_table = null; 00122 break; 00123 00124 case 'record' : 00125 $this->import_table_data($this->current_table, $this->current_row); 00126 $this->current_row = null;; 00127 break; 00128 00129 case 'field' : 00130 $field = $this->current_field; 00131 if ($this->current_data_is_null) { 00132 $this->current_row->$field = null; 00133 } else { 00134 $this->current_row->$field = $this->current_data; 00135 } 00136 $this->current_field = null; 00137 $this->current_data = null; 00138 $this->current_data_is_null = null; 00139 break; 00140 00141 default : 00142 throw new dbtransfer_exception('malformedxmlexception'); 00143 } 00144 } 00145 00153 protected function cdata($parser, $cdata) { 00154 if (isset($this->current_field)) { 00155 $this->current_data .= $cdata; 00156 } 00157 } 00158 }