]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-14679 var_export now used instead of addsingleslashes in installer; refactored...
authorskodak <skodak>
Sun, 22 Jun 2008 22:53:40 +0000 (22:53 +0000)
committerskodak <skodak>
Sun, 22 Jun 2008 22:53:40 +0000 (22:53 +0000)
admin/cliupgrade.php
install.php
lib/dml/adodb_moodle_database.php
lib/dml/moodle_database.php
lib/dml/pdo_moodle_database.php
lib/dml/postgres7_adodb_moodle_database.php
lib/dml/simpletest/testdml.php
lib/installlib.php

index 94b8a21b80fbca8b893f6150468ebe41ec3975f9..2def6b953f7e81707ebbfd43e4017b1e1235a47a 100644 (file)
@@ -465,7 +465,7 @@ if (!file_exists(dirname(dirname(__FILE__)) . '/config.php')) {
         error_reporting(0);  // Hide errors
 
         if (! $dbconnected = $DB->connect($INSTALL['dbhost'], $INSTALL['dbuser'], $INSTALL['dbpass'], $INSTALL['dbname'], false, $INSTALL['prefix'])) {
-            if (!$DB->create_database($INSTALL['dbhost'], $INSTALL['dbuser'], $INSTALL['dbpass'])) {
+            if (!$DB->create_database($INSTALL['dbhost'], $INSTALL['dbuser'], $INSTALL['dbpass'], $INSTALL['dbname'])) {
                  $errormsg = get_string('dbcreationerror', 'install');
                  $nextstage = DATABASE;
             } else {
@@ -599,28 +599,18 @@ if (!file_exists(dirname(dirname(__FILE__)) . '/config.php')) {
     $str .= "\r\n";
 
     $database = $databases[$CONFFILE['dbtype']];
-    $database->connect($CONFFILE['dbhost'], $CONFFILE['dbuser'], $CONFFILE['dbpass'], $CONFFILE['dbname'], false, $CONFFILE['prefix']);
-    $dbconfig = $database->export_dbconfig();
-    $dbconfig->persistent = false;
+    $dbconfig = $database->export_dbconfig($CONFFILE['dbhost'], $CONFFILE['dbuser'], $CONFFILE['dbpass'], $CONFFILE['dbname'], false, $CONFFILE['prefix']);
 
     foreach ($dbconfig as $key=>$value) {
         $key = str_pad($key, 9);
-        if (is_bool($value)) {
-            if ($value) {
-                $str .= '$CFG->'.$key.' = true;'."\r\n";
-            } else {
-                $str .= '$CFG->'.$key.' = false;'."\r\n";
-            }
-        } else {
-            $str .= '$CFG->'.$key.' = \''.addsingleslashes($value)."';\r\n";
-        }
+        $str .= '$CFG->'.$key.' = '.var_export($value, true).";\r\n";
     }
     $str .= "\r\n";
 
-    $str .= '$CFG->wwwroot   = \''.addsingleslashes($CONFFILE['wwwrootform'])."';\r\n";
-    $str .= '$CFG->dirroot   = \''.addsingleslashes($CONFFILE['dirrootform'])."';\r\n";
-    $str .= '$CFG->dataroot  = \''.addsingleslashes($CONFFILE['dataroot'])."';\r\n";
-    $str .= '$CFG->admin     = \''.addsingleslashes($CONFFILE['admindirname'])."';\r\n";
+    $str .= '$CFG->wwwroot   = '.var_export($CONFFILE['wwwrootform'], true).";\r\n";
+    $str .= '$CFG->dirroot   = '.var_export($CONFFILE['dirrootform'], true).";\r\n";
+    $str .= '$CFG->dataroot  = '.var_export($CONFFILE['dataroot'], true).";\r\n";
+    $str .= '$CFG->admin     = '.var_export($CONFFILE['admindirname'], true).";\r\n";
     $str .= "\r\n";
 
     $str .= '$CFG->directorypermissions = 00777;  // try 02777 on a server in Safe Mode'."\r\n";
index 68900a0af5519642ad3af0e200b911e18ceeb972..7f309993a5abb039f99fc7e23f302c3a975566e8 100644 (file)
@@ -461,35 +461,18 @@ if ($nextstage == SAVE) {
     $str .= "\r\n";
 
     $DB = $databases[$INSTALL['dbtype']];
-    $DB->connect($INSTALL['dbhost'], $INSTALL['dbuser'], $INSTALL['dbpass'], $INSTALL['dbname'], false, $INSTALL['prefix']);
-    $dbconfig = $DB->export_dbconfig();
-    $dbconfig->persistent = false;
+    $dbconfig = $DB->export_dbconfig($INSTALL['dbhost'], $INSTALL['dbuser'], $INSTALL['dbpass'], $INSTALL['dbname'], false, $INSTALL['prefix']);
 
     foreach ($dbconfig as $key=>$value) {
         $key = str_pad($key, 9);
-        if (is_bool($value)) {
-            if ($value) {
-                $str .= '$CFG->'.$key.' = true;'."\r\n";
-            } else {
-                $str .= '$CFG->'.$key.' = false;'."\r\n";
-            }
-        } else if (is_array($value)) {
-            if (empty($value)) {
-                $value = 'array()';
-            } else {
-                $value = 'unserialize(\'' . addsingleslashes(serialize($value)) . '\')';
-            }
-            $str .= '$CFG->'.$key.' = '. $value . ";\r\n";
-        } else {
-            $str .= '$CFG->'.$key.' = \''.addsingleslashes($value)."';\r\n";
-        }
+        $str .= '$CFG->'.$key.' = '.var_export($value, true).";\r\n";
     }
     $str .= "\r\n";
 
-    $str .= '$CFG->wwwroot   = \''.addsingleslashes($INSTALL['wwwrootform'])."';\r\n";
-    $str .= '$CFG->dirroot   = \''.addsingleslashes($INSTALL['dirrootform'])."';\r\n";
-    $str .= '$CFG->dataroot  = \''.addsingleslashes($INSTALL['dataroot'])."';\r\n";
-    $str .= '$CFG->admin     = \''.addsingleslashes($INSTALL['admindirname'])."';\r\n";
+    $str .= '$CFG->wwwroot   = '.var_export($INSTALL['wwwrootform'], true).";\r\n";
+    $str .= '$CFG->dirroot   = '.var_export($INSTALL['dirrootform'], true).";\r\n";
+    $str .= '$CFG->dataroot  = '.var_export($INSTALL['dataroot'], true).";\r\n";
+    $str .= '$CFG->admin     = '.var_export($INSTALL['admindirname'], true).";\r\n";
     $str .= "\r\n";
 
     $str .= '$CFG->directorypermissions = 00777;  // try 02777 on a server in Safe Mode'."\r\n";
index c8b4042c0a0d68ee1c9841ea700954899af83e13..ba3cc9ea5e803bed32f0d39fa9fb3f5eea3eda2c 100644 (file)
@@ -12,31 +12,22 @@ abstract class adodb_moodle_database extends moodle_database {
     protected $adodb;
 
     /**
-     * Returns localised database type name
+     * Returns general database library name
      * Note: can be used before connect()
-     * @return string
+     * @return string db type adodb, pdo, native
      */
-    public function get_name() {
-        $dbtype = $this->get_dbtype();
-        return get_string($dbtype, 'install');
+    protected function get_dblibrary() {
+        return 'adodb';
     }
 
     /**
-     * Returns db related part of config.php
+     * Returns localised database type name
      * Note: can be used before connect()
      * @return string
      */
-    public function export_dbconfig() {
-        $cfg = new stdClass();
-        $cfg->dbtype    = $this->get_dbtype();
-        $cfg->dblibrary = 'adodb';
-        $cfg->dbhost    = $this->dbhost;
-        $cfg->dbname    = $this->dbname;
-        $cfg->dbuser    = $this->dbuser;
-        $cfg->dbpass    = $this->dbpass;
-        $cfg->prefix    = $this->prefix;
-
-        return $cfg;
+    public function get_name() {
+        $dbtype = $this->get_dbtype();
+        return get_string($dbtype, 'install');
     }
 
     /**
@@ -45,16 +36,10 @@ abstract class adodb_moodle_database extends moodle_database {
     protected abstract function preconfigure_dbconnection();
 
     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->store_settings($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, $dboptions);
+
         $this->preconfigure_dbconnection();
 
         require_once($CFG->libdir.'/adodb/adodb.inc.php');
index 152a74b3ccd6d9ca7439fc6ce8645b46293f90ae..ab06ee7226fd7ff8a2f828bf793476463e7458e7 100644 (file)
@@ -99,6 +99,13 @@ abstract class moodle_database {
      */
     protected abstract function get_dbtype();
 
+    /**
+     * Returns general database library name
+     * Note: can be used before connect()
+     * @return string db type adodb, pdo, native
+     */
+    protected abstract function get_dblibrary();
+
     /**
      * Returns localised database type name
      * Note: can be used before connect()
@@ -118,7 +125,23 @@ abstract class moodle_database {
      * Note: can be used before connect()
      * @return string
      */
-    public abstract function export_dbconfig();
+    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();
+        $cfg->dbhost    = $this->dbhost;
+        $cfg->dbname    = $this->dbname;
+        $cfg->dbuser    = $this->dbuser;
+        $cfg->dbpass    = $this->dbpass;
+        $cfg->prefix    = $this->prefix;
+        if ($this->dboptions) {
+            $cfg->dboptions = $this->dboptions;
+        }
+
+        return $cfg;
+    }
 
     /**
      * Connect to db
@@ -134,6 +157,27 @@ abstract class moodle_database {
      */
     public abstract function connect($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null);
 
+    /**
+     * Store various database settings
+     * @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 void
+     */
+    protected function store_settings($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;
+    }
+
     /**
      * Attempt to create the database
      * @param string $dbhost
index a84e4415e5e7d723a4053abd6f156f46edcbc692..1a432252f1c7c802069adbf8d99b17265fd01efa 100644 (file)
@@ -35,13 +35,7 @@ abstract class pdo_moodle_database extends moodle_database {
      * @return bool success
      */
     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;
+        $this->store_settings($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, $dboptions);
 
         try {
             $this->pdb = new PDO($this->get_dsn(), $this->dbuser, $this->dbpass, $this->get_pdooptions());
@@ -77,6 +71,15 @@ abstract class pdo_moodle_database extends moodle_database {
         ///TODO: not needed preconfigure_dbconnection() stuff for PDO drivers?
     }
 
+    /**
+     * Returns general database library name
+     * Note: can be used before connect()
+     * @return string db type adodb, pdo, native
+     */
+    protected function get_dblibrary() {
+        return 'pdo';
+    }
+
     /**
      * Returns localised database type name
      * Note: can be used before connect()
@@ -95,24 +98,6 @@ abstract class pdo_moodle_database extends moodle_database {
         return get_string('databasesettingssub_' . $this->get_dbtype() . '_pdo', 'install');
     }
 
-    /**
-     * 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->dblibrary = 'pdo';
-        $cfg->dbhost    = $this->dbhost;
-        $cfg->dbname    = $this->dbname;
-        $cfg->dbuser    = $this->dbuser;
-        $cfg->dbpass    = $this->dbpass;
-        $cfg->prefix    = $this->prefix;
-        $cfg->dboptions = $this->dboptions;
-        return $cfg;
-    }
-
     /**
      * Returns database server info array
      * @return array
index 6a7d3fc429e9e78eeac697e4ed82bdcbd37ef1af..0ab6728a5c699a0f65d9d4e65f087fe77aef034e 100644 (file)
@@ -72,10 +72,12 @@ class postgres7_adodb_moodle_database extends adodb_moodle_database {
      * Note: can be used before connect()
      * @return string
      */
-    public function export_dbconfig() {
+    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  = 'adodb';
+        $cfg->dblibrary  = $this->get_dblibrary();
         if ($this->dbhost == 'localhost' or $this->dbhost == '127.0.0.1') {
             $cfg->dbhost = "user='{$this->dbuser}' password='{$this->dbpass}' dbname='{$this->dbname}'";
             $cfg->dbname = '';
@@ -88,6 +90,9 @@ class postgres7_adodb_moodle_database extends adodb_moodle_database {
             $cfg->dbpass = $this->dbpass;
         }
         $cfg->prefix     = $this->prefix;
+        if ($this->dboptions) {
+            $cfg->dboptions = $this->dboptions;
+        }
 
         return $cfg;
     }
index 0ccbadd59eb7bf46e13afd9aa6356ded8d90fbcc..f307d634f15994f4463d675788c8db14a2f091b5 100755 (executable)
@@ -1121,9 +1121,9 @@ class moodle_database_for_testing extends moodle_database {
     public function driver_installed(){}
     public function get_dbfamily(){}
     protected function get_dbtype(){}
+    protected function get_dblibrary(){}
     public function get_name(){}
     public function get_configuration_hints(){}
-    public function export_dbconfig(){}
     public function connect($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null){}
     public function get_server_info(){}
     protected function allowed_param_types(){}
index 44ae6f8bd758c7defd65d764e2a004247de0e2c1..d342861da45febf7ee10f21af0119058d7ec0d29 100644 (file)
@@ -79,12 +79,3 @@ function check_memory_limit() {
 function inst_check_php_version() {
     return check_php_version("5.2.0");
 }
-
-/**
- * Add slashes for single quotes and backslashes
- * so they can be included in single quoted string
- * (for config.php)
- */
-function addsingleslashes($input){
-    return preg_replace("/(['\\\])/", "\\\\$1", $input);
-}