Fix backwards canCreateAccounts() check, remove setting search_path for $wgDBuser...
[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 if ( !$this->canCreateAccounts() ) {
166 $this->setVar( '_CreateDBAccount', false );
167 $create = false;
168 } else {
169 $create = $this->getVar( '_CreateDBAccount' );
170 }
171
172 // Don't test the web account if it is the same as the admin.
173 if ( !$create && $this->getVar( 'wgDBuser' ) != $this->getVar( '_InstallUser' ) ) {
174 // Test the web account
175 try {
176 $this->useAdmin = false;
177 return $this->openConnection();
178 } catch ( DBConnectionError $e ) {
179 return Status::newFatal( 'config-connection-error', $e->getMessage() );
180 }
181 }
182
183 return Status::newGood();
184 }
185
186 public function preInstall() {
187 $commitCB = array(
188 'name' => 'pg-commit',
189 'callback' => array( $this, 'commitChanges' ),
190 );
191 $userCB = array(
192 'name' => 'user',
193 'callback' => array( $this, 'setupUser' ),
194 );
195 $plpgCB = array(
196 'name' => 'pg-plpgsql',
197 'callback' => array( $this, 'setupPLpgSQL' ),
198 );
199 $this->parent->addInstallStep( $commitCB, 'interwiki' );
200 $this->parent->addInstallStep( $userCB );
201 $this->parent->addInstallStep( $plpgCB, 'database' );
202 }
203
204 function setupDatabase() {
205 $this->useAdmin = true;
206 $status = $this->getConnection();
207 if ( !$status->isOK() ) {
208 return $status;
209 }
210 $this->setupSchemaVars();
211 $conn = $status->value;
212
213 $dbName = $this->getVar( 'wgDBname' );
214 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $dbName );
215 $rows = $conn->numRows( $conn->query( $SQL ) );
216 if( !$rows ) {
217 $schema = $this->getVar( 'wgDBmwschema' );
218 $user = $this->getVar( 'wgDBuser' );
219
220 $safeschema = $conn->addIdentifierQuotes( $schema );
221 $safeuser = $conn->addIdentifierQuotes( $user );
222
223 $safedb = $conn->addIdentifierQuotes( $dbName );
224
225 $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__ );
226
227 $this->useAdmin = false;
228 $status = $this->getConnection();
229 if ( !$status->isOK() ) {
230 return $status;
231 }
232 $conn = $status->value;
233
234 $result = $conn->schemaExists( $schema );
235 if( !$result ) {
236 $result = $conn->query( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
237 if( !$result ) {
238 $status->fatal( 'config-install-pg-schema-failed', $user, $schema );
239 }
240 } else {
241 $safeschema2 = $conn->addQuotes( $schema );
242 $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n".
243 "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n" .
244 "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n" .
245 "AND p.relkind IN ('r','S','v')\n";
246 $SQL .= "UNION\n";
247 $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n".
248 "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n" .
249 "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n" .
250 "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2";
251 $conn->query( "SET search_path = $safeschema" );
252 $res = $conn->query( $SQL );
253 }
254 }
255 return $status;
256 }
257
258 function commitChanges() {
259 $this->db->query( 'COMMIT' );
260 return Status::newGood();
261 }
262
263 function setupUser() {
264 if ( !$this->getVar( '_CreateDBAccount' ) ) {
265 return Status::newGood();
266 }
267
268 $this->useAdmin = true;
269 $status = $this->getConnection();
270
271 if ( !$status->isOK() ) {
272 return $status;
273 }
274
275 $schema = $this->getVar( 'wgDBmwschema' );
276 $safeuser = $this->db->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
277 $safeusercheck = $this->db->addQuotes( $this->getVar( 'wgDBuser' ) );
278 $safepass = $this->db->addQuotes( $this->getVar( 'wgDBpassword' ) );
279 $safeschema = $this->db->addIdentifierQuotes( $schema );
280
281 $rows = $this->db->numRows(
282 $this->db->query( "SELECT 1 FROM pg_catalog.pg_shadow WHERE usename = $safeusercheck" )
283 );
284 if ( $rows < 1 ) {
285 $res = $this->db->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__ );
286 if ( $res !== true && !( $res instanceOf ResultWrapper ) ) {
287 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $res );
288 }
289 }
290
291 return $status;
292 }
293
294 function getLocalSettings() {
295 $port = $this->getVar( 'wgDBport' );
296 $schema = $this->getVar( 'wgDBmwschema' );
297 return
298 "# Postgres specific settings
299 \$wgDBport = \"{$port}\";
300 \$wgDBmwschema = \"{$schema}\";";
301 }
302
303 public function preUpgrade() {
304 global $wgDBuser, $wgDBpassword;
305
306 # Normal user and password are selected after this step, so for now
307 # just copy these two
308 $wgDBuser = $this->getVar( '_InstallUser' );
309 $wgDBpassword = $this->getVar( '_InstallPassword' );
310 }
311
312 public function createTables() {
313 $schema = $this->getVar( 'wgDBmwschema' );
314
315 $this->db = null;
316 $this->useAdmin = false;
317 $status = $this->getConnection();
318 if ( !$status->isOK() ) {
319 return $status;
320 }
321 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
322
323 if( $this->db->tableExists( 'user' ) ) {
324 $status->warning( 'config-install-tables-exist' );
325 return $status;
326 }
327
328 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
329 $this->db->begin( __METHOD__ );
330
331
332 if( !$this->db->schemaExists( $schema ) ) {
333 $status->error( 'config-install-pg-schema-not-exist' );
334 return $status;
335 }
336 $safeschema = $this->db->addIdentifierQuotes( $schema );
337 $this->db->query( "SET search_path = $safeschema" );
338 $error = $this->db->sourceFile( $this->db->getSchema() );
339 if( $error !== true ) {
340 $this->db->reportQueryError( $error, 0, '', __METHOD__ );
341 $this->db->rollback( __METHOD__ );
342 $status->fatal( 'config-install-tables-failed', $error );
343 } else {
344 $this->db->commit( __METHOD__ );
345 }
346 // Resume normal operations
347 if( $status->isOk() ) {
348 $this->enableLB();
349 }
350 return $status;
351 }
352
353 public function setupPLpgSQL() {
354 $this->useAdmin = true;
355 $status = $this->getConnection();
356 if ( !$status->isOK() ) {
357 return $status;
358 }
359
360 $rows = $this->db->numRows(
361 $this->db->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
362 );
363 if ( $rows < 1 ) {
364 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
365 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
366 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
367 $rows = $this->db->numRows( $this->db->query( $SQL ) );
368 $dbName = $this->getVar( 'wgDBname' );
369 if ( $rows >= 1 ) {
370 $result = $this->db->query( 'CREATE LANGUAGE plpgsql' );
371 if ( !$result ) {
372 return Status::newFatal( 'config-pg-no-plpgsql', $dbName );
373 }
374 } else {
375 return Status::newFatal( 'config-pg-no-plpgsql', $dbName );
376 }
377 }
378 return Status::newGood();
379 }
380 }