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