|
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 00045 class database_importer { 00047 protected $mdb; 00049 protected $manager; 00051 protected $schema; 00057 protected $check_schema; 00061 protected $transactionmode = 'allinone'; 00063 protected $transaction; 00064 00074 public function __construct(moodle_database $mdb, $check_schema=true) { 00075 $this->mdb = $mdb; 00076 $this->manager = $mdb->get_manager(); 00077 $this->schema = $this->manager->get_install_xml_schema(); 00078 $this->check_schema = $check_schema; 00079 } 00080 00085 public function set_transaction_mode($mode) { 00086 if (!in_array($mode, array('pertable', 'allinone', 'none'))) { 00087 throw new coding_exception('Unknown transaction mode', $mode); 00088 } 00089 $this->transactionmode = $mode; 00090 } 00091 00104 public function begin_database_import($version, $timestamp) { 00105 global $CFG; 00106 00107 if (!$this->mdb->get_tables()) { 00108 // not tables yet, time to create all tables 00109 $this->manager->install_from_xmldb_structure($this->schema); 00110 } 00111 00112 if (round($version, 2) !== round($CFG->version, 2)) { // version might be in decimal format too 00113 $a = (object)array('schemaver'=>$version, 'currentver'=>$CFG->version); 00114 throw new dbtransfer_exception('importversionmismatchexception', $a); 00115 } 00116 00117 if ($this->check_schema and $errors = $this->manager->check_database_schema($this->schema)) { 00118 $details = ''; 00119 foreach ($errors as $table=>$items) { 00120 $details .= '<div>'.get_string('table').' '.$table.':'; 00121 $details .= '<ul>'; 00122 foreach ($items as $item) { 00123 $details .= '<li>'.$item.'</li>'; 00124 } 00125 $details .= '</ul></div>'; 00126 } 00127 throw new dbtransfer_exception('importschemaexception', $details); 00128 } 00129 if ($this->transactionmode == 'allinone') { 00130 $this->transaction = $this->mdb->start_delegated_transaction(); 00131 } 00132 } 00133 00145 public function begin_table_import($tablename, $schemaHash) { 00146 if ($this->transactionmode == 'pertable') { 00147 $this->transaction = $this->mdb->start_delegated_transaction(); 00148 } 00149 if (!$table = $this->schema->getTable($tablename)) { 00150 throw new dbtransfer_exception('unknowntableexception', $tablename); 00151 } 00152 if ($schemaHash != $table->getHash()) { 00153 throw new dbtransfer_exception('differenttableexception', $tablename); 00154 } 00155 // this should not happen, unless someone drops tables after import started 00156 if (!$this->manager->table_exists($table)) { 00157 throw new ddl_table_missing_exception($tablename); 00158 } 00159 $this->mdb->delete_records($tablename); 00160 } 00161 00168 public function finish_table_import($tablename) { 00169 $table = $this->schema->getTable($tablename); 00170 $fields = $table->getFields(); 00171 foreach ($fields as $field) { 00172 if ($field->getSequence()) { 00173 $this->manager->reset_sequence($tablename); 00174 return; 00175 } 00176 } 00177 if ($this->transactionmode == 'pertable') { 00178 $this->transaction->allow_commit(); 00179 } 00180 } 00181 00187 public function finish_database_import() { 00188 if ($this->transactionmode == 'allinone') { 00189 $this->transaction->allow_commit(); 00190 } 00191 } 00192 00206 public function import_table_data($tablename, $data) { 00207 $this->mdb->import_record($tablename, $data); 00208 } 00209 00214 public function import_database() { 00215 // implement in subclasses 00216 } 00217 }