X-Git-Url: https://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2Finstaller%2FPostgresInstaller.php;h=3ac2b3a8d270fc193ef6636e954e6e16d81234be;hb=a2697d7d14b951dd7e3f6e292c9bed4bcffef1b6;hp=a811dff078fc44a734656e3139e4470262810f9f;hpb=b8e4029eab6dd9fe6e90f47d340a95888f2b9c4c;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/installer/PostgresInstaller.php b/includes/installer/PostgresInstaller.php index a811dff078..3ac2b3a8d2 100644 --- a/includes/installer/PostgresInstaller.php +++ b/includes/installer/PostgresInstaller.php @@ -2,6 +2,21 @@ /** * PostgreSQL-specific installer. * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * * @file * @ingroup Deployment */ @@ -21,11 +36,16 @@ class PostgresInstaller extends DatabaseInstaller { 'wgDBuser', 'wgDBpassword', 'wgDBmwschema', - 'wgDBts2schema', ); - var $minimumVersion = '8.1'; - private $ts2MaxVersion = '8.3'; // Doing ts2 is not necessary in PG > 8.3 + protected $internalDefaults = array( + '_InstallUser' => 'postgres', + ); + + var $minimumVersion = '8.3'; + var $maxRoleSearchDepth = 5; + + protected $pgConns = array(); function getName() { return 'postgres'; @@ -40,10 +60,9 @@ class PostgresInstaller extends DatabaseInstaller { $this->getTextBox( 'wgDBserver', 'config-db-host', array(), $this->parent->getHelpBox( 'config-db-host-help' ) ) . $this->getTextBox( 'wgDBport', 'config-db-port' ) . Html::openElement( 'fieldset' ) . - Html::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) . + Html::element( 'legend', array(), wfMessage( 'config-db-wiki-settings' )->text() ) . $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent->getHelpBox( 'config-db-name-help' ) ) . $this->getTextBox( 'wgDBmwschema', 'config-db-schema', array(), $this->parent->getHelpBox( 'config-db-schema-help' ) ) . - $this->getTextBox( 'wgDBts2schema', 'config-db-ts2-schema' ) . Html::closeElement( 'fieldset' ) . $this->getInstallUserBox(); } @@ -51,7 +70,7 @@ class PostgresInstaller extends DatabaseInstaller { function submitConnectForm() { // Get variables from the request $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBport', - 'wgDBname', 'wgDBmwschema', 'wgDBts2schema' ) ); + 'wgDBname', 'wgDBmwschema' ) ); // Validate them $status = Status::newGood(); @@ -63,9 +82,6 @@ class PostgresInstaller extends DatabaseInstaller { if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) { $status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] ); } - if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBts2schema'] ) ) { - $status->fatal( 'config-invalid-ts2schema', $newValues['wgDBts2schema'] ); - } // Submit user box if ( $status->isOK() ) { @@ -75,71 +91,198 @@ class PostgresInstaller extends DatabaseInstaller { return $status; } - // Try to connect - $status->merge( $this->getConnection() ); + $status = $this->getPgConnection( 'create-db' ); if ( !$status->isOK() ) { return $status; } - -/* //Make sure install user can create - $status->merge( $this->canCreateAccounts() ); - if ( !$status->isOK() ) { - return $status; - } */ + /** + * @var $conn DatabaseBase + */ + $conn = $status->value; // Check version - $version = $this->db->getServerVersion(); + $version = $conn->getServerVersion(); if ( version_compare( $version, $this->minimumVersion ) < 0 ) { return Status::newFatal( 'config-postgres-old', $this->minimumVersion, $version ); } $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) ); $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) ); + return Status::newGood(); + } + + public function getConnection() { + $status = $this->getPgConnection( 'create-tables' ); + if ( $status->isOK() ) { + $this->db = $status->value; + } return $status; } - function getConnection($database = 'template1') { - $status = Status::newGood(); + public function openConnection() { + return $this->openPgConnection( 'create-tables' ); + } + /** + * Open a PG connection with given parameters + * @param $user string User name + * @param $password string Password + * @param $dbName string Database name + * @return Status + */ + protected function openConnectionWithParams( $user, $password, $dbName ) { + $status = Status::newGood(); try { - $this->db = new DatabasePostgres( + $db = new DatabasePostgres( $this->getVar( 'wgDBserver' ), - $this->getVar( '_InstallUser' ), - $this->getVar( '_InstallPassword' ), - $database ); - $status->value = $this->db; + $user, + $password, + $dbName); + $status->value = $db; } catch ( DBConnectionError $e ) { $status->fatal( 'config-connection-error', $e->getMessage() ); } return $status; } - protected function canCreateAccounts() { - $status = $this->getConnection(); + /** + * Get a special type of connection + * @param $type string See openPgConnection() for details. + * @return Status + */ + protected function getPgConnection( $type ) { + if ( isset( $this->pgConns[$type] ) ) { + return Status::newGood( $this->pgConns[$type] ); + } + $status = $this->openPgConnection( $type ); + + if ( $status->isOK() ) { + /** + * @var $conn DatabaseBase + */ + $conn = $status->value; + $conn->clearFlag( DBO_TRX ); + $conn->commit( __METHOD__ ); + $this->pgConns[$type] = $conn; + } + return $status; + } + + /** + * Get a connection of a specific PostgreSQL-specific type. Connections + * of a given type are cached. + * + * PostgreSQL lacks cross-database operations, so after the new database is + * created, you need to make a separate connection to connect to that + * database and add tables to it. + * + * New tables are owned by the user that creates them, and MediaWiki's + * PostgreSQL support has always assumed that the table owner will be + * $wgDBuser. So before we create new tables, we either need to either + * connect as the other user or to execute a SET ROLE command. Using a + * separate connection for this allows us to avoid accidental cross-module + * dependencies. + * + * @param $type string The type of connection to get: + * - create-db: A connection for creating DBs, suitable for pre- + * installation. + * - create-schema: A connection to the new DB, for creating schemas and + * other similar objects in the new DB. + * - create-tables: A connection with a role suitable for creating tables. + * + * @return Status object. On success, a connection object will be in the + * value member. + */ + protected function openPgConnection( $type ) { + switch ( $type ) { + case 'create-db': + return $this->openConnectionToAnyDB( + $this->getVar( '_InstallUser' ), + $this->getVar( '_InstallPassword' ) ); + case 'create-schema': + return $this->openConnectionWithParams( + $this->getVar( '_InstallUser' ), + $this->getVar( '_InstallPassword' ), + $this->getVar( 'wgDBname' ) ); + case 'create-tables': + $status = $this->openPgConnection( 'create-schema' ); + if ( $status->isOK() ) { + /** + * @var $conn DatabaseBase + */ + $conn = $status->value; + $safeRole = $conn->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) ); + $conn->query( "SET ROLE $safeRole" ); + } + return $status; + default: + throw new MWException( "Invalid special connection type: \"$type\"" ); + } + } + + public function openConnectionToAnyDB( $user, $password ) { + $dbs = array( + 'template1', + 'postgres', + ); + if ( !in_array( $this->getVar( 'wgDBname' ), $dbs ) ) { + array_unshift( $dbs, $this->getVar( 'wgDBname' ) ); + } + $conn = false; + $status = Status::newGood(); + foreach ( $dbs as $db ) { + try { + $conn = new DatabasePostgres( + $this->getVar( 'wgDBserver' ), + $user, + $password, + $db ); + } catch ( DBConnectionError $error ) { + $conn = false; + $status->fatal( 'config-pg-test-error', $db, + $error->getMessage() ); + } + if ( $conn !== false ) { + break; + } + } + if ( $conn !== false ) { + return Status::newGood( $conn ); + } else { + return $status; + } + } + + protected function getInstallUserPermissions() { + $status = $this->getPgConnection( 'create-db' ); if ( !$status->isOK() ) { return false; } + /** + * @var $conn DatabaseBase + */ $conn = $status->value; - $superuser = $this->getVar( '_InstallUser' ); - $rights = $conn->selectField( 'pg_catalog.pg_user', - 'CASE WHEN usesuper IS TRUE THEN - CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END - ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END - END AS rights', - array( 'usename' => $superuser ), __METHOD__ - ); + $row = $conn->selectRow( '"pg_catalog"."pg_roles"', '*', + array( 'rolname' => $superuser ), __METHOD__ ); + return $row; + } - if( !$rights ) { + protected function canCreateAccounts() { + $perms = $this->getInstallUserPermissions(); + if ( !$perms ) { return false; } + return $perms->rolsuper === 't' || $perms->rolcreaterole === 't'; + } - if( $rights != 1 && $rights != 3 ) { + protected function isSuperUser() { + $perms = $this->getInstallUserPermissions(); + if ( !$perms ) { return false; } - - return true; + return $perms->rolsuper === 't'; } public function getSettingsForm() { @@ -159,135 +302,188 @@ class PostgresInstaller extends DatabaseInstaller { return $status; } + $same = $this->getVar( 'wgDBuser' ) === $this->getVar( '_InstallUser' ); + + if ( $same ) { + $exists = true; + } else { + // Check if the web user exists + // Connect to the database with the install user + $status = $this->getPgConnection( 'create-db' ); + if ( !$status->isOK() ) { + return $status; + } + $exists = $status->value->roleExists( $this->getVar( 'wgDBuser' ) ); + } + // Validate the create checkbox - $canCreate = $this->canCreateAccounts(); - if ( !$canCreate ) { + if ( $this->canCreateAccounts() && !$same && !$exists ) { + $create = $this->getVar( '_CreateDBAccount' ); + } else { $this->setVar( '_CreateDBAccount', false ); $create = false; - } else { - $create = $this->getVar( '_CreateDBAccount' ); } - if ( !$create ) { - // Test the web account - try { - new DatabasePostgres( - $this->getVar( 'wgDBserver' ), - $this->getVar( 'wgDBuser' ), - $this->getVar( 'wgDBpassword' ), - false, - false, - 0, - $this->getVar( 'wgDBprefix' ) - ); - } catch ( DBConnectionError $e ) { - return Status::newFatal( 'config-connection-error', $e->getMessage() ); + if ( !$create && !$exists ) { + if ( $this->canCreateAccounts() ) { + $msg = 'config-install-user-missing-create'; + } else { + $msg = 'config-install-user-missing'; } + return Status::newFatal( $msg, $this->getVar( 'wgDBuser' ) ); } - return Status::newGood(); + if ( !$exists ) { + // No more checks to do + return Status::newGood(); + } + + // Existing web account. Test the connection. + $status = $this->openConnectionToAnyDB( + $this->getVar( 'wgDBuser' ), + $this->getVar( 'wgDBpassword' ) ); + if ( !$status->isOK() ) { + return $status; + } + + // The web user is conventionally the table owner in PostgreSQL + // installations. Make sure the install user is able to create + // objects on behalf of the web user. + if ( $same || $this->canCreateObjectsForWebUser() ) { + return Status::newGood(); + } else { + return Status::newFatal( 'config-pg-not-in-role' ); + } + } + + /** + * Returns true if the install user is able to create objects owned + * by the web user, false otherwise. + * @return bool + */ + protected function canCreateObjectsForWebUser() { + if ( $this->isSuperUser() ) { + return true; + } + + $status = $this->getPgConnection( 'create-db' ); + if ( !$status->isOK() ) { + return false; + } + $conn = $status->value; + $installerId = $conn->selectField( '"pg_catalog"."pg_roles"', 'oid', + array( 'rolname' => $this->getVar( '_InstallUser' ) ), __METHOD__ ); + $webId = $conn->selectField( '"pg_catalog"."pg_roles"', 'oid', + array( 'rolname' => $this->getVar( 'wgDBuser' ) ), __METHOD__ ); + + return $this->isRoleMember( $conn, $installerId, $webId, $this->maxRoleSearchDepth ); + } + + /** + * Recursive helper for canCreateObjectsForWebUser(). + * @param $conn DatabaseBase object + * @param $targetMember int Role ID of the member to look for + * @param $group int Role ID of the group to look for + * @param $maxDepth int Maximum recursive search depth + * @return bool + */ + protected function isRoleMember( $conn, $targetMember, $group, $maxDepth ) { + if ( $targetMember === $group ) { + // A role is always a member of itself + return true; + } + // Get all members of the given group + $res = $conn->select( '"pg_catalog"."pg_auth_members"', array( 'member' ), + array( 'roleid' => $group ), __METHOD__ ); + foreach ( $res as $row ) { + if ( $row->member == $targetMember ) { + // Found target member + return true; + } + // Recursively search each member of the group to see if the target + // is a member of it, up to the given maximum depth. + if ( $maxDepth > 0 ) { + if ( $this->isRoleMember( $conn, $targetMember, $row->member, $maxDepth - 1 ) ) { + // Found member of member + return true; + } + } + } + return false; } public function preInstall() { + $createDbAccount = array( + 'name' => 'user', + 'callback' => array( $this, 'setupUser' ), + ); $commitCB = array( 'name' => 'pg-commit', 'callback' => array( $this, 'commitChanges' ), ); - $userCB = array( - 'name' => 'user', - 'callback' => array( $this, 'setupUser' ), + $plpgCB = array( + 'name' => 'pg-plpgsql', + 'callback' => array( $this, 'setupPLpgSQL' ), ); - $ts2CB = array( - 'name' => 'pg-ts2', - 'callback' => array( $this, 'setupTs2' ), + $schemaCB = array( + 'name' => 'schema', + 'callback' => array( $this, 'setupSchema' ) ); + + if( $this->getVar( '_CreateDBAccount' ) ) { + $this->parent->addInstallStep( $createDbAccount, 'database' ); + } $this->parent->addInstallStep( $commitCB, 'interwiki' ); - $this->parent->addInstallStep( $userCB ); - $this->parent->addInstallStep( $ts2CB, 'setupDatabase' ); + $this->parent->addInstallStep( $plpgCB, 'database' ); + $this->parent->addInstallStep( $schemaCB, 'database' ); } function setupDatabase() { - $status = $this->getConnection(); + $status = $this->getPgConnection( 'create-db' ); if ( !$status->isOK() ) { return $status; } $conn = $status->value; $dbName = $this->getVar( 'wgDBname' ); - $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $dbName ); - $rows = $conn->numRows( $conn->doQuery( $SQL ) ); - if( !$rows ) { - $schema = $this->getVar( 'wgDBmwschema' ); - $user = $this->getVar( 'wgDBuser' ); - - $safeschema = $conn->addIdentifierQuotes( $schema ); - $safeuser = $conn->addIdentifierQuotes( $user ); + $exists = $conn->selectField( '"pg_catalog"."pg_database"', '1', + array( 'datname' => $dbName ), __METHOD__ ); + if ( !$exists ) { $safedb = $conn->addIdentifierQuotes( $dbName ); - - $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__ ); - - $conn = new DatabasePostgres( - $this->getVar( 'wgDBserver' ), - $this->getVar( 'wgDBuser' ), - $this->getVar( 'wgDBpassword' ), - $dbName, - false, - 0, - $this->getVar( 'wgDBprefix' ) - ); - - $result = $conn->schemaExists( $schema ); - if( !$result ) { - $result = $conn->doQuery( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" ); - if( !$result ) { - $status->fatal( 'config-install-pg-schema-failed', $user, $schema ); - } - } else { - $safeschema2 = $conn->addQuotes( $schema ); - $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n". - "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n". - "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n". - "AND p.relkind IN ('r','S','v')\n"; - $SQL .= "UNION\n"; - $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n". - "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n". - "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n". - "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2"; - $res = $conn->doQuery( $SQL ); - $conn->doQuery( "SET search_path = $safeschema" ); - } + $conn->query( "CREATE DATABASE $safedb", __METHOD__ ); } - return $status; + return Status::newGood(); } - /** - * Ts2 isn't needed in newer versions of Postgres, so wrap it in a nice big - * version check and skip it if we're new. Maybe we can bump $minimumVersion - * one day and render this obsolete :) - * - * @return Status - */ - function setupTs2() { - if( version_compare( $this->db->getServerVersion(), $this->ts2MaxVersion, '<' ) ) { - if ( !$this->db->tableExists( 'pg_ts_cfg', $wgDBts2schema ) ) { - return Status::newFatal( - 'config-install-pg-ts2-failed', - $this->getVar( 'wgDBname' ), - 'http://www.devx.com/opensource/Article/21674/0/page/2' - ); - } - $safeuser = $this->db->addQuotes( $this->getVar( 'wgDBuser' ) ); - foreach ( array( 'cfg', 'cfgmap', 'dict', 'parser' ) as $table ) { - $sql = "GRANT SELECT ON pg_ts_$table TO $safeuser"; - $this->db->query( $sql, __METHOD__ ); + function setupSchema() { + // Get a connection to the target database + $status = $this->getPgConnection( 'create-schema' ); + if ( !$status->isOK() ) { + return $status; + } + $conn = $status->value; + + // Create the schema if necessary + $schema = $this->getVar( 'wgDBmwschema' ); + $safeschema = $conn->addIdentifierQuotes( $schema ); + $safeuser = $conn->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) ); + if( !$conn->schemaExists( $schema ) ) { + try { + $conn->query( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" ); + } catch ( DBQueryError $e ) { + return Status::newFatal( 'config-install-pg-schema-failed', + $this->getVar( '_InstallUser' ), $schema ); } } + + // Select the new schema in the current connection + $conn->determineCoreSchema( $schema ); return Status::newGood(); } function commitChanges() { - $this->db->query( 'COMMIT' ); + $this->db->commit( __METHOD__ ); return Status::newGood(); } @@ -296,34 +492,46 @@ class PostgresInstaller extends DatabaseInstaller { return Status::newGood(); } - $status = $this->getConnection(); + $status = $this->getPgConnection( 'create-db' ); if ( !$status->isOK() ) { return $status; } + $conn = $status->value; - $db = $this->getVar( 'wgDBname' ); - $this->db->selectDB( $db ); - $safeuser = $this->db->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) ); - $safepass = $this->db->addQuotes( $this->getVar( 'wgDBpassword' ) ); - $res = $this->db->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__ ); - return $status; + $safeuser = $conn->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) ); + $safepass = $conn->addQuotes( $this->getVar( 'wgDBpassword' ) ); - if ( $res !== true ) { - $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ) ); + // Check if the user already exists + $userExists = $conn->roleExists( $this->getVar( 'wgDBuser' ) ); + if ( !$userExists ) { + // Create the user + try { + $sql = "CREATE ROLE $safeuser NOCREATEDB LOGIN PASSWORD $safepass"; + + // If the install user is not a superuser, we need to make the install + // user a member of the new user's group, so that the install user will + // be able to create a schema and other objects on behalf of the new user. + if ( !$this->isSuperUser() ) { + $sql .= ' ROLE' . $conn->addIdentifierQuotes( $this->getVar( '_InstallUser' ) ); + } + + $conn->query( $sql, __METHOD__ ); + } catch ( DBQueryError $e ) { + return Status::newFatal( 'config-install-user-create-failed', + $this->getVar( 'wgDBuser' ), $e->getMessage() ); + } } - return $status; + return Status::newGood(); } function getLocalSettings() { $port = $this->getVar( 'wgDBport' ); $schema = $this->getVar( 'wgDBmwschema' ); - $ts2 = $this->getVar( 'wgDBts2schema' ); return "# Postgres specific settings \$wgDBport = \"{$port}\"; -\$wgDBmwschema = \"{$schema}\"; -\$wgDBts2schema = \"{$ts2}\";"; +\$wgDBmwschema = \"{$schema}\";"; } public function preUpgrade() { @@ -335,24 +543,84 @@ class PostgresInstaller extends DatabaseInstaller { $wgDBpassword = $this->getVar( '_InstallPassword' ); } - private function setupPLpgSQL() { - $rows = $this->numRows( - $this->db->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" ) - ); - if ( $rows < 1 ) { - // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it - $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ". - "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'"; - $rows = $this->numRows( $this->db->query( $SQL ) ); - global $wgDBname; - if ( $rows >= 1 ) { - $result = $this->db->query( 'CREATE LANGUAGE plpgsql' ); - if ( !$result ) { - return Status::newFatal( 'config-pg-no-plpgsql', $wgDBname ); - } - } else { - return Status::newFatal( 'config-pg-no-plpgsql', $wgDBname ); + public function createTables() { + $schema = $this->getVar( 'wgDBmwschema' ); + + $status = $this->getConnection(); + if ( !$status->isOK() ) { + return $status; + } + + /** + * @var $conn DatabaseBase + */ + $conn = $status->value; + + if( $conn->tableExists( 'archive' ) ) { + $status->warning( 'config-install-tables-exist' ); + $this->enableLB(); + return $status; + } + + $conn->begin( __METHOD__ ); + + if( !$conn->schemaExists( $schema ) ) { + $status->fatal( 'config-install-pg-schema-not-exist' ); + return $status; + } + $error = $conn->sourceFile( $conn->getSchemaPath() ); + if( $error !== true ) { + $conn->reportQueryError( $error, 0, '', __METHOD__ ); + $conn->rollback( __METHOD__ ); + $status->fatal( 'config-install-tables-failed', $error ); + } else { + $conn->commit( __METHOD__ ); + } + // Resume normal operations + if( $status->isOk() ) { + $this->enableLB(); + } + return $status; + } + + public function setupPLpgSQL() { + // Connect as the install user, since it owns the database and so is + // the user that needs to run "CREATE LANGAUGE" + $status = $this->getPgConnection( 'create-schema' ); + if ( !$status->isOK() ) { + return $status; + } + /** + * @var $conn DatabaseBase + */ + $conn = $status->value; + + $exists = $conn->selectField( '"pg_catalog"."pg_language"', 1, + array( 'lanname' => 'plpgsql' ), __METHOD__ ); + if ( $exists ) { + // Already exists, nothing to do + return Status::newGood(); + } + + // plpgsql is not installed, but if we have a pg_pltemplate table, we + // should be able to create it + $exists = $conn->selectField( + array( '"pg_catalog"."pg_class"', '"pg_catalog"."pg_namespace"' ), + 1, + array( + 'pg_namespace.oid=relnamespace', + 'nspname' => 'pg_catalog', + 'relname' => 'pg_pltemplate', + ), + __METHOD__ ); + if ( $exists ) { + try { + $conn->query( 'CREATE LANGUAGE plpgsql' ); + } catch ( DBQueryError $e ) { + return Status::newFatal( 'config-pg-no-plpgsql', $this->getVar( 'wgDBname' ) ); } + } else { + return Status::newFatal( 'config-pg-no-plpgsql', $this->getVar( 'wgDBname' ) ); } return Status::newGood(); }