3 * PostgreSQL-specific installer.
10 * Class for setting up the MediaWiki database using Postgres.
15 class PostgresInstaller
extends DatabaseInstaller
{
17 protected $globalNames = array(
26 protected $internalDefaults = array(
27 '_InstallUser' => 'postgres',
30 var $minimumVersion = '8.3';
31 var $maxRoleSearchDepth = 5;
33 protected $pgConns = array();
39 public function isCompiled() {
40 return self
::checkExtension( 'pgsql' );
43 function getConnectForm() {
45 $this->getTextBox( 'wgDBserver', 'config-db-host', array(), $this->parent
->getHelpBox( 'config-db-host-help' ) ) .
46 $this->getTextBox( 'wgDBport', 'config-db-port' ) .
47 Html
::openElement( 'fieldset' ) .
48 Html
::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
49 $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent
->getHelpBox( 'config-db-name-help' ) ) .
50 $this->getTextBox( 'wgDBmwschema', 'config-db-schema', array(), $this->parent
->getHelpBox( 'config-db-schema-help' ) ) .
51 Html
::closeElement( 'fieldset' ) .
52 $this->getInstallUserBox();
55 function submitConnectForm() {
56 // Get variables from the request
57 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBport',
58 'wgDBname', 'wgDBmwschema' ) );
61 $status = Status
::newGood();
62 if ( !strlen( $newValues['wgDBname'] ) ) {
63 $status->fatal( 'config-missing-db-name' );
64 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
65 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
67 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) {
68 $status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] );
72 if ( $status->isOK() ) {
73 $status->merge( $this->submitInstallUserBox() );
75 if ( !$status->isOK() ) {
79 $status = $this->getPgConnection( 'create-db' );
80 if ( !$status->isOK() ) {
83 $conn = $status->value
;
86 $version = $conn->getServerVersion();
87 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
88 return Status
::newFatal( 'config-postgres-old', $this->minimumVersion
, $version );
91 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
92 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
93 return Status
::newGood();
96 public function getConnection() {
97 $status = $this->getPgConnection( 'create-tables' );
98 if ( $status->isOK() ) {
99 $this->db
= $status->value
;
104 public function openConnection() {
105 return $this->openPgConnection( 'create-tables' );
109 * Open a PG connection with given parameters
110 * @param $user User name
111 * @param $password Password
112 * @param $dbName Database name
115 protected function openConnectionWithParams( $user, $password, $dbName ) {
116 $status = Status
::newGood();
118 $db = new DatabasePostgres(
119 $this->getVar( 'wgDBserver' ),
123 $status->value
= $db;
124 } catch ( DBConnectionError
$e ) {
125 $status->fatal( 'config-connection-error', $e->getMessage() );
131 * Get a special type of connection
132 * @param $type See openPgConnection() for details.
135 protected function getPgConnection( $type ) {
136 if ( isset( $this->pgConns
[$type] ) ) {
137 return Status
::newGood( $this->pgConns
[$type] );
139 $status = $this->openPgConnection( $type );
141 if ( $status->isOK() ) {
142 $conn = $status->value
;
143 $conn->clearFlag( DBO_TRX
);
145 $this->pgConns
[$type] = $conn;
151 * Get a connection of a specific PostgreSQL-specific type. Connections
152 * of a given type are cached.
154 * PostgreSQL lacks cross-database operations, so after the new database is
155 * created, you need to make a separate connection to connect to that
156 * database and add tables to it.
158 * New tables are owned by the user that creates them, and MediaWiki's
159 * PostgreSQL support has always assumed that the table owner will be
160 * $wgDBuser. So before we create new tables, we either need to either
161 * connect as the other user or to execute a SET ROLE command. Using a
162 * separate connection for this allows us to avoid accidental cross-module
165 * @param $type The type of connection to get:
166 * - create-db: A connection for creating DBs, suitable for pre-
168 * - create-schema: A connection to the new DB, for creating schemas and
169 * other similar objects in the new DB.
170 * - create-tables: A connection with a role suitable for creating tables.
172 * @return A Status object. On success, a connection object will be in the
175 protected function openPgConnection( $type ) {
178 return $this->openConnectionToAnyDB(
179 $this->getVar( '_InstallUser' ),
180 $this->getVar( '_InstallPassword' ) );
181 case 'create-schema':
182 return $this->openConnectionWithParams(
183 $this->getVar( '_InstallUser' ),
184 $this->getVar( '_InstallPassword' ),
185 $this->getVar( 'wgDBname' ) );
186 case 'create-tables':
187 $status = $this->openPgConnection( 'create-schema' );
188 if ( $status->isOK() ) {
189 $conn = $status->value
;
190 $safeRole = $conn->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
191 $conn->query( "SET ROLE $safeRole" );
195 throw new MWException( "Invalid special connection type: \"$type\"" );
199 public function openConnectionToAnyDB( $user, $password ) {
204 if ( !in_array( $this->getVar( 'wgDBname' ), $dbs ) ) {
205 array_unshift( $dbs, $this->getVar( 'wgDBname' ) );
207 $status = Status
::newGood();
208 foreach ( $dbs as $db ) {
210 $conn = new DatabasePostgres(
211 $this->getVar( 'wgDBserver' ),
215 } catch ( DBConnectionError
$error ) {
217 $status->fatal( 'config-pg-test-error', $db,
218 $error->getMessage() );
220 if ( $conn !== false ) {
224 if ( $conn !== false ) {
225 return Status
::newGood( $conn );
231 protected function getInstallUserPermissions() {
232 $status = $this->getPgConnection( 'create-db' );
233 if ( !$status->isOK() ) {
236 $conn = $status->value
;
237 $superuser = $this->getVar( '_InstallUser' );
239 $row = $conn->selectRow( '"pg_catalog"."pg_roles"', '*',
240 array( 'rolname' => $superuser ), __METHOD__
);
244 protected function canCreateAccounts() {
245 $perms = $this->getInstallUserPermissions();
249 return $perms->rolsuper
=== 't' ||
$perms->rolcreaterole
=== 't';
252 protected function isSuperUser() {
253 $perms = $this->getInstallUserPermissions();
257 return $perms->rolsuper
=== 't';
260 public function getSettingsForm() {
261 if ( $this->canCreateAccounts() ) {
262 $noCreateMsg = false;
264 $noCreateMsg = 'config-db-web-no-create-privs';
266 $s = $this->getWebUserBox( $noCreateMsg );
271 public function submitSettingsForm() {
272 $status = $this->submitWebUserBox();
273 if ( !$status->isOK() ) {
277 $same = $this->getVar( 'wgDBuser' ) === $this->getVar( '_InstallUser' );
279 // Check if the web user exists
280 // Connect to the database with the install user
281 $status = $this->getPgConnection( 'create-db' );
282 if ( !$status->isOK() ) {
285 $exists = $status->value
->roleExists( $this->getVar( 'wgDBuser' ) );
287 // Validate the create checkbox
288 if ( $this->canCreateAccounts() && !$same && !$exists ) {
289 $create = $this->getVar( '_CreateDBAccount' );
291 $this->setVar( '_CreateDBAccount', false );
295 if ( !$create && !$exists ) {
296 if ( $this->canCreateAccounts() ) {
297 $msg = 'config-install-user-missing-create';
299 $msg = 'config-install-user-missing';
301 return Status
::newFatal( $msg, $this->getVar( 'wgDBuser' ) );
305 // No more checks to do
306 return Status
::newGood();
309 // Existing web account. Test the connection.
310 $status = $this->openConnectionToAnyDB(
311 $this->getVar( 'wgDBuser' ),
312 $this->getVar( 'wgDBpassword' ) );
313 if ( !$status->isOK() ) {
317 // The web user is conventionally the table owner in PostgreSQL
318 // installations. Make sure the install user is able to create
319 // objects on behalf of the web user.
320 if ( $this->canCreateObjectsForWebUser() ) {
321 return Status
::newGood();
323 return Status
::newFatal( 'config-pg-not-in-role' );
328 * Returns true if the install user is able to create objects owned
329 * by the web user, false otherwise.
331 protected function canCreateObjectsForWebUser() {
332 if ( $this->isSuperUser() ) {
336 $status = $this->getPgConnection( 'create-db' );
337 if ( !$status->isOK() ) {
340 $conn = $status->value
;
341 $installerId = $conn->selectField( '"pg_catalog"."pg_roles"', 'oid',
342 array( 'rolname' => $this->getVar( '_InstallUser' ) ), __METHOD__
);
343 $webId = $conn->selectField( '"pg_catalog"."pg_roles"', 'oid',
344 array( 'rolname' => $this->getVar( 'wgDBuser' ) ), __METHOD__
);
346 return $this->isRoleMember( $conn, $installerId, $webId, $this->maxRoleSearchDepth
);
350 * Recursive helper for canCreateObjectsForWebUser().
351 * @param $conn Database object
352 * @param $targetMember Role ID of the member to look for
353 * @param $group Role ID of the group to look for
354 * @param $maxDepth Maximum recursive search depth
356 protected function isRoleMember( $conn, $targetMember, $group, $maxDepth ) {
357 if ( $targetMember === $group ) {
358 // A role is always a member of itself
361 // Get all members of the given group
362 $res = $conn->select( '"pg_catalog"."pg_auth_members"', array( 'member' ),
363 array( 'roleid' => $group ), __METHOD__
);
364 foreach ( $res as $row ) {
365 if ( $row->member
== $targetMember ) {
366 // Found target member
369 // Recursively search each member of the group to see if the target
370 // is a member of it, up to the given maximum depth.
371 if ( $maxDepth > 0 ) {
372 if ( $this->isRoleMember( $conn, $targetMember, $row->member
, $maxDepth - 1 ) ) {
373 // Found member of member
381 public function preInstall() {
383 'name' => 'pg-commit',
384 'callback' => array( $this, 'commitChanges' ),
387 'name' => 'pg-plpgsql',
388 'callback' => array( $this, 'setupPLpgSQL' ),
392 'callback' => array( $this, 'setupSchema' )
394 $this->parent
->addInstallStep( $commitCB, 'interwiki' );
395 $this->parent
->addInstallStep( $plpgCB, 'database' );
396 $this->parent
->addInstallStep( $schemaCB, 'database' );
397 if( $this->getVar( '_CreateDBAccount' ) ) {
398 $this->parent
->addInstallStep( array(
400 'callback' => array( $this, 'setupUser' ),
405 function setupDatabase() {
406 $status = $this->getPgConnection( 'create-db' );
407 if ( !$status->isOK() ) {
410 $conn = $status->value
;
412 $dbName = $this->getVar( 'wgDBname' );
413 $schema = $this->getVar( 'wgDBmwschema' );
414 $user = $this->getVar( 'wgDBuser' );
415 $safeschema = $conn->addIdentifierQuotes( $schema );
416 $safeuser = $conn->addIdentifierQuotes( $user );
418 $exists = $conn->selectField( '"pg_catalog"."pg_database"', '1',
419 array( 'datname' => $dbName ), __METHOD__
);
421 $safedb = $conn->addIdentifierQuotes( $dbName );
422 $conn->query( "CREATE DATABASE $safedb", __METHOD__
);
424 return Status
::newGood();
427 function setupSchema() {
428 // Get a connection to the target database
429 $status = $this->getPgConnection( 'create-schema' );
430 if ( !$status->isOK() ) {
433 $conn = $status->value
;
435 // Create the schema if necessary
436 $schema = $this->getVar( 'wgDBmwschema' );
437 $safeschema = $conn->addIdentifierQuotes( $schema );
438 $safeuser = $conn->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
439 if( !$conn->schemaExists( $schema ) ) {
441 $conn->query( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
442 } catch ( DBQueryError
$e ) {
443 return Status
::newFatal( 'config-install-pg-schema-failed',
444 $this->getVar( '_InstallUser' ), $schema );
448 // If we created a user, alter it now to search the new schema by default
449 if ( $this->getVar( '_CreateDBAccount' ) ) {
450 $conn->query( "ALTER ROLE $safeuser SET search_path = $safeschema, public",
454 // Select the new schema in the current connection
455 $conn->query( "SET search_path = $safeschema" );
456 return Status
::newGood();
459 function commitChanges() {
460 $this->db
->query( 'COMMIT' );
461 return Status
::newGood();
464 function setupUser() {
465 if ( !$this->getVar( '_CreateDBAccount' ) ) {
466 return Status
::newGood();
469 $status = $this->getPgConnection( 'create-db' );
470 if ( !$status->isOK() ) {
473 $conn = $status->value
;
475 $schema = $this->getVar( 'wgDBmwschema' );
476 $safeuser = $conn->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
477 $safepass = $conn->addQuotes( $this->getVar( 'wgDBpassword' ) );
478 $safeschema = $conn->addIdentifierQuotes( $schema );
480 // Check if the user already exists
481 $userExists = $conn->roleExists( $this->getVar( 'wgDBuser' ) );
482 if ( !$userExists ) {
485 $sql = "CREATE ROLE $safeuser NOCREATEDB LOGIN PASSWORD $safepass";
487 // If the install user is not a superuser, we need to make the install
488 // user a member of the new user's group, so that the install user will
489 // be able to create a schema and other objects on behalf of the new user.
490 if ( !$this->isSuperUser() ) {
491 $sql .= ' ROLE' . $conn->addIdentifierQuotes( $this->getVar( '_InstallUser' ) );
494 $conn->query( $sql, __METHOD__
);
495 } catch ( DBQueryError
$e ) {
496 return Status
::newFatal( 'config-install-user-create-failed',
497 $this->getVar( 'wgDBuser' ), $e->getMessage() );
501 return Status
::newGood();
504 function getLocalSettings() {
505 $port = $this->getVar( 'wgDBport' );
506 $schema = $this->getVar( 'wgDBmwschema' );
508 "# Postgres specific settings
509 \$wgDBport = \"{$port}\";
510 \$wgDBmwschema = \"{$schema}\";";
513 public function preUpgrade() {
514 global $wgDBuser, $wgDBpassword;
516 # Normal user and password are selected after this step, so for now
517 # just copy these two
518 $wgDBuser = $this->getVar( '_InstallUser' );
519 $wgDBpassword = $this->getVar( '_InstallPassword' );
522 public function createTables() {
523 $schema = $this->getVar( 'wgDBmwschema' );
525 $status = $this->getConnection();
526 if ( !$status->isOK() ) {
529 $conn = $status->value
;
531 if( $conn->tableExists( 'user' ) ) {
532 $status->warning( 'config-install-tables-exist' );
536 $conn->begin( __METHOD__
);
538 if( !$conn->schemaExists( $schema ) ) {
539 $status->fatal( 'config-install-pg-schema-not-exist' );
542 $error = $conn->sourceFile( $conn->getSchema() );
543 if( $error !== true ) {
544 $conn->reportQueryError( $error, 0, '', __METHOD__
);
545 $conn->rollback( __METHOD__
);
546 $status->fatal( 'config-install-tables-failed', $error );
548 $conn->commit( __METHOD__
);
550 // Resume normal operations
551 if( $status->isOk() ) {
557 public function setupPLpgSQL() {
558 // Connect as the install user, since it owns the database and so is
559 // the user that needs to run "CREATE LANGAUGE"
560 $status = $this->getPgConnection( 'create-schema' );
561 if ( !$status->isOK() ) {
564 $conn = $status->value
;
566 $exists = $conn->selectField( '"pg_catalog"."pg_language"', 1,
567 array( 'lanname' => 'plpgsql' ), __METHOD__
);
569 // Already exists, nothing to do
570 return Status
::newGood();
573 // plpgsql is not installed, but if we have a pg_pltemplate table, we
574 // should be able to create it
575 $exists = $conn->selectField(
576 array( '"pg_catalog"."pg_class"', '"pg_catalog"."pg_namespace"' ),
579 'pg_namespace.oid=relnamespace',
580 'nspname' => 'pg_catalog',
581 'relname' => 'pg_pltemplate',
586 $conn->query( 'CREATE LANGUAGE plpgsql' );
587 } catch ( DBQueryError
$e ) {
588 return Status
::newFatal( 'config-pg-no-plpgsql', $this->getVar( 'wgDBname' ) );
591 return Status
::newFatal( 'config-pg-no-plpgsql', $this->getVar( 'wgDBname' ) );
593 return Status
::newGood();