merged master
[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' => 'SYSDBA',
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
63 $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(), wfMsg( 'config-db-wiki-settings' ) ) .
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( wfMsg( 'config-db-account-oracle-warn' ) ).
71 $this->getInstallUserBox().
72 $this->getWebUserBox();
73 }
74
75 public function submitInstallUserBox() {
76 parent::submitInstallUserBox();
77 $this->parent->setVar( '_InstallDBname', $this->getVar( '_InstallUser' ) );
78 return Status::newGood();
79 }
80
81 public function submitConnectForm() {
82 // Get variables from the request
83 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBprefix', 'wgDBuser', 'wgDBpassword' ) );
84 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
85
86 // Validate them
87 $status = Status::newGood();
88 if ( !strlen( $newValues['wgDBserver'] ) ) {
89 $status->fatal( 'config-missing-db-server-oracle' );
90 } elseif ( !preg_match( '/^[a-zA-Z0-9_\.]+$/', $newValues['wgDBserver'] ) ) {
91 $status->fatal( 'config-invalid-db-server-oracle', $newValues['wgDBserver'] );
92 }
93 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
94 $status->fatal( 'config-invalid-schema', $newValues['wgDBprefix'] );
95 }
96 if ( !$status->isOK() ) {
97 return $status;
98 }
99
100 // Submit user box
101 $status = $this->submitInstallUserBox();
102 if ( !$status->isOK() ) {
103 return $status;
104 }
105
106 // Try to connect trough multiple scenarios
107 // Scenario 1: Install with a manually created account
108 $status = $this->getConnection();
109 if ( !$status->isOK() ) {
110 if ( $this->connError == 28009 ) {
111 // _InstallUser seems to be a SYSDBA
112 // Scenario 2: Create user with SYSDBA and install with new user
113 $status = $this->submitWebUserBox();
114 if ( !$status->isOK() ) {
115 return $status;
116 }
117 $status = $this->openSYSDBAConnection();
118 if ( !$status->isOK() ) {
119 return $status;
120 }
121 if ( !$this->getVar( '_CreateDBAccount' ) ) {
122 $status->fatal('config-db-sys-create-oracle');
123 }
124 } else {
125 return $status;
126 }
127 } else {
128 // check for web user credentials
129 // Scenario 3: Install with a priviliged user but use a restricted user
130 $statusIS3 = $this->submitWebUserBox();
131 if ( !$statusIS3->isOK() ) {
132 return $statusIS3;
133 }
134 }
135
136 /**
137 * @var $conn DatabaseBase
138 */
139 $conn = $status->value;
140
141 // Check version
142 $version = $conn->getServerVersion();
143 if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
144 return Status::newFatal( 'config-oracle-old', $this->minimumVersion, $version );
145 }
146
147 return $status;
148 }
149
150 public function openConnection() {
151 $status = Status::newGood();
152 try {
153 $db = new DatabaseOracle(
154 $this->getVar( 'wgDBserver' ),
155 $this->getVar( '_InstallUser' ),
156 $this->getVar( '_InstallPassword' ),
157 $this->getVar( '_InstallDBname' ),
158 0,
159 $this->getVar( 'wgDBprefix' )
160 );
161 $status->value = $db;
162 } catch ( DBConnectionError $e ) {
163 $this->connError = $e->db->lastErrno();
164 $status->fatal( 'config-connection-error', $e->getMessage() );
165 }
166 return $status;
167 }
168
169 public function openSYSDBAConnection() {
170 $status = Status::newGood();
171 try {
172 $db = new DatabaseOracle(
173 $this->getVar( 'wgDBserver' ),
174 $this->getVar( '_InstallUser' ),
175 $this->getVar( '_InstallPassword' ),
176 $this->getVar( '_InstallDBname' ),
177 DBO_SYSDBA,
178 $this->getVar( 'wgDBprefix' )
179 );
180 $status->value = $db;
181 } catch ( DBConnectionError $e ) {
182 $this->connError = $e->db->lastErrno();
183 $status->fatal( 'config-connection-error', $e->getMessage() );
184 }
185 return $status;
186 }
187
188 public function needsUpgrade() {
189 $tempDBname = $this->getVar( 'wgDBname' );
190 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
191 $retVal = parent::needsUpgrade();
192 $this->parent->setVar( 'wgDBname', $tempDBname );
193 return $retVal;
194 }
195
196 public function preInstall() {
197 # Add our user callback to installSteps, right before the tables are created.
198 $callback = array(
199 'name' => 'user',
200 'callback' => array( $this, 'setupUser' )
201 );
202 $this->parent->addInstallStep( $callback, 'database' );
203 }
204
205
206 public function setupDatabase() {
207 $status = Status::newGood();
208 return $status;
209 }
210
211 public function setupUser() {
212 global $IP;
213
214 if ( !$this->getVar( '_CreateDBAccount' ) ) {
215 return Status::newGood();
216 }
217
218 // normaly only SYSDBA users can create accounts
219 $status = $this->openSYSDBAConnection();
220 if ( !$status->isOK() ) {
221 if ( $this->connError == 1031 ) {
222 // insufficient privileges (looks like a normal user)
223 $status = $this->openConnection();
224 if ( !$status->isOK() ) {
225 return $status;
226 }
227 } else {
228 return $status;
229 }
230 }
231 $this->db = $status->value;
232 $this->setupSchemaVars();
233
234 if ( !$this->db->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
235 $this->db->setFlag( DBO_DDLMODE );
236 $error = $this->db->sourceFile( "$IP/maintenance/oracle/user.sql" );
237 if ( $error !== true || !$this->db->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
238 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
239 }
240 } elseif ( $this->db->getFlag( DBO_SYSDBA ) ) {
241 $status->fatal( 'config-db-sys-user-exists-oracle', $this->getVar( 'wgDBuser' ) );
242 }
243
244 if ($status->isOK()) {
245 // user created or already existing, switching back to a normal connection
246 // as the new user has all needed privileges to setup the rest of the schema
247 // i will be using that user as _InstallUser from this point on
248 $this->db->close();
249 $this->db = false;
250 $this->parent->setVar( '_InstallUser', $this->getVar( 'wgDBuser' ) );
251 $this->parent->setVar( '_InstallPassword', $this->getVar( 'wgDBpassword' ) );
252 $this->parent->setVar( '_InstallDBname', $this->getVar( 'wgDBuser' ) );
253 $status = $this->getConnection();
254 }
255
256 return $status;
257 }
258
259 /**
260 * Overload: after this action field info table has to be rebuilt
261 * @return Status
262 */
263 public function createTables() {
264 $this->setupSchemaVars();
265 $this->db->setFlag( DBO_DDLMODE );
266 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
267 $status = parent::createTables();
268 $this->db->clearFlag( DBO_DDLMODE );
269
270 $this->db->query( 'BEGIN fill_wiki_info; END;' );
271
272 return $status;
273 }
274
275 public function getSchemaVars() {
276 $varNames = array(
277 # These variables are used by maintenance/oracle/user.sql
278 '_OracleDefTS',
279 '_OracleTempTS',
280 'wgDBuser',
281 'wgDBpassword',
282
283 # These are used by tables.sql
284 'wgDBprefix',
285 );
286 $vars = array();
287 foreach ( $varNames as $name ) {
288 $vars[$name] = $this->getVar( $name );
289 }
290 return $vars;
291 }
292
293 public function getLocalSettings() {
294 $prefix = $this->getVar( 'wgDBprefix' );
295 return
296 "# Oracle specific settings
297 \$wgDBprefix = \"{$prefix}\";
298 ";
299 }
300
301 }