### Eclipse Workspace Patch 1.0
#P moodle
Index: lib/xmldb/xmldb_statement.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/xmldb/xmldb_statement.php,v
retrieving revision 1.4
diff -u -r1.4 xmldb_statement.php
--- lib/xmldb/xmldb_statement.php	13 Jul 2008 11:42:37 -0000	1.4
+++ lib/xmldb/xmldb_statement.php	13 Jul 2008 11:43:22 -0000
@@ -1,4 +1,4 @@
-<?php // $Id: xmldb_statement.php,v 1.4 2008/07/13 11:42:37 skodak Exp $
+<?php // $Id: xmldb_statement.php,v 1.3 2008/07/13 10:52:45 skodak Exp $
 
 ///////////////////////////////////////////////////////////////////////////
 //                                                                       //
@@ -153,11 +153,13 @@
                     $fields = $this->getFieldsFromInsertSentence($sentence);
                     $values = $this->getValuesFromInsertSentence($sentence);
                 /// Check that we aren't inserting the id field
+/* //ids should be the same when moving database
                     if (in_array('id', $fields)) {
                         $this->errormsg = 'Cannot insert the "id" field. It is an autonumeric column';
                         $this->debug($this->errormsg);
                         $result = false;
                     }
+*/
                     if ($result && count($fields) == 0) {
                         $this->errormsg = 'Missing fields in sentence "' . $sentence . '"';
                         $this->debug($this->errormsg);
Index: lib/dml/postgres7_adodb_moodle_database.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/dml/postgres7_adodb_moodle_database.php,v
retrieving revision 1.15
diff -u -r1.15 postgres7_adodb_moodle_database.php
--- lib/dml/postgres7_adodb_moodle_database.php	22 Jun 2008 22:53:41 -0000	1.15
+++ lib/dml/postgres7_adodb_moodle_database.php	13 Jul 2008 11:43:22 -0000
@@ -73,8 +73,6 @@
      * @return string
      */
     public function export_dbconfig($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null) {
-        $this->store_settings($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, $dboptions);
-
         $cfg = new stdClass();
         $cfg->dbtype     = $this->get_dbtype();
         $cfg->dblibrary  = $this->get_dblibrary();
Index: lib/dml/moodle_database.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/dml/moodle_database.php,v
retrieving revision 1.37
diff -u -r1.37 moodle_database.php
--- lib/dml/moodle_database.php	4 Jul 2008 12:05:04 -0000	1.37
+++ lib/dml/moodle_database.php	13 Jul 2008 11:43:21 -0000
@@ -122,12 +122,10 @@
 
     /**
      * Returns db related part of config.php
-     * Note: can be used before connect()
+     * Note: can be used before connect() if store_settings is called to cache the connection parameters
      * @return string
      */
-    public function export_dbconfig($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null) {
-        $this->store_settings($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, $dboptions);
-
+    public function export_dbconfig() {
         $cfg = new stdClass();
         $cfg->dbtype    = $this->get_dbtype();
         $cfg->dblibrary = $this->get_dblibrary();
@@ -168,7 +166,7 @@
      * @param array $dboptions driver specific options
      * @return void
      */
-    protected function store_settings($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null) {
+    public function store_settings($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null) {
         $this->dbhost    = $dbhost;
         $this->dbuser    = $dbuser;
         $this->dbpass    = $dbpass;
@@ -1489,4 +1487,48 @@
     public function perf_get_queries() {
         return $this->writes + $this->reads;
     }
+    
+    /*
+     * Return an array with names of supported database drivers.
+     * @return array $arr array with supported driver names
+     */
+    public static function get_supported_drivers() {
+        return array (
+            'mysqli_adodb',
+            'mysql_adodb',
+            'postgres7_adodb',
+            'mssql_n_adodb',
+            'mssql_adodb',
+            'odbc_mssql_adodb',
+            'oci8po_adodb',
+            'sqlite3_pdo',
+        );
+    }
+    
+    /*
+     * Returns a moodle_database object for the specified database driver. 
+     * @param $external true is this is not the moodle default database
+     * @return object $obj moodle_database object
+     */
+    public static function get_driver($driver, $external = false) {
+        global $CFG;
+        $classname = $driver.'_moodle_database';
+        require_once ("$CFG->libdir/dml/$classname.php");
+        return new $classname ($external);
+    }
+    
+    /*
+     * Returns an array with moodle_database objects for all supported database drivers, 
+     * indexed by driver name.
+     * @param $external true is this is not the moodle default database
+     * @return array $arr array with moodle_database objects
+     */
+    public static function get_all_drivers($external = false) {
+        $databases = array ();
+        $supported =& moodle_database::get_supported_drivers();
+        foreach($supported as $driver) {
+            $databases[$driver] =& moodle_database::get_driver($driver, $external);
+        }
+        return $databases;
+    }
 }
Index: lib/dml/database_column_info.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/dml/database_column_info.php,v
retrieving revision 1.3
diff -u -r1.3 database_column_info.php
--- lib/dml/database_column_info.php	12 Jun 2008 15:18:11 -0000	1.3
+++ lib/dml/database_column_info.php	13 Jul 2008 11:43:20 -0000
@@ -119,9 +119,11 @@
                 $this->has_default    = false;
                 $this->default_value  = null;
                 $this->unique         = true;
+                break;
             case 'C':
                 $this->auto_increment = false;
                 $this->binary         = false;
+                break;
         }
     }
 }
Index: lib/dml/pdo_moodle_database.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/dml/pdo_moodle_database.php,v
retrieving revision 1.7
diff -u -r1.7 pdo_moodle_database.php
--- lib/dml/pdo_moodle_database.php	22 Jun 2008 22:53:41 -0000	1.7
+++ lib/dml/pdo_moodle_database.php	13 Jul 2008 11:43:22 -0000
@@ -177,7 +177,18 @@
      * @return bool success
      */
     public function change_database_structure($sql) {
-        return $this->execute($sql);
+        try {
+            $this->lastError = null;
+            if($this->debug) {
+                $this->debug_query($sql);
+            }
+            $this->pdb->exec($sql);
+            return true;
+        } catch (PDOException $ex) {
+            $this->lastError = $ex->getMessage();
+            $this->report_error($sql, null, $ex);
+            return false;
+        }
     }
 
     public function delete_records_select($table, $select, array $params=null) {
Index: install.php
===================================================================
RCS file: /cvsroot/moodle/moodle/install.php,v
retrieving revision 1.105
diff -u -r1.105 install.php
--- install.php	22 Jun 2008 22:53:40 -0000	1.105
+++ install.php	13 Jul 2008 11:43:15 -0000
@@ -154,6 +154,7 @@
 require_once($CFG->libdir.'/environmentlib.php');
 require_once($CFG->libdir.'/xmlize.php');
 require_once($CFG->libdir.'/componentlib.class.php');
+require_once($CFG->libdir.'/dml/moodle_database.php');
 require_once($CFG->dirroot.'/version.php');
 
 /// Set version and release
@@ -161,22 +162,7 @@
 $INSTALL['release'] = $release;
 
 /// list all supported drivers - unsupported must be installed manually ;-)
-$supported = array (
-    'mysqli_adodb',
-    'mysql_adodb',
-    'postgres7_adodb',
-    'mssql_n_adodb',
-    'mssql_adodb',
-    'odbc_mssql_adodb',
-    'oci8po_adodb',
-    'sqlite3_pdo',
-);
-$databases = array ();
-foreach($supported as $driver) {
-    $classname = $driver.'_moodle_database';
-    require_once ("$CFG->libdir/dml/$classname.php");
-    $databases[$driver] = new $classname ();
-}
+$databases =& moodle_database::get_all_drivers();
 
 /// guess the www root
 if ($INSTALL['wwwroot'] == '') {
@@ -461,7 +447,8 @@
     $str .= "\r\n";
 
     $DB = $databases[$INSTALL['dbtype']];
-    $dbconfig = $DB->export_dbconfig($INSTALL['dbhost'], $INSTALL['dbuser'], $INSTALL['dbpass'], $INSTALL['dbname'], false, $INSTALL['prefix']);
+    $DB->store_settings($INSTALL['dbhost'], $INSTALL['dbuser'], $INSTALL['dbpass'], $INSTALL['dbname'], false, $INSTALL['prefix']);
+    $dbconfig = $DB->export_dbconfig();
 
     foreach ($dbconfig as $key=>$value) {
         $key = str_pad($key, 9);
Index: lib/ddl/database_manager.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/ddl/database_manager.php,v
retrieving revision 1.11
diff -u -r1.11 database_manager.php
--- lib/ddl/database_manager.php	22 Jun 2008 16:51:56 -0000	1.11
+++ lib/ddl/database_manager.php	13 Jul 2008 11:43:20 -0000
@@ -386,9 +386,7 @@
     }
 
     /**
-     * This function will load one entire XMLDB file, generating all the needed
-     * SQL statements, specific for each RDBMS ($CFG->dbtype) and, finally, it
-     * will execute all those statements against the DB.
+     * This function will load one entire XMLDB file and call install_from_xmldb_structure.
      *
      * @param $file full path to the XML file to be used
      * @return void
@@ -413,6 +411,18 @@
 
         $xmldb_structure = $xmldb_file->getStructure();
 
+        $this->install_from_xmldb_structure($xmldb_file->getStructure());
+    }
+
+    /**
+     * This function will generate all the needed SQL statements, specific for each 
+     * RDBMS type and, finally, it will execute all those statements against the DB.
+     *
+     * @param object $structure xmldb_structure object
+     * @return void
+     */
+    public function install_from_xmldb_structure($xmldb_structure) {
+
         /// Do this function silenty (to avoid output in install/upgrade process)
         $olddbdebug = $this->mdb->get_debug();
         $this->mdb->set_debug(false);
@@ -896,6 +906,14 @@
 
         $this->execute_sql_arr($sqlarr);
     }
+
+    /* 
+     * Returns the SQL generator managed by this object.
+     * @return object SQL generator instance
+     */
+    public function get_generator() {
+        return $this->generator;
+    }
 }
 
 
Index: lang/en_utf8/moodle.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lang/en_utf8/moodle.php,v
retrieving revision 1.185
diff -u -r1.185 moodle.php
--- lang/en_utf8/moodle.php	28 May 2008 03:13:11 -0000	1.185
+++ lang/en_utf8/moodle.php	13 Jul 2008 11:43:19 -0000
@@ -356,6 +356,7 @@
 $string['datemostrecentlast'] = 'Date - most recent last';
 $string['day'] = 'day';
 $string['days'] = 'days';
+$string['dbmover'] = 'Database mover';
 $string['decodinginternallinks'] = 'Decoding internal links';
 $string['default'] = 'Default';
 $string['defaultcoursefullname'] = 'Course Fullname 101';
@@ -508,7 +509,7 @@
 $a->admin';
 $string['emailpasswordconfirmationsubject'] = '$a: Change password confirmation';
 $string['emailpasswordconfirmmaybesent'] = '<p>If you supplied a correct username or email address then an email should have been sent to you.</p>
-   <p>It contains easy instructions to confirm and complete this password change.
+   <p>It contains easy instructions to confirm and complete this password change.
 If you continue to have difficulty, please contact the site administrator.</p>';
 $string['emailpasswordconfirmsent'] = 'An email should have been sent to your address at <b>$a</b>.
 <br />It contains easy instructions to confirm and complete this password change.
Index: admin/cliupgrade.php
===================================================================
RCS file: /cvsroot/moodle/moodle/admin/cliupgrade.php,v
retrieving revision 1.23
diff -u -r1.23 cliupgrade.php
--- admin/cliupgrade.php	22 Jun 2008 22:53:41 -0000	1.23
+++ admin/cliupgrade.php	13 Jul 2008 11:43:16 -0000
@@ -599,7 +599,8 @@
     $str .= "\r\n";
 
     $database = $databases[$CONFFILE['dbtype']];
-    $dbconfig = $database->export_dbconfig($CONFFILE['dbhost'], $CONFFILE['dbuser'], $CONFFILE['dbpass'], $CONFFILE['dbname'], false, $CONFFILE['prefix']);
+    $database->store_settings($CONFFILE['dbhost'], $CONFFILE['dbuser'], $CONFFILE['dbpass'], $CONFFILE['dbname'], false, $CONFFILE['prefix']);
+    $dbconfig = $database->export_dbconfig();
 
     foreach ($dbconfig as $key=>$value) {
         $key = str_pad($key, 9);
Index: admin/settings/misc.php
===================================================================
RCS file: /cvsroot/moodle/moodle/admin/settings/misc.php,v
retrieving revision 1.19
diff -u -r1.19 misc.php
--- admin/settings/misc.php	25 May 2008 11:37:45 -0000	1.19
+++ admin/settings/misc.php	13 Jul 2008 11:43:16 -0000
@@ -17,6 +17,8 @@
 
     // XMLDB editor
     $ADMIN->add('misc', new admin_externalpage('xmldbeditor', get_string('xmldbeditor'), "$CFG->wwwroot/$CFG->admin/xmldb/"));
+    // Database mover tool
+    $ADMIN->add('misc', new admin_externalpage('dbmover', get_string('dbmover'), "$CFG->wwwroot/$CFG->admin/dbmover/"));
 
 
     // hidden scripts linked from elsewhere
Index: admin/dbmover/dbmoverlib.php
===================================================================
RCS file: admin/dbmover/dbmoverlib.php
diff -N admin/dbmover/dbmoverlib.php
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ admin/dbmover/dbmoverlib.php	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,134 @@
+<?php //  $Id:$
+
+function dbmover_rewrite_config($database, $backup = true) {
+    global $CFG, $DB;
+    if ($database != $DB) {
+        $config = file_get_contents($CFG->dirroot . '/config.php');
+        $file_put_contents($CFG->dirroot . '/config-old.php', $config);
+        $dbconfig = $database->export_dbconfig();
+
+        foreach ($dbconfig as $key=>$value) {
+            $newvalue = '$CFG->'. str_pad($key, 9) . ' = ' . var_export($value, true) . ';';
+            $config = preg_replace('/\\$CFG->'. $key . '\s*=.*;/x', $newvalue, $config, -1, $count);
+            if(!$count) {
+                $config = str_replace('$CFG->prefix', $newvalue . "\r\n" . '$CFG->prefix', $config);
+            }
+        }
+        file_put_contents($CFG->dirroot . '/config.php', $config);
+    }
+    return true;
+}
+
+function dbmover_get_data($tablename) {
+    global $DB;
+    $statement =& new xmldb_statement($tablename . '_data');
+    $statement->setType(XMLDB_STATEMENT_INSERT);
+    $statement->setTable($tablename);
+    $rs = $DB->get_recordset_sql('SELECT * FROM {' . $tablename . '}');
+    foreach($rs as $row) {
+        $row = (array)$row;
+        foreach($row as $k => $v) {
+            $row[$k] = "'" . htmlspecialchars($v, ENT_QUOTES) . "'";
+        }
+        $text = '(' . implode(',', array_keys($row)) . ') VALUES (' . implode(',', $row) . ')';
+        $statement->addSentence($text);
+    }
+    return $statement;
+}
+
+
+function dbmover_transfer_onthefly($database) {
+    $generator = $database->get_manager()->get_generator();
+    
+    $dbdirs = get_db_directories();
+    foreach ($dbdirs as $dbdir) {
+        $xmldb_file = new xmldb_file($dbdir.'/install.xml');
+        if (!($xmldb_file->fileExists() && $xmldb_file->loadXMLStructure())) {
+            continue;
+        }
+        $structure =& $xmldb_file->getStructure();
+        $tables =& $structure->getTables();
+
+        foreach($tables as $table) {
+            $sqls = $generator->getCreateTableSQL($table);
+            foreach($sqls as $sql) {
+                $database->change_database_structure($sql);
+            }
+            
+            $sqls = $generator->getExecuteStatementSQL(dbmover_get_data($table->getName()));
+            foreach($sqls as $sql) {
+                $database->change_database_structure($sql);
+            }
+        }
+    }
+}
+
+function dbmover_get_structure() {
+    $export =& new xmldb_structure('export');
+    $dbdirs = get_db_directories();
+    foreach ($dbdirs as $dbdir) {
+        $xmldb_file = new xmldb_file($dbdir.'/install.xml');
+        if (!($xmldb_file->fileExists() && $xmldb_file->loadXMLStructure())) {
+            continue;
+        }
+        $structure =& $xmldb_file->getStructure();
+        $tables =& $structure->getTables();
+
+        foreach($tables as $table) {
+            $table->setPrevious(null);
+            $table->setNext(null);
+            $export->addTable($table);
+            $export->addStatement(dbmover_get_data($table->getName()));
+        }
+    }
+    return $export;
+}
+
+function dbmover_read_structure($file) {
+    $xmldb_file = new xmldb_file();
+    if (!($xmldb_file->fileExists() && $xmldb_file->loadXMLStructure())) {
+        return null;
+    }
+    return $xmldb_file->getStructure();
+}
+
+function dbmover_database_connect($data, $createonfail = true) {
+    global $CFG, $DB;
+    if($CFG->dbtype . '_' .$CFG->dblibrary == $data->dbtype && $CFG->dbhost == $data->dbhost && 
+        $CFG->dbname == $data->dbname && $CFG->dbuser == $data->dbuser && $CFG->dbpass == $data->dbpass) {
+        return $DB;
+    }
+    $database = moodle_database::get_all_drivers();
+    $database = $database[$data->dbtype];
+    
+    if(!$database->connect($data->dbhost, $data->dbuser, $data->dbpass, $data->dbname, false, $data->prefix) &&
+       (!$createonfail || 
+           !($database->create_database($data->dbhost, $data->dbuser, $data->dbpass, $data->dbname) && 
+            $database->connect($data->dbhost, $data->dbuser, $data->dbpass, $data->dbname, false, $data->prefix)))) {
+        return null;
+    }
+    return $database;
+}
+
+function dbmover_save_structure($structure, $path = null) {
+    global $CFG;
+    if(is_null($path)) {
+        $path = $CFG->dataroot . '/database-' . $structure->getVersion() . '.xml';
+    }
+    $structure->path = $path;
+    $structure->calculateHash(true);
+    return file_put_contents($path, $structure->xmlOutput());
+}
+
+function dbmover_print_structure($structure, $httpheader = 'Content-Type: application/xhtml+xml') {
+    if($httpheader) {
+        header($httpheader);
+    }
+    echo $structure->xmlOutput();
+}
+
+function dbmover_transfer_structure($structure, $database) {
+    // TODO: clean destination database before sending new data
+    $manager =& $database->get_manager();
+    $manager->install_from_xmldb_structure($structure);
+}
Index: admin/dbmover/dbmover_form.php
===================================================================
RCS file: admin/dbmover/dbmover_form.php
diff -N admin/dbmover/dbmover_form.php
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ admin/dbmover/dbmover_form.php	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,67 @@
+<?php  //$Id:$
+
+require_once($CFG->libdir.'/formslib.php');
+require_once($CFG->libdir.'/dml/moodle_database.php');
+
+class dbmover_form extends moodleform {
+
+    function definition() {
+        global $CFG;
+        $mform = $this->_form;
+
+        $drivers =& moodle_database::get_all_drivers();
+        foreach($drivers as $key => $driver) {
+            $drivers[$key] = $driver->get_name();
+        }
+
+        $mform->addElement('header', 'database', get_string('destinationdb', 'dbmover'));
+
+        $obj = $mform->addElement('select', 'dbtype', get_string('dbtype', 'install'), $drivers);
+        $obj->setValue($CFG->dbtype . '_' .$CFG->dblibrary);
+
+        $obj = $mform->addElement('text', 'dbhost', get_string('dbhost', 'install'));
+        $obj->setValue($CFG->dbhost);
+        $mform->setType('dbhost', PARAM_RAW);
+        
+        $obj = $mform->addElement('text', 'dbname', get_string('database','install'));
+        $obj->setValue($CFG->dbname);
+        $mform->setType('dbname', PARAM_RAW);
+        
+        $obj = $mform->addElement('text', 'dbuser', get_string('user'));
+        $obj->setValue($CFG->dbuser);
+        $mform->setType('dbuser', PARAM_RAW);
+        
+        $obj = $mform->addElement('passwordunmask', 'dbpass', get_string('password'));
+        $obj->setValue($CFG->dbpass);
+        $mform->setType('dbpass', PARAM_RAW);
+        
+        $obj = $mform->addElement('text', 'prefix', get_string('dbprefix', 'install'));
+        $obj->setValue($CFG->prefix);
+        $mform->setType('prefix', PARAM_RAW);
+        
+        $mform->addElement('header', 'data', get_string('datatransfer', 'dbmover'));
+        // visible elements
+        $obj = $mform->addElement('radio', 'datasource', get_string('datasource', 'dbmover'), get_string('dbdataexists', 'dbmover'), 0);
+
+        $obj = array();
+        $obj[] = $mform->createElement('radio', 'datasource', '', get_string('dbcopywithfile', 'dbmover'), 1);
+        $obj[] = $mform->createElement('checkbox', 'savefile', '', get_string('savefile', 'dbmover'));
+        $obj[] = $mform->createElement('checkbox', 'downloadfile', '', get_string('downloadfile', 'dbmover'));
+        $obj = $mform->addElement('group', 'grp1', '', $obj, null, false);
+        
+        $obj = $mform->addElement('radio', 'datasource', '', get_string('dbcopyonthefly', 'dbmover'), 2);
+        $obj = $mform->addElement('radio', 'datasource', '', get_string('dbdatafile', 'dbmover'), 3);
+        $mform->setType('datasource', PARAM_INT);
+
+        $obj = $mform->addElement('file', 'datafile', get_string('file'), 'size="40"');
+//        $mform->addRule('datafile', get_string('required'), 'required', null, 'client');
+        
+        $this->add_action_buttons(false, get_string('dbmove', 'dbmover'));
+    }
+
+    public function get_upload_filepath($elname) {
+        return @$this->_upload_manager->files[$elname]['tmp_name'];
+    }
+    
+}
+?>
Index: admin/dbmover/index.php
===================================================================
RCS file: admin/dbmover/index.php
diff -N admin/dbmover/index.php
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ admin/dbmover/index.php	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,97 @@
+<?php // $Id$
+
+///////////////////////////////////////////////////////////////////////////
+//                                                                       //
+// NOTICE OF COPYRIGHT                                                   //
+//                                                                       //
+// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
+//          http://moodle.com                                            //
+//                                                                       //
+// Copyright (C) 1999 onwards Martin Dougiamas     http://dougiamas.com  //
+//           (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com  //
+//                                                                       //
+// This program is free software; you can redistribute it and/or modify  //
+// it under the terms of the GNU General Public License as published by  //
+// the Free Software Foundation; either version 2 of the License, or     //
+// (at your option) any later version.                                   //
+//                                                                       //
+// This program is distributed in the hope that it will be useful,       //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of        //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
+// GNU General Public License for more details:                          //
+//                                                                       //
+//          http://www.gnu.org/copyleft/gpl.html                         //
+//                                                                       //
+///////////////////////////////////////////////////////////////////////////
+
+/// This is the main script for the database moving tool (DB Mover)..
+
+    require_once('../../config.php');
+    require_once($CFG->libdir.'/adminlib.php');
+
+    require_login();
+    require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));
+
+    require_once($CFG->dirroot.'/admin/dbmover/dbmoverlib.php');
+    require_once($CFG->dirroot.'/admin/dbmover/dbmover_form.php');
+
+    admin_externalpage_setup('dbmover');
+    //create form
+    $form = new dbmover_form();
+    global $DB;
+    if ($data = $form->get_data()) {
+        @set_time_limit(0);
+        if(!$database = dbmover_database_connect($data, $data->datasource != 0)) {
+            $error = get_string('dbnotsetup', 'debug');
+        } else {
+            switch($data->datasource) {
+            default:
+            case 0:
+                if($DB != $database) {
+                    dbmover_rewrite_config($database);
+                }
+                break;
+            case 1:
+                @raise_memory_limit('128M');
+                $structure = dbmover_get_structure();
+                if(@$data->savefile) {
+                    dbmover_save_structure($structure);
+                }
+                if($DB != $database) {
+                    dbmover_transfer_structure($structure, $database);
+                    dbmover_rewrite_config($database);
+                }
+                if(@$data->downloadfile) {
+                    dbmover_print_structure($structure);
+                    exit();
+                }
+                break;
+            case 2:
+                if($DB != $database) {
+                    dbmover_transfer_onthefly($database);
+                    dbmover_rewrite_config($database);
+                }
+                break;
+            case 3:
+                @raise_memory_limit('128M');
+                $structure = dbmover_read_structure($form->get_upload_filepath('file'));
+                if($structure) {
+                    dbmover_transfer_structure($structure, $database);
+                    dbmover_rewrite_config($database);
+                } else {
+                    $error = get_string('invalidxmldbstructure', 'dbmover');
+                }
+                break;
+            }
+        }
+        if(!@$error) {
+            redirect($CFG->wwwroot.'/admin/dbmover', get_string('transfersuccess', 'dbmover'));
+        }
+    }
+
+    admin_externalpage_print_header();
+    if(@$error) {
+        print_error($error);
+    }
+    $form->display();
+    admin_externalpage_print_footer();
