Merge "(bug 32774) Added config options and flags for SSL and compression in DB."
[lhc/web/wiklou.git] / includes / installer / OracleInstaller.php
index 8146776..72ec800 100644 (file)
@@ -2,20 +2,35 @@
 /**
  * Oracle-specific installer.
  *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup Deployment
  */
+
 /**
  * Class for setting up the MediaWiki database using Oracle.
- * 
+ *
  * @ingroup Deployment
  * @since 1.17
  */
 class OracleInstaller extends DatabaseInstaller {
 
        protected $globalNames = array(
-               'wgDBport',
+               'wgDBserver',
                'wgDBname',
                'wgDBuser',
                'wgDBpassword',
@@ -25,11 +40,13 @@ class OracleInstaller extends DatabaseInstaller {
        protected $internalDefaults = array(
                '_OracleDefTS' => 'USERS',
                '_OracleTempTS' => 'TEMP',
-               '_OracleUseSysdba' => true
+               '_InstallUser' => 'SYSDBA',
        );
 
        public $minimumVersion = '9.0.1'; // 9iR1
 
+       protected $connError = null;
+
        public function getName() {
                return 'oracle';
        }
@@ -38,38 +55,29 @@ class OracleInstaller extends DatabaseInstaller {
                return self::checkExtension( 'oci8' );
        }
 
-       public function getWebUserBox( $noCreateMsg = false ) {
-               $name = $this->getName();
-               $this->parent->setVar( '_SameAccount', false );
-               $this->parent->setVar( '_CreateDBAccount', true );
-               $this->parent->setVar( 'wgDBname', '' );
-               return Xml::openElement( 'fieldset' ) .
-                       Xml::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
-                       Xml::openElement( 'div', array( 'id' => 'dbOtherAccount' ) ) .
-                       $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
-                       $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
-                       $this->parent->getHelpBox( 'config-db-web-help' ).
-                       $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create', array( 'disabled' => true ) ).
-                       Xml::closeElement( 'div' ) . Xml::closeElement( 'fieldset' );
-       }
-
        public function getConnectForm() {
-               $this->parent->setVar( '_InstallUser', 'sys' );
-               $this->parent->setVar( 'wgDBserver', '' );
+               if ( $this->getVar( 'wgDBserver' ) == 'localhost' ) {
+                       $this->parent->setVar( 'wgDBserver', '' );
+               }
                return
-                       $this->getTextBox( 'wgDBserver', 'config-db-host-oracle' ) .
-                       $this->parent->getHelpBox( 'config-db-host-oracle-help' ) . 
-                       Xml::openElement( 'fieldset' ) .
-                       Xml::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
+                       $this->getTextBox( 'wgDBserver', 'config-db-host-oracle', array(), $this->parent->getHelpBox( 'config-db-host-oracle-help' ) ) .
+                       Html::openElement( 'fieldset' ) .
+                       Html::element( 'legend', array(), wfMessage( 'config-db-wiki-settings' )->text() ) .
                        $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
                        $this->getTextBox( '_OracleDefTS', 'config-oracle-def-ts' ) .
-                       $this->getTextBox( '_OracleTempTS', 'config-oracle-temp-ts' ) .
-                       $this->parent->getHelpBox( 'config-db-oracle-help' ) .
-                       Xml::closeElement( 'fieldset' ) .
+                       $this->getTextBox( '_OracleTempTS', 'config-oracle-temp-ts', array(), $this->parent->getHelpBox( 'config-db-oracle-help' ) ) .
+                       Html::closeElement( 'fieldset' ) .
+                       $this->parent->getWarningBox( wfMessage( 'config-db-account-oracle-warn' )->text() ).
                        $this->getInstallUserBox().
                        $this->getWebUserBox();
        }
 
+       public function submitInstallUserBox() {
+               parent::submitInstallUserBox();
+               $this->parent->setVar( '_InstallDBname', $this->getVar( '_InstallUser' ) );
+               return Status::newGood();
+       }
+
        public function submitConnectForm() {
                // Get variables from the request
                $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBprefix', 'wgDBuser', 'wgDBpassword' ) );
@@ -95,11 +103,39 @@ class OracleInstaller extends DatabaseInstaller {
                        return $status;
                }
 
-               // Try to connect
+               // Try to connect trough multiple scenarios
+               // Scenario 1: Install with a manually created account
                $status = $this->getConnection();
                if ( !$status->isOK() ) {
-                       return $status;
+                       if ( $this->connError == 28009 ) {
+                               // _InstallUser seems to be a SYSDBA
+                               // Scenario 2: Create user with SYSDBA and install with new user
+                               $status = $this->submitWebUserBox();
+                               if ( !$status->isOK() ) {
+                                       return $status;
+                               }
+                               $status = $this->openSYSDBAConnection();
+                               if ( !$status->isOK() ) {
+                                       return $status;
+                               }
+                               if ( !$this->getVar( '_CreateDBAccount' ) ) {
+                                       $status->fatal('config-db-sys-create-oracle');
+                               }
+                       } else {
+                               return $status;
+                       }
+               } else {
+                       // check for web user credentials
+                       // Scenario 3: Install with a priviliged user but use a restricted user
+                       $statusIS3 = $this->submitWebUserBox();
+                       if ( !$statusIS3->isOK() ) {
+                               return $statusIS3;
+                       }
                }
+
+               /**
+                * @var $conn DatabaseBase
+                */
                $conn = $status->value;
 
                // Check version
@@ -111,32 +147,39 @@ class OracleInstaller extends DatabaseInstaller {
                return $status;
        }
 
-       public function getConnection() {
+       public function openConnection() {
                $status = Status::newGood();
                try {
-                       if ( $this->getVar( '_OracleUseSysdba' ) ) {
-                               $this->db = new DatabaseOracle(
-                                       $this->getVar( 'wgDBserver' ),
-                                       $this->getVar( '_InstallUser' ),
-                                       $this->getVar( '_InstallPassword' ),
-                                       $this->getVar( 'wgDBname' ),
-                                       false,
-                                       DBO_SYSDBA,
-                                       $this->getVar( 'wgDBprefix' )
-                               );
-                       } else {
-                               $this->db = new DatabaseOracle(
-                                       $this->getVar( 'wgDBserver' ),
-                                       $this->getVar( 'wgDBuser' ),
-                                       $this->getVar( 'wgDBpassword' ),
-                                       $this->getVar( 'wgDBname' ),
-                                       false,
-                                       0,
-                                       $this->getVar( 'wgDBprefix' )
-                               );
-                       }
-                       $status->value = $this->db;
+                       $db = new DatabaseOracle(
+                               $this->getVar( 'wgDBserver' ),
+                               $this->getVar( '_InstallUser' ),
+                               $this->getVar( '_InstallPassword' ),
+                               $this->getVar( '_InstallDBname' ),
+                               0,
+                               $this->getVar( 'wgDBprefix' )
+                       );
+                       $status->value = $db;
+               } catch ( DBConnectionError $e ) {
+                       $this->connError = $e->db->lastErrno();
+                       $status->fatal( 'config-connection-error', $e->getMessage() );
+               }
+               return $status;
+       }
+
+       public function openSYSDBAConnection() {
+               $status = Status::newGood();
+               try {
+                       $db = new DatabaseOracle(
+                               $this->getVar( 'wgDBserver' ),
+                               $this->getVar( '_InstallUser' ),
+                               $this->getVar( '_InstallPassword' ),
+                               $this->getVar( '_InstallDBname' ),
+                               DBO_SYSDBA,
+                               $this->getVar( 'wgDBprefix' )
+                       );
+                       $status->value = $db;
                } catch ( DBConnectionError $e ) {
+                       $this->connError = $e->db->lastErrno();
                        $status->fatal( 'config-connection-error', $e->getMessage() );
                }
                return $status;
@@ -153,17 +196,14 @@ class OracleInstaller extends DatabaseInstaller {
        public function preInstall() {
                # Add our user callback to installSteps, right before the tables are created.
                $callback = array(
-                       array(
-                               'name' => 'user',
-                               'callback' => array( $this, 'setupUser' ),
-                       )
+                       'name' => 'user',
+                       'callback' => array( $this, 'setupUser' )
                );
-               $this->parent->addInstallStepFollowing( "database", $callback );
+               $this->parent->addInstallStep( $callback, 'database' );
        }
 
 
        public function setupDatabase() {
-               $this->parent->setVar( '_OracleUseSysdba', false );
                $status = Status::newGood();
                return $status;
        }
@@ -174,17 +214,43 @@ class OracleInstaller extends DatabaseInstaller {
                if ( !$this->getVar( '_CreateDBAccount' ) ) {
                        return Status::newGood();
                }
-               $status = $this->getConnection();
+
+               // normaly only SYSDBA users can create accounts
+               $status = $this->openSYSDBAConnection();
                if ( !$status->isOK() ) {
-                       return $status;
+                       if ( $this->connError == 1031 ) {
+                               // insufficient  privileges (looks like a normal user)
+                               $status = $this->openConnection();
+                               if ( !$status->isOK() ) {
+                                       return $status;
+                               }
+                       } else {
+                               return $status;
+                       }
                }
+               $this->db = $status->value;
+               $this->setupSchemaVars();
 
-               global $_OracleDefTS, $_OracleTempTS;
-               $_OracleDefTS = $this->getVar( '_OracleDefTS' );
-               $_OracleTempTS = $this->getVar( '_OracleTempTS' );
-               $error = $this->db->sourceFile( "$IP/maintenance/oracle/user.sql" );
-               if ( $error !== true || !$this->db->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
-                       $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
+               if ( !$this->db->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
+                       $this->db->setFlag( DBO_DDLMODE );
+                       $error = $this->db->sourceFile( "$IP/maintenance/oracle/user.sql" );
+                       if ( $error !== true || !$this->db->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
+                               $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
+                       }
+               } elseif ( $this->db->getFlag( DBO_SYSDBA ) ) {
+                       $status->fatal( 'config-db-sys-user-exists-oracle', $this->getVar( 'wgDBuser' ) );
+               }
+
+               if ($status->isOK()) {
+                       // user created or already existing, switching back to a normal connection
+                       // as the new user has all needed privileges to setup the rest of the schema
+                       // i will be using that user as _InstallUser from this point on
+                       $this->db->close();
+                       $this->db = false;
+                       $this->parent->setVar( '_InstallUser', $this->getVar( 'wgDBuser' ) );
+                       $this->parent->setVar( '_InstallPassword', $this->getVar( 'wgDBpassword' ) );
+                       $this->parent->setVar( '_InstallDBname', $this->getVar( 'wgDBuser' ) );
+                       $status = $this->getConnection();
                }
 
                return $status;
@@ -192,16 +258,38 @@ class OracleInstaller extends DatabaseInstaller {
 
        /**
         * Overload: after this action field info table has to be rebuilt
+        * @return Status
         */
        public function createTables() {
+               $this->setupSchemaVars();
+               $this->db->setFlag( DBO_DDLMODE );
+               $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
                $status = parent::createTables();
+               $this->db->clearFlag( DBO_DDLMODE );
 
-               $this->db->doQuery( 'BEGIN fill_wiki_info; END;' );
+               $this->db->query( 'BEGIN fill_wiki_info; END;' );
 
                return $status;
        }
 
-               
+       public function getSchemaVars() {
+               $varNames = array(
+                       # These variables are used by maintenance/oracle/user.sql
+                       '_OracleDefTS',
+                       '_OracleTempTS',
+                       'wgDBuser',
+                       'wgDBpassword',
+
+                       # These are used by tables.sql
+                       'wgDBprefix',
+               );
+               $vars = array();
+               foreach ( $varNames as $name ) {
+                       $vars[$name] = $this->getVar( $name );
+               }
+               return $vars;
+       }
+
        public function getLocalSettings() {
                $prefix = $this->getVar( 'wgDBprefix' );
                return