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