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() ) {
85 $version = $this->db
->getServerVersion();
86 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
87 return Status
::newFatal( 'config-postgres-old', $this->minimumVersion
, $version );
90 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
91 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
95 function getConnection() {
96 $status = Status
::newGood();
99 $this->db
= new DatabasePostgres(
100 $this->getVar( 'wgDBserver' ),
101 $this->getVar( '_InstallUser' ),
102 $this->getVar( '_InstallPassword' ),
104 $status->value
= $this->db
;
105 } catch ( DBConnectionError
$e ) {
106 $status->fatal( 'config-connection-error', $e->getMessage() );
111 protected function canCreateAccounts() {
112 $status = $this->getConnection();
113 if ( !$status->isOK() ) {
116 $conn = $status->value
;
118 $superuser = $this->getVar( '_InstallUser' );
120 $rights = $conn->query( 'pg_catalog.pg_user',
122 CASE WHEN usesuper IS TRUE THEN
123 CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
124 ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
126 array( 'usename' => $superuser ), __METHOD__
133 if( $rights != 1 && $rights != 3 ) {
140 public function preInstall() {
142 'name' => 'pg-commit',
143 'callback' => array( $this, 'commitChanges' ),
147 'callback' => array( $this, 'setupUser' ),
151 'callback' => array( $this, 'setupTs2' ),
153 $this->parent
->addInstallStep( $commitCB, 'interwiki' );
154 $this->parent
->addInstallStep( $userCB );
157 function setupDatabase() {
158 $status = $this->getConnection();
159 if ( !$status->isOK() ) {
162 $conn = $status->value
;
164 // Make sure that we can write to the correct schema
165 // If not, Postgres will happily and silently go to the next search_path item
166 $schema = $this->getVar( 'wgDBmwschema' );
167 $ctest = 'mediawiki_test_table';
168 $safeschema = $conn->addIdentifierQuotes( $schema );
169 if ( $conn->tableExists( $ctest, $schema ) ) {
170 $conn->query( "DROP TABLE $safeschema.$ctest" );
172 $res = $conn->query( "CREATE TABLE $safeschema.$ctest(a int)" );
174 $status->fatal( 'config-install-pg-schema-failed',
175 $this->getVar( 'wgDBuser'), $schema );
177 $conn->query( "DROP TABLE $safeschema.$ctest" );
179 $dbName = $this->getVar( 'wgDBname' );
180 if( !$conn->selectDB( $dbName ) ) {
181 $safedb = $conn->addIdentifierQuotes( $dbName );
182 $safeuser = $conn->addQuotes( $this->getVar( 'wgDBuser' ) );
183 $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__
);
184 $conn->selectDB( $dbName );
190 * Ts2 isn't needed in newer versions of Postgres, so wrap it in a nice big
191 * version check and skip it if we're new. Maybe we can bump $minimumVersion
192 * one day and render this obsolete :)
196 function setupTs2() {
197 if( version_compare( $this->db
->getServerVersion(), $this->ts2MaxVersion
, '<' ) ) {
198 if ( !$this->db
->tableExists( 'pg_ts_cfg', $wgDBts2schema ) ) {
199 return Status
::newFatal(
200 'config-install-pg-ts2-failed',
201 $this->getVar( 'wgDBname' ),
202 'http://www.devx.com/opensource/Article/21674/0/page/2'
205 $safeuser = $this->db
->addQuotes( $this->getVar( 'wgDBuser' ) );
206 foreach ( array( 'cfg', 'cfgmap', 'dict', 'parser' ) as $table ) {
207 $sql = "GRANT SELECT ON pg_ts_$table TO $safeuser";
208 $this->db
->query( $sql, __METHOD__
);
211 return Status
::newGood();
214 function commitChanges() {
215 $this->db
->query( 'COMMIT' );
216 return Status
::newGood();
219 function setupUser() {
220 if ( !$this->getVar( '_CreateDBAccount' ) ) {
221 return Status
::newGood();
224 $status = $this->getConnection();
225 if ( !$status->isOK() ) {
229 $db = $this->getVar( 'wgDBname' );
230 $this->db
->selectDB( $db );
231 $safeuser = $this->db
->addQuotes( $this->getVar( 'wgDBuser' ) );
232 $safepass = $this->db
->addQuotes( $this->getVar( 'wgDBpassword' ) );
233 $res = $this->db
->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__
);
234 if ( $res !== true ) {
235 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ) );
241 function getLocalSettings() {
242 $port = $this->getVar( 'wgDBport' );
243 $schema = $this->getVar( 'wgDBmwschema' );
244 $ts2 = $this->getVar( 'wgDBts2schema' );
246 "# Postgres specific settings
247 \$wgDBport = \"{$port}\";
248 \$wgDBmwschema = \"{$schema}\";
249 \$wgDBts2schema = \"{$ts2}\";";
252 public function preUpgrade() {
253 global $wgDBuser, $wgDBpassword;
255 # Normal user and password are selected after this step, so for now
256 # just copy these two
257 $wgDBuser = $this->getVar( '_InstallUser' );
258 $wgDBpassword = $this->getVar( '_InstallPassword' );
261 private function setupPLpgSQL() {
262 $rows = $this->numRows(
263 $this->db
->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
266 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
267 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
268 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
269 $rows = $this->numRows( $this->db
->query( $SQL ) );
272 $result = $this->db
->query( 'CREATE LANGUAGE plpgsql' );
274 return Status
::newFatal( 'config-pg-no-plpgsql', $wgDBname );
277 return Status
::newFatal( 'config-pg-no-plpgsql', $wgDBname );
280 return Status
::newGood();