MediaWiki  REL1_20
Ibm_db2Installer.php
Go to the documentation of this file.
00001 <?php
00030 class Ibm_db2Installer extends DatabaseInstaller {
00031 
00032 
00033         protected $globalNames = array(
00034                 'wgDBserver',
00035                 'wgDBport',
00036                 'wgDBname',
00037                 'wgDBuser',
00038                 'wgDBpassword',
00039                 'wgDBmwschema',
00040         );
00041 
00042         protected $internalDefaults = array(
00043                 '_InstallUser' => 'db2admin'
00044         );
00045 
00050         public function getName(){
00051                 return 'ibm_db2';
00052         }
00053 
00058         public function isCompiled() {
00059                 return self::checkExtension( 'ibm_db2' );
00060         }
00061 
00066         public function getConnectForm() {
00067                 return
00068                         $this->getTextBox( 'wgDBserver', 'config-db-host', array(), $this->parent->getHelpBox( 'config-db-host-help' ) ) .
00069                         $this->getTextBox( 'wgDBport', 'config-db-port', array(), $this->parent->getHelpBox( 'config-db-port' ) ) .
00070                         Html::openElement( 'fieldset' ) .
00071                         Html::element( 'legend', array(), wfMessage( 'config-db-wiki-settings' )->text() ) .
00072                         $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent->getHelpBox( 'config-db-name-help' ) ) .
00073                         $this->getTextBox( 'wgDBmwschema', 'config-db-schema', array(), $this->parent->getHelpBox( 'config-db-schema-help' ) ) .
00074                         Html::closeElement( 'fieldset' ) .
00075                         $this->getInstallUserBox();
00076         }
00077 
00082         public function submitConnectForm() {
00083                 // Get variables from the request
00084                 $newValues = $this->setVarsFromRequest(
00085                         array( 'wgDBserver', 'wgDBport', 'wgDBname',
00086                                 'wgDBmwschema', 'wgDBuser', 'wgDBpassword' ) );
00087 
00088                 // Validate them
00089                 $status = Status::newGood();
00090                 if ( !strlen( $newValues['wgDBname'] ) ) {
00091                         $status->fatal( 'config-missing-db-name' );
00092                 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
00093                         $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
00094                 }
00095                 if ( !strlen( $newValues['wgDBmwschema'] ) ) {
00096                         $status->fatal( 'config-invalid-schema' );
00097                 }
00098                 elseif ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) {
00099                         $status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] );
00100                 }
00101                 if ( !strlen( $newValues['wgDBport'] ) ) {
00102                         $status->fatal( 'config-invalid-port' );
00103                 }
00104                 elseif ( !preg_match( '/^[0-9_]*$/', $newValues['wgDBport'] ) ) {
00105                         $status->fatal( 'config-invalid-port', $newValues['wgDBport'] );
00106                 }
00107 
00108                 // Submit user box
00109                 if ( $status->isOK() ) {
00110                         $status->merge( $this->submitInstallUserBox() );
00111                 }
00112                 if ( !$status->isOK() ) {
00113                         return $status;
00114                 }
00115 
00116                 global $wgDBport;
00117                 $wgDBport = $newValues['wgDBport'];
00118 
00119                 // Try to connect
00120                 $status->merge( $this->getConnection() );
00121                 if ( !$status->isOK() ) {
00122                         return $status;
00123                 }
00124 
00125                 $this->parent->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
00126                 $this->parent->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
00127 
00128                 return $status;
00129         }
00130 
00135         public function openConnection() {
00136                 $status = Status::newGood();
00137                 try {
00138                         $db = new DatabaseIbm_db2(
00139                                 $this->getVar( 'wgDBserver' ),
00140                                 $this->getVar( '_InstallUser' ),
00141                                 $this->getVar( '_InstallPassword' ),
00142                                 $this->getVar( 'wgDBname' ),
00143                                 0,
00144                                 $this->getVar( 'wgDBmwschema' )
00145                         );
00146                         $status->value = $db;
00147                 } catch ( DBConnectionError $e ) {
00148                         $status->fatal( 'config-connection-error', $e->getMessage() );
00149                 }
00150                 return $status;
00151         }
00152 
00157         public function setupDatabase() {
00158                 $status = $this->getConnection();
00159                 if ( !$status->isOK() ) {
00160                         return $status;
00161                 }
00165                 $conn = $status->value;
00166                 $dbName = $this->getVar( 'wgDBname' );
00167                 if( !$conn->selectDB( $dbName ) ) {
00168                         $conn->query( "CREATE DATABASE "
00169                                 . $conn->addIdentifierQuotes( $dbName )
00170                                 . " AUTOMATIC STORAGE YES"
00171                                 . " USING CODESET UTF-8 TERRITORY US COLLATE USING SYSTEM"
00172                                 . " PAGESIZE 32768", __METHOD__ );
00173                         $conn->selectDB( $dbName );
00174                 }
00175                 $this->setupSchemaVars();
00176                 return $status;
00177         }
00178 
00185         public function createTables() {
00186                 $status = $this->getConnection();
00187                 if ( !$status->isOK() ) {
00188                         return $status;
00189                 }
00190                 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
00191 
00192                 if( $this->db->tableExists( 'user' ) ) {
00193                         $status->warning( 'config-install-tables-exist' );
00194                         return $status;
00195                 }
00196 
00197                 /* Check for pagesize */
00198                 $status = $this->checkPageSize();
00199                 if ( !$status->isOK() ) {
00200                         return $status;
00201                 }
00202 
00203                 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
00204                 $this->db->begin( __METHOD__ );
00205 
00206                 $error = $this->db->sourceFile( $this->db->getSchemaPath() );
00207                 if( $error !== true ) {
00208                         $this->db->reportQueryError( $error, 0, '', __METHOD__ );
00209                         $this->db->rollback( __METHOD__ );
00210                         $status->fatal( 'config-install-tables-failed', $error );
00211                 } else {
00212                         $this->db->commit( __METHOD__ );
00213                 }
00214                 // Resume normal operations
00215                 if( $status->isOk() ) {
00216                         $this->enableLB();
00217                 }
00218                 return $status;
00219         }
00220 
00226         public function checkPageSize() {
00227                 $status = $this->getConnection();
00228                 if ( !$status->isOK() ) {
00229                         return $status;
00230                 }
00231                 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
00232 
00233                 try {
00234                         $result = $this->db->query( 'SELECT PAGESIZE FROM SYSCAT.TABLESPACES FOR READ ONLY' );
00235                         if( $result == false ) {
00236                                 $status->fatal( 'config-connection-error', '' );
00237                         } else {
00238                                 $row = $this->db->fetchRow( $result );
00239                                 while ( $row ) {
00240                                         if( $row[0] >= 32768 ) {
00241                                                 return $status;
00242                                         }
00243                                         $row = $this->db->fetchRow( $result );
00244                                 }
00245                                 $status->fatal( 'config-ibm_db2-low-db-pagesize', '' );
00246                         }
00247                 } catch ( DBUnexpectedError $e ) {
00248                         $status->fatal( 'config-connection-error', $e->getMessage() );
00249                 }
00250 
00251                 return $status;
00252         }
00253 
00258         public function getLocalSettings() {
00259                 $schema = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgDBmwschema' ) );
00260                 $port = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgDBport' ) );
00261                 return
00262 "# IBM_DB2 specific settings
00263 \$wgDBmwschema         = \"{$schema}\";
00264 \$wgDBport             = \"{$port}\";";
00265         }
00266 
00267         public function __construct( $parent ) {
00268                 parent::__construct( $parent );
00269         }
00270 }