3 * PostgreSQL-specific installer.
10 * Class for setting up the MediaWiki database using Postgres.
15 class PostgresInstaller
extends DatabaseInstaller
{
17 protected $globalNames = array(
27 var $minimumVersion = '8.3';
33 public function isCompiled() {
34 return self
::checkExtension( 'pgsql' );
37 function getConnectForm() {
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();
49 function submitConnectForm() {
50 // Get variables from the request
51 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBport',
52 'wgDBname', 'wgDBmwschema' ) );
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'] );
61 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) {
62 $status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] );
66 if ( $status->isOK() ) {
67 $status->merge( $this->submitInstallUserBox() );
69 if ( !$status->isOK() ) {
74 $status->merge( $this->getConnection() );
75 if ( !$status->isOK() ) {
79 /* //Make sure install user can create
80 $status->merge( $this->canCreateAccounts() );
81 if ( !$status->isOK() ) {
86 $version = $this->db
->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' ) );
96 function openConnection( $database = 'template1' ) {
97 $status = Status
::newGood();
99 $db = new DatabasePostgres(
100 $this->getVar( 'wgDBserver' ),
101 $this->getVar( '_InstallUser' ),
102 $this->getVar( '_InstallPassword' ),
104 $status->value
= $db;
105 } catch ( DBConnectionError
$e ) {
106 $status->fatal( 'config-connection-error', $e->getMessage() );
111 protected function canCreateAccounts() {
112 $status = $this->getConnection();
113 if ( !$status->isOK() ) {
116 $conn = $status->value
;
118 $superuser = $this->getVar( '_InstallUser' );
120 $rights = $conn->selectField( 'pg_catalog.pg_user',
121 'CASE WHEN usesuper IS TRUE THEN
122 CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
123 ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
125 array( 'usename' => $superuser ), __METHOD__
132 if( $rights != 1 && $rights != 3 ) {
139 public function getSettingsForm() {
140 if ( $this->canCreateAccounts() ) {
141 $noCreateMsg = false;
143 $noCreateMsg = 'config-db-web-no-create-privs';
145 $s = $this->getWebUserBox( $noCreateMsg );
150 public function submitSettingsForm() {
151 $status = $this->submitWebUserBox();
152 if ( !$status->isOK() ) {
156 // Validate the create checkbox
157 $canCreate = $this->canCreateAccounts();
159 $this->setVar( '_CreateDBAccount', false );
162 $create = $this->getVar( '_CreateDBAccount' );
166 // Test the web account
168 new DatabasePostgres(
169 $this->getVar( 'wgDBserver' ),
170 $this->getVar( 'wgDBuser' ),
171 $this->getVar( 'wgDBpassword' ),
175 $this->getVar( 'wgDBprefix' )
177 } catch ( DBConnectionError
$e ) {
178 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
182 return Status
::newGood();
185 public function preInstall() {
187 'name' => 'pg-commit',
188 'callback' => array( $this, 'commitChanges' ),
192 'callback' => array( $this, 'setupUser' ),
195 'name' => 'pg-plpgsql',
196 'callback' => array( $this, 'setupPLpgSQL' ),
198 $this->parent
->addInstallStep( $commitCB, 'interwiki' );
199 $this->parent
->addInstallStep( $userCB );
200 $this->parent
->addInstallStep( $plpgCB, 'database' );
203 function setupDatabase() {
204 $status = $this->getConnection();
205 if ( !$status->isOK() ) {
208 $this->setupSchemaVars();
209 $conn = $status->value
;
211 $dbName = $this->getVar( 'wgDBname' );
212 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $dbName );
213 $rows = $conn->numRows( $conn->query( $SQL ) );
215 $schema = $this->getVar( 'wgDBmwschema' );
216 $user = $this->getVar( 'wgDBuser' );
218 $safeschema = $conn->addIdentifierQuotes( $schema );
219 $safeuser = $conn->addIdentifierQuotes( $user );
221 $safedb = $conn->addIdentifierQuotes( $dbName );
223 $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__
);
225 $conn = new DatabasePostgres(
226 $this->getVar( 'wgDBserver' ),
227 $this->getVar( 'wgDBuser' ),
228 $this->getVar( 'wgDBpassword' ),
232 $this->getVar( 'wgDBprefix' )
235 $result = $conn->schemaExists( $schema );
237 $result = $conn->query( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
239 $status->fatal( 'config-install-pg-schema-failed', $user, $schema );
242 $safeschema2 = $conn->addQuotes( $schema );
243 $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n".
244 "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n" .
245 "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n" .
246 "AND p.relkind IN ('r','S','v')\n";
248 $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n".
249 "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n" .
250 "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n" .
251 "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2";
252 $res = $conn->query( $SQL );
253 $conn->query( "SET search_path = $safeschema" );
259 function commitChanges() {
260 $this->db
->query( 'COMMIT' );
261 return Status
::newGood();
264 function setupUser() {
265 if ( !$this->getVar( '_CreateDBAccount' ) ) {
266 return Status
::newGood();
269 $status = $this->getConnection();
270 if ( !$status->isOK() ) {
274 $db = $this->getVar( 'wgDBname' );
275 $this->db
->selectDB( $db );
276 $safeuser = $this->db
->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
277 $safepass = $this->db
->addQuotes( $this->getVar( 'wgDBpassword' ) );
278 $res = $this->db
->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__
);
281 if ( $res !== true ) {
282 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ) );
288 function getLocalSettings() {
289 $port = $this->getVar( 'wgDBport' );
290 $schema = $this->getVar( 'wgDBmwschema' );
291 $ts2 = $this->getVar( 'wgDBts2schema' );
293 "# Postgres specific settings
294 \$wgDBport = \"{$port}\";
295 \$wgDBmwschema = \"{$schema}\";
296 \$wgDBts2schema = \"{$ts2}\";";
299 public function preUpgrade() {
300 global $wgDBuser, $wgDBpassword;
302 # Normal user and password are selected after this step, so for now
303 # just copy these two
304 $wgDBuser = $this->getVar( '_InstallUser' );
305 $wgDBpassword = $this->getVar( '_InstallPassword' );
308 public function setupPLpgSQL() {
309 $status = $this->getConnection();
310 if ( !$status->isOK() ) {
314 $rows = $this->db
->numRows(
315 $this->db
->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
318 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
319 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
320 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
321 $rows = $this->db
->numRows( $this->db
->query( $SQL ) );
322 $dbName = $this->getVar( 'wgDBname' );
324 $result = $this->db
->query( 'CREATE LANGUAGE plpgsql' );
326 return Status
::newFatal( 'config-pg-no-plpgsql', $dbName );
329 return Status
::newFatal( 'config-pg-no-plpgsql', $dbName );
332 return Status
::newGood();