Merge "Expose the latest modified index seen by EtcdConfig"
[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 return $this->doOpenConnection();
169 }
170
171 public function openSYSDBAConnection() {
172 return $this->doOpenConnection( DatabaseOracle::DBO_SYSDBA );
173 }
174
175 /**
176 * @param int $flags
177 * @return Status Status with DatabaseOracle or null as the value
178 */
179 private function doOpenConnection( $flags = 0 ) {
180 $status = Status::newGood();
181 try {
182 $db = Database::factory(
183 'oracle',
184 [
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' ),
190 'flags' => $flags
191 ]
192 );
193 $status->value = $db;
194 } catch ( DBConnectionError $e ) {
195 $this->connError = $e->db->lastErrno();
196 $status->fatal( 'config-connection-error', $e->getMessage() );
197 }
198
199 return $status;
200 }
201
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 );
207
208 return $retVal;
209 }
210
211 public function preInstall() {
212 # Add our user callback to installSteps, right before the tables are created.
213 $callback = [
214 'name' => 'user',
215 'callback' => [ $this, 'setupUser' ]
216 ];
217 $this->parent->addInstallStep( $callback, 'database' );
218 }
219
220 public function setupDatabase() {
221 $status = Status::newGood();
222
223 return $status;
224 }
225
226 public function setupUser() {
227 global $IP;
228
229 if ( !$this->getVar( '_CreateDBAccount' ) ) {
230 return Status::newGood();
231 }
232
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() ) {
240 return $status;
241 }
242 } else {
243 return $status;
244 }
245 }
246
247 $this->db = $status->value;
248 $this->setupSchemaVars();
249
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 );
255 }
256 } elseif ( $this->db->getFlag( DBO_SYSDBA ) ) {
257 $status->fatal( 'config-db-sys-user-exists-oracle', $this->getVar( 'wgDBuser' ) );
258 }
259
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
264 $this->db->close();
265 $this->db = false;
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();
270 }
271
272 return $status;
273 }
274
275 /**
276 * Overload: after this action field info table has to be rebuilt
277 * @return Status
278 */
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 );
285
286 $this->db->query( 'BEGIN fill_wiki_info; END;' );
287
288 return $status;
289 }
290
291 public function getSchemaVars() {
292 $varNames = [
293 # These variables are used by maintenance/oracle/user.sql
294 '_OracleDefTS',
295 '_OracleTempTS',
296 'wgDBuser',
297 'wgDBpassword',
298
299 # These are used by tables.sql
300 'wgDBprefix',
301 ];
302 $vars = [];
303 foreach ( $varNames as $name ) {
304 $vars[$name] = $this->getVar( $name );
305 }
306
307 return $vars;
308 }
309
310 public function getLocalSettings() {
311 $prefix = $this->getVar( 'wgDBprefix' );
312
313 return "# Oracle specific settings
314 \$wgDBprefix = \"{$prefix}\";
315 ";
316 }
317
318 /**
319 * Function checks the format of Oracle connect string
320 * The actual validity of the string is checked by attempting to connect
321 *
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
325 *
326 * @since 1.22
327 *
328 * @param string $connect_string
329 *
330 * @return bool Whether the connection string is valid.
331 */
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
337 // phpcs:enable
338 return (bool)$isValid;
339 }
340 }