MediaWiki  REL1_19
OracleInstaller.php
Go to the documentation of this file.
00001 <?php
00015 class OracleInstaller extends DatabaseInstaller {
00016 
00017         protected $globalNames = array(
00018                 'wgDBserver',
00019                 'wgDBname',
00020                 'wgDBuser',
00021                 'wgDBpassword',
00022                 'wgDBprefix',
00023         );
00024 
00025         protected $internalDefaults = array(
00026                 '_OracleDefTS' => 'USERS',
00027                 '_OracleTempTS' => 'TEMP',
00028                 '_InstallUser' => 'SYSDBA',
00029         );
00030 
00031         public $minimumVersion = '9.0.1'; // 9iR1
00032 
00033         protected $connError = null;
00034 
00035         public function getName() {
00036                 return 'oracle';
00037         }
00038 
00039         public function isCompiled() {
00040                 return self::checkExtension( 'oci8' );
00041         }
00042 
00043         public function getConnectForm() {
00044                 if ( $this->getVar( 'wgDBserver' ) == 'localhost' ) {
00045                         $this->parent->setVar( 'wgDBserver', '' );
00046                 }
00047                 return
00048                         $this->getTextBox( 'wgDBserver', 'config-db-host-oracle', array(), $this->parent->getHelpBox( 'config-db-host-oracle-help' ) ) .
00049                         Html::openElement( 'fieldset' ) .
00050                         Html::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
00051                         $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
00052                         $this->getTextBox( '_OracleDefTS', 'config-oracle-def-ts' ) .
00053                         $this->getTextBox( '_OracleTempTS', 'config-oracle-temp-ts', array(), $this->parent->getHelpBox( 'config-db-oracle-help' ) ) .
00054                         Html::closeElement( 'fieldset' ) .
00055                         $this->parent->getWarningBox( wfMsg( 'config-db-account-oracle-warn' ) ).
00056                         $this->getInstallUserBox().
00057                         $this->getWebUserBox();
00058         }
00059 
00060         public function submitInstallUserBox() {
00061                 parent::submitInstallUserBox();
00062                 $this->parent->setVar( '_InstallDBname', $this->getVar( '_InstallUser' ) );
00063                 return Status::newGood();
00064         }
00065 
00066         public function submitConnectForm() {
00067                 // Get variables from the request
00068                 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBprefix', 'wgDBuser', 'wgDBpassword' ) );
00069                 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
00070 
00071                 // Validate them
00072                 $status = Status::newGood();
00073                 if ( !strlen( $newValues['wgDBserver'] ) ) {
00074                         $status->fatal( 'config-missing-db-server-oracle' );
00075                 } elseif ( !preg_match( '/^[a-zA-Z0-9_\.]+$/', $newValues['wgDBserver'] ) ) {
00076                         $status->fatal( 'config-invalid-db-server-oracle', $newValues['wgDBserver'] );
00077                 }
00078                 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
00079                         $status->fatal( 'config-invalid-schema', $newValues['wgDBprefix'] );
00080                 }
00081                 if ( !$status->isOK() ) {
00082                         return $status;
00083                 }
00084 
00085                 // Submit user box
00086                 $status = $this->submitInstallUserBox();
00087                 if ( !$status->isOK() ) {
00088                         return $status;
00089                 }
00090 
00091                 // Try to connect trough multiple scenarios
00092                 // Scenario 1: Install with a manually created account
00093                 $status = $this->getConnection();
00094                 if ( !$status->isOK() ) {
00095                         if ( $this->connError == 28009 ) {
00096                                 // _InstallUser seems to be a SYSDBA
00097                                 // Scenario 2: Create user with SYSDBA and install with new user
00098                                 $status = $this->submitWebUserBox();
00099                                 if ( !$status->isOK() ) {
00100                                         return $status;
00101                                 }
00102                                 $status = $this->openSYSDBAConnection();
00103                                 if ( !$status->isOK() ) {
00104                                         return $status;
00105                                 }
00106                                 if ( !$this->getVar( '_CreateDBAccount' ) ) {
00107                                         $status->fatal('config-db-sys-create-oracle');
00108                                 }
00109                         } else {
00110                                 return $status;
00111                         }
00112                 } else {
00113                         // check for web user credentials
00114                         // Scenario 3: Install with a priviliged user but use a restricted user
00115                         $statusIS3 = $this->submitWebUserBox();
00116                         if ( !$statusIS3->isOK() ) {
00117                                 return $statusIS3;
00118                         }
00119                 }
00120 
00124                 $conn = $status->value;
00125 
00126                 // Check version
00127                 $version = $conn->getServerVersion();
00128                 if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
00129                         return Status::newFatal( 'config-oracle-old', $this->minimumVersion, $version );
00130                 }
00131 
00132                 return $status;
00133         }
00134 
00135         public function openConnection() {
00136                 $status = Status::newGood();
00137                 try {
00138                         $db = new DatabaseOracle(
00139                                 $this->getVar( 'wgDBserver' ),
00140                                 $this->getVar( '_InstallUser' ),
00141                                 $this->getVar( '_InstallPassword' ),
00142                                 $this->getVar( '_InstallDBname' ),
00143                                 0,
00144                                 $this->getVar( 'wgDBprefix' )
00145                         );
00146                         $status->value = $db;
00147                 } catch ( DBConnectionError $e ) {
00148                         $this->connError = $e->db->lastErrno();
00149                         $status->fatal( 'config-connection-error', $e->getMessage() );
00150                 }
00151                 return $status;
00152         }
00153 
00154         public function openSYSDBAConnection() {
00155                 $status = Status::newGood();
00156                 try {
00157                         $db = new DatabaseOracle(
00158                                 $this->getVar( 'wgDBserver' ),
00159                                 $this->getVar( '_InstallUser' ),
00160                                 $this->getVar( '_InstallPassword' ),
00161                                 $this->getVar( '_InstallDBname' ),
00162                                 DBO_SYSDBA,
00163                                 $this->getVar( 'wgDBprefix' )
00164                         );
00165                         $status->value = $db;
00166                 } catch ( DBConnectionError $e ) {
00167                         $this->connError = $e->db->lastErrno();
00168                         $status->fatal( 'config-connection-error', $e->getMessage() );
00169                 }
00170                 return $status;
00171         }
00172 
00173         public function needsUpgrade() {
00174                 $tempDBname = $this->getVar( 'wgDBname' );
00175                 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
00176                 $retVal = parent::needsUpgrade();
00177                 $this->parent->setVar( 'wgDBname', $tempDBname );
00178                 return $retVal;
00179         }
00180 
00181         public function preInstall() {
00182                 # Add our user callback to installSteps, right before the tables are created.
00183                 $callback = array(
00184                         'name' => 'user',
00185                         'callback' => array( $this, 'setupUser' )
00186                 );
00187                 $this->parent->addInstallStep( $callback, 'database' );
00188         }
00189 
00190 
00191         public function setupDatabase() {
00192                 $status = Status::newGood();
00193                 return $status;
00194         }
00195 
00196         public function setupUser() {
00197                 global $IP;
00198 
00199                 if ( !$this->getVar( '_CreateDBAccount' ) ) {
00200                         return Status::newGood();
00201                 }
00202 
00203                 // normaly only SYSDBA users can create accounts
00204                 $status = $this->openSYSDBAConnection();
00205                 if ( !$status->isOK() ) {
00206                         if ( $this->connError == 1031 ) {
00207                                 // insufficient  privileges (looks like a normal user)
00208                                 $status = $this->openConnection();
00209                                 if ( !$status->isOK() ) {
00210                                         return $status;
00211                                 }
00212                         } else {
00213                                 return $status;
00214                         }
00215                 }
00216                 $this->db = $status->value;
00217                 $this->setupSchemaVars();
00218 
00219                 if ( !$this->db->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
00220                         $this->db->setFlag( DBO_DDLMODE );
00221                         $error = $this->db->sourceFile( "$IP/maintenance/oracle/user.sql" );
00222                         if ( $error !== true || !$this->db->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
00223                                 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
00224                         }
00225                 } elseif ( $this->db->getFlag( DBO_SYSDBA ) ) {
00226                         $status->fatal( 'config-db-sys-user-exists-oracle', $this->getVar( 'wgDBuser' ) );
00227                 }
00228 
00229                 if ($status->isOK()) {
00230                         // user created or already existing, switching back to a normal connection
00231                         // as the new user has all needed privileges to setup the rest of the schema
00232                         // i will be using that user as _InstallUser from this point on
00233                         $this->db->close();
00234                         $this->db = false;
00235                         $this->parent->setVar( '_InstallUser', $this->getVar( 'wgDBuser' ) );
00236                         $this->parent->setVar( '_InstallPassword', $this->getVar( 'wgDBpassword' ) );
00237                         $this->parent->setVar( '_InstallDBname', $this->getVar( 'wgDBuser' ) );
00238                         $status = $this->getConnection();
00239                 }
00240 
00241                 return $status;
00242         }
00243 
00247         public function createTables() {
00248                 $this->setupSchemaVars();
00249                 $this->db->setFlag( DBO_DDLMODE );
00250                 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
00251                 $status = parent::createTables();
00252                 $this->db->clearFlag( DBO_DDLMODE );
00253 
00254                 $this->db->query( 'BEGIN fill_wiki_info; END;' );
00255 
00256                 return $status;
00257         }
00258 
00259         public function getSchemaVars() {
00260                 $varNames = array(
00261                         # These variables are used by maintenance/oracle/user.sql
00262                         '_OracleDefTS',
00263                         '_OracleTempTS',
00264                         'wgDBuser',
00265                         'wgDBpassword',
00266 
00267                         # These are used by tables.sql
00268                         'wgDBprefix',
00269                 );
00270                 $vars = array();
00271                 foreach ( $varNames as $name ) {
00272                         $vars[$name] = $this->getVar( $name );
00273                 }
00274                 return $vars;
00275         }
00276 
00277         public function getLocalSettings() {
00278                 $prefix = $this->getVar( 'wgDBprefix' );
00279                 return
00280 "# Oracle specific settings
00281 \$wgDBprefix         = \"{$prefix}\";
00282 ";
00283         }
00284 
00285 }