Skip to content

Commit

Permalink
Installer fixed, prefixes improved, moved conn settings to connect() …
Browse files Browse the repository at this point in the history
…and some more improvements. MDL-14864
  • Loading branch information
stronk7 committed May 19, 2008
1 parent 6395aa3 commit 6d5a22b
Show file tree
Hide file tree
Showing 20 changed files with 597 additions and 338 deletions.
9 changes: 0 additions & 9 deletions admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,6 @@
print_error('withoutversion', 'debug'); // without version, stop
}

/// Check DB prefix requirements are fulfilled
if (empty($CFG->prefix) && $DB->get_dbfamily() != 'mysql') { //Enforce prefixes for everybody but mysql
print_error('prefixcannotbeempty', 'debug', '', array($CFG->prefix, $CFG->dbtype));
}

if ($DB->get_dbfamily() == 'oracle' && strlen($CFG->prefix) > 2) { //Max prefix length for Oracle is 2cc
print_error('prefixlimit', 'debug', '', $CFG->prefix);
}

/// Check if the main tables have been installed yet or not.
if (!$tables = $DB->get_tables() ) { // No tables yet at all.
$maintables = false;
Expand Down
336 changes: 125 additions & 211 deletions install.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lang/en_utf8/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
$string['configintrotimezones'] = 'This page will search for new information about world timezones (including daylight savings time rules) and update your local database with this information. These locations will be checked, in order: $a This procedure is generally very safe and can not break normal installations. Do you wish to update your timezones now?';
$string['configiplookup'] = 'When you click on an IP address (such as 34.12.222.93), such as in the logs, you are shown a map with a best guess of where that IP is located. There are different plugins for this that you can choose from, each has benefits and disadvantages.';
$string['configkeeptagnamecase'] = 'Check this if you want tag names to keep the original casing as entered by users who created them';
$string['configlang'] = 'Choose a default language for the whole site. Users can override this setting later.';
$string['configlang'] = 'Choose a default language for the whole site. Users can override this setting later. You can also set \$CFG->lang=\"yourlangcode_utf8\"; in config.php if you want localised error messages for database connection problems.';
$string['configlangcache'] = 'Cache the language menu. Saves a lot of memory and processing power. If you enable this, the menu takes a few minutes to update after you have added or removed languages.';
$string['configlangdir'] = 'Most languages are printed left-to-right, but some, like Arabic and Hebrew, are printed right-to-left.';
$string['configlanglist'] = 'Leave this blank to allow users to choose from any language you have in this installation of Moodle. However, you can shorten the language menu by entering a comma-separated list of language codes that you want. For example: en,es_es,fr,it';
Expand Down
4 changes: 2 additions & 2 deletions lang/en_utf8/debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
$string['nomodules'] = 'No modules found!!';
$string['phpvaroff'] = 'The PHP server variable \'$a[0]\' should be Off - $a[1]';
$string['phpvaron'] = 'The PHP server variable \'$a[0]\' is not turned On - $a[1]';
$string['prefixcannotbeempty'] = 'Table prefix \"$a[0]\" cannot be empty for your target DB ($a[1])';
$string['prefixlimit'] = 'Table prefix \"$a\" maximum allowed length for Oracle DBs is 2cc.';
$string['prefixcannotbeempty'] = 'Table prefix \"$a[0]\" cannot be empty for your target DB ($a[1])'; // obsoleted 2.0
$string['prefixlimit'] = 'Table prefix \"$a\" maximum allowed length for Oracle DBs is 2cc.'; // obsoleted in 2.0
$string['withoutversion'] = 'Main version.php was not readable or specified';
$string['sessionmissing'] = '$a object missing from session';
$string['siteisnotdefined'] = 'Site is not defined!';
Expand Down
9 changes: 9 additions & 0 deletions lang/en_utf8/error.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@
$string['csvinvalidcolsnum'] = 'INVALID CSV FILE; Each line must include 49 or 70 fields';
$string['csvinvalidcols'] = '<b>INVALID CSV FILE:</b> First line must include "Header Fields" and the file must be type of <br />"Expanded Fields/Comma Separated"<br />or<br /> "Expanded Fields with CAVV Result Code/Comma Separated"';
$string['csvweirdcolumns'] = 'Invalid CSV file format - number of columns is not constant!';
$string['dbconnectionfailed'] = '<p>Error: Database connection failed.</p>
<p>It is possible that the database is overloaded or otherwise not running properly.</p>
<p>The site administrator should also check that the database details have been correctly specified in config.php</p>$a';
$string['dbdriverproblem'] = '<p>Error: database driver problem detected</p>
<p>The site administrator should verify server configuration</p><p>$a</p>';
$string['dbupdatefailed'] = 'Database update failed.';
$string['destinationcmnotexit'] = 'The destination course module does not exist';
$string['downloadedfilecheckfailed'] = 'Downloaded file check failed.';
Expand Down Expand Up @@ -255,6 +260,10 @@
$string['pathdoesnotstartslash'] = 'No valid arguments supplied, path does not start with slash!';
$string['pleasereport'] = 'If you have time, please let us know what you were trying to do when the error occurred:';
$string['pluginrequirementsnotmet'] = 'Plugin \"$a->pluginname\" ($a->pluginversion) could not be installed. It requires a newer version of Moodle (currently you are using $a->currentmoodle, you need $a->requiremoodle).';
$string['prefixcannotbeempty'] = '<p>Error: database table prefix can not be empty ($a)</p>
<p>The site administrator must fix this problem.</p>';
$string['prefixtoolong'] = '<p>Error: database table prefix is too long ($a->dbfamily)</p>
<p>The site administrator must fix this problem. Maximum length for table prefixes in $a->dbfamily is $a->maxlength characters.</p>';
$string['processingstops'] = 'Processing stops here. Remaining records ignored.';
$string['refoundtoorigi'] = 'Refunded to original amount: $a';
$string['refoundto'] = 'Can be refunded to $a';
Expand Down
59 changes: 55 additions & 4 deletions lib/dml/adodb_moodle_database.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php //$Id$

require_once($CFG->libdir.'/adodb/adodb.inc.php');
require_once($CFG->libdir.'/dml/moodle_database.php');
require_once($CFG->libdir.'/dml/adodb_moodle_recordset.php');

Expand All @@ -13,11 +12,57 @@ abstract class adodb_moodle_database extends moodle_database {
protected $db;
protected $columns = array(); // I wish we had a shared memory cache for this :-(

public function __construct($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix) {
parent::__construct($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix);
/**
* Returns localised database type name
* Note: can be used before connect()
* @return string
*/
public function get_name() {
$dbtype = $this->get_dbtype();
return get_string($dbtype, 'install');
}

public function connect() {
/**
* Returns db related part of config.php
* Note: can be used before connect()
* @return string
*/
public function export_dbconfig() {
$cfg = new stdClass();
$cfg->dbtype = $this->get_dbtype();
$cfg->library = 'adodb';
$cfg->dbhost = $this->dbhost;
$cfg->dbname = $this->dbname;
$cfg->dbuser = $this->dbuser;
$cfg->dbpass = $this->dbpass;
$cfg->prefix = $this->prefix;

return $cfg;
}

//TODO: preconfigure_dbconnection(): Decide if this should be declared as abstract because all adodb drivers will need it
/**
* Adodb preconnection routines, ususally sets up needed defines;
*/
protected function preconfigure_dbconnection() {
// empty
}

public function connect($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null) {
$this->dbhost = $dbhost;
$this->dbuser = $dbuser;
$this->dbpass = $dbpass;
$this->dbname = $dbname;
$this->dbpersist = $dbpersist;
$this->prefix = $prefix;
$this->dboptions = (array)$dboptions;

global $CFG;

$this->preconfigure_dbconnection();

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

$this->db = ADONewConnection($this->get_dbtype());

global $db; $db = $this->db; // TODO: BC only for now
Expand All @@ -39,10 +84,15 @@ public function connect() {
return true;
}

//TODO: configure_dbconnection(): Decide if this should be declared as abstract because all adodb drivers will need it
/**
* Adodb post connection routines, usually sets up encoding,e tc.
*/
protected function configure_dbconnection() {
// empty
}

//TODO: make all dblibraries return this info in a structured way (new server_info class or so, like database_column_info class)
/**
* Returns database server info array
* @return array
Expand Down Expand Up @@ -185,6 +235,7 @@ public function execute($sql, array $params=null) {
return $result;
}

//TODO: do we want the *_raw() functions being public? I see the benefits but... won't that cause problems. To decide.
/**
* Insert new record into database, as fast as possible, no safety checks, lobs not supported.
* @param string $table name
Expand Down
108 changes: 71 additions & 37 deletions lib/dml/moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
*/
abstract class moodle_database {

/**
* supports :name, "?" or both types of parameters?
* must be set in implementing class constructor
*/
protected $param_types;

// manipulates the db structure
protected $database_manager;

// db connection options
Expand All @@ -22,65 +17,104 @@ abstract class moodle_database {
protected $dbpersist;
protected $prefix;

// TODO: perf stuff goes here
// TODO: do we really need record caching??
/**
* Non-moodle external database used.
*/
protected $external;

/**
* Contructor - sets up database connection, prints error in case of problem.
* @param string $dbhost
* @param string $dbuser
* @param string $dbpass
* @param string $dbname
* @param string $dbpersist
* @param string $prefix table prefix
*/
public function __construct($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix) {
$this->dbhost = $dbhost;
$this->dbuser = $dbuser;
$this->dbpass = $dbpass;
$this->dbname = $dbname;
$this->dbpersist = $dbpersist;
$this->prefix = $prefix;
}
* Database or driver specific options, such as sockets or TCPIP db connections
*/
protected $dboptions;

// TODO: perf stuff goes here
// TODO: do we really need record caching??

/**
* Returns database family type - describes SQL dialect
* @return string db family name (mysql, postgres, mssql, oracle, etc.)
* Contructor - instantiates the database, specifying if it's external (connect to other systems) or no (Moodle DB)
* note this has effect to decide if prefix checks must be performed or no
* @param bool true means external database used
*/
public abstract function get_dbfamily();
public function __construct($external=false) {
$this->external = $external;
}

/**
* Returns database server info array
* @return array
* Detects if all needed PHP stuff installed.
* Note: can be used before connect()
* @return mixed true if ok, string if something
*/
public abstract function get_server_info();
public abstract function driver_installed();

/**
* Returns database table prefix
* Note: can be used before connect()
* @return string database table prefix
*/
public function get_prefix() {
return $this->prefix;
}

/**
* Returns database type
* Returns database family type - describes SQL dialect
* Note: can be used before connect()
* @return string db family name (mysql, postgres, mssql, oracle, etc.)
*/
public abstract function get_dbfamily();

/**
* Returns more specific database driver type
* Note: can be used before connect()
* @return string db type mysql, mysqli, postgres7
*/
protected abstract function get_dbtype();

/**
* Returns supported query parameter types
* @return bitmask
* Returns localised database type name
* Note: can be used before connect()
* @return string
*/
protected abstract function allowed_param_types();
public abstract function get_name();

/**
* Returns localised database description
* Note: can be used before connect()
* @return string
*/
public abstract function get_configuration_hints();

/**
* Connect to db specified in constructor
* Must be called before any other methods.
* Returns db related part of config.php
* Note: can be used before connect()
* @return string
*/
public abstract function export_dbconfig();

/**
* Connect to db
* Must be called before other methods.
* @param string $dbhost
* @param string $dbuser
* @param string $dbpass
* @param string $dbname
* @param bool $dbpersist
* @param mixed $prefix string means moodle db prefix, false used for external databases where prefix not used
* @param array $dboptions driver specific options
* @return bool success
*/
public abstract function connect();
public abstract function connect($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null);

/**
* Returns database server info array
* @return array
*/
public abstract function get_server_info();

/**
* Returns supported query parameter types
* @return bitmask
*/
protected abstract function allowed_param_types();

/**
* Returns last error reported by database engine.
Expand Down
41 changes: 38 additions & 3 deletions lib/dml/mssql_adodb_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,34 @@
* @package dmlib
*/
class mssql_adodb_moodle_database extends adodb_moodle_database {
function __construct ($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix) {
parent::__construct($dbhost, $dbuser, $dbpass, $dbname, false, $prefix);

public function connect($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null) {
if ($prefix == '' and !$this->external) {
//Enforce prefixes for everybody but mysql
print_error('prefixcannotbeempty', 'error', '', $this->get_dbfamily());
}
return parent::connect($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, $dboptions);
}

protected function configure_dbconnection() {
/**
* Detects if all needed PHP stuff installed.
* Do not connect to connect to db if this test fails.
* @return mixed true if ok, string if something
*/
public function driver_installed() {
if (!function_exists('mssql_connect')) {
return get_string('mssqlextensionisnotpresentinphp', 'install');
}
return true;
}

protected function preconfigure_dbconnection() {
if (!defined('ADODB_ASSOC_CASE')) {
define ('ADODB_ASSOC_CASE', 2);
}
}

protected function configure_dbconnection() {
$this->db->SetFetchMode(ADODB_FETCH_ASSOC);

/// No need to set charset. It must be specified in the driver conf
Expand Down Expand Up @@ -49,6 +69,21 @@ protected function get_dbtype() {
return 'mssql';
}

/**
* Returns localised database description
* Note: can be used before connect()
* @return string
*/
public function get_configuration_hints() {
$str = get_string('databasesettingssub_mssql', 'install');
$str .= "<p style='text-align:right'><a href=\"javascript:void(0)\" ";
$str .= "onclick=\"return window.open('http://docs.moodle.org/en/Installing_MSSQL_for_PHP')\"";
$str .= ">";
$str .= '<img src="pix/docs.gif' . '" alt="Docs" class="iconhelp" />';
$str .= get_string('moodledocslink', 'install') . '</a></p>';
return $str;
}

/**
* Returns supported query parameter types
* @return bitmask
Expand Down
18 changes: 15 additions & 3 deletions lib/dml/mssql_n_adodb_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
* @package dmlib
*/
class mssql_n_adodb_moodle_database extends mssql_adodb_moodle_database {
function __construct ($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix) {
parent::__construct($dbhost, $dbuser, $dbpass, $dbname, false, $prefix);
}

/**
* Returns database type
Expand All @@ -21,4 +18,19 @@ protected function get_dbtype() {
return 'mssql_n';
}

/**
* Returns localised database description
* Note: can be used before connect()
* @return string
*/
public function get_configuration_hints() {
$str = get_string('databasesettingssub_mssql_n', 'install');
$str .= "<p style='text-align:right'><a href=\"javascript:void(0)\" ";
$str .= "onclick=\"return window.open('http://docs.moodle.org/en/Installing_MSSQL_for_PHP')\"";
$str .= ">";
$str .= '<img src="pix/docs.gif' . '" alt="Docs" class="iconhelp" />';
$str .= get_string('moodledocslink', 'install') . '</a></p>';
return $str;
}

}
Loading

0 comments on commit 6d5a22b

Please sign in to comment.