Merge "(bug 27757) API method for retrieving tokens"
[lhc/web/wiklou.git] / includes / installer / OracleInstaller.php
1 <?php
2 /**
3 * Oracle-specific installer.
4 *
5 * @file
6 * @ingroup Deployment
7 */
8
9 /**
10 * Class for setting up the MediaWiki database using Oracle.
11 *
12 * @ingroup Deployment
13 * @since 1.17
14 */
15 class OracleInstaller extends DatabaseInstaller {
16
17 protected $globalNames = array(
18 'wgDBserver',
19 'wgDBname',
20 'wgDBuser',
21 'wgDBpassword',
22 'wgDBprefix',
23 );
24
25 protected $internalDefaults = array(
26 '_OracleDefTS' => 'USERS',
27 '_OracleTempTS' => 'TEMP',
28 '_InstallUser' => 'SYSDBA',
29 );
30
31 public $minimumVersion = '9.0.1'; // 9iR1
32
33 protected $connError = null;
34
35 public function getName() {
36 return 'oracle';
37 }
38
39 public function isCompiled() {
40 return self::checkExtension( 'oci8' );
41 }
42
43 public function getConnectForm() {
44 if ( $this->getVar( 'wgDBserver' ) == 'localhost' ) {
45 $this->parent->setVar( 'wgDBserver', '' );
46 }
47 return
48 $this->getTextBox( 'wgDBserver', 'config-db-host-oracle', array(), $this->parent->getHelpBox( 'config-db-host-oracle-help' ) ) .
49 Html::openElement( 'fieldset' ) .
50 Html::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
51 $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
52 $this->getTextBox( '_OracleDefTS', 'config-oracle-def-ts' ) .
53 $this->getTextBox( '_OracleTempTS', 'config-oracle-temp-ts', array(), $this->parent->getHelpBox( 'config-db-oracle-help' ) ) .
54 Html::closeElement( 'fieldset' ) .
55 $this->parent->getWarningBox( wfMsg( 'config-db-account-oracle-warn' ) ).
56 $this->getInstallUserBox().
57 $this->getWebUserBox();
58 }
59
60 public function submitInstallUserBox() {
61 parent::submitInstallUserBox();
62 $this->parent->setVar( '_InstallDBname', $this->getVar( '_InstallUser' ) );
63 return Status::newGood();
64 }
65
66 public function submitConnectForm() {
67 // Get variables from the request
68 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBprefix', 'wgDBuser', 'wgDBpassword' ) );
69 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
70
71 // Validate them
72 $status = Status::newGood();
73 if ( !strlen( $newValues['wgDBserver'] ) ) {
74 $status->fatal( 'config-missing-db-server-oracle' );
75 } elseif ( !preg_match( '/^[a-zA-Z0-9_\.]+$/', $newValues['wgDBserver'] ) ) {
76 $status->fatal( 'config-invalid-db-server-oracle', $newValues['wgDBserver'] );
77 }
78 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
79 $status->fatal( 'config-invalid-schema', $newValues['wgDBprefix'] );
80 }
81 if ( !$status->isOK() ) {
82 return $status;
83 }
84
85 // Submit user box
86 $status = $this->submitInstallUserBox();
87 if ( !$status->isOK() ) {
88 return $status;
89 }
90
91 // Try to connect trough multiple scenarios
92 // Scenario 1: Install with a manually created account
93 $status = $this->getConnection();
94 if ( !$status->isOK() ) {
95 if ( $this->connError == 28009 ) {
96 // _InstallUser seems to be a SYSDBA
97 // Scenario 2: Create user with SYSDBA and install with new user
98 $status = $this->submitWebUserBox();
99 if ( !$status->isOK() ) {
100 return $status;
101 }
102 $status = $this->openSYSDBAConnection();
103 if ( !$status->isOK() ) {
104 return $status;
105 }
106 if ( !$this->getVar( '_CreateDBAccount' ) ) {
107 $status->fatal('config-db-sys-create-oracle');
108 }
109 } else {
110 return $status;
111 }
112 } else {
113 // check for web user credentials
114 // Scenario 3: Install with a priviliged user but use a restricted user
115 $statusIS3 = $this->submitWebUserBox();
116 if ( !$statusIS3->isOK() ) {
117 return $statusIS3;
118 }
119 }
120
121 /**
122 * @var $conn DatabaseBase
123 */
124 $conn = $status->value;
125
126 // Check version
127 $version = $conn->getServerVersion();
128 if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
129 return Status::newFatal( 'config-oracle-old', $this->minimumVersion, $version );
130 }
131
132 return $status;
133 }
134
135 public function openConnection() {
136 $status = Status::newGood();
137 try {
138 $db = new DatabaseOracle(
139 $this->getVar( 'wgDBserver' ),
140 $this->getVar( '_InstallUser' ),
141 $this->getVar( '_InstallPassword' ),
142 $this->getVar( '_InstallDBname' ),
143 0,
144 $this->getVar( 'wgDBprefix' )
145 );
146 $status->value = $db;
147 } catch ( DBConnectionError $e ) {
148 $this->connError = $e->db->lastErrno();
149 $status->fatal( 'config-connection-error', $e->getMessage() );
150 }
151 return $status;
152 }
153
154 public function openSYSDBAConnection() {
155 $status = Status::newGood();
156 try {
157 $db = new DatabaseOracle(
158 $this->getVar( 'wgDBserver' ),
159 $this->getVar( '_InstallUser' ),
160 $this->getVar( '_InstallPassword' ),
161 $this->getVar( '_InstallDBname' ),
162 DBO_SYSDBA,
163 $this->getVar( 'wgDBprefix' )
164 );
165 $status->value = $db;
166 } catch ( DBConnectionError $e ) {
167 $this->connError = $e->db->lastErrno();
168 $status->fatal( 'config-connection-error', $e->getMessage() );
169 }
170 return $status;
171 }
172
173 public function needsUpgrade() {
174 $tempDBname = $this->getVar( 'wgDBname' );
175 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
176 $retVal = parent::needsUpgrade();
177 $this->parent->setVar( 'wgDBname', $tempDBname );
178 return $retVal;
179 }
180
181 public function preInstall() {
182 # Add our user callback to installSteps, right before the tables are created.
183 $callback = array(
184 'name' => 'user',
185 'callback' => array( $this, 'setupUser' )
186 );
187 $this->parent->addInstallStep( $callback, 'database' );
188 }
189
190
191 public function setupDatabase() {
192 $status = Status::newGood();
193 return $status;
194 }
195
196 public function setupUser() {
197 global $IP;
198
199 if ( !$this->getVar( '_CreateDBAccount' ) ) {
200 return Status::newGood();
201 }
202
203 // normaly only SYSDBA users can create accounts
204 $status = $this->openSYSDBAConnection();
205 if ( !$status->isOK() ) {
206 if ( $this->connError == 1031 ) {
207 // insufficient privileges (looks like a normal user)
208 $status = $this->openConnection();
209 if ( !$status->isOK() ) {
210 return $status;
211 }
212 } else {
213 return $status;
214 }
215 }
216 $this->db = $status->value;
217 $this->setupSchemaVars();
218
219 if ( !$this->db->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
220 $this->db->setFlag( DBO_DDLMODE );
221 $error = $this->db->sourceFile( "$IP/maintenance/oracle/user.sql" );
222 if ( $error !== true || !$this->db->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
223 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
224 }
225 } elseif ( $this->db->getFlag( DBO_SYSDBA ) ) {
226 $status->fatal( 'config-db-sys-user-exists-oracle', $this->getVar( 'wgDBuser' ) );
227 }
228
229 if ($status->isOK()) {
230 // user created or already existing, switching back to a normal connection
231 // as the new user has all needed privileges to setup the rest of the schema
232 // i will be using that user as _InstallUser from this point on
233 $this->db->close();
234 $this->db = false;
235 $this->parent->setVar( '_InstallUser', $this->getVar( 'wgDBuser' ) );
236 $this->parent->setVar( '_InstallPassword', $this->getVar( 'wgDBpassword' ) );
237 $this->parent->setVar( '_InstallDBname', $this->getVar( 'wgDBuser' ) );
238 $status = $this->getConnection();
239 }
240
241 return $status;
242 }
243
244 /**
245 * Overload: after this action field info table has to be rebuilt
246 * @return Status
247 */
248 public function createTables() {
249 $this->setupSchemaVars();
250 $this->db->setFlag( DBO_DDLMODE );
251 $this->parent->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
252 $status = parent::createTables();
253 $this->db->clearFlag( DBO_DDLMODE );
254
255 $this->db->query( 'BEGIN fill_wiki_info; END;' );
256
257 return $status;
258 }
259
260 public function getSchemaVars() {
261 $varNames = array(
262 # These variables are used by maintenance/oracle/user.sql
263 '_OracleDefTS',
264 '_OracleTempTS',
265 'wgDBuser',
266 'wgDBpassword',
267
268 # These are used by tables.sql
269 'wgDBprefix',
270 );
271 $vars = array();
272 foreach ( $varNames as $name ) {
273 $vars[$name] = $this->getVar( $name );
274 }
275 return $vars;
276 }
277
278 public function getLocalSettings() {
279 $prefix = $this->getVar( 'wgDBprefix' );
280 return
281 "# Oracle specific settings
282 \$wgDBprefix = \"{$prefix}\";
283 ";
284 }
285
286 }