ENHANCEMENT #3903 Initial changes to installer to support selection of different database drivers

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/phpinstaller/branches/2.4@98659 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
sharvey 2010-02-10 03:22:41 +00:00 committed by Sam Minnee
parent bf784c989c
commit 260239fc5a

View File

@ -40,13 +40,12 @@ foreach($envFiles as $envFile) {
} }
} }
// Load database config // Load database config
if(isset($_REQUEST['mysql'])) { if(isset($_REQUEST['db'])) {
$databaseConfig = $_REQUEST['mysql']; $databaseConfig = $_REQUEST['db'];
} else { } else {
$_REQUEST['mysql'] = $databaseConfig = array( $_REQUEST['db'] = $databaseConfig = array(
"type" => "MySQLDatabase", "type" => defined('SS_DATABASE_CLASS') ? SS_DATABASE_CLASS : "MySQLDatabase",
"server" => defined('SS_DATABASE_SERVER') ? SS_DATABASE_SERVER : "localhost", "server" => defined('SS_DATABASE_SERVER') ? SS_DATABASE_SERVER : "localhost",
"username" => defined('SS_DATABASE_USERNAME') ? SS_DATABASE_USERNAME : "root", "username" => defined('SS_DATABASE_USERNAME') ? SS_DATABASE_USERNAME : "root",
"password" => defined('SS_DATABASE_PASSWORD') ? SS_DATABASE_PASSWORD : "", "password" => defined('SS_DATABASE_PASSWORD') ? SS_DATABASE_PASSWORD : "",
@ -58,8 +57,8 @@ if(isset($_REQUEST['admin'])) {
$adminConfig = $_REQUEST['admin']; $adminConfig = $_REQUEST['admin'];
} else { } else {
$_REQUEST['admin'] = $adminConfig = array( $_REQUEST['admin'] = $adminConfig = array(
'username' => 'admin', 'username' => defined('SS_DEFAULT_ADMIN_USERNAME') ? SS_DEFAULT_ADMIN_USERNAME : 'admin',
'password' => '', 'password' => defined('SS_DEFAULT_ADMIN_PASSWORD') ? SS_DEFAULT_ADMIN_PASSWORD : 'password',
'firstname' => '', 'firstname' => '',
'surname' => '' 'surname' => ''
); );
@ -115,6 +114,25 @@ if($installFromCli && ($req->hasErrors() || $dbReq->hasErrors())) {
exit(1); exit(1);
} }
// This is a mapping of module directory name to database class
$otherDatabaseLocations = array(
'mssql' => array(
'class' => 'MSSQLDatabase',
'title' => 'SQL Server 2008'
),
'postgresql' => array(
'class' => 'PostgreSQLDatabase',
'title' => 'PostgreSQL'
)
);
// MySQL support comes out of the box with sapphire - others databases
// live in their own module directories. We need to check the existance of them!
$foundDatabaseClasses = array('MySQLDatabase' => 'MySQL');
foreach($otherDatabaseLocations as $dir => $details) {
if(file_exists($dir)) $foundDatabaseClasses[$details['class']] = $details['title'];
}
if((isset($_REQUEST['go']) || $installFromCli) && !$req->hasErrors() && !$dbReq->hasErrors() && $adminConfig['username'] && $adminConfig['password']) { if((isset($_REQUEST['go']) || $installFromCli) && !$req->hasErrors() && !$dbReq->hasErrors() && $adminConfig['username'] && $adminConfig['password']) {
// Confirm before reinstalling // Confirm before reinstalling
if(!isset($_REQUEST['force_reinstall']) && !$installFromCli && $alreadyInstalled) { if(!isset($_REQUEST['force_reinstall']) && !$installFromCli && $alreadyInstalled) {
@ -124,8 +142,7 @@ if((isset($_REQUEST['go']) || $installFromCli) && !$req->hasErrors() && !$dbReq-
$inst = new Installer(); $inst = new Installer();
if($_REQUEST) $inst->install($_REQUEST); if($_REQUEST) $inst->install($_REQUEST);
else $inst->install(array( else $inst->install(array(
'database' => $databaseConfig['type'], 'db' => $databaseConfig,
'mysql' => $databaseConfig,
'admin' => $adminConfig, 'admin' => $adminConfig,
)); ));
} }
@ -151,29 +168,27 @@ class InstallRequirements {
* Just check that the database configuration is okay * Just check that the database configuration is okay
*/ */
function checkdatabase($databaseConfig) { function checkdatabase($databaseConfig) {
if($this->requireFunction( if($this->requireDatabaseFunction(
'mysql_connect', $databaseConfig,
array( array(
"PHP Configuration", "Database Configuration",
"MySQL support", "Database support",
"MySQL support not included in PHP.") "Database support not included in PHP.")
) )
) { ) {
if($this->requireMySQLServer( if($this->requireDatabaseServer(
$databaseConfig['server'], $databaseConfig,
array( array(
"MySQL Configuration", "Database Configuration",
"MySQL server exists", "Database server exists",
"I couldn't find a MySQL server on '$databaseConfig[server]'", $databaseConfig['server'] "I couldn't find a database server on '$databaseConfig[server]'", $databaseConfig['server']
) )
)) { )) {
if($this->requireMysqlConnection( if($this->requireDatabaseConnection(
$databaseConfig['server'], $databaseConfig,
$databaseConfig['username'],
$databaseConfig['password'],
array( array(
"MySQL Configuration", "Database Configuration",
"MySQL access credentials correct", "Database access credentials correct",
"That username/password doesn't work" "That username/password doesn't work"
) )
)) { )) {
@ -188,12 +203,9 @@ class InstallRequirements {
); );
} }
$this->requireDatabaseOrCreatePermissions( $this->requireDatabaseOrCreatePermissions(
$databaseConfig['server'], $databaseConfig,
$databaseConfig['username'],
$databaseConfig['password'],
$databaseConfig['database'],
array( array(
"MySQL Configuration", "Database Configuration",
"Can I access/create the database", "Can I access/create the database",
"I can't create new databases and the database '$databaseConfig[database]' doesn't exist" "I can't create new databases and the database '$databaseConfig[database]' doesn't exist"
) )
@ -341,7 +353,8 @@ class InstallRequirements {
function showTable($section = null) { function showTable($section = null) {
if($section) { if($section) {
$tests = $this->tests[$section]; $tests = $this->tests[$section];
echo "<table class=\"testResults\" width=\"100%\">"; $id = strtolower(str_replace(' ', '_', $section));
echo "<table id=\"{$id}_results\" class=\"testResults\" width=\"100%\">";
foreach($tests as $test => $result) { foreach($tests as $test => $result) {
echo "<tr class=\"$result[0]\"><td>$test</td><td>" . nl2br(htmlentities($result[1])) . "</td></tr>"; echo "<tr class=\"$result[0]\"><td>$test</td><td>" . nl2br(htmlentities($result[1])) . "</td></tr>";
} }
@ -540,37 +553,119 @@ class InstallRequirements {
} }
} }
function requireMysqlConnection($server, $username, $password, $testDetails) { function requireDatabaseFunction($databaseConfig, $testDetails) {
$this->testing($testDetails); $this->testing($testDetails);
$conn = @mysql_connect($server, $username, $password); $okay = false;
if($conn) { if($databaseConfig['type'] == 'MySQLDatabase') {
return true; if(function_exists('mysql_connect')) {
/* $okay = true;
if(mysql_query("CREATE DATABASE testing123")) {
mysql_query("DROP DATABASE testing123");
return true;
} else {
$testDetails[2] .= " (user '$username' doesn't have CREATE DATABASE permissions.)";
$this->error($testDetails);
} }
*/ } elseif($databaseConfig['type'] == 'MSSQLDatabase') {
} else { if(function_exists('mssql_connect') || function_exists('sqlsrv_connect')) {
$testDetails[2] .= ": " . mysql_error(); $okay = true;
$this->error($testDetails); }
} elseif($databaseConfig['type'] = 'PostgreSQLDatabase') {
if(function_exists('pg_connect')) {
$okay = true;
} }
} }
function requireMySQLServer($server, $testDetails) { if(!$okay) {
$this->error($testDetails);
return false;
} else {
return true;
}
}
function requireDatabaseConnection($databaseConfig, $testDetails) {
$this->testing($testDetails); $this->testing($testDetails);
$conn = @mysql_connect($server, null, null);
if($databaseConfig['type'] == 'MySQLDatabase') {
$conn = @mysql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']);
if($conn || mysql_errno() < 2000) { if($conn || mysql_errno() < 2000) {
return true; return true;
} else { } else {
$testDetails[2] .= ": " . mysql_error(); $testDetails[2] .= ": " . mysql_error();
$this->error($testDetails); $this->error($testDetails);
} }
} elseif($databaseConfig['type'] == 'MSSQLDatabase') {
if(function_exists('mssql_connect')) {
$conn = @mssql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password'], true);
} else {
$connectionInfo = array(
'UID' => $databaseConfig['username'],
'PWD' => $databaseConfig['password'],
);
$conn = @sqlsrv_connect($databaseConfig['server'], $connectionInfo);
}
if($conn) {
return true;
} else {
$this->error($testDetails);
}
} elseif($databaseConfig['type'] == 'PostgreSQLDatabase') {
$username = $databaseConfig['username'] ? $databaseConfig['username'] : '';
$password = $databaseConfig['password'] ? $databaseConfig['password'] : '';
$server = $databaseConfig['server'];
$userPart = $username ? " user=$username" : '';
$passwordPart = $password ? " password=$password" : '';
$connstring = "host=$server port=5432 {$userPart}{$passwordPart}";
$conn = @pg_connect($connstring);
if($conn) {
return true;
} else {
$this->error($testDetails);
}
}
}
function requireDatabaseServer($databaseConfig, $testDetails) {
$this->testing($testDetails);
if($databaseConfig['type'] == 'MySQLDatabase') {
$conn = @mysql_connect($databaseConfig['server'], null, null);
if($conn || mysql_errno() < 2000) {
return true;
} else {
$testDetails[2] .= ": " . mysql_error();
$this->error($testDetails);
}
} elseif($databaseConfig['type'] == 'MSSQLDatabase') {
if(function_exists('mssql_connect')) {
$conn = @mssql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password'], true);
} else {
$connectionInfo = array(
'UID' => $databaseConfig['username'],
'PWD' => $databaseConfig['password'],
);
$conn = @sqlsrv_connect($databaseConfig['server'], $connectionInfo);
}
if($conn) {
return true;
} else {
$testDetails[2] .= ": SQL Server requires a valid username and password to determine if the server exists.";
$this->error($testDetails);
}
} elseif($databaseConfig['type'] == 'PostgreSQLDatabase') {
$username = $databaseConfig['username'] ? $databaseConfig['username'] : '';
$password = $databaseConfig['password'] ? $databaseConfig['password'] : '';
$server = $databaseConfig['server'];
$userPart = $username ? " user=$username" : '';
$passwordPart = $password ? " password=$password" : '';
$connstring = "host=$server port=5432 {$userPart}{$passwordPart}";
$conn = @pg_connect($connstring);
if($conn) {
return true;
} else {
$testDetails[2] .= ": PostgreSQL requires a valid username and password to determine if the server exists.";
$this->error($testDetails);
}
}
} }
function requireMySQLVersion($version, $testDetails) { function requireMySQLVersion($version, $testDetails) {
@ -595,31 +690,58 @@ class InstallRequirements {
} }
} }
function requireDatabaseOrCreatePermissions($databaseConfig, $testDetails) {
function requireDatabaseOrCreatePermissions($server, $username, $password, $database, $testDetails) {
$this->testing($testDetails); $this->testing($testDetails);
$conn = @mysql_connect($server, $username, $password); $okay = '';
if(@mysql_select_db($database)) { if($databaseConfig['type'] == 'MySQLDatabase') {
$conn = @mysql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']);
if(@mysql_select_db($databaseConfig['database'])) {
$okay = "Database '$database' exists"; $okay = "Database '$database' exists";
} else { } else {
if(@mysql_query("CREATE DATABASE testing123")) { if(@mysql_query("CREATE DATABASE testing123")) {
mysql_query("DROP DATABASE testing123"); mysql_query("DROP DATABASE testing123");
$okay = "Able to create a new database"; $okay = "Able to create a new database";
}
}
} elseif($databaseConfig['type'] == 'MSSQLDatabase') {
if(@mysql_select_db($databaseConfig['database'])) {
$okay = "Database '$database' exists";
} else { } else {
$testDetails[2] .= " (user '$username' doesn't have CREATE DATABASE permissions.)"; if(function_exists('mssql_connect') && @mssql_query("CREATE DATABASE testing123")) {
$this->error($testDetails); mssql_query("DROP DATABASE testing123");
return; $okay = "Able to create a new database";
} elseif(function_exists('sqlsrv_connect') && @sqlsrv_query("CREATE DATABASE testing123")) {
sqlsrv_query("DROP DATABASE testing123");
$okay = "Able to create a new database";
}
}
} elseif($databaseConfig['type'] == 'PostgreSQLDatabase') {
$database = $databaseConfig['database'];
$dbExists = false;
$result = pg_query("SELECT datname FROM pg_database WHERE datname = '$database'");
if(pg_fetch_array($result)) $dbExists = true;
if($dbExists) {
$okay = "Database '$database' exists";
} else {
if(@pg_query("CREATE DATABASE testing123")) {
pg_query("DROP DATABASE testing123");
$okay = "Able to create a new database";
}
} }
} }
if($okay) { if($okay) {
$testDetails[3] = $okay; $testDetails[3] = $okay;
$this->testing($testDetails); $this->testing($testDetails);
return true;
} else {
$testDetails[2] .= " (user '$databaseConfig[username]' doesn't have CREATE DATABASE permissions.)";
$this->error($testDetails);
return false;
} }
} }
function requireServerVariables($varNames, $errorMessage) { function requireServerVariables($varNames, $errorMessage) {
@ -750,7 +872,7 @@ class Installer extends InstallRequirements {
} }
$phpVersion = urlencode(phpversion()); $phpVersion = urlencode(phpversion());
$conn = @mysql_connect($config['mysql']['server'], null, null); $conn = @mysql_connect($config['db']['server'], null, null);
$databaseVersion = urlencode('MySQL ' . mysql_get_server_info()); $databaseVersion = urlencode('MySQL ' . mysql_get_server_info());
$webserver = urlencode($_SERVER['SERVER_SOFTWARE']); $webserver = urlencode($_SERVER['SERVER_SOFTWARE']);
@ -778,7 +900,7 @@ global \$project;
\$project = 'mysite'; \$project = 'mysite';
global \$database; global \$database;
\$database = "{$config['mysql']['database']}"; \$database = "{$config['db']['database']}";
require_once("conf/ConfigureFromEnv.php"); require_once("conf/ConfigureFromEnv.php");
@ -800,7 +922,7 @@ PHP
$devServers = $this->var_export_array_nokeys(explode("\n", $_POST['devsites'])); $devServers = $this->var_export_array_nokeys(explode("\n", $_POST['devsites']));
$escapedPassword = addslashes($config['mysql']['password']); $escapedPassword = addslashes($config['db']['password']);
$this->createFile("mysite/_config.php", <<<PHP $this->createFile("mysite/_config.php", <<<PHP
<?php <?php
@ -809,11 +931,11 @@ global \$project;
global \$databaseConfig; global \$databaseConfig;
\$databaseConfig = array( \$databaseConfig = array(
"type" => "$config[database]", "type" => "{$config['db']['type']}",
"server" => "{$config['mysql']['server']}", "server" => "{$config['db']['server']}",
"username" => "{$config['mysql']['username']}", "username" => "{$config['db']['username']}",
"password" => '{$escapedPassword}', "password" => '{$escapedPassword}',
"database" => "{$config['mysql']['database']}", "database" => "{$config['db']['database']}",
); );
// Sites running on the following servers will be // Sites running on the following servers will be