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.1';
28 private $ts2MaxVersion = '8.3'; // Doing ts2 is not necessary in PG > 8.3
34 public function isCompiled() {
35 return self
::checkExtension( 'pgsql' );
38 function getConnectForm() {
40 $this->getTextBox( 'wgDBserver', 'config-db-host', array(), $this->parent
->getHelpBox( 'config-db-host-help' ) ) .
41 $this->getTextBox( 'wgDBport', 'config-db-port' ) .
42 Html
::openElement( 'fieldset' ) .
43 Html
::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
44 $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent
->getHelpBox( 'config-db-name-help' ) ) .
45 $this->getTextBox( 'wgDBmwschema', 'config-db-schema', array(), $this->parent
->getHelpBox( 'config-db-schema-help' ) ) .
46 $this->getTextBox( 'wgDBts2schema', 'config-db-ts2-schema' ) .
47 Html
::closeElement( 'fieldset' ) .
48 $this->getInstallUserBox();
51 function submitConnectForm() {
52 // Get variables from the request
53 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBport',
54 'wgDBname', 'wgDBmwschema', 'wgDBts2schema' ) );
57 $status = Status
::newGood();
58 if ( !strlen( $newValues['wgDBname'] ) ) {
59 $status->fatal( 'config-missing-db-name' );
60 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
61 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
63 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) {
64 $status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] );
66 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBts2schema'] ) ) {
67 $status->fatal( 'config-invalid-ts2schema', $newValues['wgDBts2schema'] );
71 if ( $status->isOK() ) {
72 $status->merge( $this->submitInstallUserBox() );
74 if ( !$status->isOK() ) {
79 $status->merge( $this->getConnection() );
80 if ( !$status->isOK() ) {
84 /* //Make sure install user can create
85 $status->merge( $this->canCreateAccounts() );
86 if ( !$status->isOK() ) {
91 $version = $this->db
->getServerVersion();
92 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
93 return Status
::newFatal( 'config-postgres-old', $this->minimumVersion
, $version );
96 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
97 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
101 function getConnection($database = 'template1') {
102 $status = Status
::newGood();
103 if( is_null( $this->db
) ) {
105 $this->db
= new DatabasePostgres(
106 $this->getVar( 'wgDBserver' ),
107 $this->getVar( '_InstallUser' ),
108 $this->getVar( '_InstallPassword' ),
110 $status->value
= $this->db
;
111 } catch ( DBConnectionError
$e ) {
112 $status->fatal( 'config-connection-error', $e->getMessage() );
115 $status->value
= $this->db
;
120 protected function canCreateAccounts() {
121 $status = $this->getConnection();
122 if ( !$status->isOK() ) {
125 $conn = $status->value
;
127 $superuser = $this->getVar( '_InstallUser' );
129 $rights = $conn->selectField( 'pg_catalog.pg_user',
130 'CASE WHEN usesuper IS TRUE THEN
131 CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
132 ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
134 array( 'usename' => $superuser ), __METHOD__
141 if( $rights != 1 && $rights != 3 ) {
148 public function getSettingsForm() {
149 if ( $this->canCreateAccounts() ) {
150 $noCreateMsg = false;
152 $noCreateMsg = 'config-db-web-no-create-privs';
154 $s = $this->getWebUserBox( $noCreateMsg );
159 public function submitSettingsForm() {
160 $status = $this->submitWebUserBox();
161 if ( !$status->isOK() ) {
165 // Validate the create checkbox
166 $canCreate = $this->canCreateAccounts();
168 $this->setVar( '_CreateDBAccount', false );
171 $create = $this->getVar( '_CreateDBAccount' );
175 // Test the web account
177 new DatabasePostgres(
178 $this->getVar( 'wgDBserver' ),
179 $this->getVar( 'wgDBuser' ),
180 $this->getVar( 'wgDBpassword' ),
184 $this->getVar( 'wgDBprefix' )
186 } catch ( DBConnectionError
$e ) {
187 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
191 return Status
::newGood();
194 public function preInstall() {
196 'name' => 'pg-commit',
197 'callback' => array( $this, 'commitChanges' ),
201 'callback' => array( $this, 'setupUser' ),
205 'callback' => array( $this, 'setupTs2' ),
208 'name' => 'pg-plpgsql',
209 'callback' => array( $this, 'setupPLpgSQL' ),
211 $this->parent
->addInstallStep( $commitCB, 'interwiki' );
212 $this->parent
->addInstallStep( $userCB );
213 $this->parent
->addInstallStep( $ts2CB, 'database' );
214 $this->parent
->addInstallStep( $plpgCB, 'database' );
217 function setupDatabase() {
218 $status = $this->getConnection();
219 if ( !$status->isOK() ) {
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 );
266 $conn->query( "SET search_path = $safeschema" );
273 * Ts2 isn't needed in newer versions of Postgres, so wrap it in a nice big
274 * version check and skip it if we're new. Maybe we can bump $minimumVersion
275 * one day and render this obsolete :)
279 function setupTs2() {
280 $status = $this->getConnection();
281 if ( !$status->isOK() ) {
285 if( version_compare( $this->db
->getServerVersion(), $this->ts2MaxVersion
, '<' ) ) {
286 if ( !$this->db
->tableExists( 'pg_ts_cfg', $this->getVar( 'wgDBts2schema' ) ) ) {
287 return Status
::newFatal(
288 'config-install-pg-ts2-failed',
289 $this->getVar( 'wgDBname' ),
290 'http://www.devx.com/opensource/Article/21674/0/page/2'
293 $safeuser = $this->db
->addQuotes( $this->getVar( 'wgDBuser' ) );
294 foreach ( array( 'cfg', 'cfgmap', 'dict', 'parser' ) as $table ) {
295 $sql = "GRANT SELECT ON pg_ts_$table TO $safeuser";
296 $this->db
->query( $sql, __METHOD__
);
299 return Status
::newGood();
302 function commitChanges() {
303 $this->db
->query( 'COMMIT' );
304 return Status
::newGood();
307 function setupUser() {
308 if ( !$this->getVar( '_CreateDBAccount' ) ) {
309 return Status
::newGood();
312 $status = $this->getConnection();
313 if ( !$status->isOK() ) {
317 $db = $this->getVar( 'wgDBname' );
318 $this->db
->selectDB( $db );
319 $safeuser = $this->db
->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
320 $safepass = $this->db
->addQuotes( $this->getVar( 'wgDBpassword' ) );
321 $res = $this->db
->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__
);
324 if ( $res !== true ) {
325 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ) );
331 function getLocalSettings() {
332 $port = $this->getVar( 'wgDBport' );
333 $schema = $this->getVar( 'wgDBmwschema' );
334 $ts2 = $this->getVar( 'wgDBts2schema' );
336 "# Postgres specific settings
337 \$wgDBport = \"{$port}\";
338 \$wgDBmwschema = \"{$schema}\";
339 \$wgDBts2schema = \"{$ts2}\";";
342 public function preUpgrade() {
343 global $wgDBuser, $wgDBpassword;
345 # Normal user and password are selected after this step, so for now
346 # just copy these two
347 $wgDBuser = $this->getVar( '_InstallUser' );
348 $wgDBpassword = $this->getVar( '_InstallPassword' );
351 public function setupPLpgSQL() {
352 $status = $this->getConnection();
353 if ( !$status->isOK() ) {
357 $rows = $this->db
->numRows(
358 $this->db
->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
361 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
362 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
363 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
364 $rows = $this->db
->numRows( $this->db
->query( $SQL ) );
365 $dbName = $this->getVar( 'wgDBname' );
367 $result = $this->db
->query( 'CREATE LANGUAGE plpgsql' );
369 return Status
::newFatal( 'config-pg-no-plpgsql', $dbName );
372 return Status
::newFatal( 'config-pg-no-plpgsql', $dbName );
375 return Status
::newGood();