Also redirect if prefixed https cookie is preset
[lhc/web/wiklou.git] / includes / installer / OracleInstaller.php
1 <?php
2 /**
3 * Oracle-specific installer.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Deployment
22 */
23
24 /**
25 * Class for setting up the MediaWiki database using Oracle.
26 *
27 * @ingroup Deployment
28 * @since 1.17
29 */
30 class OracleInstaller extends DatabaseInstaller {
31
32 protected $globalNames = array(
33 'wgDBserver',
34 'wgDBname',
35 'wgDBuser',
36 'wgDBpassword',
37 'wgDBprefix',
38 );
39
40 protected $internalDefaults = array(
41 '_OracleDefTS' => 'USERS',
42 '_OracleTempTS' => 'TEMP',
43 '_InstallUser' => 'SYSTEM',
44 );
45
46 public $minimumVersion = '9.0.1'; // 9iR1
47
48 protected $connError = null;
49
50 public function getName() {
51 return 'oracle';
52 }
53
54 public function isCompiled() {
55 return self::checkExtension( 'oci8' );
56 }
57
58 public function getConnectForm() {
59 if ( $this->getVar( 'wgDBserver' ) == 'localhost' ) {
60 $this->parent->setVar( 'wgDBserver', '' );
61 }
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();
72 }
73
74 public function submitInstallUserBox() {
75 parent::submitInstallUserBox();
76 $this->parent->setVar( '_InstallDBname', $this->getVar( '_InstallUser' ) );
77 return Status::newGood();
78 }
79
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' ) );
84
85 // Validate them
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'] );
91 }
92 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
93 $status->fatal( 'config-invalid-schema', $newValues['wgDBprefix'] );
94 }
95 if ( !$status->isOK() ) {
96 return $status;
97 }
98
99 // Submit user box
100 $status = $this->submitInstallUserBox();
101 if ( !$status->isOK() ) {
102 return $status;
103 }
104
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() ) {
114 return $status;
115 }
116 $status = $this->openSYSDBAConnection();
117 if ( !$status->isOK() ) {
118 return $status;
119 }
120 if ( !$this->getVar( '_CreateDBAccount' ) ) {
121 $status->fatal( 'config-db-sys-create-oracle' );
122 }
123 } else {
124 return $status;
125 }
126 } else {
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() ) {
131 return $statusIS3;
132 }
133 }
134
135 /**
136 * @var $conn DatabaseBase
137 */
138 $conn = $status->value;
139
140 // Check version
141 $version = $conn->getServerVersion();
142 if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
143 return Status::newFatal( 'config-oracle-old', $this->minimumVersion, $version );
144 }
145
146 return $status;
147 }
148
149 public function openConnection() {
150 $status = Status::newGood();
151 try {
152 $db = new DatabaseOracle(
153 $this->getVar( 'wgDBserver' ),
154 $this->getVar( '_InstallUser' ),
155 $this->getVar( '_InstallPassword' ),
156 $this->getVar( '_InstallDBname' ),
157 0,
158 $this->getVar( 'wgDBprefix' )
159 );
160 $status->value = $db;
161 } catch ( DBConnectionError $e ) {
162 $this->connError = $e->db->lastErrno();
163 $status->fatal( 'config-connection-error', $e->getMessage() );
164 }
165 return $status;
166 }
167
168 public function openSYSDBAConnection() {
169 $status = Status::newGood();
170 try {
171 $db = new DatabaseOracle(
172 $this->getVar( 'wgDBserver' ),
173 $this->getVar( '_InstallUser' ),
174 $this->getVar( '_InstallPassword' ),
175 $this->getVar( '_InstallDBname' ),
176 DBO_SYSDBA,
177 $this->getVar( 'wgDBprefix' )
178 );
179 $status->value = $db;
180 } catch ( DBConnectionError $e ) {
181 $this->connError = $e->db->lastErrno();
182 $status->fatal( 'config-connection-error', $e->getMessage() );
183 }
184 return $status;
185 }
186
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 );
192 return $retVal;
193 }
194
195 public function preInstall() {
196 # Add our user callback to installSteps, right before the tables are created.
197 $callback = array(
198 'name' => 'user',
199 'callback' => array( $this, 'setupUser' )
200 );
201 $this->parent->addInstallStep( $callback, 'database' );
202 }
203
204 public function setupDatabase() {
205 $status = Status::newGood();
206 return $status;
207 }
208
209 public function setupUser() {
210 global $IP;
211
212 if ( !$this->getVar( '_CreateDBAccount' ) ) {
213 return Status::newGood();
214 }
215
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() ) {
223 return $status;
224 }
225 } else {
226 return $status;
227 }
228 }
229 $this->db = $status->value;
230 $this->setupSchemaVars();
231
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 );
237 }
238 } elseif ( $this->db->getFlag( DBO_SYSDBA ) ) {
239 $status->fatal( 'config-db-sys-user-exists-oracle', $this->getVar( 'wgDBuser' ) );
240 }
241
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
246 $this->db->close();
247 $this->db = false;
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();
252 }
253
254 return $status;
255 }
256
257 /**
258 * Overload: after this action field info table has to be rebuilt
259 * @return Status
260 */
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 );
267
268 $this->db->query( 'BEGIN fill_wiki_info; END;' );
269
270 return $status;
271 }
272
273 public function getSchemaVars() {
274 $varNames = array(
275 # These variables are used by maintenance/oracle/user.sql
276 '_OracleDefTS',
277 '_OracleTempTS',
278 'wgDBuser',
279 'wgDBpassword',
280
281 # These are used by tables.sql
282 'wgDBprefix',
283 );
284 $vars = array();
285 foreach ( $varNames as $name ) {
286 $vars[$name] = $this->getVar( $name );
287 }
288 return $vars;
289 }
290
291 public function getLocalSettings() {
292 $prefix = $this->getVar( 'wgDBprefix' );
293 return
294 "# Oracle specific settings
295 \$wgDBprefix = \"{$prefix}\";
296 ";
297 }
298
299 /**
300 * Function checks the format of Oracle connect string
301 * The actual validity of the string is checked by attempting to connect
302 *
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
306 *
307 * @since 1.22
308 *
309 * @param string $connect_string
310 *
311 * @return bool Whether the connection string is valid.
312 */
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;
317 }
318
319 }