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';
33 public function isCompiled() {
34 return self
::checkExtension( 'pgsql' );
37 function getConnectForm() {
39 $this->getTextBox( 'wgDBserver', 'config-db-host' ) .
40 $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' ) .
45 $this->parent
->getHelpBox( 'config-db-name-help' ) .
46 $this->getTextBox( 'wgDBmwschema', 'config-db-schema' ) .
47 $this->getTextBox( 'wgDBts2schema', 'config-db-ts2-schema' ) .
48 $this->parent
->getHelpBox( 'config-db-schema-help' ) .
49 Html
::closeElement( 'fieldset' ) .
50 $this->getInstallUserBox();
53 function submitConnectForm() {
54 // Get variables from the request
55 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBport',
56 'wgDBname', 'wgDBmwschema', 'wgDBts2schema' ) );
59 $status = Status
::newGood();
60 if ( !strlen( $newValues['wgDBname'] ) ) {
61 $status->fatal( 'config-missing-db-name' );
62 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
63 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
65 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) {
66 $status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] );
68 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBts2schema'] ) ) {
69 $status->fatal( 'config-invalid-ts2schema', $newValues['wgDBts2schema'] );
73 if ( $status->isOK() ) {
74 $status->merge( $this->submitInstallUserBox() );
76 if ( !$status->isOK() ) {
81 $status->merge( $this->getConnection() );
82 if ( !$status->isOK() ) {
87 $version = $this->db
->getServerVersion();
88 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
89 return Status
::newFatal( 'config-postgres-old', $this->minimumVersion
, $version );
92 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
93 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
97 function getConnection() {
98 $status = Status
::newGood();
101 $this->db
= new DatabasePostgres(
102 $this->getVar( 'wgDBserver' ),
103 $this->getVar( '_InstallUser' ),
104 $this->getVar( '_InstallPassword' ),
105 $this->getVar( 'wgDBname' ) );
106 $status->value
= $this->db
;
107 } catch ( DBConnectionError
$e ) {
108 $status->fatal( 'config-connection-error', $e->getMessage() );
113 public function preInstall() {
114 # Add our user callback to installSteps, right before the tables are created.
116 'name' => 'pg-commit',
117 'callback' => array( $this, 'commitChanges' ),
119 $this->parent
->addInstallStepFollowing( 'interwiki', $callback );
122 function setupDatabase() {
123 $status = $this->getConnection();
124 if ( !$status->isOK() ) {
127 $conn = $status->value
;
129 // Make sure that we can write to the correct schema
130 // If not, Postgres will happily and silently go to the next search_path item
131 $schema = $this->getVar( 'wgDBmwschema' );
132 $ctest = 'mediawiki_test_table';
133 $safeschema = $conn->addIdentifierQuotes( $schema );
134 if ( $conn->tableExists( $ctest, $schema ) ) {
135 $conn->doQuery( "DROP TABLE $safeschema.$ctest" );
137 $res = $conn->doQuery( "CREATE TABLE $safeschema.$ctest(a int)" );
139 $status->fatal( 'config-install-pg-schema-failed',
140 $this->getVar( 'wgDBuser'), $schema );
142 $conn->doQuery( "DROP TABLE $safeschema.$ctest" );
147 function commitChanges() {
148 $this->db
->query( 'COMMIT' );
150 return Status
::newGood();
153 function getLocalSettings() {
154 $port = $this->getVar( 'wgDBport' );
155 $schema = $this->getVar( 'wgDBmwschema' );
156 $ts2 = $this->getVar( 'wgDBts2schema' );
158 "# Postgres specific settings
159 \$wgDBport = \"{$port}\";
160 \$wgDBmwschema = \"{$schema}\";
161 \$wgDBts2schema = \"{$ts2}\";";
164 public function preUpgrade() {
165 global $wgDBuser, $wgDBpassword;
167 # Normal user and password are selected after this step, so for now
168 # just copy these two
169 $wgDBuser = $this->getVar( '_InstallUser' );
170 $wgDBpassword = $this->getVar( '_InstallPassword' );