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