--- /cvsroot/moodle/moodle/admin/dbtransfer/lib.php +++ /cvsroot/moodle/moodle/admin/dbtransfer/lib.php @@ -0,0 +1,174 @@ +libdir . '/xmldb/xmldb_structure.php'); + +abstract class database_exporter { + protected $mdb; + protected $manager; + public function __construct(moodle_database $mdb = null) { + if(is_null($mdb)) { + global $DB; + $mdb = $DB; + } + $this->mdb = $mdb; + $this->manager = $mdb->get_manager(); + } + + public function begin_database_export($description = NULL) { + } + + public function begin_table_export(xmldb_table $table) { + } + + public abstract function export_table_data(xmldb_table $table, $data); + + public function finish_table_export() { + } + + public function finish_database_export(){ + } + + public function export_database(xmldb_structure $schema, $description = NULL) { + $this->begin_database_export($description); + $tables = $schema->getTables(); + foreach($tables as $table) { + $this->export_table($table); + } + $this->finish_database_export(); + } + + public function export_table(xmldb_table $table) { + $this->begin_table_export($table); + $rs = $this->mdb->get_recordset_sql('SELECT * FROM {' . $table->getName() . '}'); + foreach($rs as $row) { + $this->export_table_data($table, $row); + } + $this->finish_table_export(); + } +} + +class DatabaseImporter { + protected $mdb; + protected $manager; + public function __construct(moodle_database $mdb = NULL) { + if(is_null($mdb)) { + global $DB; + $mdb = $DB; + } + $this->mdb = $mdb; + $this->manager = $mdb->get_manager(); + } + + public function begin_database_import($version, $timestamp, $description = NULL) { + } + + public function begin_table_import(xmldb_table $table) { + if(!$this->manager->table_exists($table)) { + $this->manager->create_table($table); + } + } + + function import_table_data(xmldb_table $table, $data) { + $this->mdb->import_record($table->getName(), $data); + } + + public function finish_table_import() { + } + + public function finish_database_import(){ + } + + public function import_database(xmldb_structure $schema, $description = NULL) { + $this->begin_database_export($description); + $tables = $structure->getTables(); + foreach($tables as $table) { + $this->import_table($table); + } + $this->finish_database_export(); + } + + public function import_table(xmldb_table $table) { + $this->begin_table_export($table); + $rs = $this->mdb->get_recordset_sql('SELECT * FROM {' . $table->getName() . '}'); + foreach($rs as $row) { + $this->export_table_data($table, $row); + } + $this->finish_table_export(); + } +} + +abstract class xml_database_exporter extends database_exporter { + protected abstract function output($text); + + public function begin_database_export($description = NULL) { + global $CFG; + $this->output(''); + $this->output(''); + } + + public function begin_table_export(xmldb_table $table) { + $this->output(''); + } + + public function export_table_data(xmldb_table $table, $data) { + $this->output(''); + foreach($data as $key=>$value) { + if(is_null($value)) { + $this->output(''); + } else { + $this->output('' . htmlspecialchars($value, ENT_NOQUOTES). ''); + } + } + $this->output(''); + } + + public function finish_table_export() { + $this->output('
'); + } + + public function finish_database_export() { + $this->output('
'); + } +} + +class file_xml_database_exporter extends xml_database_exporter { + protected $filepath; + protected $file; + function __construct($filepath = NULL, moodle_database $mdb = NULL) { + parent::__construct($mdb); + if(is_null($filepath)) { + $filepath = 'php://output'; + } + $this->filepath = $filepath; + } + + public function begin_database_export($description = NULL) { + $this->file = fopen($this->filepath, "w"); + parent::begin_database_export($description); + } + + protected function output($text) { + fwrite($this->file, $text); + } + + public function finish_database_export() { + parent::finish_database_export(); + fclose($this->file); + } +} + +class string_xml_database_exporter extends xml_database_exporter { + protected $data; + public function begin_database_export($description = NULL) { + $this->data = ''; + parent::begin_database_export($description); + } + + protected function output($text) { + $this->data .= $text; + } + + public function get_output() { + return $this->data; + } +}