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 = [
40 protected $internalDefaults = [
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(
65 'config-db-host-oracle',
67 $this->parent
->getHelpBox( 'config-db-host-oracle-help' )
69 Html
::openElement( 'fieldset' ) .
70 Html
::element( 'legend', [], wfMessage( 'config-db-wiki-settings' )->text() ) .
71 $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
72 $this->getTextBox( '_OracleDefTS', 'config-oracle-def-ts' ) .
75 'config-oracle-temp-ts',
77 $this->parent
->getHelpBox( 'config-db-oracle-help' )
79 Html
::closeElement( 'fieldset' ) .
80 $this->parent
->getWarningBox( wfMessage( 'config-db-account-oracle-warn' )->text() ) .
81 $this->getInstallUserBox() .
82 $this->getWebUserBox();
85 public function submitInstallUserBox() {
86 parent
::submitInstallUserBox();
87 $this->parent
->setVar( '_InstallDBname', $this->getVar( '_InstallUser' ) );
89 return Status
::newGood();
92 public function submitConnectForm() {
93 // Get variables from the request
94 $newValues = $this->setVarsFromRequest( [
100 $this->parent
->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
103 $status = Status
::newGood();
104 if ( !strlen( $newValues['wgDBserver'] ) ) {
105 $status->fatal( 'config-missing-db-server-oracle' );
106 } elseif ( !self
::checkConnectStringFormat( $newValues['wgDBserver'] ) ) {
107 $status->fatal( 'config-invalid-db-server-oracle', $newValues['wgDBserver'] );
109 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
110 $status->fatal( 'config-invalid-schema', $newValues['wgDBprefix'] );
112 if ( !$status->isOK() ) {
117 $status = $this->submitInstallUserBox();
118 if ( !$status->isOK() ) {
122 // Try to connect trough multiple scenarios
123 // Scenario 1: Install with a manually created account
124 $status = $this->getConnection();
125 if ( !$status->isOK() ) {
126 if ( $this->connError
== 28009 ) {
127 // _InstallUser seems to be a SYSDBA
128 // Scenario 2: Create user with SYSDBA and install with new user
129 $status = $this->submitWebUserBox();
130 if ( !$status->isOK() ) {
133 $status = $this->openSYSDBAConnection();
134 if ( !$status->isOK() ) {
137 if ( !$this->getVar( '_CreateDBAccount' ) ) {
138 $status->fatal( 'config-db-sys-create-oracle' );
144 // check for web user credentials
145 // Scenario 3: Install with a priviliged user but use a restricted user
146 $statusIS3 = $this->submitWebUserBox();
147 if ( !$statusIS3->isOK() ) {
153 * @var $conn DatabaseBase
155 $conn = $status->value
;
158 $version = $conn->getServerVersion();
159 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
160 return Status
::newFatal( 'config-oracle-old', $this->minimumVersion
, $version );
166 public function openConnection() {
167 $status = Status
::newGood();
169 $db = new DatabaseOracle(
170 $this->getVar( 'wgDBserver' ),
171 $this->getVar( '_InstallUser' ),
172 $this->getVar( '_InstallPassword' ),
173 $this->getVar( '_InstallDBname' ),
175 $this->getVar( 'wgDBprefix' )
177 $status->value
= $db;
178 } catch ( DBConnectionError
$e ) {
179 $this->connError
= $e->db
->lastErrno();
180 $status->fatal( 'config-connection-error', $e->getMessage() );
186 public function openSYSDBAConnection() {
187 $status = Status
::newGood();
189 $db = new DatabaseOracle(
190 $this->getVar( 'wgDBserver' ),
191 $this->getVar( '_InstallUser' ),
192 $this->getVar( '_InstallPassword' ),
193 $this->getVar( '_InstallDBname' ),
195 $this->getVar( 'wgDBprefix' )
197 $status->value
= $db;
198 } catch ( DBConnectionError
$e ) {
199 $this->connError
= $e->db
->lastErrno();
200 $status->fatal( 'config-connection-error', $e->getMessage() );
206 public function needsUpgrade() {
207 $tempDBname = $this->getVar( 'wgDBname' );
208 $this->parent
->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
209 $retVal = parent
::needsUpgrade();
210 $this->parent
->setVar( 'wgDBname', $tempDBname );
215 public function preInstall() {
216 # Add our user callback to installSteps, right before the tables are created.
219 'callback' => [ $this, 'setupUser' ]
221 $this->parent
->addInstallStep( $callback, 'database' );
224 public function setupDatabase() {
225 $status = Status
::newGood();
230 public function setupUser() {
233 if ( !$this->getVar( '_CreateDBAccount' ) ) {
234 return Status
::newGood();
237 // normaly only SYSDBA users can create accounts
238 $status = $this->openSYSDBAConnection();
239 if ( !$status->isOK() ) {
240 if ( $this->connError
== 1031 ) {
241 // insufficient privileges (looks like a normal user)
242 $status = $this->openConnection();
243 if ( !$status->isOK() ) {
251 $this->db
= $status->value
;
252 $this->setupSchemaVars();
254 if ( !$this->db
->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
255 $this->db
->setFlag( DBO_DDLMODE
);
256 $error = $this->db
->sourceFile( "$IP/maintenance/oracle/user.sql" );
257 if ( $error !== true ||
!$this->db
->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
258 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
260 } elseif ( $this->db
->getFlag( DBO_SYSDBA
) ) {
261 $status->fatal( 'config-db-sys-user-exists-oracle', $this->getVar( 'wgDBuser' ) );
264 if ( $status->isOK() ) {
265 // user created or already existing, switching back to a normal connection
266 // as the new user has all needed privileges to setup the rest of the schema
267 // i will be using that user as _InstallUser from this point on
270 $this->parent
->setVar( '_InstallUser', $this->getVar( 'wgDBuser' ) );
271 $this->parent
->setVar( '_InstallPassword', $this->getVar( 'wgDBpassword' ) );
272 $this->parent
->setVar( '_InstallDBname', $this->getVar( 'wgDBuser' ) );
273 $status = $this->getConnection();
280 * Overload: after this action field info table has to be rebuilt
283 public function createTables() {
284 $this->setupSchemaVars();
285 $this->db
->setFlag( DBO_DDLMODE
);
286 $this->parent
->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
287 $status = parent
::createTables();
288 $this->db
->clearFlag( DBO_DDLMODE
);
290 $this->db
->query( 'BEGIN fill_wiki_info; END;' );
295 public function getSchemaVars() {
297 # These variables are used by maintenance/oracle/user.sql
303 # These are used by tables.sql
307 foreach ( $varNames as $name ) {
308 $vars[$name] = $this->getVar( $name );
314 public function getLocalSettings() {
315 $prefix = $this->getVar( 'wgDBprefix' );
317 return "# Oracle specific settings
318 \$wgDBprefix = \"{$prefix}\";
323 * Function checks the format of Oracle connect string
324 * The actual validity of the string is checked by attempting to connect
326 * Regex should be able to validate all connect string formats
327 * [//](host|tns_name)[:port][/service_name][:POOLED]
328 * http://www.orafaq.com/wiki/EZCONNECT
332 * @param string $connect_string
334 * @return bool Whether the connection string is valid.
336 public static function checkConnectStringFormat( $connect_string ) {
337 // @@codingStandardsIgnoreStart Long lines with regular expressions.
338 // @todo Very long regular expression. Make more readable?
339 $isValid = preg_match( '/^[[:alpha:]][\w\-]*(?:\.[[:alpha:]][\w\-]*){0,2}$/', $connect_string ); // TNS name
340 $isValid |
= preg_match( '/^(?:\/\/)?[\w\-\.]+(?::[\d]+)?(?:\/(?:[\w\-\.]+(?::(pooled|dedicated|shared))?)?(?:\/[\w\-\.]+)?)?$/', $connect_string ); // EZConnect
341 // @@codingStandardsIgnoreEnd
342 return (bool)$isValid;