See Release Notes
Long Term Support Release
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 * XML format importer class 19 * 20 * @package core_dtl 21 * @copyright 2008 Andrei Bautu 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 /** 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. 31 */ 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; 38 39 /** 40 * Creates and setups a SAX parser. Subclasses should use this method to 41 * create the XML parser. 42 * 43 * @return resource XML parser resource. 44 */ 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); 51 return $parser; 52 } 53 54 /** 55 * Callback function. Called by the XML parser for opening tags processing. 56 * 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 60 * @return void 61 */ 62 protected function tag_open($parser, $tag, $attributes) { 63 switch ($tag) { 64 case 'moodle_database' : 65 if (empty($attributes['version']) || empty($attributes['timestamp'])) { 66 throw new dbtransfer_exception('malformedxmlexception'); 67 } 68 $this->begin_database_import($attributes['version'], $attributes['timestamp']); 69 break; 70 case 'table' : 71 if (isset($this->current_table)) { 72 throw new dbtransfer_exception('malformedxmlexception'); 73 } 74 if (empty($attributes['name']) || empty($attributes['schemaHash'])) { 75 throw new dbtransfer_exception('malformedxmlexception'); 76 } 77 $this->current_table = $attributes['name']; 78 $this->begin_table_import($this->current_table, $attributes['schemaHash']); 79 break; 80 case 'record' : 81 if (isset($this->current_row) || !isset($this->current_table)) { 82 throw new dbtransfer_exception('malformedxmlexception'); 83 } 84 $this->current_row = new stdClass(); 85 break; 86 case 'field' : 87 if (isset($this->current_field) || !isset($this->current_row)) { 88 throw new dbtransfer_exception('malformedxmlexception'); 89 } 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; 94 } else { 95 $this->current_data_is_null = false; 96 } 97 break; 98 default : 99 throw new dbtransfer_exception('malformedxmlexception'); 100 } 101 } 102 103 /** 104 * Callback function. Called by the XML parser for closing tags processing. 105 * 106 * @param resource $parser XML parser resource. 107 * @param string $tag name of opening tag 108 * @return void 109 */ 110 protected function tag_close($parser, $tag) { 111 switch ($tag) { 112 case 'moodle_database' : 113 $this->finish_database_import(); 114 break; 115 116 case 'table' : 117 $this->finish_table_import($this->current_table); 118 $this->current_table = null; 119 break; 120 121 case 'record' : 122 $this->import_table_data($this->current_table, $this->current_row); 123 $this->current_row = null; 124 break; 125 126 case 'field' : 127 $field = $this->current_field; 128 if ($this->current_data_is_null) { 129 $this->current_row->$field = null; 130 } else { 131 $this->current_row->$field = $this->current_data; 132 } 133 $this->current_field = null; 134 $this->current_data = null; 135 $this->current_data_is_null = null; 136 break; 137 138 default : 139 throw new dbtransfer_exception('malformedxmlexception'); 140 } 141 } 142 143 /** 144 * Callback function. Called by the XML parser for character data processing. 145 * 146 * @param resource $parser XML parser resource. 147 * @param string $data character data to be processed 148 * @return void 149 */ 150 protected function cdata($parser, $data) { 151 if (isset($this->current_field)) { 152 $this->current_data .= $data; 153 } 154 } 155 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body