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__
140 if( !$rights ||
( $rights != 1 && $rights != 3 ) ) {
147 public function getSettingsForm() {
148 if ( $this->canCreateAccounts() ) {
149 $noCreateMsg = false;
151 $noCreateMsg = 'config-db-web-no-create-privs';
153 $s = $this->getWebUserBox( $noCreateMsg );
158 public function submitSettingsForm() {
159 $status = $this->submitWebUserBox();
160 if ( !$status->isOK() ) {
164 // Validate the create checkbox
166 $canCreate = $this->canCreateAccounts();
168 $this->setVar( '_CreateDBAccount', false );
171 $create = $this->getVar( '_CreateDBAccount' );
174 // Don't test the web account if it is the same as the admin.
175 if ( !$create && $this->getVar( 'wgDBuser' ) != $this->getVar( '_InstallUser' ) ) {
176 // Test the web account
178 $this->useAdmin
= false;
179 return $this->openConnection();
180 } catch ( DBConnectionError
$e ) {
181 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
185 return Status
::newGood();
188 public function preInstall() {
190 'name' => 'pg-commit',
191 'callback' => array( $this, 'commitChanges' ),
195 'callback' => array( $this, 'setupUser' ),
198 'name' => 'pg-plpgsql',
199 'callback' => array( $this, 'setupPLpgSQL' ),
201 $this->parent
->addInstallStep( $commitCB, 'interwiki' );
202 $this->parent
->addInstallStep( $userCB );
203 $this->parent
->addInstallStep( $plpgCB, 'database' );
206 function setupDatabase() {
207 $this->useAdmin
= true;
208 $status = $this->getConnection();
209 if ( !$status->isOK() ) {
212 $this->setupSchemaVars();
213 $conn = $status->value
;
215 $dbName = $this->getVar( 'wgDBname' );
216 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $dbName );
217 $rows = $conn->numRows( $conn->query( $SQL ) );
219 $schema = $this->getVar( 'wgDBmwschema' );
220 $user = $this->getVar( 'wgDBuser' );
222 $safeschema = $conn->addIdentifierQuotes( $schema );
223 $safeuser = $conn->addIdentifierQuotes( $user );
225 $safedb = $conn->addIdentifierQuotes( $dbName );
227 $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__
);
229 $this->useAdmin
= false;
230 $status = $this->getConnection();
231 if ( !$status->isOK() ) {
234 $conn = $status->value
;
236 $result = $conn->schemaExists( $schema );
238 $result = $conn->query( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
240 $status->fatal( 'config-install-pg-schema-failed', $user, $schema );
243 $safeschema2 = $conn->addQuotes( $schema );
244 $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n".
245 "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n" .
246 "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n" .
247 "AND p.relkind IN ('r','S','v')\n";
249 $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n".
250 "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n" .
251 "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n" .
252 "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2";
253 $conn->query( "SET search_path = $safeschema" );
254 $res = $conn->query( $SQL );
260 function commitChanges() {
261 $this->db
->query( 'COMMIT' );
262 return Status
::newGood();
265 function setupUser() {
266 if ( !$this->getVar( '_CreateDBAccount' ) ) {
267 return Status
::newGood();
270 $this->useAdmin
= true;
271 $status = $this->getConnection();
273 if ( !$status->isOK() ) {
277 $schema = $this->getVar( 'wgDBmwschema' );
278 $safeuser = $this->db
->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
279 $safeusercheck = $this->db
->addQuotes( $this->getVar( 'wgDBuser' ) );
280 $safepass = $this->db
->addQuotes( $this->getVar( 'wgDBpassword' ) );
281 $safeschema = $this->db
->addIdentifierQuotes( $schema );
283 $rows = $this->db
->numRows(
284 $this->db
->query( "SELECT 1 FROM pg_catalog.pg_shadow WHERE usename = $safeusercheck" )
287 $res = $this->db
->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__
);
288 if ( $res !== true && !( $res instanceOf ResultWrapper
) ) {
289 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $res );
291 $this->db
->query("ALTER USER $safeuser SET search_path = $safeschema");
297 function getLocalSettings() {
298 $port = $this->getVar( 'wgDBport' );
299 $schema = $this->getVar( 'wgDBmwschema' );
301 "# Postgres specific settings
302 \$wgDBport = \"{$port}\";
303 \$wgDBmwschema = \"{$schema}\";";
306 public function preUpgrade() {
307 global $wgDBuser, $wgDBpassword;
309 # Normal user and password are selected after this step, so for now
310 # just copy these two
311 $wgDBuser = $this->getVar( '_InstallUser' );
312 $wgDBpassword = $this->getVar( '_InstallPassword' );
315 public function createTables() {
316 $schema = $this->getVar( 'wgDBmwschema' );
317 $user = $this->getVar( 'wgDBuser' );
320 $this->useAdmin
= false;
321 $status = $this->getConnection();
322 if ( !$status->isOK() ) {
325 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
327 if( $this->db
->tableExists( 'user' ) ) {
328 $status->warning( 'config-install-tables-exist' );
332 $this->db
->setFlag( DBO_DDLMODE
); // For Oracle's handling of schema files
333 $this->db
->begin( __METHOD__
);
336 if( !$this->db
->schemaExists( $schema ) ) {
337 $status->error( 'config-install-pg-schema-not-exist' );
340 $safeschema = $this->db
->addIdentifierQuotes( $schema );
341 $this->db
->query( "SET search_path = $safeschema" );
342 $error = $this->db
->sourceFile( $this->db
->getSchema() );
343 if( $error !== true ) {
344 $this->db
->reportQueryError( $error, 0, '', __METHOD__
);
345 $this->db
->rollback( __METHOD__
);
346 $status->fatal( 'config-install-tables-failed', $error );
348 $this->db
->commit( __METHOD__
);
350 // Resume normal operations
351 if( $status->isOk() ) {
357 public function setupPLpgSQL() {
358 $this->useAdmin
= true;
359 $status = $this->getConnection();
360 if ( !$status->isOK() ) {
364 $rows = $this->db
->numRows(
365 $this->db
->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
368 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
369 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
370 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
371 $rows = $this->db
->numRows( $this->db
->query( $SQL ) );
372 $dbName = $this->getVar( 'wgDBname' );
374 $result = $this->db
->query( 'CREATE LANGUAGE plpgsql' );
376 return Status
::newFatal( 'config-pg-no-plpgsql', $dbName );
379 return Status
::newFatal( 'config-pg-no-plpgsql', $dbName );
382 return Status
::newGood();