]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - includes/installer/OracleInstaller.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / includes / installer / OracleInstaller.php
index 8c3e40e10ead41d0c4170fba49c222ef611bbf54..e0fbe1f92f47be4db78dbc25fb845920ef19b13e 100644 (file)
@@ -2,10 +2,28 @@
 /**
  * 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
  */
 
+use Wikimedia\Rdbms\Database;
+use Wikimedia\Rdbms\DBConnectionError;
+
 /**
  * Class for setting up the MediaWiki database using Oracle.
  *
  */
 class OracleInstaller extends DatabaseInstaller {
 
-       protected $globalNames = array(
+       protected $globalNames = [
                'wgDBserver',
                'wgDBname',
                'wgDBuser',
                'wgDBpassword',
                'wgDBprefix',
-       );
+       ];
 
-       protected $internalDefaults = array(
+       protected $internalDefaults = [
                '_OracleDefTS' => 'USERS',
                '_OracleTempTS' => 'TEMP',
-               '_InstallUser' => 'SYSDBA',
-       );
-       
-       public $minimumVersion = '9.0.1'; // 9iR1
+               '_InstallUser' => 'SYSTEM',
+       ];
+
+       public static $minimumVersion = '9.0.1'; // 9iR1
+       protected static $notMiniumumVerisonMessage = 'config-oracle-old';
 
        protected $connError = null;
 
@@ -44,35 +63,51 @@ class OracleInstaller extends DatabaseInstaller {
                if ( $this->getVar( 'wgDBserver' ) == 'localhost' ) {
                        $this->parent->setVar( 'wgDBserver', '' );
                }
-               return
-                       $this->getTextBox( 'wgDBserver', 'config-db-host-oracle', array(), $this->parent->getHelpBox( 'config-db-host-oracle-help' ) ) .
+
+               return $this->getTextBox(
+                       'wgDBserver',
+                       'config-db-host-oracle',
+                       [],
+                       $this->parent->getHelpBox( 'config-db-host-oracle-help' )
+               ) .
                        Html::openElement( 'fieldset' ) .
-                       Html::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
+                       Html::element( 'legend', [], 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', array(), $this->parent->getHelpBox( 'config-db-oracle-help' ) ) .
+                       $this->getTextBox(
+                               '_OracleTempTS',
+                               'config-oracle-temp-ts',
+                               [],
+                               $this->parent->getHelpBox( 'config-db-oracle-help' )
+                       ) .
                        Html::closeElement( 'fieldset' ) .
-                       $this->parent->getWarningBox( wfMsg( 'config-db-account-oracle-warn' ) ).
-                       $this->getInstallUserBox().
+                       $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' ) );
+               $newValues = $this->setVarsFromRequest( [
+                       'wgDBserver',
+                       'wgDBprefix',
+                       'wgDBuser',
+                       'wgDBpassword'
+               ] );
                $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
 
                // Validate them
                $status = Status::newGood();
                if ( !strlen( $newValues['wgDBserver'] ) ) {
                        $status->fatal( 'config-missing-db-server-oracle' );
-               } elseif ( !preg_match( '/^[a-zA-Z0-9_\.]+$/', $newValues['wgDBserver'] ) ) {
+               } elseif ( !self::checkConnectStringFormat( $newValues['wgDBserver'] ) ) {
                        $status->fatal( 'config-invalid-db-server-oracle', $newValues['wgDBserver'] );
                }
                if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
@@ -92,7 +127,7 @@ class OracleInstaller extends DatabaseInstaller {
                // Scenario 1: Install with a manually created account
                $status = $this->getConnection();
                if ( !$status->isOK() ) {
-                       if ( $this->connError == 28009 ) { 
+                       if ( $this->connError == 28009 ) {
                                // _InstallUser seems to be a SYSDBA
                                // Scenario 2: Create user with SYSDBA and install with new user
                                $status = $this->submitWebUserBox();
@@ -104,7 +139,7 @@ class OracleInstaller extends DatabaseInstaller {
                                        return $status;
                                }
                                if ( !$this->getVar( '_CreateDBAccount' ) ) {
-                                       $status->fatal('config-db-sys-create-oracle');
+                                       $status->fatal( 'config-db-sys-create-oracle' );
                                }
                        } else {
                                return $status;
@@ -117,52 +152,50 @@ class OracleInstaller extends DatabaseInstaller {
                                return $statusIS3;
                        }
                }
+
+               /**
+                * @var Database $conn
+                */
                $conn = $status->value;
 
                // Check version
-               $version = $conn->getServerVersion();
-               if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
-                       return Status::newFatal( 'config-oracle-old', $this->minimumVersion, $version );
-               }
+               $status->merge( static::meetsMinimumRequirement( $conn->getServerVersion() ) );
 
                return $status;
        }
 
        public function openConnection() {
-               $status = Status::newGood();
-               try {
-                       $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;
+               return $this->doOpenConnection();
        }
 
        public function openSYSDBAConnection() {
+               return $this->doOpenConnection( DatabaseOracle::DBO_SYSDBA );
+       }
+
+       /**
+        * @param int $flags
+        * @return Status Status with DatabaseOracle or null as the value
+        */
+       private function doOpenConnection( $flags = 0 ) {
                $status = Status::newGood();
                try {
-                       $db = new DatabaseOracle(
-                               $this->getVar( 'wgDBserver' ),
-                               $this->getVar( '_InstallUser' ),
-                               $this->getVar( '_InstallPassword' ),
-                               $this->getVar( '_InstallDBname' ),
-                               DBO_SYSDBA,
-                               $this->getVar( 'wgDBprefix' )
+                       $db = Database::factory(
+                               'oracle',
+                               [
+                                       'host' => $this->getVar( 'wgDBserver' ),
+                                       'user' => $this->getVar( '_InstallUser' ),
+                                       'password' => $this->getVar( '_InstallPassword' ),
+                                       'dbname' => $this->getVar( '_InstallDBname' ),
+                                       'tablePrefix' => $this->getVar( 'wgDBprefix' ),
+                                       'flags' => $flags
+                               ]
                        );
                        $status->value = $db;
                } catch ( DBConnectionError $e ) {
                        $this->connError = $e->db->lastErrno();
                        $status->fatal( 'config-connection-error', $e->getMessage() );
                }
+
                return $status;
        }
 
@@ -171,21 +204,22 @@ class OracleInstaller extends DatabaseInstaller {
                $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
                $retVal = parent::needsUpgrade();
                $this->parent->setVar( 'wgDBname', $tempDBname );
+
                return $retVal;
        }
 
        public function preInstall() {
                # Add our user callback to installSteps, right before the tables are created.
-               $callback = array(
+               $callback = [
                        'name' => 'user',
-                       'callback' => array( $this, 'setupUser' )
-               );
+                       'callback' => [ $this, 'setupUser' ]
+               ];
                $this->parent->addInstallStep( $callback, 'database' );
        }
 
-
        public function setupDatabase() {
                $status = Status::newGood();
+
                return $status;
        }
 
@@ -199,7 +233,7 @@ class OracleInstaller extends DatabaseInstaller {
                // normaly only SYSDBA users can create accounts
                $status = $this->openSYSDBAConnection();
                if ( !$status->isOK() ) {
-                       if ( $this->connError == 1031 ) { 
+                       if ( $this->connError == 1031 ) {
                                // insufficient  privileges (looks like a normal user)
                                $status = $this->openConnection();
                                if ( !$status->isOK() ) {
@@ -209,6 +243,7 @@ class OracleInstaller extends DatabaseInstaller {
                                return $status;
                        }
                }
+
                $this->db = $status->value;
                $this->setupSchemaVars();
 
@@ -222,10 +257,12 @@ class OracleInstaller extends DatabaseInstaller {
                        $status->fatal( 'config-db-sys-user-exists-oracle', $this->getVar( 'wgDBuser' ) );
                }
 
-               if ($status->isOK()) {
+               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' ) );
@@ -237,11 +274,12 @@ class OracleInstaller extends DatabaseInstaller {
 
        /**
         * Overload: after this action field info table has to be rebuilt
+        * @return Status
         */
        public function createTables() {
                $this->setupSchemaVars();
-               $this->db->selectDB( $this->getVar( 'wgDBuser' ) );
                $this->db->setFlag( DBO_DDLMODE );
+               $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
                $status = parent::createTables();
                $this->db->clearFlag( DBO_DDLMODE );
 
@@ -251,7 +289,7 @@ class OracleInstaller extends DatabaseInstaller {
        }
 
        public function getSchemaVars() {
-               $varNames = array(
+               $varNames = [
                        # These variables are used by maintenance/oracle/user.sql
                        '_OracleDefTS',
                        '_OracleTempTS',
@@ -260,20 +298,43 @@ class OracleInstaller extends DatabaseInstaller {
 
                        # These are used by tables.sql
                        'wgDBprefix',
-               );
-               $vars = array();
+               ];
+               $vars = [];
                foreach ( $varNames as $name ) {
                        $vars[$name] = $this->getVar( $name );
                }
+
                return $vars;
        }
 
        public function getLocalSettings() {
                $prefix = $this->getVar( 'wgDBprefix' );
-               return
-"# Oracle specific settings
-\$wgDBprefix         = \"{$prefix}\";
+
+               return "# Oracle specific settings
+\$wgDBprefix = \"{$prefix}\";
 ";
        }
 
+       /**
+        * Function checks the format of Oracle connect string
+        * The actual validity of the string is checked by attempting to connect
+        *
+        * Regex should be able to validate all connect string formats
+        * [//](host|tns_name)[:port][/service_name][:POOLED]
+        * http://www.orafaq.com/wiki/EZCONNECT
+        *
+        * @since 1.22
+        *
+        * @param string $connect_string
+        *
+        * @return bool Whether the connection string is valid.
+        */
+       public static function checkConnectStringFormat( $connect_string ) {
+               // @@codingStandardsIgnoreStart Long lines with regular expressions.
+               // @todo Very long regular expression. Make more readable?
+               $isValid = preg_match( '/^[[:alpha:]][\w\-]*(?:\.[[:alpha:]][\w\-]*){0,2}$/', $connect_string ); // TNS name
+               $isValid |= preg_match( '/^(?:\/\/)?[\w\-\.]+(?::[\d]+)?(?:\/(?:[\w\-\.]+(?::(pooled|dedicated|shared))?)?(?:\/[\w\-\.]+)?)?$/', $connect_string ); // EZConnect
+               // @@codingStandardsIgnoreEnd
+               return (bool)$isValid;
+       }
 }