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
25 * Class for setting up the MediaWiki database using Oracle.
30 class OracleInstaller
extends DatabaseInstaller
{
32 protected $globalNames = array(
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() {
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' ) );
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() ) {
100 $status = $this->submitInstallUserBox();
101 if ( !$status->isOK() ) {
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() ) {
116 $status = $this->openSYSDBAConnection();
117 if ( !$status->isOK() ) {
120 if ( !$this->getVar( '_CreateDBAccount' ) ) {
121 $status->fatal( 'config-db-sys-create-oracle' );
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() ) {
136 * @var $conn DatabaseBase
138 $conn = $status->value
;
141 $version = $conn->getServerVersion();
142 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
143 return Status
::newFatal( 'config-oracle-old', $this->minimumVersion
, $version );
149 public function openConnection() {
150 $status = Status
::newGood();
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() );
168 public function openSYSDBAConnection() {
169 $status = Status
::newGood();
171 $db = new DatabaseOracle(
172 $this->getVar( 'wgDBserver' ),
173 $this->getVar( '_InstallUser' ),
174 $this->getVar( '_InstallPassword' ),
175 $this->getVar( '_InstallDBname' ),
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() );
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 );
195 public function preInstall() {
196 # Add our user callback to installSteps, right before the tables are created.
199 'callback' => array( $this, 'setupUser' )
201 $this->parent
->addInstallStep( $callback, 'database' );
204 public function setupDatabase() {
205 $status = Status
::newGood();
209 public function setupUser() {
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() ) {
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
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();
258 * Overload: after this action field info table has to be rebuilt
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;' );
273 public function getSchemaVars() {
275 # These variables are used by maintenance/oracle/user.sql
281 # These are used by tables.sql
285 foreach ( $varNames as $name ) {
286 $vars[$name] = $this->getVar( $name );
291 public function getLocalSettings() {
292 $prefix = $this->getVar( 'wgDBprefix' );
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
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;