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
24 use Wikimedia\Rdbms\Database
;
25 use Wikimedia\Rdbms\DBConnectionError
;
28 * Class for setting up the MediaWiki database using Oracle.
33 class OracleInstaller
extends DatabaseInstaller
{
35 protected $globalNames = [
43 protected $internalDefaults = [
44 '_OracleDefTS' => 'USERS',
45 '_OracleTempTS' => 'TEMP',
46 '_InstallUser' => 'SYSTEM',
49 public static $minimumVersion = '9.0.1'; // 9iR1
50 protected static $notMiniumumVerisonMessage = 'config-oracle-old';
52 protected $connError = null;
54 public function getName() {
58 public function isCompiled() {
59 return self
::checkExtension( 'oci8' );
62 public function getConnectForm() {
63 if ( $this->getVar( 'wgDBserver' ) == 'localhost' ) {
64 $this->parent
->setVar( 'wgDBserver', '' );
67 return $this->getTextBox(
69 'config-db-host-oracle',
71 $this->parent
->getHelpBox( 'config-db-host-oracle-help' )
73 Html
::openElement( 'fieldset' ) .
74 Html
::element( 'legend', [], wfMessage( 'config-db-wiki-settings' )->text() ) .
75 $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
76 $this->getTextBox( '_OracleDefTS', 'config-oracle-def-ts' ) .
79 'config-oracle-temp-ts',
81 $this->parent
->getHelpBox( 'config-db-oracle-help' )
83 Html
::closeElement( 'fieldset' ) .
84 $this->parent
->getWarningBox( wfMessage( 'config-db-account-oracle-warn' )->text() ) .
85 $this->getInstallUserBox() .
86 $this->getWebUserBox();
89 public function submitInstallUserBox() {
90 parent
::submitInstallUserBox();
91 $this->parent
->setVar( '_InstallDBname', $this->getVar( '_InstallUser' ) );
93 return Status
::newGood();
96 public function submitConnectForm() {
97 // Get variables from the request
98 $newValues = $this->setVarsFromRequest( [
104 $this->parent
->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
107 $status = Status
::newGood();
108 if ( !strlen( $newValues['wgDBserver'] ) ) {
109 $status->fatal( 'config-missing-db-server-oracle' );
110 } elseif ( !self
::checkConnectStringFormat( $newValues['wgDBserver'] ) ) {
111 $status->fatal( 'config-invalid-db-server-oracle', $newValues['wgDBserver'] );
113 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
114 $status->fatal( 'config-invalid-schema', $newValues['wgDBprefix'] );
116 if ( !$status->isOK() ) {
121 $status = $this->submitInstallUserBox();
122 if ( !$status->isOK() ) {
126 // Try to connect trough multiple scenarios
127 // Scenario 1: Install with a manually created account
128 $status = $this->getConnection();
129 if ( !$status->isOK() ) {
130 if ( $this->connError
== 28009 ) {
131 // _InstallUser seems to be a SYSDBA
132 // Scenario 2: Create user with SYSDBA and install with new user
133 $status = $this->submitWebUserBox();
134 if ( !$status->isOK() ) {
137 $status = $this->openSYSDBAConnection();
138 if ( !$status->isOK() ) {
141 if ( !$this->getVar( '_CreateDBAccount' ) ) {
142 $status->fatal( 'config-db-sys-create-oracle' );
148 // check for web user credentials
149 // Scenario 3: Install with a priviliged user but use a restricted user
150 $statusIS3 = $this->submitWebUserBox();
151 if ( !$statusIS3->isOK() ) {
157 * @var Database $conn
159 $conn = $status->value
;
162 $status->merge( static::meetsMinimumRequirement( $conn->getServerVersion() ) );
167 public function openConnection() {
168 $status = Status
::newGood();
170 $db = new DatabaseOracle(
171 $this->getVar( 'wgDBserver' ),
172 $this->getVar( '_InstallUser' ),
173 $this->getVar( '_InstallPassword' ),
174 $this->getVar( '_InstallDBname' ),
176 $this->getVar( 'wgDBprefix' )
178 $status->value
= $db;
179 } catch ( DBConnectionError
$e ) {
180 $this->connError
= $e->db
->lastErrno();
181 $status->fatal( 'config-connection-error', $e->getMessage() );
187 public function openSYSDBAConnection() {
188 $status = Status
::newGood();
190 $db = new DatabaseOracle(
191 $this->getVar( 'wgDBserver' ),
192 $this->getVar( '_InstallUser' ),
193 $this->getVar( '_InstallPassword' ),
194 $this->getVar( '_InstallDBname' ),
196 $this->getVar( 'wgDBprefix' )
198 $status->value
= $db;
199 } catch ( DBConnectionError
$e ) {
200 $this->connError
= $e->db
->lastErrno();
201 $status->fatal( 'config-connection-error', $e->getMessage() );
207 public function needsUpgrade() {
208 $tempDBname = $this->getVar( 'wgDBname' );
209 $this->parent
->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
210 $retVal = parent
::needsUpgrade();
211 $this->parent
->setVar( 'wgDBname', $tempDBname );
216 public function preInstall() {
217 # Add our user callback to installSteps, right before the tables are created.
220 'callback' => [ $this, 'setupUser' ]
222 $this->parent
->addInstallStep( $callback, 'database' );
225 public function setupDatabase() {
226 $status = Status
::newGood();
231 public function setupUser() {
234 if ( !$this->getVar( '_CreateDBAccount' ) ) {
235 return Status
::newGood();
238 // normaly only SYSDBA users can create accounts
239 $status = $this->openSYSDBAConnection();
240 if ( !$status->isOK() ) {
241 if ( $this->connError
== 1031 ) {
242 // insufficient privileges (looks like a normal user)
243 $status = $this->openConnection();
244 if ( !$status->isOK() ) {
252 $this->db
= $status->value
;
253 $this->setupSchemaVars();
255 if ( !$this->db
->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
256 $this->db
->setFlag( DBO_DDLMODE
);
257 $error = $this->db
->sourceFile( "$IP/maintenance/oracle/user.sql" );
258 if ( $error !== true ||
!$this->db
->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
259 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
261 } elseif ( $this->db
->getFlag( DBO_SYSDBA
) ) {
262 $status->fatal( 'config-db-sys-user-exists-oracle', $this->getVar( 'wgDBuser' ) );
265 if ( $status->isOK() ) {
266 // user created or already existing, switching back to a normal connection
267 // as the new user has all needed privileges to setup the rest of the schema
268 // i will be using that user as _InstallUser from this point on
271 $this->parent
->setVar( '_InstallUser', $this->getVar( 'wgDBuser' ) );
272 $this->parent
->setVar( '_InstallPassword', $this->getVar( 'wgDBpassword' ) );
273 $this->parent
->setVar( '_InstallDBname', $this->getVar( 'wgDBuser' ) );
274 $status = $this->getConnection();
281 * Overload: after this action field info table has to be rebuilt
284 public function createTables() {
285 $this->setupSchemaVars();
286 $this->db
->setFlag( DBO_DDLMODE
);
287 $this->parent
->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
288 $status = parent
::createTables();
289 $this->db
->clearFlag( DBO_DDLMODE
);
291 $this->db
->query( 'BEGIN fill_wiki_info; END;' );
296 public function getSchemaVars() {
298 # These variables are used by maintenance/oracle/user.sql
304 # These are used by tables.sql
308 foreach ( $varNames as $name ) {
309 $vars[$name] = $this->getVar( $name );
315 public function getLocalSettings() {
316 $prefix = $this->getVar( 'wgDBprefix' );
318 return "# Oracle specific settings
319 \$wgDBprefix = \"{$prefix}\";
324 * Function checks the format of Oracle connect string
325 * The actual validity of the string is checked by attempting to connect
327 * Regex should be able to validate all connect string formats
328 * [//](host|tns_name)[:port][/service_name][:POOLED]
329 * http://www.orafaq.com/wiki/EZCONNECT
333 * @param string $connect_string
335 * @return bool Whether the connection string is valid.
337 public static function checkConnectStringFormat( $connect_string ) {
338 // @@codingStandardsIgnoreStart Long lines with regular expressions.
339 // @todo Very long regular expression. Make more readable?
340 $isValid = preg_match( '/^[[:alpha:]][\w\-]*(?:\.[[:alpha:]][\w\-]*){0,2}$/', $connect_string ); // TNS name
341 $isValid |
= preg_match( '/^(?:\/\/)?[\w\-\.]+(?::[\d]+)?(?:\/(?:[\w\-\.]+(?::(pooled|dedicated|shared))?)?(?:\/[\w\-\.]+)?)?$/', $connect_string ); // EZConnect
342 // @@codingStandardsIgnoreEnd
343 return (bool)$isValid;