Skip to content

Commit

Permalink
Merge remote-tracking branch 'moodlehq/MOODLE_23_STABLE' into gu23
Browse files Browse the repository at this point in the history
  • Loading branch information
thepurpleblob committed Jul 27, 2012
2 parents 5c44013 + 6975216 commit e6c25f9
Show file tree
Hide file tree
Showing 79 changed files with 1,281 additions and 331 deletions.
191 changes: 191 additions & 0 deletions admin/tool/dbtransfer/cli/migrate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* This script migrates data from current database to another
*
* This script is not intended for beginners!
* Potential problems:
* - su to apache account or sudo before execution
* - already broken DB scheme or invalid data
*
* @package tool_dbtransfer
* @copyright 2012 Petr Skoda {@link http://skodak.org/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

define('CLI_SCRIPT', true);

require(__DIR__.'/../../../../config.php');
require_once($CFG->libdir.'/clilib.php');
require_once(__DIR__.'/../locallib.php');

$help =
"Database migration script.
It is strongly recommended to turn off the web server
or enable CLI maintenance mode before starting the migration.
Options:
--dbtype=TYPE Database type.
--dblibrary=TYPE Database library. Defaults to 'native'.
--dbhost=HOST Database host.
--dbname=NAME Database name.
--dbuser=USERNAME Database user.
--dbpass=PASSWORD Database password.
--dbport=NUMBER Database port.
--prefix=STRING Table prefix for above database tables.
--dbsocket=PATH Use database sockets. Available for some databases only.
-h, --help Print out this help.
Example:
\$ sudo -u www-data /usr/bin/php admin/tool/dbtransfer/cli/migrate.php
";

// Now get cli options.
list($options, $unrecognized) = cli_get_params(
array(
'dbtype' => null,
'dblibrary' => 'native',
'dbhost' => null,
'dbname' => null,
'dbuser' => null,
'dbpass' => null,
'dbport' => null,
'prefix' => null,
'dbsocket' => null,
'maintenance' => null,
'list' => false,
'help' => false,
),
array(
'm' => 'maintenance',
'l' => 'list',
'h' => 'help',
)
);

if ($options['help']) {
echo $help;
exit(0);
}

if (empty($CFG->version)) {
cli_error(get_string('missingconfigversion', 'debug'));
}

echo "\n".get_string('cliheading', 'tool_dbtransfer')."\n\n";

$drivers = tool_dbtransfer_get_drivers();

if (!isset($options['dbtype'])) {
$choose = array();
foreach ($drivers as $driver => $name) {
list($dbtype, $dblibrary) = explode('/', $driver);
$choose[$dbtype] = $dbtype;
}
$optionsstr = implode(', ', $choose);
cli_heading(get_string('databasetypehead', 'install')." ($optionsstr)");
$options['dbtype'] = cli_input(get_string('clitypevalue', 'admin'), '', $choose, true);
}

$choose = array();
foreach ($drivers as $driver => $name) {
list($dbtype, $dblibrary) = explode('/', $driver);
if ($dbtype === $options['dbtype']) {
$choose[$dblibrary] = $dblibrary;
}
}
if (!isset($options['dblibrary']) or !isset($choose[$options['dblibrary']])) {
$optionsstr = implode(', ', $choose);
cli_heading('Database library'." ($optionsstr)"); // Note: no need to localise unless we add real PDO drivers.
$options['dblibrary'] = cli_input(get_string('clitypevalue', 'admin'), '', $choose, true);
}

if (!isset($options['dbhost'])) {
cli_heading(get_string('databasehost', 'install'));
$options['dbhost'] = cli_input(get_string('clitypevalue', 'admin'));
}

if (!isset($options['dbname'])) {
cli_heading(get_string('databasename', 'install'));
$options['dbname'] = cli_input(get_string('clitypevalue', 'admin'));
}

if (!isset($options['dbuser'])) {
cli_heading(get_string('databaseuser', 'install'));
$options['dbuser'] = cli_input(get_string('clitypevalue', 'admin'));
}

if (!isset($options['dbpass'])) {
cli_heading(get_string('databasepass', 'install'));
$options['dbpass'] = cli_input(get_string('clitypevalue', 'admin'));
}

if (!isset($options['prefix'])) {
cli_heading(get_string('dbprefix', 'install'));
$options['prefix'] = cli_input(get_string('clitypevalue', 'admin'));
}

if (!isset($options['dbport'])) {
cli_heading(get_string('dbport', 'install'));
$options['dbport'] = cli_input(get_string('clitypevalue', 'admin'));
}

if ($CFG->ostype !== 'WINDOWS') {
if (!isset($options['dbsocket'])) {
cli_heading(get_string('databasesocket', 'install'));
$options['dbsocket'] = cli_input(get_string('clitypevalue', 'admin'));
}
}

$a = (object)array('dbtypefrom' => $CFG->dbtype, 'dbtype' => $options['dbtype'],
'dbname' => $options['dbname'], 'dbhost' => $options['dbhost']);
cli_heading(get_string('transferringdbto', 'tool_dbtransfer', $a));

// Try target DB connection.
$problem = '';

$targetdb = moodle_database::get_driver_instance($options['dbtype'], $options['dblibrary']);
$dboptions = array();
if ($options['dbport']) {
$dboptions['dbport'] = $options['dbport'];
}
if ($options['dbsocket']) {
$dboptions['dbsocket'] = $options['dbsocket'];
}
try {
$targetdb->connect($options['dbhost'], $options['dbuser'], $options['dbpass'], $options['dbname'],
$options['prefix'], $dboptions);
if ($targetdb->get_tables()) {
$problem .= get_string('targetdatabasenotempty', 'tool_dbtransfer');
}
} catch (moodle_exception $e) {
$problem .= $e->debuginfo."\n\n";
$problem .= get_string('notargetconectexception', 'tool_dbtransfer');
}

if ($problem !== '') {
echo $problem."\n\n";
exit(1);
}

$feedback = new text_progress_trace();
tool_dbtransfer_transfer_database($DB, $targetdb, $feedback);
$feedback->finished();

cli_heading(get_string('success'));
exit(0);
21 changes: 15 additions & 6 deletions admin/tool/dbtransfer/database_export_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,28 @@
/**
* Transfer form
*
* @package tool
* @subpackage dbtransfer
* @copyright 2008 Petr Skoda
* @package tool_dbtransfer
* @copyright 2008 Petr Skoda {@link http://skodak.org/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die;

require_once $CFG->libdir.'/formslib.php';
require_once($CFG->libdir.'/formslib.php');

class database_export_form extends moodleform {

function definition() {
/**
* Definition of db export settings form.
*
* @package tool_dbtransfer
* @copyright 2008 Petr Skoda {@link http://skodak.org/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class database_export_form extends moodleform {
/**
* Define the export form.
*/
public function definition() {
$mform = $this->_form;

$mform->addElement('header', 'database', get_string('dbexport', 'tool_dbtransfer'));
Expand Down
85 changes: 57 additions & 28 deletions admin/tool/dbtransfer/database_transfer_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,84 @@
/**
* Transfer form
*
* @package tool
* @subpackage dbtransfer
* @copyright 2008 Petr Skoda
* @package tool_dbtransfer
* @copyright 2008 Petr Skoda {@link http://skodak.org/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die;

require_once $CFG->libdir.'/formslib.php';
require_once($CFG->libdir.'/formslib.php');
require_once(__DIR__.'/locallib.php');


/**
* Definition of db transfer settings form.
*
* @copyright 2008 Petr Skoda {@link http://skodak.org/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class database_transfer_form extends moodleform {

function definition() {
/**
* Define transfer form.
*/
protected function definition() {
global $CFG;

$mform = $this->_form;

$mform->addElement('header', 'database', get_string('dbtransfer', 'tool_dbtransfer'));
$mform->addElement('header', 'database', get_string('targetdatabase', 'tool_dbtransfer'));

$supported = array (
'mysqli/native',
'pgsql/native',
'mssql/native',
'oci/native',
'sqlsrv/native',
);
$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;
}
$drivers = tool_dbtransfer_get_drivers();
$drivers = array_reverse($drivers, true);
$drivers[''] = get_string('choosedots');
$drivers = array_reverse($drivers, true);

$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', 'dbhost', get_string('databasehost', 'install'));
$mform->addElement('text', 'dbname', get_string('databasename', 'install'));
$mform->addElement('text', 'dbuser', get_string('databaseuser', 'install'));
$mform->addElement('passwordunmask', 'dbpass', get_string('databasepass', 'install'));
$mform->addElement('text', 'prefix', get_string('dbprefix', 'install'));
$mform->addElement('text', 'dbport', get_string('dbport', 'install'));
$mform->addElement('text', 'dbsocket', get_string('databasesocket', 'install'));
if ($CFG->ostype !== 'WINDOWS') {
$mform->addElement('text', 'dbsocket', get_string('databasesocket', 'install'));
} else {
$mform->addElement('hidden', 'dbsocket');
}

$mform->addRule('driver', get_string('required'), 'required', null);
$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);
if (!isset($drivers['mysqli/native'])) {
$mform->addRule('prefix', get_string('required'), 'required', null);
}

$mform->addElement('header', 'database', get_string('options', 'tool_dbtransfer'));

$mform->addElement('advcheckbox', 'enablemaintenance', get_string('enablemaintenance', 'tool_dbtransfer'));
$mform->addHelpButton('enablemaintenance', 'enablemaintenance', 'tool_dbtransfer');

$this->add_action_buttons(false, get_string('transferdata', 'tool_dbtransfer'));
}

/**
* Validate prefix is present for non-mysql drivers.
* @param array $data
* @param array $files
* @return array
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if ($data['driver'] !== 'mysqli/native') {
// This is a bloody hack, let's pretend we do not need to look at db family...
if ($data['prefix'] === '') {
$errors['prefix'] = get_string('required');
}
}
return $errors;
}
}
11 changes: 5 additions & 6 deletions admin/tool/dbtransfer/dbexport.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
/**
* Export
*
* @package tool
* @subpackage dbtransfer
* @copyright 2008 Petr Skoda
* @package tool_dbtransfer
* @copyright 2008 Petr Skoda {@link http://skodak.org/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

Expand All @@ -32,15 +31,15 @@
require_login();
admin_externalpage_setup('tooldbexport');

//create form
// Create form.
$form = new database_export_form();

if ($data = $form->get_data()) {
dbtransfer_export_xml_database($data->description, $DB);
tool_dbtransfer_export_xml_database($data->description, $DB);
die;
}

echo $OUTPUT->header();
// TODO: add some more info here
// TODO: add some more info here.
$form->display();
echo $OUTPUT->footer();
Loading

0 comments on commit e6c25f9

Please sign in to comment.