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