Merge "Integration tests for FirejailCommand"
[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 use Wikimedia\Rdbms\Database;
25 use Wikimedia\Rdbms\DBConnectionError;
26
27 /**
28 * Class for setting up the MediaWiki database using Oracle.
29 *
30 * @ingroup Deployment
31 * @since 1.17
32 */
33 class OracleInstaller extends DatabaseInstaller {
34
35 protected $globalNames = [
36 'wgDBserver',
37 'wgDBname',
38 'wgDBuser',
39 'wgDBpassword',
40 'wgDBprefix',
41 ];
42
43 protected $internalDefaults = [
44 '_OracleDefTS' => 'USERS',
45 '_OracleTempTS' => 'TEMP',
46 '_InstallUser' => 'SYSTEM',
47 ];
48
49 public static $minimumVersion = '9.0.1'; // 9iR1
50 protected static $notMiniumumVerisonMessage = 'config-oracle-old';
51
52 protected $connError = null;
53
54 public function getName() {
55 return 'oracle';
56 }
57
58 public function isCompiled() {
59 return self::checkExtension( 'oci8' );
60 }
61
62 public function getConnectForm() {
63 if ( $this->getVar( 'wgDBserver' ) == 'localhost' ) {
64 $this->parent->setVar( 'wgDBserver', '' );
65 }
66
67 return $this->getTextBox(
68 'wgDBserver',
69 'config-db-host-oracle',
70 [],
71 $this->parent->getHelpBox( 'config-db-host-oracle-help' )
72 ) .
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' ) .
77 $this->getTextBox(
78 '_OracleTempTS',
79 'config-oracle-temp-ts',
80 [],
81 $this->parent->getHelpBox( 'config-db-oracle-help' )
82 ) .
83 Html::closeElement( 'fieldset' ) .
84 $this->parent->getWarningBox( wfMessage( 'config-db-account-oracle-warn' )->text() ) .
85 $this->getInstallUserBox() .
86 $this->getWebUserBox();
87 }
88
89 public function submitInstallUserBox() {
90 parent::submitInstallUserBox();
91 $this->parent->setVar( '_InstallDBname', $this->getVar( '_InstallUser' ) );
92
93 return Status::newGood();
94 }
95
96 public function submitConnectForm() {
97 // Get variables from the request
98 $newValues = $this->setVarsFromRequest( [
99 'wgDBserver',
100 'wgDBprefix',
101 'wgDBuser',
102 'wgDBpassword'
103 ] );
104 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
105
106 // Validate them
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'] );
112 }
113 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
114 $status->fatal( 'config-invalid-schema', $newValues['wgDBprefix'] );
115 }
116 if ( !$status->isOK() ) {
117 return $status;
118 }
119
120 // Submit user box
121 $status = $this->submitInstallUserBox();
122 if ( !$status->isOK() ) {
123 return $status;
124 }
125
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() ) {
135 return $status;
136 }
137 $status = $this->openSYSDBAConnection();
138 if ( !$status->isOK() ) {
139 return $status;
140 }
141 if ( !$this->getVar( '_CreateDBAccount' ) ) {
142 $status->fatal( 'config-db-sys-create-oracle' );
143 }
144 } else {
145 return $status;
146 }
147 } else {
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() ) {
152 return $statusIS3;
153 }
154 }
155
156 /**
157 * @var Database $conn
158 */
159 $conn = $status->value;
160
161 // Check version
162 $status->merge( static::meetsMinimumRequirement( $conn->getServerVersion() ) );
163
164 return $status;
165 }
166
167 public function openConnection() {
168 $status = Status::newGood();
169 try {
170 $db = new DatabaseOracle(
171 $this->getVar( 'wgDBserver' ),
172 $this->getVar( '_InstallUser' ),
173 $this->getVar( '_InstallPassword' ),
174 $this->getVar( '_InstallDBname' ),
175 0,
176 $this->getVar( 'wgDBprefix' )
177 );
178 $status->value = $db;
179 } catch ( DBConnectionError $e ) {
180 $this->connError = $e->db->lastErrno();
181 $status->fatal( 'config-connection-error', $e->getMessage() );
182 }
183
184 return $status;
185 }
186
187 public function openSYSDBAConnection() {
188 $status = Status::newGood();
189 try {
190 $db = new DatabaseOracle(
191 $this->getVar( 'wgDBserver' ),
192 $this->getVar( '_InstallUser' ),
193 $this->getVar( '_InstallPassword' ),
194 $this->getVar( '_InstallDBname' ),
195 DBO_SYSDBA,
196 $this->getVar( 'wgDBprefix' )
197 );
198 $status->value = $db;
199 } catch ( DBConnectionError $e ) {
200 $this->connError = $e->db->lastErrno();
201 $status->fatal( 'config-connection-error', $e->getMessage() );
202 }
203
204 return $status;
205 }
206
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 );
212
213 return $retVal;
214 }
215
216 public function preInstall() {
217 # Add our user callback to installSteps, right before the tables are created.
218 $callback = [
219 'name' => 'user',
220 'callback' => [ $this, 'setupUser' ]
221 ];
222 $this->parent->addInstallStep( $callback, 'database' );
223 }
224
225 public function setupDatabase() {
226 $status = Status::newGood();
227
228 return $status;
229 }
230
231 public function setupUser() {
232 global $IP;
233
234 if ( !$this->getVar( '_CreateDBAccount' ) ) {
235 return Status::newGood();
236 }
237
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() ) {
245 return $status;
246 }
247 } else {
248 return $status;
249 }
250 }
251
252 $this->db = $status->value;
253 $this->setupSchemaVars();
254
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 );
260 }
261 } elseif ( $this->db->getFlag( DBO_SYSDBA ) ) {
262 $status->fatal( 'config-db-sys-user-exists-oracle', $this->getVar( 'wgDBuser' ) );
263 }
264
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
269 $this->db->close();
270 $this->db = false;
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();
275 }
276
277 return $status;
278 }
279
280 /**
281 * Overload: after this action field info table has to be rebuilt
282 * @return Status
283 */
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 );
290
291 $this->db->query( 'BEGIN fill_wiki_info; END;' );
292
293 return $status;
294 }
295
296 public function getSchemaVars() {
297 $varNames = [
298 # These variables are used by maintenance/oracle/user.sql
299 '_OracleDefTS',
300 '_OracleTempTS',
301 'wgDBuser',
302 'wgDBpassword',
303
304 # These are used by tables.sql
305 'wgDBprefix',
306 ];
307 $vars = [];
308 foreach ( $varNames as $name ) {
309 $vars[$name] = $this->getVar( $name );
310 }
311
312 return $vars;
313 }
314
315 public function getLocalSettings() {
316 $prefix = $this->getVar( 'wgDBprefix' );
317
318 return "# Oracle specific settings
319 \$wgDBprefix = \"{$prefix}\";
320 ";
321 }
322
323 /**
324 * Function checks the format of Oracle connect string
325 * The actual validity of the string is checked by attempting to connect
326 *
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
330 *
331 * @since 1.22
332 *
333 * @param string $connect_string
334 *
335 * @return bool Whether the connection string is valid.
336 */
337 public static function checkConnectStringFormat( $connect_string ) {
338 // phpcs:disable Generic.Files.LineLength
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 // phpcs:enable
343 return (bool)$isValid;
344 }
345 }