Simplify the check to make it more understandable
[mediawiki.git] / includes / installer / OracleInstaller.php
blob7f04f75ec8bff9966d7ef0c287243525cd712bee
1 <?php
2 /**
3 * Oracle-specific installer.
5 * @file
6 * @ingroup Deployment
7 */
9 /**
10 * Class for setting up the MediaWiki database using Oracle.
12 * @ingroup Deployment
13 * @since 1.17
15 class OracleInstaller extends DatabaseInstaller {
17 protected $globalNames = array(
18 'wgDBserver',
19 'wgDBname',
20 'wgDBuser',
21 'wgDBpassword',
22 'wgDBprefix',
25 protected $internalDefaults = array(
26 '_OracleDefTS' => 'USERS',
27 '_OracleTempTS' => 'TEMP',
28 '_InstallUser' => 'SYSDBA',
31 public $minimumVersion = '9.0.1'; // 9iR1
33 protected $connError = null;
35 public function getName() {
36 return 'oracle';
39 public function isCompiled() {
40 return self::checkExtension( 'oci8' );
43 public function getConnectForm() {
44 if ( $this->getVar( 'wgDBserver' ) == 'localhost' ) {
45 $this->parent->setVar( 'wgDBserver', '' );
47 return
48 $this->getTextBox( 'wgDBserver', 'config-db-host-oracle', array(), $this->parent->getHelpBox( 'config-db-host-oracle-help' ) ) .
49 Html::openElement( 'fieldset' ) .
50 Html::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
51 $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
52 $this->getTextBox( '_OracleDefTS', 'config-oracle-def-ts' ) .
53 $this->getTextBox( '_OracleTempTS', 'config-oracle-temp-ts', array(), $this->parent->getHelpBox( 'config-db-oracle-help' ) ) .
54 Html::closeElement( 'fieldset' ) .
55 $this->parent->getWarningBox( wfMsg( 'config-db-account-oracle-warn' ) ).
56 $this->getInstallUserBox().
57 $this->getWebUserBox();
60 public function submitInstallUserBox() {
61 parent::submitInstallUserBox();
62 $this->parent->setVar( '_InstallDBname', $this->getVar( '_InstallUser' ) );
63 return Status::newGood();
66 public function submitConnectForm() {
67 // Get variables from the request
68 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBprefix', 'wgDBuser', 'wgDBpassword' ) );
69 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
71 // Validate them
72 $status = Status::newGood();
73 if ( !strlen( $newValues['wgDBserver'] ) ) {
74 $status->fatal( 'config-missing-db-server-oracle' );
75 } elseif ( !preg_match( '/^[a-zA-Z0-9_\.]+$/', $newValues['wgDBserver'] ) ) {
76 $status->fatal( 'config-invalid-db-server-oracle', $newValues['wgDBserver'] );
78 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
79 $status->fatal( 'config-invalid-schema', $newValues['wgDBprefix'] );
81 if ( !$status->isOK() ) {
82 return $status;
85 // Submit user box
86 $status = $this->submitInstallUserBox();
87 if ( !$status->isOK() ) {
88 return $status;
91 // Try to connect trough multiple scenarios
92 // Scenario 1: Install with a manually created account
93 $status = $this->getConnection();
94 if ( !$status->isOK() ) {
95 if ( $this->connError == 28009 ) {
96 // _InstallUser seems to be a SYSDBA
97 // Scenario 2: Create user with SYSDBA and install with new user
98 $status = $this->submitWebUserBox();
99 if ( !$status->isOK() ) {
100 return $status;
102 $status = $this->openSYSDBAConnection();
103 if ( !$status->isOK() ) {
104 return $status;
106 if ( !$this->getVar( '_CreateDBAccount' ) ) {
107 $status->fatal('config-db-sys-create-oracle');
109 } else {
110 return $status;
112 } else {
113 // check for web user credentials
114 // Scenario 3: Install with a priviliged user but use a restricted user
115 $statusIS3 = $this->submitWebUserBox();
116 if ( !$statusIS3->isOK() ) {
117 return $statusIS3;
122 * @var $conn DatabaseBase
124 $conn = $status->value;
126 // Check version
127 $version = $conn->getServerVersion();
128 if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
129 return Status::newFatal( 'config-oracle-old', $this->minimumVersion, $version );
132 return $status;
135 public function openConnection() {
136 $status = Status::newGood();
137 try {
138 $db = new DatabaseOracle(
139 $this->getVar( 'wgDBserver' ),
140 $this->getVar( '_InstallUser' ),
141 $this->getVar( '_InstallPassword' ),
142 $this->getVar( '_InstallDBname' ),
144 $this->getVar( 'wgDBprefix' )
146 $status->value = $db;
147 } catch ( DBConnectionError $e ) {
148 $this->connError = $e->db->lastErrno();
149 $status->fatal( 'config-connection-error', $e->getMessage() );
151 return $status;
154 public function openSYSDBAConnection() {
155 $status = Status::newGood();
156 try {
157 $db = new DatabaseOracle(
158 $this->getVar( 'wgDBserver' ),
159 $this->getVar( '_InstallUser' ),
160 $this->getVar( '_InstallPassword' ),
161 $this->getVar( '_InstallDBname' ),
162 DBO_SYSDBA,
163 $this->getVar( 'wgDBprefix' )
165 $status->value = $db;
166 } catch ( DBConnectionError $e ) {
167 $this->connError = $e->db->lastErrno();
168 $status->fatal( 'config-connection-error', $e->getMessage() );
170 return $status;
173 public function needsUpgrade() {
174 $tempDBname = $this->getVar( 'wgDBname' );
175 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
176 $retVal = parent::needsUpgrade();
177 $this->parent->setVar( 'wgDBname', $tempDBname );
178 return $retVal;
181 public function preInstall() {
182 # Add our user callback to installSteps, right before the tables are created.
183 $callback = array(
184 'name' => 'user',
185 'callback' => array( $this, 'setupUser' )
187 $this->parent->addInstallStep( $callback, 'database' );
191 public function setupDatabase() {
192 $status = Status::newGood();
193 return $status;
196 public function setupUser() {
197 global $IP;
199 if ( !$this->getVar( '_CreateDBAccount' ) ) {
200 return Status::newGood();
203 // normaly only SYSDBA users can create accounts
204 $status = $this->openSYSDBAConnection();
205 if ( !$status->isOK() ) {
206 if ( $this->connError == 1031 ) {
207 // insufficient privileges (looks like a normal user)
208 $status = $this->openConnection();
209 if ( !$status->isOK() ) {
210 return $status;
212 } else {
213 return $status;
216 $this->db = $status->value;
217 $this->setupSchemaVars();
219 if ( !$this->db->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
220 $this->db->setFlag( DBO_DDLMODE );
221 $error = $this->db->sourceFile( "$IP/maintenance/oracle/user.sql" );
222 if ( $error !== true || !$this->db->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
223 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
225 } elseif ( $this->db->getFlag( DBO_SYSDBA ) ) {
226 $status->fatal( 'config-db-sys-user-exists-oracle', $this->getVar( 'wgDBuser' ) );
229 if ($status->isOK()) {
230 // user created or already existing, switching back to a normal connection
231 // as the new user has all needed privileges to setup the rest of the schema
232 // i will be using that user as _InstallUser from this point on
233 $this->parent->setVar( '_InstallUser', $this->getVar( 'wgDBuser' ) );
234 $this->parent->setVar( '_InstallPassword', $this->getVar( 'wgDBpassword' ) );
235 $this->parent->setVar( '_InstallDBname', $this->getVar( 'wgDBuser' ) );
236 $status = $this->getConnection();
239 return $status;
243 * Overload: after this action field info table has to be rebuilt
245 public function createTables() {
246 $this->setupSchemaVars();
247 $this->db->selectDB( $this->getVar( 'wgDBuser' ) );
248 $this->db->setFlag( DBO_DDLMODE );
249 $status = parent::createTables();
250 $this->db->clearFlag( DBO_DDLMODE );
252 $this->db->query( 'BEGIN fill_wiki_info; END;' );
254 return $status;
257 public function getSchemaVars() {
258 $varNames = array(
259 # These variables are used by maintenance/oracle/user.sql
260 '_OracleDefTS',
261 '_OracleTempTS',
262 'wgDBuser',
263 'wgDBpassword',
265 # These are used by tables.sql
266 'wgDBprefix',
268 $vars = array();
269 foreach ( $varNames as $name ) {
270 $vars[$name] = $this->getVar( $name );
272 return $vars;
275 public function getLocalSettings() {
276 $prefix = $this->getVar( 'wgDBprefix' );
277 return
278 "# Oracle specific settings
279 \$wgDBprefix = \"{$prefix}\";