objectcache: Always use interim values on WAN cache tombstones
[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\DBConnectionError;
25
26 /**
27 * Class for setting up the MediaWiki database using Oracle.
28 *
29 * @ingroup Deployment
30 * @since 1.17
31 */
32 class OracleInstaller extends DatabaseInstaller {
33
34 protected $globalNames = [
35 'wgDBserver',
36 'wgDBname',
37 'wgDBuser',
38 'wgDBpassword',
39 'wgDBprefix',
40 ];
41
42 protected $internalDefaults = [
43 '_OracleDefTS' => 'USERS',
44 '_OracleTempTS' => 'TEMP',
45 '_InstallUser' => 'SYSTEM',
46 ];
47
48 public $minimumVersion = '9.0.1'; // 9iR1
49
50 protected $connError = null;
51
52 public function getName() {
53 return 'oracle';
54 }
55
56 public function isCompiled() {
57 return self::checkExtension( 'oci8' );
58 }
59
60 public function getConnectForm() {
61 if ( $this->getVar( 'wgDBserver' ) == 'localhost' ) {
62 $this->parent->setVar( 'wgDBserver', '' );
63 }
64
65 return $this->getTextBox(
66 'wgDBserver',
67 'config-db-host-oracle',
68 [],
69 $this->parent->getHelpBox( 'config-db-host-oracle-help' )
70 ) .
71 Html::openElement( 'fieldset' ) .
72 Html::element( 'legend', [], wfMessage( 'config-db-wiki-settings' )->text() ) .
73 $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
74 $this->getTextBox( '_OracleDefTS', 'config-oracle-def-ts' ) .
75 $this->getTextBox(
76 '_OracleTempTS',
77 'config-oracle-temp-ts',
78 [],
79 $this->parent->getHelpBox( 'config-db-oracle-help' )
80 ) .
81 Html::closeElement( 'fieldset' ) .
82 $this->parent->getWarningBox( wfMessage( 'config-db-account-oracle-warn' )->text() ) .
83 $this->getInstallUserBox() .
84 $this->getWebUserBox();
85 }
86
87 public function submitInstallUserBox() {
88 parent::submitInstallUserBox();
89 $this->parent->setVar( '_InstallDBname', $this->getVar( '_InstallUser' ) );
90
91 return Status::newGood();
92 }
93
94 public function submitConnectForm() {
95 // Get variables from the request
96 $newValues = $this->setVarsFromRequest( [
97 'wgDBserver',
98 'wgDBprefix',
99 'wgDBuser',
100 'wgDBpassword'
101 ] );
102 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
103
104 // Validate them
105 $status = Status::newGood();
106 if ( !strlen( $newValues['wgDBserver'] ) ) {
107 $status->fatal( 'config-missing-db-server-oracle' );
108 } elseif ( !self::checkConnectStringFormat( $newValues['wgDBserver'] ) ) {
109 $status->fatal( 'config-invalid-db-server-oracle', $newValues['wgDBserver'] );
110 }
111 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
112 $status->fatal( 'config-invalid-schema', $newValues['wgDBprefix'] );
113 }
114 if ( !$status->isOK() ) {
115 return $status;
116 }
117
118 // Submit user box
119 $status = $this->submitInstallUserBox();
120 if ( !$status->isOK() ) {
121 return $status;
122 }
123
124 // Try to connect trough multiple scenarios
125 // Scenario 1: Install with a manually created account
126 $status = $this->getConnection();
127 if ( !$status->isOK() ) {
128 if ( $this->connError == 28009 ) {
129 // _InstallUser seems to be a SYSDBA
130 // Scenario 2: Create user with SYSDBA and install with new user
131 $status = $this->submitWebUserBox();
132 if ( !$status->isOK() ) {
133 return $status;
134 }
135 $status = $this->openSYSDBAConnection();
136 if ( !$status->isOK() ) {
137 return $status;
138 }
139 if ( !$this->getVar( '_CreateDBAccount' ) ) {
140 $status->fatal( 'config-db-sys-create-oracle' );
141 }
142 } else {
143 return $status;
144 }
145 } else {
146 // check for web user credentials
147 // Scenario 3: Install with a priviliged user but use a restricted user
148 $statusIS3 = $this->submitWebUserBox();
149 if ( !$statusIS3->isOK() ) {
150 return $statusIS3;
151 }
152 }
153
154 /**
155 * @var $conn Database
156 */
157 $conn = $status->value;
158
159 // Check version
160 $version = $conn->getServerVersion();
161 if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
162 return Status::newFatal( 'config-oracle-old', $this->minimumVersion, $version );
163 }
164
165 return $status;
166 }
167
168 public function openConnection() {
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 0,
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
185 return $status;
186 }
187
188 public function openSYSDBAConnection() {
189 $status = Status::newGood();
190 try {
191 $db = new DatabaseOracle(
192 $this->getVar( 'wgDBserver' ),
193 $this->getVar( '_InstallUser' ),
194 $this->getVar( '_InstallPassword' ),
195 $this->getVar( '_InstallDBname' ),
196 DBO_SYSDBA,
197 $this->getVar( 'wgDBprefix' )
198 );
199 $status->value = $db;
200 } catch ( DBConnectionError $e ) {
201 $this->connError = $e->db->lastErrno();
202 $status->fatal( 'config-connection-error', $e->getMessage() );
203 }
204
205 return $status;
206 }
207
208 public function needsUpgrade() {
209 $tempDBname = $this->getVar( 'wgDBname' );
210 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
211 $retVal = parent::needsUpgrade();
212 $this->parent->setVar( 'wgDBname', $tempDBname );
213
214 return $retVal;
215 }
216
217 public function preInstall() {
218 # Add our user callback to installSteps, right before the tables are created.
219 $callback = [
220 'name' => 'user',
221 'callback' => [ $this, 'setupUser' ]
222 ];
223 $this->parent->addInstallStep( $callback, 'database' );
224 }
225
226 public function setupDatabase() {
227 $status = Status::newGood();
228
229 return $status;
230 }
231
232 public function setupUser() {
233 global $IP;
234
235 if ( !$this->getVar( '_CreateDBAccount' ) ) {
236 return Status::newGood();
237 }
238
239 // normaly only SYSDBA users can create accounts
240 $status = $this->openSYSDBAConnection();
241 if ( !$status->isOK() ) {
242 if ( $this->connError == 1031 ) {
243 // insufficient privileges (looks like a normal user)
244 $status = $this->openConnection();
245 if ( !$status->isOK() ) {
246 return $status;
247 }
248 } else {
249 return $status;
250 }
251 }
252
253 $this->db = $status->value;
254 $this->setupSchemaVars();
255
256 if ( !$this->db->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
257 $this->db->setFlag( DBO_DDLMODE );
258 $error = $this->db->sourceFile( "$IP/maintenance/oracle/user.sql" );
259 if ( $error !== true || !$this->db->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
260 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
261 }
262 } elseif ( $this->db->getFlag( DBO_SYSDBA ) ) {
263 $status->fatal( 'config-db-sys-user-exists-oracle', $this->getVar( 'wgDBuser' ) );
264 }
265
266 if ( $status->isOK() ) {
267 // user created or already existing, switching back to a normal connection
268 // as the new user has all needed privileges to setup the rest of the schema
269 // i will be using that user as _InstallUser from this point on
270 $this->db->close();
271 $this->db = false;
272 $this->parent->setVar( '_InstallUser', $this->getVar( 'wgDBuser' ) );
273 $this->parent->setVar( '_InstallPassword', $this->getVar( 'wgDBpassword' ) );
274 $this->parent->setVar( '_InstallDBname', $this->getVar( 'wgDBuser' ) );
275 $status = $this->getConnection();
276 }
277
278 return $status;
279 }
280
281 /**
282 * Overload: after this action field info table has to be rebuilt
283 * @return Status
284 */
285 public function createTables() {
286 $this->setupSchemaVars();
287 $this->db->setFlag( DBO_DDLMODE );
288 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
289 $status = parent::createTables();
290 $this->db->clearFlag( DBO_DDLMODE );
291
292 $this->db->query( 'BEGIN fill_wiki_info; END;' );
293
294 return $status;
295 }
296
297 public function getSchemaVars() {
298 $varNames = [
299 # These variables are used by maintenance/oracle/user.sql
300 '_OracleDefTS',
301 '_OracleTempTS',
302 'wgDBuser',
303 'wgDBpassword',
304
305 # These are used by tables.sql
306 'wgDBprefix',
307 ];
308 $vars = [];
309 foreach ( $varNames as $name ) {
310 $vars[$name] = $this->getVar( $name );
311 }
312
313 return $vars;
314 }
315
316 public function getLocalSettings() {
317 $prefix = $this->getVar( 'wgDBprefix' );
318
319 return "# Oracle specific settings
320 \$wgDBprefix = \"{$prefix}\";
321 ";
322 }
323
324 /**
325 * Function checks the format of Oracle connect string
326 * The actual validity of the string is checked by attempting to connect
327 *
328 * Regex should be able to validate all connect string formats
329 * [//](host|tns_name)[:port][/service_name][:POOLED]
330 * http://www.orafaq.com/wiki/EZCONNECT
331 *
332 * @since 1.22
333 *
334 * @param string $connect_string
335 *
336 * @return bool Whether the connection string is valid.
337 */
338 public static function checkConnectStringFormat( $connect_string ) {
339 // @@codingStandardsIgnoreStart Long lines with regular expressions.
340 // @todo Very long regular expression. Make more readable?
341 $isValid = preg_match( '/^[[:alpha:]][\w\-]*(?:\.[[:alpha:]][\w\-]*){0,2}$/', $connect_string ); // TNS name
342 $isValid |= preg_match( '/^(?:\/\/)?[\w\-\.]+(?::[\d]+)?(?:\/(?:[\w\-\.]+(?::(pooled|dedicated|shared))?)?(?:\/[\w\-\.]+)?)?$/', $connect_string ); // EZConnect
343 // @@codingStandardsIgnoreEnd
344 return (bool)$isValid;
345 }
346 }