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' ),
204 $this->parent
->addInstallStep( $commitCB, 'interwiki' );
205 $this->parent
->addInstallStep( $userCB );
206 $this->parent
->addInstallStep( $ts2CB, 'setupDatabase' );
209 function setupDatabase() {
210 $status = $this->getConnection();
211 if ( !$status->isOK() ) {
214 $conn = $status->value
;
216 $dbName = $this->getVar( 'wgDBname' );
217 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $wgDBname );
218 $rows = $conn->numRows( $conn->doQuery( $SQL ) );
220 $schema = $this->getVar( 'wgDBmwschema' );
221 $user = $this->getVar( 'wgDBuser' );
223 $safeschema = $conn->addIdentifierQuotes( $schema );
224 $safeuser = $conn->addIdentifierQuotes( $user );
226 $safedb = $conn->addIdentifierQuotes( $dbName );
228 $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__
);
230 $conn = new DatabasePostgres(
231 $this->getVar( 'wgDBserver' ),
232 $this->getVar( 'wgDBuser' ),
233 $this->getVar( 'wgDBpassword' ),
237 $this->getVar( 'wgDBprefix' )
240 $result = $conn->schemaExists( $schema );
242 $result = $conn->doQuery( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
244 $status->fatal( 'config-install-pg-schema-failed', $user, $schema );
247 $safeschema2 = $conn->addQuotes( $schema );
248 $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n".
249 "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n".
250 "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n".
251 "AND p.relkind IN ('r','S','v')\n";
253 $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n".
254 "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n".
255 "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n".
256 "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2";
257 $res = $conn->doQuery( $SQL );
258 $conn->doQuery( "SET search_path = $safeschema" );
265 * Ts2 isn't needed in newer versions of Postgres, so wrap it in a nice big
266 * version check and skip it if we're new. Maybe we can bump $minimumVersion
267 * one day and render this obsolete :)
271 function setupTs2() {
272 if( version_compare( $this->db
->getServerVersion(), $this->ts2MaxVersion
, '<' ) ) {
273 if ( !$this->db
->tableExists( 'pg_ts_cfg', $wgDBts2schema ) ) {
274 return Status
::newFatal(
275 'config-install-pg-ts2-failed',
276 $this->getVar( 'wgDBname' ),
277 'http://www.devx.com/opensource/Article/21674/0/page/2'
280 $safeuser = $this->db
->addQuotes( $this->getVar( 'wgDBuser' ) );
281 foreach ( array( 'cfg', 'cfgmap', 'dict', 'parser' ) as $table ) {
282 $sql = "GRANT SELECT ON pg_ts_$table TO $safeuser";
283 $this->db
->query( $sql, __METHOD__
);
286 return Status
::newGood();
289 function commitChanges() {
290 $this->db
->query( 'COMMIT' );
291 return Status
::newGood();
294 function setupUser() {
295 if ( !$this->getVar( '_CreateDBAccount' ) ) {
296 return Status
::newGood();
299 $status = $this->getConnection();
300 if ( !$status->isOK() ) {
304 $db = $this->getVar( 'wgDBname' );
305 $this->db
->selectDB( $db );
306 $safeuser = $this->db
->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
307 $safepass = $this->db
->addQuotes( $this->getVar( 'wgDBpassword' ) );
308 $res = $this->db
->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__
);
311 if ( $res !== true ) {
312 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ) );
318 function getLocalSettings() {
319 $port = $this->getVar( 'wgDBport' );
320 $schema = $this->getVar( 'wgDBmwschema' );
321 $ts2 = $this->getVar( 'wgDBts2schema' );
323 "# Postgres specific settings
324 \$wgDBport = \"{$port}\";
325 \$wgDBmwschema = \"{$schema}\";
326 \$wgDBts2schema = \"{$ts2}\";";
329 public function preUpgrade() {
330 global $wgDBuser, $wgDBpassword;
332 # Normal user and password are selected after this step, so for now
333 # just copy these two
334 $wgDBuser = $this->getVar( '_InstallUser' );
335 $wgDBpassword = $this->getVar( '_InstallPassword' );
338 private function setupPLpgSQL() {
339 $rows = $this->numRows(
340 $this->db
->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
343 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
344 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
345 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
346 $rows = $this->numRows( $this->db
->query( $SQL ) );
349 $result = $this->db
->query( 'CREATE LANGUAGE plpgsql' );
351 return Status
::newFatal( 'config-pg-no-plpgsql', $wgDBname );
354 return Status
::newFatal( 'config-pg-no-plpgsql', $wgDBname );
357 return Status
::newGood();