3 * PostgreSQL-specific installer.
10 * Class for setting up the MediaWiki database using Postgres.
15 class PostgresInstaller
extends DatabaseInstaller
{
17 protected $globalNames = array(
26 var $minimumVersion = '8.3';
32 public function isCompiled() {
33 return self
::checkExtension( 'pgsql' );
36 function getConnectForm() {
38 $this->getTextBox( 'wgDBserver', 'config-db-host', array(), $this->parent
->getHelpBox( 'config-db-host-help' ) ) .
39 $this->getTextBox( 'wgDBport', 'config-db-port' ) .
40 Html
::openElement( 'fieldset' ) .
41 Html
::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
42 $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent
->getHelpBox( 'config-db-name-help' ) ) .
43 $this->getTextBox( 'wgDBmwschema', 'config-db-schema', array(), $this->parent
->getHelpBox( 'config-db-schema-help' ) ) .
44 Html
::closeElement( 'fieldset' ) .
45 $this->getInstallUserBox();
48 function submitConnectForm() {
49 // Get variables from the request
50 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBport',
51 'wgDBname', 'wgDBmwschema' ) );
54 $status = Status
::newGood();
55 if ( !strlen( $newValues['wgDBname'] ) ) {
56 $status->fatal( 'config-missing-db-name' );
57 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
58 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
60 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) {
61 $status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] );
65 if ( $status->isOK() ) {
66 $status->merge( $this->submitInstallUserBox() );
68 if ( !$status->isOK() ) {
73 $status->merge( $this->getConnection( 'template1' ) );
74 if ( !$status->isOK() ) {
78 /* //Make sure install user can create
79 $status->merge( $this->canCreateAccounts() );
80 if ( !$status->isOK() ) {
85 $version = $this->db
->getServerVersion();
86 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
87 return Status
::newFatal( 'config-postgres-old', $this->minimumVersion
, $version );
90 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
91 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
95 function openConnection( $database = 'template1' ) {
96 $status = Status
::newGood();
98 $db = new DatabasePostgres(
99 $this->getVar( 'wgDBserver' ),
100 $this->getVar( '_InstallUser' ),
101 $this->getVar( '_InstallPassword' ),
103 $status->value
= $db;
104 } catch ( DBConnectionError
$e ) {
105 $status->fatal( 'config-connection-error', $e->getMessage() );
110 function getConnection($database = null) {
111 $status = Status
::newGood();
113 if( is_null( $database ) ) {
114 $dbname = $this->getVar( 'wgDBname' );
115 $dbuser = $this->getVar( 'wgDBuser' );
116 $dbpass = $this->getVar( 'wgDBpassword' );
119 $dbuser = $this->getVar( '_InstallUser' );
120 $dbpass = $this->getVar( '_InstallPassword' );
124 $this->db
= new DatabasePostgres(
125 $this->getVar( 'wgDBserver' ),
126 $dbuser, $dbpass, $dbname );
127 $status->value
= $this->db
;
128 } catch ( DBConnectionError
$e ) {
129 $status->fatal( 'config-connection-error', $e->getMessage() );
134 protected function canCreateAccounts() {
135 $status = $this->getConnection( 'template1' );
136 if ( !$status->isOK() ) {
139 $conn = $status->value
;
141 $superuser = $this->getVar( '_InstallUser' );
143 $rights = $conn->selectField( 'pg_catalog.pg_user',
144 'CASE WHEN usesuper IS TRUE THEN
145 CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
146 ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
148 array( 'usename' => $superuser ), __METHOD__
155 if( $rights != 1 && $rights != 3 ) {
162 public function getSettingsForm() {
163 if ( $this->canCreateAccounts() ) {
164 $noCreateMsg = false;
166 $noCreateMsg = 'config-db-web-no-create-privs';
168 $s = $this->getWebUserBox( $noCreateMsg );
173 public function submitSettingsForm() {
174 $status = $this->submitWebUserBox();
175 if ( !$status->isOK() ) {
179 // Validate the create checkbox
180 $canCreate = $this->canCreateAccounts();
182 $this->setVar( '_CreateDBAccount', false );
185 $create = $this->getVar( '_CreateDBAccount' );
189 // Test the web account
191 new DatabasePostgres(
192 $this->getVar( 'wgDBserver' ),
193 $this->getVar( 'wgDBuser' ),
194 $this->getVar( 'wgDBpassword' ),
198 $this->getVar( 'wgDBprefix' )
200 } catch ( DBConnectionError
$e ) {
201 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
205 return Status
::newGood();
208 public function preInstall() {
210 'name' => 'pg-commit',
211 'callback' => array( $this, 'commitChanges' ),
215 'callback' => array( $this, 'setupUser' ),
218 'name' => 'pg-plpgsql',
219 'callback' => array( $this, 'setupPLpgSQL' ),
221 $this->parent
->addInstallStep( $commitCB, 'interwiki' );
222 $this->parent
->addInstallStep( $userCB );
223 $this->parent
->addInstallStep( $plpgCB, 'database' );
226 function setupDatabase() {
227 $status = $this->getConnection( 'template1' );
228 if ( !$status->isOK() ) {
231 $this->setupSchemaVars();
232 $conn = $status->value
;
234 $dbName = $this->getVar( 'wgDBname' );
235 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $dbName );
236 $rows = $conn->numRows( $conn->query( $SQL ) );
238 $schema = $this->getVar( 'wgDBmwschema' );
239 $user = $this->getVar( 'wgDBuser' );
241 $safeschema = $conn->addIdentifierQuotes( $schema );
242 $safeuser = $conn->addIdentifierQuotes( $user );
244 $safedb = $conn->addIdentifierQuotes( $dbName );
246 $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__
);
248 $conn = new DatabasePostgres(
249 $this->getVar( 'wgDBserver' ),
250 $this->getVar( 'wgDBuser' ),
251 $this->getVar( 'wgDBpassword' ),
255 $this->getVar( 'wgDBprefix' )
258 $result = $conn->schemaExists( $schema );
260 $result = $conn->query( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
262 $status->fatal( 'config-install-pg-schema-failed', $user, $schema );
265 $safeschema2 = $conn->addQuotes( $schema );
266 $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n".
267 "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n" .
268 "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n" .
269 "AND p.relkind IN ('r','S','v')\n";
271 $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n".
272 "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n" .
273 "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n" .
274 "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2";
275 $res = $conn->query( $SQL );
276 $conn->query( "SET search_path = $safeschema" );
282 function commitChanges() {
283 $this->db
->query( 'COMMIT' );
284 return Status
::newGood();
287 function setupUser() {
288 if ( !$this->getVar( '_CreateDBAccount' ) ) {
289 return Status
::newGood();
292 $status = $this->getConnection( 'template1' );
293 if ( !$status->isOK() ) {
297 $db = $this->getVar( 'wgDBname' );
298 $this->db
->selectDB( $db );
299 $safeuser = $this->db
->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
300 $safeusercheck = $this->db
->addQuotes( $this->getVar( 'wgDBuser' ) );
301 $safepass = $this->db
->addQuotes( $this->getVar( 'wgDBpassword' ) );
303 $rows = $this->db
->numRows(
304 $this->db
->query( "SELECT 1 FROM pg_catalog.pg_shadow WHERE usename = $safeusercheck" )
307 $res = $this->db
->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__
);
309 if ( $res !== true && !( $res instanceOf ResultWrapper
) ) {
310 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $res );
317 function getLocalSettings() {
318 $port = $this->getVar( 'wgDBport' );
319 $schema = $this->getVar( 'wgDBmwschema' );
321 "# Postgres specific settings
322 \$wgDBport = \"{$port}\";
323 \$wgDBmwschema = \"{$schema}\";";
326 public function preUpgrade() {
327 global $wgDBuser, $wgDBpassword;
329 # Normal user and password are selected after this step, so for now
330 # just copy these two
331 $wgDBuser = $this->getVar( '_InstallUser' );
332 $wgDBpassword = $this->getVar( '_InstallPassword' );
335 public function setupPLpgSQL() {
336 $status = $this->getConnection();
337 if ( !$status->isOK() ) {
341 $rows = $this->db
->numRows(
342 $this->db
->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
345 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
346 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
347 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
348 $rows = $this->db
->numRows( $this->db
->query( $SQL ) );
349 $dbName = $this->getVar( 'wgDBname' );
351 $result = $this->db
->query( 'CREATE LANGUAGE plpgsql' );
353 return Status
::newFatal( 'config-pg-no-plpgsql', $dbName );
356 return Status
::newFatal( 'config-pg-no-plpgsql', $dbName );
359 return Status
::newGood();