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';
27 private $useAdmin = false;
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() ) {
73 $this->useAdmin
= true;
75 $status->merge( $this->getConnection() );
76 if ( !$status->isOK() ) {
80 //Make sure install user can create
81 if( !$this->canCreateAccounts() ) {
82 $status->fatal( 'config-pg-no-create-privs' );
84 if ( !$status->isOK() ) {
89 $version = $this->db
->getServerVersion();
90 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
91 return Status
::newFatal( 'config-postgres-old', $this->minimumVersion
, $version );
94 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
95 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
99 public function openConnection() {
100 $status = Status
::newGood();
102 if ( $this->useAdmin
) {
103 $db = new DatabasePostgres(
104 $this->getVar( 'wgDBserver' ),
105 $this->getVar( '_InstallUser' ),
106 $this->getVar( '_InstallPassword' ),
109 $db = new DatabasePostgres(
110 $this->getVar( 'wgDBserver' ),
111 $this->getVar( 'wgDBuser' ),
112 $this->getVar( 'wgDBpassword' ),
113 $this->getVar( 'wgDBname' ) );
115 $status->value
= $db;
116 } catch ( DBConnectionError
$e ) {
117 $status->fatal( 'config-connection-error', $e->getMessage() );
122 protected function canCreateAccounts() {
123 $this->useAdmin
= true;
124 $status = $this->getConnection();
125 if ( !$status->isOK() ) {
128 $conn = $status->value
;
130 $superuser = $this->getVar( '_InstallUser' );
132 $rights = $conn->selectField( 'pg_catalog.pg_user',
133 'CASE WHEN usesuper IS TRUE THEN
134 CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
135 ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
137 array( 'usename' => $superuser ), __METHOD__
144 if( $rights != 1 && $rights != 3 ) {
151 public function getSettingsForm() {
152 if ( $this->canCreateAccounts() ) {
153 $noCreateMsg = false;
155 $noCreateMsg = 'config-db-web-no-create-privs';
157 $s = $this->getWebUserBox( $noCreateMsg );
162 public function submitSettingsForm() {
163 $status = $this->submitWebUserBox();
164 if ( !$status->isOK() ) {
168 // Validate the create checkbox
169 $canCreate = $this->canCreateAccounts();
171 $this->setVar( '_CreateDBAccount', false );
174 $create = $this->getVar( '_CreateDBAccount' );
178 // Test the web account
180 new DatabasePostgres(
181 $this->getVar( 'wgDBserver' ),
182 $this->getVar( 'wgDBuser' ),
183 $this->getVar( 'wgDBpassword' ),
187 $this->getVar( 'wgDBprefix' )
189 } catch ( DBConnectionError
$e ) {
190 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
194 return Status
::newGood();
197 public function preInstall() {
199 'name' => 'pg-commit',
200 'callback' => array( $this, 'commitChanges' ),
204 'callback' => array( $this, 'setupUser' ),
207 'name' => 'pg-plpgsql',
208 'callback' => array( $this, 'setupPLpgSQL' ),
210 $this->parent
->addInstallStep( $commitCB, 'interwiki' );
211 $this->parent
->addInstallStep( $userCB );
212 $this->parent
->addInstallStep( $plpgCB, 'database' );
215 function setupDatabase() {
216 $this->useAdmin
= TRUE;
217 $status = $this->getConnection();
218 if ( !$status->isOK() ) {
221 $this->setupSchemaVars();
222 $conn = $status->value
;
224 $dbName = $this->getVar( 'wgDBname' );
225 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $dbName );
226 $rows = $conn->numRows( $conn->query( $SQL ) );
228 $schema = $this->getVar( 'wgDBmwschema' );
229 $user = $this->getVar( 'wgDBuser' );
231 $safeschema = $conn->addIdentifierQuotes( $schema );
232 $safeuser = $conn->addIdentifierQuotes( $user );
234 $safedb = $conn->addIdentifierQuotes( $dbName );
236 $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__
);
238 $conn = new DatabasePostgres(
239 $this->getVar( 'wgDBserver' ),
240 $this->getVar( 'wgDBuser' ),
241 $this->getVar( 'wgDBpassword' ),
245 $this->getVar( 'wgDBprefix' )
248 $result = $conn->schemaExists( $schema );
250 $result = $conn->query( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
252 $status->fatal( 'config-install-pg-schema-failed', $user, $schema );
255 $safeschema2 = $conn->addQuotes( $schema );
256 $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n".
257 "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n" .
258 "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n" .
259 "AND p.relkind IN ('r','S','v')\n";
261 $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n".
262 "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n" .
263 "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n" .
264 "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2";
265 $res = $conn->query( $SQL );
271 function commitChanges() {
272 $this->db
->query( 'COMMIT' );
273 return Status
::newGood();
276 function setupUser() {
277 if ( !$this->getVar( '_CreateDBAccount' ) ) {
278 return Status
::newGood();
281 $this->useAdmin
= TRUE;
282 $status = $this->getConnection();
284 if ( !$status->isOK() ) {
288 $db = $this->getVar( 'wgDBname' );
289 $schema = $this->getVar( 'wgDBmwschema' );
290 $this->db
->selectDB( $db );
291 $safeuser = $this->db
->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
292 $safeusercheck = $this->db
->addQuotes( $this->getVar( 'wgDBuser' ) );
293 $safepass = $this->db
->addQuotes( $this->getVar( 'wgDBpassword' ) );
294 $safeschema = $this->db
->addIdentifierQuotes( $schema );
296 $rows = $this->db
->numRows(
297 $this->db
->query( "SELECT 1 FROM pg_catalog.pg_shadow WHERE usename = $safeusercheck" )
300 $res = $this->db
->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__
);
301 if ( $res !== true && !( $res instanceOf ResultWrapper
) ) {
302 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $res );
304 $this->db
->query("ALTER USER $safeuser SET search_path = $safeschema");
310 function getLocalSettings() {
311 $port = $this->getVar( 'wgDBport' );
312 $schema = $this->getVar( 'wgDBmwschema' );
314 "# Postgres specific settings
315 \$wgDBport = \"{$port}\";
316 \$wgDBmwschema = \"{$schema}\";";
319 public function preUpgrade() {
320 global $wgDBuser, $wgDBpassword;
322 # Normal user and password are selected after this step, so for now
323 # just copy these two
324 $wgDBuser = $this->getVar( '_InstallUser' );
325 $wgDBpassword = $this->getVar( '_InstallPassword' );
328 public function createTables() {
330 $this->useAdmin
= false;
331 $status = $this->getConnection();
332 if ( !$status->isOK() ) {
335 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
337 if( $this->db
->tableExists( 'user' ) ) {
338 $status->warning( 'config-install-tables-exist' );
342 $this->db
->setFlag( DBO_DDLMODE
); // For Oracle's handling of schema files
343 $this->db
->begin( __METHOD__
);
345 $error = $this->db
->sourceFile( $this->db
->getSchema() );
346 if( $error !== true ) {
347 $this->db
->reportQueryError( $error, 0, '', __METHOD__
);
348 $this->db
->rollback( __METHOD__
);
349 $status->fatal( 'config-install-tables-failed', $error );
351 $this->db
->commit( __METHOD__
);
353 // Resume normal operations
354 if( $status->isOk() ) {
360 public function setupPLpgSQL() {
361 $this->useAdmin
= TRUE;
362 $status = $this->getConnection();
363 if ( !$status->isOK() ) {
367 $rows = $this->db
->numRows(
368 $this->db
->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
371 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
372 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
373 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
374 $rows = $this->db
->numRows( $this->db
->query( $SQL ) );
375 $dbName = $this->getVar( 'wgDBname' );
377 $result = $this->db
->query( 'CREATE LANGUAGE plpgsql' );
379 return Status
::newFatal( 'config-pg-no-plpgsql', $dbName );
382 return Status
::newFatal( 'config-pg-no-plpgsql', $dbName );
385 return Status
::newGood();