forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-15635 dtl refactoring and basic export and transfer ui (the impor…
…t UI will be part of install process soon) - based largely on code by Andrei Bautu - thanks!
- Loading branch information
skodak
committed
Sep 2, 2008
1 parent
8aff848
commit 3b49f84
Showing
17 changed files
with
246 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php //$Id$ | ||
|
||
require_once $CFG->libdir.'/formslib.php'; | ||
|
||
class database_export_form extends moodleform { | ||
|
||
function definition() { | ||
$mform = $this->_form; | ||
|
||
$mform->addElement('header', 'database', get_string('dbexport', 'dbtransfer')); | ||
$mform->addElement('textarea', 'description', get_string('description'), array('rows'=>5, 'cols'=>60)); | ||
|
||
$this->add_action_buttons(false, get_string('exportdata', 'dbtransfer')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php //$Id$ | ||
|
||
require_once $CFG->libdir.'/formslib.php'; | ||
|
||
class database_transfer_form extends moodleform { | ||
|
||
function definition() { | ||
$mform = $this->_form; | ||
|
||
$mform->addElement('header', 'database', get_string('dbtransfer', 'dbtransfer')); | ||
|
||
$supported = array ( | ||
'mysqli/adodb', | ||
'mysql/adodb', | ||
'postgres7/adodb', | ||
'mssql_n/adodb', | ||
'mssql/adodb', | ||
'odbc_mssql/adodb', | ||
'oci8po/adodb', | ||
'sqlite3/pdo', | ||
); | ||
$drivers = array(); | ||
foreach($supported as $driver) { | ||
list($dbtype, $dblibrary) = explode('/', $driver); | ||
$targetdb = moodle_database::get_driver_instance($dbtype, $dblibrary); | ||
if ($targetdb->driver_installed() !== true) { | ||
continue; | ||
} | ||
$drivers[$driver] = $driver; | ||
} | ||
|
||
$mform->addElement('select', 'driver', get_string('dbtype', 'install'), $drivers); | ||
$mform->addElement('text', 'dbhost', get_string('dbhost', 'install')); | ||
$mform->addElement('text', 'dbname', get_string('database', 'install')); | ||
$mform->addElement('text', 'dbuser', get_string('user')); | ||
$mform->addElement('text', 'dbpass', get_string('password')); | ||
$mform->addElement('text', 'prefix', get_string('dbprefix', 'install')); | ||
|
||
$mform->addRule('dbhost', get_string('required'), 'required', null); | ||
$mform->addRule('dbname', get_string('required'), 'required', null); | ||
$mform->addRule('dbuser', get_string('required'), 'required', null); | ||
$mform->addRule('dbpass', get_string('required'), 'required', null); | ||
$mform->addRule('prefix', get_string('required'), 'required', null); | ||
|
||
$this->add_action_buttons(false, get_string('transferdata', 'dbtransfer')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php //$Id$ | ||
|
||
require('../../config.php'); | ||
require_once('lib.php'); | ||
require_once('database_export_form.php'); | ||
|
||
require_login(); | ||
admin_externalpage_setup('dbexport'); | ||
|
||
//create form | ||
$form = new database_export_form(); | ||
|
||
if ($data = $form->get_data()) { | ||
dbtransfer_export_xml_database($data->description, $DB); | ||
die; | ||
} | ||
|
||
admin_externalpage_print_header(); | ||
// TODO: add some more info here | ||
$form->display(); | ||
admin_externalpage_print_footer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php //$Id$ | ||
|
||
require('../../config.php'); | ||
require_once('lib.php'); | ||
require_once('database_transfer_form.php'); | ||
|
||
require_login(); | ||
admin_externalpage_setup('dbtransfer'); | ||
|
||
//create form | ||
$form = new database_transfer_form(); | ||
|
||
if ($data = $form->get_data()) { | ||
list($dbtype, $dblibrary) = explode('/', $data->driver); | ||
$targetdb = moodle_database::get_driver_instance($dbtype, $dblibrary); | ||
if (!$targetdb->connect($data->dbhost, $data->dbuser, $data->dbpass, $data->dbname, false, $data->prefix, null)) { | ||
throw new dbtransfer_exception('notargetconectexception', null, "$CFG->wwwroot/$CFG->admin/dbtransfer/"); | ||
} | ||
if ($targetdb->get_tables()) { | ||
// TODO add exception or string... | ||
error('Sorry, tables already exist in selected database. Can not continue.'); | ||
} | ||
admin_externalpage_print_header(); | ||
dbtransfer_transfer_database($DB, $targetdb); | ||
notify(get_string('success'), 'notifysuccess'); | ||
print_continue("$CFG->wwwroot/$CFG->admin/"); | ||
admin_externalpage_print_footer(); | ||
die; | ||
} | ||
|
||
admin_externalpage_print_header(); | ||
// TODO: add some more info here | ||
$form->display(); | ||
admin_externalpage_print_footer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php //$Id$ | ||
|
||
/* | ||
TODO: | ||
- exporting to server file >2GB fails in 32bit operating systems - needs warning | ||
- we may run out of disk space exporting to srever file - we must verify the file is not truncated; read from the end of file? | ||
- when sending file >4GB - FAT32 limit, Apache limit, browser limit - needs warning | ||
- there must be some form of progress bar during export, transfer - new tracking class could be passed around | ||
- command line operation - could work around some 2G/4G limits in PHP; useful for cron full backups | ||
- by default allow exporting into empty database only (no tables with the same prefix yet) | ||
- all dangerous operation (like deleting of all data) should be confirmed by key found in special file in dataroot | ||
(user would need file access to dataroot which might prevent various "accidents") | ||
- implement "Export/import running" notification in lib/setup.php (similar to new upgrade flag in config table) | ||
- gzip compression when storing xml file - the xml is very verbose and full of repeated tags (zip is not suitable here at all) | ||
this could help us keep the files bellow 2G (expected ratio is > 10:1) | ||
*/ | ||
|
||
require_once($CFG->libdir.'/adminlib.php'); | ||
require_once($CFG->libdir.'/dtllib.php'); | ||
|
||
|
||
function dbtransfer_export_xml_database($description, $mdb) { | ||
@set_time_limit(0); | ||
|
||
session_write_close(); // release session | ||
|
||
header('Content-Type: application/xhtml+xml'); | ||
header('Content-Disposition: attachment; filename=database.xml'); | ||
header('Expires: 0'); | ||
header('Cache-Control: must-revalidate,post-check=0,pre-check=0'); | ||
header('Pragma: public'); | ||
|
||
while(@ob_flush()); | ||
|
||
$var = new file_xml_database_exporter('php://output', $mdb); | ||
$var->export_database($description); | ||
|
||
// no more output | ||
die; | ||
} | ||
|
||
|
||
function dbtransfer_transfer_database($sourcedb, $targetdb) { | ||
@set_time_limit(0); | ||
|
||
session_write_close(); // release session | ||
|
||
$var = new database_mover($sourcedb, $targetdb); | ||
$var->export_database(null); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php // $Id$ | ||
|
||
$string['dbexport'] = 'Database export'; | ||
$string['dbtransfer'] = 'Database transfer'; | ||
$string['differenttableexception'] = 'Table $a structure does not match.'; | ||
$string['exportdata'] = 'Export data'; | ||
$string['exportschemaexception'] = 'Current database structure does not match all install.xml files. <br /> $a'; | ||
$string['importschemaexception'] = 'Current database structure does not match all install.xml files. <br /> $a'; | ||
$string['importversionmismatchexception'] = 'Current version $->currentver does match exported version $a->schemaver.'; | ||
$string['malformedxmlexception'] = 'Malformed XML found, can not continue.'; | ||
$string['notargetconectexception'] = 'Can not connect target database, sorry.'; | ||
$string['transferdata'] = 'Transfer data'; | ||
$string['unknowntableexception'] = 'Unknown table $a found in export file.'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.