lowercase a few more t/f
[lhc/web/wiklou.git] / includes / installer / PostgresInstaller.php
1 <?php
2 /**
3 * PostgreSQL-specific installer.
4 *
5 * @file
6 * @ingroup Deployment
7 */
8
9 /**
10 * Class for setting up the MediaWiki database using Postgres.
11 *
12 * @ingroup Deployment
13 * @since 1.17
14 */
15 class PostgresInstaller extends DatabaseInstaller {
16
17 protected $globalNames = array(
18 'wgDBserver',
19 'wgDBport',
20 'wgDBname',
21 'wgDBuser',
22 'wgDBpassword',
23 'wgDBmwschema',
24 );
25
26 var $minimumVersion = '8.3';
27 private $useAdmin = false;
28
29 function getName() {
30 return 'postgres';
31 }
32
33 public function isCompiled() {
34 return self::checkExtension( 'pgsql' );
35 }
36
37 function getConnectForm() {
38 return
39 $this->getTextBox( 'wgDBserver', 'config-db-host', array(), $this->parent->getHelpBox( 'config-db-host-help' ) ) .
40 $this->getTextBox( 'wgDBport', 'config-db-port' ) .
41 Html::openElement( 'fieldset' ) .
42 Html::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
43 $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent->getHelpBox( 'config-db-name-help' ) ) .
44 $this->getTextBox( 'wgDBmwschema', 'config-db-schema', array(), $this->parent->getHelpBox( 'config-db-schema-help' ) ) .
45 Html::closeElement( 'fieldset' ) .
46 $this->getInstallUserBox();
47 }
48
49 function submitConnectForm() {
50 // Get variables from the request
51 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBport',
52 'wgDBname', 'wgDBmwschema' ) );
53
54 // Validate them
55 $status = Status::newGood();
56 if ( !strlen( $newValues['wgDBname'] ) ) {
57 $status->fatal( 'config-missing-db-name' );
58 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
59 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
60 }
61 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) {
62 $status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] );
63 }
64
65 // Submit user box
66 if ( $status->isOK() ) {
67 $status->merge( $this->submitInstallUserBox() );
68 }
69 if ( !$status->isOK() ) {
70 return $status;
71 }
72
73 $this->useAdmin = true;
74 // Try to connect
75 $status->merge( $this->getConnection() );
76 if ( !$status->isOK() ) {
77 return $status;
78 }
79
80 //Make sure install user can create
81 if( !$this->canCreateAccounts() ) {
82 $status->fatal( 'config-pg-no-create-privs' );
83 }
84 if ( !$status->isOK() ) {
85 return $status;
86 }
87
88 // Check version
89 $version = $this->db->getServerVersion();
90 if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
91 return Status::newFatal( 'config-postgres-old', $this->minimumVersion, $version );
92 }
93
94 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
95 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
96 return $status;
97 }
98
99 public function openConnection() {
100 $status = Status::newGood();
101 try {
102 if ( $this->useAdmin ) {
103 $db = new DatabasePostgres(
104 $this->getVar( 'wgDBserver' ),
105 $this->getVar( '_InstallUser' ),
106 $this->getVar( '_InstallPassword' ),
107 'template1' );
108 } else {
109 $db = new DatabasePostgres(
110 $this->getVar( 'wgDBserver' ),
111 $this->getVar( 'wgDBuser' ),
112 $this->getVar( 'wgDBpassword' ),
113 $this->getVar( 'wgDBname' ) );
114 }
115 $status->value = $db;
116 } catch ( DBConnectionError $e ) {
117 $status->fatal( 'config-connection-error', $e->getMessage() );
118 }
119 return $status;
120 }
121
122 protected function canCreateAccounts() {
123 $this->useAdmin = true;
124 $status = $this->getConnection();
125 if ( !$status->isOK() ) {
126 return false;
127 }
128 $conn = $status->value;
129
130 $superuser = $this->getVar( '_InstallUser' );
131
132 $rights = $conn->selectField( 'pg_catalog.pg_user',
133 'CASE WHEN usesuper IS TRUE THEN
134 CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
135 ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
136 END AS rights',
137 array( 'usename' => $superuser ), __METHOD__
138 );
139
140 if( !$rights || ( $rights != 1 && $rights != 3 ) ) {
141 return false;
142 }
143
144 return true;
145 }
146
147 public function getSettingsForm() {
148 if ( $this->canCreateAccounts() ) {
149 $noCreateMsg = false;
150 } else {
151 $noCreateMsg = 'config-db-web-no-create-privs';
152 }
153 $s = $this->getWebUserBox( $noCreateMsg );
154
155 return $s;
156 }
157
158 public function submitSettingsForm() {
159 $status = $this->submitWebUserBox();
160 if ( !$status->isOK() ) {
161 return $status;
162 }
163
164 // Validate the create checkbox
165 $create = true;
166 $canCreate = $this->canCreateAccounts();
167 if ( $canCreate ) {
168 $this->setVar( '_CreateDBAccount', false );
169 $create = false;
170 } else {
171 $create = $this->getVar( '_CreateDBAccount' );
172 }
173
174 // Don't test the web account if it is the same as the admin.
175 if ( !$create && $this->getVar( 'wgDBuser' ) != $this->getVar( '_InstallUser' ) ) {
176 // Test the web account
177 try {
178 $this->useAdmin = false;
179 return $this->openConnection();
180 } catch ( DBConnectionError $e ) {
181 return Status::newFatal( 'config-connection-error', $e->getMessage() );
182 }
183 }
184
185 return Status::newGood();
186 }
187
188 public function preInstall() {
189 $commitCB = array(
190 'name' => 'pg-commit',
191 'callback' => array( $this, 'commitChanges' ),
192 );
193 $userCB = array(
194 'name' => 'user',
195 'callback' => array( $this, 'setupUser' ),
196 );
197 $plpgCB = array(
198 'name' => 'pg-plpgsql',
199 'callback' => array( $this, 'setupPLpgSQL' ),
200 );
201 $this->parent->addInstallStep( $commitCB, 'interwiki' );
202 $this->parent->addInstallStep( $userCB );
203 $this->parent->addInstallStep( $plpgCB, 'database' );
204 }
205
206 function setupDatabase() {
207 $this->useAdmin = true;
208 $status = $this->getConnection();
209 if ( !$status->isOK() ) {
210 return $status;
211 }
212 $this->setupSchemaVars();
213 $conn = $status->value;
214
215 $dbName = $this->getVar( 'wgDBname' );
216 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $dbName );
217 $rows = $conn->numRows( $conn->query( $SQL ) );
218 if( !$rows ) {
219 $schema = $this->getVar( 'wgDBmwschema' );
220 $user = $this->getVar( 'wgDBuser' );
221
222 $safeschema = $conn->addIdentifierQuotes( $schema );
223 $safeuser = $conn->addIdentifierQuotes( $user );
224
225 $safedb = $conn->addIdentifierQuotes( $dbName );
226
227 $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__ );
228
229 $conn = new DatabasePostgres(
230 $this->getVar( 'wgDBserver' ),
231 $this->getVar( 'wgDBuser' ),
232 $this->getVar( 'wgDBpassword' ),
233 $dbName,
234 false,
235 0,
236 $this->getVar( 'wgDBprefix' )
237 );
238
239 $result = $conn->schemaExists( $schema );
240 if( !$result ) {
241 $result = $conn->query( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
242 if( !$result ) {
243 $status->fatal( 'config-install-pg-schema-failed', $user, $schema );
244 }
245 } else {
246 $safeschema2 = $conn->addQuotes( $schema );
247 $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n".
248 "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n" .
249 "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n" .
250 "AND p.relkind IN ('r','S','v')\n";
251 $SQL .= "UNION\n";
252 $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n".
253 "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n" .
254 "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n" .
255 "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2";
256 $res = $conn->query( $SQL );
257 }
258 }
259 return $status;
260 }
261
262 function commitChanges() {
263 $this->db->query( 'COMMIT' );
264 return Status::newGood();
265 }
266
267 function setupUser() {
268 if ( !$this->getVar( '_CreateDBAccount' ) ) {
269 return Status::newGood();
270 }
271
272 $this->useAdmin = TRUE;
273 $status = $this->getConnection();
274
275 if ( !$status->isOK() ) {
276 return $status;
277 }
278
279 $db = $this->getVar( 'wgDBname' );
280 $schema = $this->getVar( 'wgDBmwschema' );
281 $safeuser = $this->db->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
282 $safeusercheck = $this->db->addQuotes( $this->getVar( 'wgDBuser' ) );
283 $safepass = $this->db->addQuotes( $this->getVar( 'wgDBpassword' ) );
284 $safeschema = $this->db->addIdentifierQuotes( $schema );
285
286 $rows = $this->db->numRows(
287 $this->db->query( "SELECT 1 FROM pg_catalog.pg_shadow WHERE usename = $safeusercheck" )
288 );
289 if ( $rows < 1 ) {
290 $res = $this->db->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__ );
291 if ( $res !== true && !( $res instanceOf ResultWrapper ) ) {
292 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $res );
293 }
294 $this->db->query("ALTER USER $safeuser SET search_path = $safeschema");
295 }
296
297 return $status;
298 }
299
300 function getLocalSettings() {
301 $port = $this->getVar( 'wgDBport' );
302 $schema = $this->getVar( 'wgDBmwschema' );
303 return
304 "# Postgres specific settings
305 \$wgDBport = \"{$port}\";
306 \$wgDBmwschema = \"{$schema}\";";
307 }
308
309 public function preUpgrade() {
310 global $wgDBuser, $wgDBpassword;
311
312 # Normal user and password are selected after this step, so for now
313 # just copy these two
314 $wgDBuser = $this->getVar( '_InstallUser' );
315 $wgDBpassword = $this->getVar( '_InstallPassword' );
316 }
317
318 public function createTables() {
319 $this->db = null;
320 $this->useAdmin = false;
321 $status = $this->getConnection();
322 if ( !$status->isOK() ) {
323 return $status;
324 }
325 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
326
327 if( $this->db->tableExists( 'user' ) ) {
328 $status->warning( 'config-install-tables-exist' );
329 return $status;
330 }
331
332 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
333 $this->db->begin( __METHOD__ );
334
335 $error = $this->db->sourceFile( $this->db->getSchema() );
336 if( $error !== true ) {
337 $this->db->reportQueryError( $error, 0, '', __METHOD__ );
338 $this->db->rollback( __METHOD__ );
339 $status->fatal( 'config-install-tables-failed', $error );
340 } else {
341 $this->db->commit( __METHOD__ );
342 }
343 // Resume normal operations
344 if( $status->isOk() ) {
345 $this->enableLB();
346 }
347 return $status;
348 }
349
350 public function setupPLpgSQL() {
351 $this->useAdmin = TRUE;
352 $status = $this->getConnection();
353 if ( !$status->isOK() ) {
354 return $status;
355 }
356
357 $rows = $this->db->numRows(
358 $this->db->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
359 );
360 if ( $rows < 1 ) {
361 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
362 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
363 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
364 $rows = $this->db->numRows( $this->db->query( $SQL ) );
365 $dbName = $this->getVar( 'wgDBname' );
366 if ( $rows >= 1 ) {
367 $result = $this->db->query( 'CREATE LANGUAGE plpgsql' );
368 if ( !$result ) {
369 return Status::newFatal( 'config-pg-no-plpgsql', $dbName );
370 }
371 } else {
372 return Status::newFatal( 'config-pg-no-plpgsql', $dbName );
373 }
374 }
375 return Status::newGood();
376 }
377 }