bf8b6bdc11e6514091e09543d19bdb3a22f7205c
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() );
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 protected function canCreateAccounts() {
111 $status = $this->getConnection();
112 if ( !$status->isOK() ) {
115 $conn = $status->value
;
117 $superuser = $this->getVar( '_InstallUser' );
119 $rights = $conn->selectField( 'pg_catalog.pg_user',
120 'CASE WHEN usesuper IS TRUE THEN
121 CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
122 ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
124 array( 'usename' => $superuser ), __METHOD__
131 if( $rights != 1 && $rights != 3 ) {
138 public function getSettingsForm() {
139 if ( $this->canCreateAccounts() ) {
140 $noCreateMsg = false;
142 $noCreateMsg = 'config-db-web-no-create-privs';
144 $s = $this->getWebUserBox( $noCreateMsg );
149 public function submitSettingsForm() {
150 $status = $this->submitWebUserBox();
151 if ( !$status->isOK() ) {
155 // Validate the create checkbox
156 $canCreate = $this->canCreateAccounts();
158 $this->setVar( '_CreateDBAccount', false );
161 $create = $this->getVar( '_CreateDBAccount' );
165 // Test the web account
167 new DatabasePostgres(
168 $this->getVar( 'wgDBserver' ),
169 $this->getVar( 'wgDBuser' ),
170 $this->getVar( 'wgDBpassword' ),
174 $this->getVar( 'wgDBprefix' )
176 } catch ( DBConnectionError
$e ) {
177 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
181 return Status
::newGood();
184 public function preInstall() {
186 'name' => 'pg-commit',
187 'callback' => array( $this, 'commitChanges' ),
191 'callback' => array( $this, 'setupUser' ),
194 'name' => 'pg-plpgsql',
195 'callback' => array( $this, 'setupPLpgSQL' ),
197 $this->parent
->addInstallStep( $commitCB, 'interwiki' );
198 $this->parent
->addInstallStep( $userCB );
199 $this->parent
->addInstallStep( $plpgCB, 'database' );
202 function setupDatabase() {
203 $status = $this->getConnection();
204 if ( !$status->isOK() ) {
207 $this->setupSchemaVars();
208 $conn = $status->value
;
210 $dbName = $this->getVar( 'wgDBname' );
211 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $dbName );
212 $rows = $conn->numRows( $conn->query( $SQL ) );
214 $schema = $this->getVar( 'wgDBmwschema' );
215 $user = $this->getVar( 'wgDBuser' );
217 $safeschema = $conn->addIdentifierQuotes( $schema );
218 $safeuser = $conn->addIdentifierQuotes( $user );
220 $safedb = $conn->addIdentifierQuotes( $dbName );
222 $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__
);
224 $conn = new DatabasePostgres(
225 $this->getVar( 'wgDBserver' ),
226 $this->getVar( 'wgDBuser' ),
227 $this->getVar( 'wgDBpassword' ),
231 $this->getVar( 'wgDBprefix' )
234 $result = $conn->schemaExists( $schema );
236 $result = $conn->query( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
238 $status->fatal( 'config-install-pg-schema-failed', $user, $schema );
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";
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 $res = $conn->query( $SQL );
252 $conn->query( "SET search_path = $safeschema" );
258 function commitChanges() {
259 $this->db
->query( 'COMMIT' );
260 return Status
::newGood();
263 function setupUser() {
264 if ( !$this->getVar( '_CreateDBAccount' ) ) {
265 return Status
::newGood();
268 $status = $this->getConnection();
269 if ( !$status->isOK() ) {
273 $db = $this->getVar( 'wgDBname' );
274 $this->db
->selectDB( $db );
275 $safeuser = $this->db
->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
276 $safepass = $this->db
->addQuotes( $this->getVar( 'wgDBpassword' ) );
277 $res = $this->db
->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__
);
280 if ( $res !== true ) {
281 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ) );
287 function getLocalSettings() {
288 $port = $this->getVar( 'wgDBport' );
289 $schema = $this->getVar( 'wgDBmwschema' );
291 "# Postgres specific settings
292 \$wgDBport = \"{$port}\";
293 \$wgDBmwschema = \"{$schema}\";";
296 public function preUpgrade() {
297 global $wgDBuser, $wgDBpassword;
299 # Normal user and password are selected after this step, so for now
300 # just copy these two
301 $wgDBuser = $this->getVar( '_InstallUser' );
302 $wgDBpassword = $this->getVar( '_InstallPassword' );
305 public function setupPLpgSQL() {
306 $status = $this->getConnection();
307 if ( !$status->isOK() ) {
311 $rows = $this->db
->numRows(
312 $this->db
->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
315 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
316 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
317 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
318 $rows = $this->db
->numRows( $this->db
->query( $SQL ) );
319 $dbName = $this->getVar( 'wgDBname' );
321 $result = $this->db
->query( 'CREATE LANGUAGE plpgsql' );
323 return Status
::newFatal( 'config-pg-no-plpgsql', $dbName );
326 return Status
::newFatal( 'config-pg-no-plpgsql', $dbName );
329 return Status
::newGood();