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', '' );
63 return $this->getTextBox( 'wgDBserver', 'config-db-host-oracle', array(), $this->parent
->getHelpBox( 'config-db-host-oracle-help' ) ) .
64 Html
::openElement( 'fieldset' ) .
65 Html
::element( 'legend', array(), wfMessage( 'config-db-wiki-settings' )->text() ) .
66 $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
67 $this->getTextBox( '_OracleDefTS', 'config-oracle-def-ts' ) .
68 $this->getTextBox( '_OracleTempTS', 'config-oracle-temp-ts', array(), $this->parent
->getHelpBox( 'config-db-oracle-help' ) ) .
69 Html
::closeElement( 'fieldset' ) .
70 $this->parent
->getWarningBox( wfMessage( 'config-db-account-oracle-warn' )->text() ) .
71 $this->getInstallUserBox() .
72 $this->getWebUserBox();
75 public function submitInstallUserBox() {
76 parent
::submitInstallUserBox();
77 $this->parent
->setVar( '_InstallDBname', $this->getVar( '_InstallUser' ) );
79 return Status
::newGood();
82 public function submitConnectForm() {
83 // Get variables from the request
84 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBprefix', 'wgDBuser', 'wgDBpassword' ) );
85 $this->parent
->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
88 $status = Status
::newGood();
89 if ( !strlen( $newValues['wgDBserver'] ) ) {
90 $status->fatal( 'config-missing-db-server-oracle' );
91 } elseif ( !self
::checkConnectStringFormat( $newValues['wgDBserver'] ) ) {
92 $status->fatal( 'config-invalid-db-server-oracle', $newValues['wgDBserver'] );
94 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
95 $status->fatal( 'config-invalid-schema', $newValues['wgDBprefix'] );
97 if ( !$status->isOK() ) {
102 $status = $this->submitInstallUserBox();
103 if ( !$status->isOK() ) {
107 // Try to connect trough multiple scenarios
108 // Scenario 1: Install with a manually created account
109 $status = $this->getConnection();
110 if ( !$status->isOK() ) {
111 if ( $this->connError
== 28009 ) {
112 // _InstallUser seems to be a SYSDBA
113 // Scenario 2: Create user with SYSDBA and install with new user
114 $status = $this->submitWebUserBox();
115 if ( !$status->isOK() ) {
118 $status = $this->openSYSDBAConnection();
119 if ( !$status->isOK() ) {
122 if ( !$this->getVar( '_CreateDBAccount' ) ) {
123 $status->fatal( 'config-db-sys-create-oracle' );
129 // check for web user credentials
130 // Scenario 3: Install with a priviliged user but use a restricted user
131 $statusIS3 = $this->submitWebUserBox();
132 if ( !$statusIS3->isOK() ) {
138 * @var $conn DatabaseBase
140 $conn = $status->value
;
143 $version = $conn->getServerVersion();
144 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
145 return Status
::newFatal( 'config-oracle-old', $this->minimumVersion
, $version );
151 public function openConnection() {
152 $status = Status
::newGood();
154 $db = new DatabaseOracle(
155 $this->getVar( 'wgDBserver' ),
156 $this->getVar( '_InstallUser' ),
157 $this->getVar( '_InstallPassword' ),
158 $this->getVar( '_InstallDBname' ),
160 $this->getVar( 'wgDBprefix' )
162 $status->value
= $db;
163 } catch ( DBConnectionError
$e ) {
164 $this->connError
= $e->db
->lastErrno();
165 $status->fatal( 'config-connection-error', $e->getMessage() );
171 public function openSYSDBAConnection() {
172 $status = Status
::newGood();
174 $db = new DatabaseOracle(
175 $this->getVar( 'wgDBserver' ),
176 $this->getVar( '_InstallUser' ),
177 $this->getVar( '_InstallPassword' ),
178 $this->getVar( '_InstallDBname' ),
180 $this->getVar( 'wgDBprefix' )
182 $status->value
= $db;
183 } catch ( DBConnectionError
$e ) {
184 $this->connError
= $e->db
->lastErrno();
185 $status->fatal( 'config-connection-error', $e->getMessage() );
191 public function needsUpgrade() {
192 $tempDBname = $this->getVar( 'wgDBname' );
193 $this->parent
->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
194 $retVal = parent
::needsUpgrade();
195 $this->parent
->setVar( 'wgDBname', $tempDBname );
200 public function preInstall() {
201 # Add our user callback to installSteps, right before the tables are created.
204 'callback' => array( $this, 'setupUser' )
206 $this->parent
->addInstallStep( $callback, 'database' );
209 public function setupDatabase() {
210 $status = Status
::newGood();
215 public function setupUser() {
218 if ( !$this->getVar( '_CreateDBAccount' ) ) {
219 return Status
::newGood();
222 // normaly only SYSDBA users can create accounts
223 $status = $this->openSYSDBAConnection();
224 if ( !$status->isOK() ) {
225 if ( $this->connError
== 1031 ) {
226 // insufficient privileges (looks like a normal user)
227 $status = $this->openConnection();
228 if ( !$status->isOK() ) {
235 $this->db
= $status->value
;
236 $this->setupSchemaVars();
238 if ( !$this->db
->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
239 $this->db
->setFlag( DBO_DDLMODE
);
240 $error = $this->db
->sourceFile( "$IP/maintenance/oracle/user.sql" );
241 if ( $error !== true ||
!$this->db
->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
242 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
244 } elseif ( $this->db
->getFlag( DBO_SYSDBA
) ) {
245 $status->fatal( 'config-db-sys-user-exists-oracle', $this->getVar( 'wgDBuser' ) );
248 if ( $status->isOK() ) {
249 // user created or already existing, switching back to a normal connection
250 // as the new user has all needed privileges to setup the rest of the schema
251 // i will be using that user as _InstallUser from this point on
254 $this->parent
->setVar( '_InstallUser', $this->getVar( 'wgDBuser' ) );
255 $this->parent
->setVar( '_InstallPassword', $this->getVar( 'wgDBpassword' ) );
256 $this->parent
->setVar( '_InstallDBname', $this->getVar( 'wgDBuser' ) );
257 $status = $this->getConnection();
264 * Overload: after this action field info table has to be rebuilt
267 public function createTables() {
268 $this->setupSchemaVars();
269 $this->db
->setFlag( DBO_DDLMODE
);
270 $this->parent
->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
271 $status = parent
::createTables();
272 $this->db
->clearFlag( DBO_DDLMODE
);
274 $this->db
->query( 'BEGIN fill_wiki_info; END;' );
279 public function getSchemaVars() {
281 # These variables are used by maintenance/oracle/user.sql
287 # These are used by tables.sql
291 foreach ( $varNames as $name ) {
292 $vars[$name] = $this->getVar( $name );
298 public function getLocalSettings() {
299 $prefix = $this->getVar( 'wgDBprefix' );
301 return "# Oracle specific settings
302 \$wgDBprefix = \"{$prefix}\";
307 * Function checks the format of Oracle connect string
308 * The actual validity of the string is checked by attempting to connect
310 * Regex should be able to validate all connect string formats
311 * [//](host|tns_name)[:port][/service_name][:POOLED]
312 * http://www.orafaq.com/wiki/EZCONNECT
316 * @param string $connect_string
318 * @return bool Whether the connection string is valid.
320 public static function checkConnectStringFormat( $connect_string ) {
321 $isValid = preg_match( '/^[[:alpha:]][\w\-]*(?:\.[[:alpha:]][\w\-]*){0,2}$/', $connect_string ); // TNS name
322 $isValid |
= preg_match( '/^(?:\/\/)?[\w\-\.]+(?::[\d]+)?(?:\/(?:[\w\-\.]+(?::(pooled|dedicated|shared))?)?(?:\/[\w\-\.]+)?)?$/', $connect_string ); // EZConnect
323 return (bool)$isValid;