Reverted r75832 per my comments on CR, unanswered for 19 days. Moving all help inform...
[lhc/web/wiklou.git] / includes / installer / PostgresInstaller.php
1 <?php
2 /**
3 * PostgreSQL-specific installer.
4 *
5 * @file
6 * @ingroup Deployment
7 */
8
9 /**
10 * Class for setting up the MediaWiki database using Postgres.
11 *
12 * @ingroup Deployment
13 * @since 1.17
14 */
15 class PostgresInstaller extends DatabaseInstaller {
16
17 protected $globalNames = array(
18 'wgDBserver',
19 'wgDBport',
20 'wgDBname',
21 'wgDBuser',
22 'wgDBpassword',
23 'wgDBmwschema',
24 'wgDBts2schema',
25 );
26
27 var $minimumVersion = '8.1';
28
29 function getName() {
30 return 'postgres';
31 }
32
33 public function isCompiled() {
34 return self::checkExtension( 'pgsql' );
35 }
36
37 function getConnectForm() {
38 return
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();
51 }
52
53 function submitConnectForm() {
54 // Get variables from the request
55 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBport',
56 'wgDBname', 'wgDBmwschema', 'wgDBts2schema' ) );
57
58 // Validate them
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'] );
64 }
65 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) {
66 $status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] );
67 }
68 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBts2schema'] ) ) {
69 $status->fatal( 'config-invalid-ts2schema', $newValues['wgDBts2schema'] );
70 }
71
72 // Submit user box
73 if ( $status->isOK() ) {
74 $status->merge( $this->submitInstallUserBox() );
75 }
76 if ( !$status->isOK() ) {
77 return $status;
78 }
79
80 // Try to connect
81 $status->merge( $this->getConnection() );
82 if ( !$status->isOK() ) {
83 return $status;
84 }
85
86 // Check version
87 $version = $this->db->getServerVersion();
88 if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
89 return Status::newFatal( 'config-postgres-old', $this->minimumVersion, $version );
90 }
91
92 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
93 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
94 return $status;
95 }
96
97 function getConnection() {
98 $status = Status::newGood();
99
100 try {
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() );
109 }
110 return $status;
111 }
112
113 public function preInstall() {
114 # Add our user callback to installSteps, right before the tables are created.
115 $callback = array(
116 'name' => 'pg-commit',
117 'callback' => array( $this, 'commitChanges' ),
118 );
119 $this->parent->addInstallStepFollowing( 'interwiki', $callback );
120 }
121
122 function setupDatabase() {
123 $status = $this->getConnection();
124 if ( !$status->isOK() ) {
125 return $status;
126 }
127 $conn = $status->value;
128
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" );
136 }
137 $res = $conn->doQuery( "CREATE TABLE $safeschema.$ctest(a int)" );
138 if ( !$res ) {
139 $status->fatal( 'config-install-pg-schema-failed',
140 $this->getVar( 'wgDBuser'), $schema );
141 }
142 $conn->doQuery( "DROP TABLE $safeschema.$ctest" );
143
144 return $status;
145 }
146
147 function commitChanges() {
148 $this->db->query( 'COMMIT' );
149
150 return Status::newGood();
151 }
152
153 function getLocalSettings() {
154 $port = $this->getVar( 'wgDBport' );
155 $schema = $this->getVar( 'wgDBmwschema' );
156 $ts2 = $this->getVar( 'wgDBts2schema' );
157 return
158 "# Postgres specific settings
159 \$wgDBport = \"{$port}\";
160 \$wgDBmwschema = \"{$schema}\";
161 \$wgDBts2schema = \"{$ts2}\";";
162 }
163
164 public function preUpgrade() {
165 global $wgDBuser, $wgDBpassword;
166
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' );
171 }
172 }