Merge "Typo fix"
[mediawiki.git] / includes / installer / OracleInstaller.php
blobe34bed37572d0f3c10ed5d44cbde8893e1a694f9
1 <?php
2 /**
3 * Oracle-specific installer.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Deployment
24 /**
25 * Class for setting up the MediaWiki database using Oracle.
27 * @ingroup Deployment
28 * @since 1.17
30 class OracleInstaller extends DatabaseInstaller {
32 protected $globalNames = array(
33 'wgDBserver',
34 'wgDBname',
35 'wgDBuser',
36 'wgDBpassword',
37 'wgDBprefix',
40 protected $internalDefaults = array(
41 '_OracleDefTS' => 'USERS',
42 '_OracleTempTS' => 'TEMP',
43 '_InstallUser' => 'SYSTEM',
46 public $minimumVersion = '9.0.1'; // 9iR1
48 protected $connError = null;
50 public function getName() {
51 return 'oracle';
54 public function isCompiled() {
55 return self::checkExtension( 'oci8' );
58 public function getConnectForm() {
59 if ( $this->getVar( 'wgDBserver' ) == 'localhost' ) {
60 $this->parent->setVar( 'wgDBserver', '' );
62 return $this->getTextBox( 'wgDBserver', 'config-db-host-oracle', array(), $this->parent->getHelpBox( 'config-db-host-oracle-help' ) ) .
63 Html::openElement( 'fieldset' ) .
64 Html::element( 'legend', array(), wfMessage( 'config-db-wiki-settings' )->text() ) .
65 $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
66 $this->getTextBox( '_OracleDefTS', 'config-oracle-def-ts' ) .
67 $this->getTextBox( '_OracleTempTS', 'config-oracle-temp-ts', array(), $this->parent->getHelpBox( 'config-db-oracle-help' ) ) .
68 Html::closeElement( 'fieldset' ) .
69 $this->parent->getWarningBox( wfMessage( 'config-db-account-oracle-warn' )->text() ) .
70 $this->getInstallUserBox() .
71 $this->getWebUserBox();
74 public function submitInstallUserBox() {
75 parent::submitInstallUserBox();
76 $this->parent->setVar( '_InstallDBname', $this->getVar( '_InstallUser' ) );
77 return Status::newGood();
80 public function submitConnectForm() {
81 // Get variables from the request
82 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBprefix', 'wgDBuser', 'wgDBpassword' ) );
83 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
85 // Validate them
86 $status = Status::newGood();
87 if ( !strlen( $newValues['wgDBserver'] ) ) {
88 $status->fatal( 'config-missing-db-server-oracle' );
89 } elseif ( !self::checkConnectStringFormat( $newValues['wgDBserver'] ) ) {
90 $status->fatal( 'config-invalid-db-server-oracle', $newValues['wgDBserver'] );
92 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
93 $status->fatal( 'config-invalid-schema', $newValues['wgDBprefix'] );
95 if ( !$status->isOK() ) {
96 return $status;
99 // Submit user box
100 $status = $this->submitInstallUserBox();
101 if ( !$status->isOK() ) {
102 return $status;
105 // Try to connect trough multiple scenarios
106 // Scenario 1: Install with a manually created account
107 $status = $this->getConnection();
108 if ( !$status->isOK() ) {
109 if ( $this->connError == 28009 ) {
110 // _InstallUser seems to be a SYSDBA
111 // Scenario 2: Create user with SYSDBA and install with new user
112 $status = $this->submitWebUserBox();
113 if ( !$status->isOK() ) {
114 return $status;
116 $status = $this->openSYSDBAConnection();
117 if ( !$status->isOK() ) {
118 return $status;
120 if ( !$this->getVar( '_CreateDBAccount' ) ) {
121 $status->fatal( 'config-db-sys-create-oracle' );
123 } else {
124 return $status;
126 } else {
127 // check for web user credentials
128 // Scenario 3: Install with a priviliged user but use a restricted user
129 $statusIS3 = $this->submitWebUserBox();
130 if ( !$statusIS3->isOK() ) {
131 return $statusIS3;
136 * @var $conn DatabaseBase
138 $conn = $status->value;
140 // Check version
141 $version = $conn->getServerVersion();
142 if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
143 return Status::newFatal( 'config-oracle-old', $this->minimumVersion, $version );
146 return $status;
149 public function openConnection() {
150 $status = Status::newGood();
151 try {
152 $db = new DatabaseOracle(
153 $this->getVar( 'wgDBserver' ),
154 $this->getVar( '_InstallUser' ),
155 $this->getVar( '_InstallPassword' ),
156 $this->getVar( '_InstallDBname' ),
158 $this->getVar( 'wgDBprefix' )
160 $status->value = $db;
161 } catch ( DBConnectionError $e ) {
162 $this->connError = $e->db->lastErrno();
163 $status->fatal( 'config-connection-error', $e->getMessage() );
165 return $status;
168 public function openSYSDBAConnection() {
169 $status = Status::newGood();
170 try {
171 $db = new DatabaseOracle(
172 $this->getVar( 'wgDBserver' ),
173 $this->getVar( '_InstallUser' ),
174 $this->getVar( '_InstallPassword' ),
175 $this->getVar( '_InstallDBname' ),
176 DBO_SYSDBA,
177 $this->getVar( 'wgDBprefix' )
179 $status->value = $db;
180 } catch ( DBConnectionError $e ) {
181 $this->connError = $e->db->lastErrno();
182 $status->fatal( 'config-connection-error', $e->getMessage() );
184 return $status;
187 public function needsUpgrade() {
188 $tempDBname = $this->getVar( 'wgDBname' );
189 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
190 $retVal = parent::needsUpgrade();
191 $this->parent->setVar( 'wgDBname', $tempDBname );
192 return $retVal;
195 public function preInstall() {
196 # Add our user callback to installSteps, right before the tables are created.
197 $callback = array(
198 'name' => 'user',
199 'callback' => array( $this, 'setupUser' )
201 $this->parent->addInstallStep( $callback, 'database' );
204 public function setupDatabase() {
205 $status = Status::newGood();
206 return $status;
209 public function setupUser() {
210 global $IP;
212 if ( !$this->getVar( '_CreateDBAccount' ) ) {
213 return Status::newGood();
216 // normaly only SYSDBA users can create accounts
217 $status = $this->openSYSDBAConnection();
218 if ( !$status->isOK() ) {
219 if ( $this->connError == 1031 ) {
220 // insufficient privileges (looks like a normal user)
221 $status = $this->openConnection();
222 if ( !$status->isOK() ) {
223 return $status;
225 } else {
226 return $status;
229 $this->db = $status->value;
230 $this->setupSchemaVars();
232 if ( !$this->db->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
233 $this->db->setFlag( DBO_DDLMODE );
234 $error = $this->db->sourceFile( "$IP/maintenance/oracle/user.sql" );
235 if ( $error !== true || !$this->db->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
236 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
238 } elseif ( $this->db->getFlag( DBO_SYSDBA ) ) {
239 $status->fatal( 'config-db-sys-user-exists-oracle', $this->getVar( 'wgDBuser' ) );
242 if ( $status->isOK() ) {
243 // user created or already existing, switching back to a normal connection
244 // as the new user has all needed privileges to setup the rest of the schema
245 // i will be using that user as _InstallUser from this point on
246 $this->db->close();
247 $this->db = false;
248 $this->parent->setVar( '_InstallUser', $this->getVar( 'wgDBuser' ) );
249 $this->parent->setVar( '_InstallPassword', $this->getVar( 'wgDBpassword' ) );
250 $this->parent->setVar( '_InstallDBname', $this->getVar( 'wgDBuser' ) );
251 $status = $this->getConnection();
254 return $status;
258 * Overload: after this action field info table has to be rebuilt
259 * @return Status
261 public function createTables() {
262 $this->setupSchemaVars();
263 $this->db->setFlag( DBO_DDLMODE );
264 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
265 $status = parent::createTables();
266 $this->db->clearFlag( DBO_DDLMODE );
268 $this->db->query( 'BEGIN fill_wiki_info; END;' );
270 return $status;
273 public function getSchemaVars() {
274 $varNames = array(
275 # These variables are used by maintenance/oracle/user.sql
276 '_OracleDefTS',
277 '_OracleTempTS',
278 'wgDBuser',
279 'wgDBpassword',
281 # These are used by tables.sql
282 'wgDBprefix',
284 $vars = array();
285 foreach ( $varNames as $name ) {
286 $vars[$name] = $this->getVar( $name );
288 return $vars;
291 public function getLocalSettings() {
292 $prefix = $this->getVar( 'wgDBprefix' );
293 return
294 "# Oracle specific settings
295 \$wgDBprefix = \"{$prefix}\";
300 * Function checks the format of Oracle connect string
301 * The actual validity of the string is checked by attempting to connect
303 * Regex should be able to validate all connect string formats
304 * [//](host|tns_name)[:port][/service_name][:POOLED]
305 * http://www.orafaq.com/wiki/EZCONNECT
307 * @since 1.22
309 * @param string $connect_string
311 * @return bool Whether the connection string is valid.
313 public static function checkConnectStringFormat( $connect_string ) {
314 $isValid = preg_match( '/^[[:alpha:]][\w\-]*(?:\.[[:alpha:]][\w\-]*){0,2}$/', $connect_string ); // TNS name
315 $isValid |= preg_match( '/^(?:\/\/)?[\w\-\.]+(?::[\d]+)?(?:\/(?:[\w\-\.]+(?::(pooled|dedicated|shared))?)?(?:\/[\w\-\.]+)?)?$/', $connect_string ); // EZConnect
316 return (bool)$isValid;