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();
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() );
117 protected function canCreateAccounts() {
118 $status = $this->getConnection();
119 if ( !$status->isOK() ) {
122 $conn = $status->value
;
124 $superuser = $this->getVar( '_InstallUser' );
126 $rights = $conn->selectField( 'pg_catalog.pg_user',
127 'CASE WHEN usesuper IS TRUE THEN
128 CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
129 ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
131 array( 'usename' => $superuser ), __METHOD__
138 if( $rights != 1 && $rights != 3 ) {
145 public function getSettingsForm() {
146 if ( $this->canCreateAccounts() ) {
147 $noCreateMsg = false;
149 $noCreateMsg = 'config-db-web-no-create-privs';
151 $s = $this->getWebUserBox( $noCreateMsg );
156 public function submitSettingsForm() {
157 $status = $this->submitWebUserBox();
158 if ( !$status->isOK() ) {
162 // Validate the create checkbox
163 $canCreate = $this->canCreateAccounts();
165 $this->setVar( '_CreateDBAccount', false );
168 $create = $this->getVar( '_CreateDBAccount' );
172 // Test the web account
174 new DatabasePostgres(
175 $this->getVar( 'wgDBserver' ),
176 $this->getVar( 'wgDBuser' ),
177 $this->getVar( 'wgDBpassword' ),
181 $this->getVar( 'wgDBprefix' )
183 } catch ( DBConnectionError
$e ) {
184 return Status
::newFatal( 'config-connection-error', $e->getMessage() );
188 return Status
::newGood();
191 public function preInstall() {
193 'name' => 'pg-commit',
194 'callback' => array( $this, 'commitChanges' ),
198 'callback' => array( $this, 'setupUser' ),
202 'callback' => array( $this, 'setupTs2' ),
205 'name' => 'pg-plpgsql',
206 'callback' => array( $this, 'setupPLpgSQL' ),
208 $this->parent
->addInstallStep( $commitCB, 'interwiki' );
209 $this->parent
->addInstallStep( $userCB );
210 $this->parent
->addInstallStep( $ts2CB, 'database' );
211 $this->parent
->addInstallStep( $plpgCB, 'database' );
214 function setupDatabase() {
215 $status = $this->getConnection();
216 if ( !$status->isOK() ) {
219 $conn = $status->value
;
221 $dbName = $this->getVar( 'wgDBname' );
222 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $dbName );
223 $rows = $conn->numRows( $conn->doQuery( $SQL ) );
225 $schema = $this->getVar( 'wgDBmwschema' );
226 $user = $this->getVar( 'wgDBuser' );
228 $safeschema = $conn->addIdentifierQuotes( $schema );
229 $safeuser = $conn->addIdentifierQuotes( $user );
231 $safedb = $conn->addIdentifierQuotes( $dbName );
233 $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__
);
235 $conn = new DatabasePostgres(
236 $this->getVar( 'wgDBserver' ),
237 $this->getVar( 'wgDBuser' ),
238 $this->getVar( 'wgDBpassword' ),
242 $this->getVar( 'wgDBprefix' )
245 $result = $conn->schemaExists( $schema );
247 $result = $conn->doQuery( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
249 $status->fatal( 'config-install-pg-schema-failed', $user, $schema );
252 $safeschema2 = $conn->addQuotes( $schema );
253 $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n".
254 "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n".
255 "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n".
256 "AND p.relkind IN ('r','S','v')\n";
258 $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n".
259 "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n".
260 "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n".
261 "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2";
262 $res = $conn->doQuery( $SQL );
263 $conn->doQuery( "SET search_path = $safeschema" );
270 * Ts2 isn't needed in newer versions of Postgres, so wrap it in a nice big
271 * version check and skip it if we're new. Maybe we can bump $minimumVersion
272 * one day and render this obsolete :)
276 function setupTs2() {
277 $status = $this->getConnection();
278 if ( !$status->isOK() ) {
282 if( version_compare( $this->db
->getServerVersion(), $this->ts2MaxVersion
, '<' ) ) {
283 if ( !$this->db
->tableExists( 'pg_ts_cfg', $wgDBts2schema ) ) {
284 return Status
::newFatal(
285 'config-install-pg-ts2-failed',
286 $this->getVar( 'wgDBname' ),
287 'http://www.devx.com/opensource/Article/21674/0/page/2'
290 $safeuser = $this->db
->addQuotes( $this->getVar( 'wgDBuser' ) );
291 foreach ( array( 'cfg', 'cfgmap', 'dict', 'parser' ) as $table ) {
292 $sql = "GRANT SELECT ON pg_ts_$table TO $safeuser";
293 $this->db
->query( $sql, __METHOD__
);
296 return Status
::newGood();
299 function commitChanges() {
300 $this->db
->query( 'COMMIT' );
301 return Status
::newGood();
304 function setupUser() {
305 if ( !$this->getVar( '_CreateDBAccount' ) ) {
306 return Status
::newGood();
309 $status = $this->getConnection();
310 if ( !$status->isOK() ) {
314 $db = $this->getVar( 'wgDBname' );
315 $this->db
->selectDB( $db );
316 $safeuser = $this->db
->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
317 $safepass = $this->db
->addQuotes( $this->getVar( 'wgDBpassword' ) );
318 $res = $this->db
->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__
);
321 if ( $res !== true ) {
322 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ) );
328 function getLocalSettings() {
329 $port = $this->getVar( 'wgDBport' );
330 $schema = $this->getVar( 'wgDBmwschema' );
331 $ts2 = $this->getVar( 'wgDBts2schema' );
333 "# Postgres specific settings
334 \$wgDBport = \"{$port}\";
335 \$wgDBmwschema = \"{$schema}\";
336 \$wgDBts2schema = \"{$ts2}\";";
339 public function preUpgrade() {
340 global $wgDBuser, $wgDBpassword;
342 # Normal user and password are selected after this step, so for now
343 # just copy these two
344 $wgDBuser = $this->getVar( '_InstallUser' );
345 $wgDBpassword = $this->getVar( '_InstallPassword' );
348 public function setupPLpgSQL() {
349 $status = $this->getConnection();
350 if ( !$status->isOK() ) {
354 $rows = $this->db
->numRows(
355 $this->db
->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
358 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
359 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
360 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
361 $rows = $this->db
->numRows( $this->db
->query( $SQL ) );
362 $dbName = $this->getVar( 'wgDBname' );
364 $result = $this->db
->query( 'CREATE LANGUAGE plpgsql' );
366 return Status
::newFatal( 'config-pg-no-plpgsql', $dbName );
369 return Status
::newFatal( 'config-pg-no-plpgsql', $dbName );
372 return Status
::newGood();