MediaWiki  REL1_21
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' => 'SYSTEM',
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         public function setupDatabase() {
00206                 $status = Status::newGood();
00207                 return $status;
00208         }
00209 
00210         public function setupUser() {
00211                 global $IP;
00212 
00213                 if ( !$this->getVar( '_CreateDBAccount' ) ) {
00214                         return Status::newGood();
00215                 }
00216 
00217                 // normaly only SYSDBA users can create accounts
00218                 $status = $this->openSYSDBAConnection();
00219                 if ( !$status->isOK() ) {
00220                         if ( $this->connError == 1031 ) {
00221                                 // insufficient  privileges (looks like a normal user)
00222                                 $status = $this->openConnection();
00223                                 if ( !$status->isOK() ) {
00224                                         return $status;
00225                                 }
00226                         } else {
00227                                 return $status;
00228                         }
00229                 }
00230                 $this->db = $status->value;
00231                 $this->setupSchemaVars();
00232 
00233                 if ( !$this->db->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
00234                         $this->db->setFlag( DBO_DDLMODE );
00235                         $error = $this->db->sourceFile( "$IP/maintenance/oracle/user.sql" );
00236                         if ( $error !== true || !$this->db->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
00237                                 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
00238                         }
00239                 } elseif ( $this->db->getFlag( DBO_SYSDBA ) ) {
00240                         $status->fatal( 'config-db-sys-user-exists-oracle', $this->getVar( 'wgDBuser' ) );
00241                 }
00242 
00243                 if ( $status->isOK() ) {
00244                         // user created or already existing, switching back to a normal connection
00245                         // as the new user has all needed privileges to setup the rest of the schema
00246                         // i will be using that user as _InstallUser from this point on
00247                         $this->db->close();
00248                         $this->db = false;
00249                         $this->parent->setVar( '_InstallUser', $this->getVar( 'wgDBuser' ) );
00250                         $this->parent->setVar( '_InstallPassword', $this->getVar( 'wgDBpassword' ) );
00251                         $this->parent->setVar( '_InstallDBname', $this->getVar( 'wgDBuser' ) );
00252                         $status = $this->getConnection();
00253                 }
00254 
00255                 return $status;
00256         }
00257 
00262         public function createTables() {
00263                 $this->setupSchemaVars();
00264                 $this->db->setFlag( DBO_DDLMODE );
00265                 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
00266                 $status = parent::createTables();
00267                 $this->db->clearFlag( DBO_DDLMODE );
00268 
00269                 $this->db->query( 'BEGIN fill_wiki_info; END;' );
00270 
00271                 return $status;
00272         }
00273 
00274         public function getSchemaVars() {
00275                 $varNames = array(
00276                         # These variables are used by maintenance/oracle/user.sql
00277                         '_OracleDefTS',
00278                         '_OracleTempTS',
00279                         'wgDBuser',
00280                         'wgDBpassword',
00281 
00282                         # These are used by tables.sql
00283                         'wgDBprefix',
00284                 );
00285                 $vars = array();
00286                 foreach ( $varNames as $name ) {
00287                         $vars[$name] = $this->getVar( $name );
00288                 }
00289                 return $vars;
00290         }
00291 
00292         public function getLocalSettings() {
00293                 $prefix = $this->getVar( 'wgDBprefix' );
00294                 return
00295 "# Oracle specific settings
00296 \$wgDBprefix = \"{$prefix}\";
00297 ";
00298         }
00299 
00300 }