Merge "Fix PHP CodeSniffer warnings and errors"
[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
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();
73 }
74
75 public function submitInstallUserBox() {
76 parent::submitInstallUserBox();
77 $this->parent->setVar( '_InstallDBname', $this->getVar( '_InstallUser' ) );
78
79 return Status::newGood();
80 }
81
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' ) );
86
87 // Validate them
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'] );
93 }
94 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
95 $status->fatal( 'config-invalid-schema', $newValues['wgDBprefix'] );
96 }
97 if ( !$status->isOK() ) {
98 return $status;
99 }
100
101 // Submit user box
102 $status = $this->submitInstallUserBox();
103 if ( !$status->isOK() ) {
104 return $status;
105 }
106
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() ) {
116 return $status;
117 }
118 $status = $this->openSYSDBAConnection();
119 if ( !$status->isOK() ) {
120 return $status;
121 }
122 if ( !$this->getVar( '_CreateDBAccount' ) ) {
123 $status->fatal( 'config-db-sys-create-oracle' );
124 }
125 } else {
126 return $status;
127 }
128 } else {
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() ) {
133 return $statusIS3;
134 }
135 }
136
137 /**
138 * @var $conn DatabaseBase
139 */
140 $conn = $status->value;
141
142 // Check version
143 $version = $conn->getServerVersion();
144 if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
145 return Status::newFatal( 'config-oracle-old', $this->minimumVersion, $version );
146 }
147
148 return $status;
149 }
150
151 public function openConnection() {
152 $status = Status::newGood();
153 try {
154 $db = new DatabaseOracle(
155 $this->getVar( 'wgDBserver' ),
156 $this->getVar( '_InstallUser' ),
157 $this->getVar( '_InstallPassword' ),
158 $this->getVar( '_InstallDBname' ),
159 0,
160 $this->getVar( 'wgDBprefix' )
161 );
162 $status->value = $db;
163 } catch ( DBConnectionError $e ) {
164 $this->connError = $e->db->lastErrno();
165 $status->fatal( 'config-connection-error', $e->getMessage() );
166 }
167
168 return $status;
169 }
170
171 public function openSYSDBAConnection() {
172 $status = Status::newGood();
173 try {
174 $db = new DatabaseOracle(
175 $this->getVar( 'wgDBserver' ),
176 $this->getVar( '_InstallUser' ),
177 $this->getVar( '_InstallPassword' ),
178 $this->getVar( '_InstallDBname' ),
179 DBO_SYSDBA,
180 $this->getVar( 'wgDBprefix' )
181 );
182 $status->value = $db;
183 } catch ( DBConnectionError $e ) {
184 $this->connError = $e->db->lastErrno();
185 $status->fatal( 'config-connection-error', $e->getMessage() );
186 }
187
188 return $status;
189 }
190
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 );
196
197 return $retVal;
198 }
199
200 public function preInstall() {
201 # Add our user callback to installSteps, right before the tables are created.
202 $callback = array(
203 'name' => 'user',
204 'callback' => array( $this, 'setupUser' )
205 );
206 $this->parent->addInstallStep( $callback, 'database' );
207 }
208
209 public function setupDatabase() {
210 $status = Status::newGood();
211
212 return $status;
213 }
214
215 public function setupUser() {
216 global $IP;
217
218 if ( !$this->getVar( '_CreateDBAccount' ) ) {
219 return Status::newGood();
220 }
221
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() ) {
229 return $status;
230 }
231 } else {
232 return $status;
233 }
234 }
235 $this->db = $status->value;
236 $this->setupSchemaVars();
237
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 );
243 }
244 } elseif ( $this->db->getFlag( DBO_SYSDBA ) ) {
245 $status->fatal( 'config-db-sys-user-exists-oracle', $this->getVar( 'wgDBuser' ) );
246 }
247
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
252 $this->db->close();
253 $this->db = false;
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();
258 }
259
260 return $status;
261 }
262
263 /**
264 * Overload: after this action field info table has to be rebuilt
265 * @return Status
266 */
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 );
273
274 $this->db->query( 'BEGIN fill_wiki_info; END;' );
275
276 return $status;
277 }
278
279 public function getSchemaVars() {
280 $varNames = array(
281 # These variables are used by maintenance/oracle/user.sql
282 '_OracleDefTS',
283 '_OracleTempTS',
284 'wgDBuser',
285 'wgDBpassword',
286
287 # These are used by tables.sql
288 'wgDBprefix',
289 );
290 $vars = array();
291 foreach ( $varNames as $name ) {
292 $vars[$name] = $this->getVar( $name );
293 }
294
295 return $vars;
296 }
297
298 public function getLocalSettings() {
299 $prefix = $this->getVar( 'wgDBprefix' );
300
301 return "# Oracle specific settings
302 \$wgDBprefix = \"{$prefix}\";
303 ";
304 }
305
306 /**
307 * Function checks the format of Oracle connect string
308 * The actual validity of the string is checked by attempting to connect
309 *
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
313 *
314 * @since 1.22
315 *
316 * @param string $connect_string
317 *
318 * @return bool Whether the connection string is valid.
319 */
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;
324 }
325 }