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 return $this->doOpenConnection();
171 public function openSYSDBAConnection() {
172 return $this->doOpenConnection( DatabaseOracle
::DBO_SYSDBA
);
177 * @return Status Status with DatabaseOracle or null as the value
179 private function doOpenConnection( $flags = 0 ) {
180 $status = Status
::newGood();
182 $db = Database
::factory(
185 'host' => $this->getVar( 'wgDBserver' ),
186 'user' => $this->getVar( '_InstallUser' ),
187 'password' => $this->getVar( '_InstallPassword' ),
188 'dbname' => $this->getVar( '_InstallDBname' ),
189 'tablePrefix' => $this->getVar( 'wgDBprefix' ),
193 $status->value
= $db;
194 } catch ( DBConnectionError
$e ) {
195 $this->connError
= $e->db
->lastErrno();
196 $status->fatal( 'config-connection-error', $e->getMessage() );
202 public function needsUpgrade() {
203 $tempDBname = $this->getVar( 'wgDBname' );
204 $this->parent
->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
205 $retVal = parent
::needsUpgrade();
206 $this->parent
->setVar( 'wgDBname', $tempDBname );
211 public function preInstall() {
212 # Add our user callback to installSteps, right before the tables are created.
215 'callback' => [ $this, 'setupUser' ]
217 $this->parent
->addInstallStep( $callback, 'database' );
220 public function setupDatabase() {
221 $status = Status
::newGood();
226 public function setupUser() {
229 if ( !$this->getVar( '_CreateDBAccount' ) ) {
230 return Status
::newGood();
233 // normaly only SYSDBA users can create accounts
234 $status = $this->openSYSDBAConnection();
235 if ( !$status->isOK() ) {
236 if ( $this->connError
== 1031 ) {
237 // insufficient privileges (looks like a normal user)
238 $status = $this->openConnection();
239 if ( !$status->isOK() ) {
247 $this->db
= $status->value
;
248 $this->setupSchemaVars();
250 if ( !$this->db
->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
251 $this->db
->setFlag( DBO_DDLMODE
);
252 $error = $this->db
->sourceFile( "$IP/maintenance/oracle/user.sql" );
253 if ( $error !== true ||
!$this->db
->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
254 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
256 } elseif ( $this->db
->getFlag( DBO_SYSDBA
) ) {
257 $status->fatal( 'config-db-sys-user-exists-oracle', $this->getVar( 'wgDBuser' ) );
260 if ( $status->isOK() ) {
261 // user created or already existing, switching back to a normal connection
262 // as the new user has all needed privileges to setup the rest of the schema
263 // i will be using that user as _InstallUser from this point on
266 $this->parent
->setVar( '_InstallUser', $this->getVar( 'wgDBuser' ) );
267 $this->parent
->setVar( '_InstallPassword', $this->getVar( 'wgDBpassword' ) );
268 $this->parent
->setVar( '_InstallDBname', $this->getVar( 'wgDBuser' ) );
269 $status = $this->getConnection();
276 * Overload: after this action field info table has to be rebuilt
279 public function createTables() {
280 $this->setupSchemaVars();
281 $this->db
->setFlag( DBO_DDLMODE
);
282 $this->parent
->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
283 $status = parent
::createTables();
284 $this->db
->clearFlag( DBO_DDLMODE
);
286 $this->db
->query( 'BEGIN fill_wiki_info; END;' );
291 public function getSchemaVars() {
293 # These variables are used by maintenance/oracle/user.sql
299 # These are used by tables.sql
303 foreach ( $varNames as $name ) {
304 $vars[$name] = $this->getVar( $name );
310 public function getLocalSettings() {
311 $prefix = $this->getVar( 'wgDBprefix' );
313 return "# Oracle specific settings
314 \$wgDBprefix = \"{$prefix}\";
319 * Function checks the format of Oracle connect string
320 * The actual validity of the string is checked by attempting to connect
322 * Regex should be able to validate all connect string formats
323 * [//](host|tns_name)[:port][/service_name][:POOLED]
324 * http://www.orafaq.com/wiki/EZCONNECT
328 * @param string $connect_string
330 * @return bool Whether the connection string is valid.
332 public static function checkConnectStringFormat( $connect_string ) {
333 // phpcs:disable Generic.Files.LineLength
334 // @todo Very long regular expression. Make more readable?
335 $isValid = preg_match( '/^[[:alpha:]][\w\-]*(?:\.[[:alpha:]][\w\-]*){0,2}$/', $connect_string ); // TNS name
336 $isValid |
= preg_match( '/^(?:\/\/)?[\w\-\.]+(?::[\d]+)?(?:\/(?:[\w\-\.]+(?::(pooled|dedicated|shared))?)?(?:\/[\w\-\.]+)?)?$/', $connect_string ); // EZConnect
338 return (bool)$isValid;