OverloardQ or other PG person, please review
[lhc/web/wiklou.git] / includes / installer / PostgresInstaller.php
1 <?php
2
3 /**
4 * Class for setting up the MediaWiki database using Postgres.
5 *
6 * @ingroup Deployment
7 * @since 1.17
8 */
9 class PostgresInstaller extends DatabaseInstaller {
10
11 protected $globalNames = array(
12 'wgDBserver',
13 'wgDBport',
14 'wgDBname',
15 'wgDBuser',
16 'wgDBpassword',
17 'wgDBmwschema',
18 'wgDBts2schema',
19 );
20
21 var $minimumVersion = '8.1';
22
23 function getName() {
24 return 'postgres';
25 }
26
27 public function isCompiled() {
28 return self::checkExtension( 'pgsql' );
29 }
30
31 function getConnectForm() {
32 return
33 $this->getTextBox( 'wgDBserver', 'config-db-host' ) .
34 $this->parent->getHelpBox( 'config-db-host-help' ) .
35 $this->getTextBox( 'wgDBport', 'config-db-port' ) .
36 Xml::openElement( 'fieldset' ) .
37 Xml::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
38 $this->getTextBox( 'wgDBname', 'config-db-name' ) .
39 $this->parent->getHelpBox( 'config-db-name-help' ) .
40 $this->getTextBox( 'wgDBmwschema', 'config-db-schema' ) .
41 $this->getTextBox( 'wgDBts2schema', 'config-db-ts2-schema' ) .
42 $this->parent->getHelpBox( 'config-db-schema-help' ) .
43 Xml::closeElement( 'fieldset' ) .
44 $this->getInstallUserBox();
45 }
46
47 function submitConnectForm() {
48 // Get variables from the request
49 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBport',
50 'wgDBname', 'wgDBmwschema', 'wgDBts2schema' ) );
51
52 // Validate them
53 $status = Status::newGood();
54 if ( !strlen( $newValues['wgDBname'] ) ) {
55 $status->fatal( 'config-missing-db-name' );
56 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
57 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
58 }
59 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) {
60 $status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] );
61 }
62 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBts2schema'] ) ) {
63 $status->fatal( 'config-invalid-ts2schema', $newValues['wgDBts2schema'] );
64 }
65
66 // Submit user box
67 if ( $status->isOK() ) {
68 $status->merge( $this->submitInstallUserBox() );
69 }
70 if ( !$status->isOK() ) {
71 return $status;
72 }
73
74 // Try to connect
75 if ( $status->isOK() ) {
76 $status->merge( $this->getConnection() );
77 }
78 if ( !$status->isOK() ) {
79 return $status;
80 }
81
82 // Check version
83 $version = $this->db->getServerVersion();
84 if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
85 return Status::newFatal( 'config-postgres-old', $this->minimumVersion, $version );
86 }
87
88 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
89 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
90 return $status;
91 }
92
93 function getConnection() {
94 $status = Status::newGood();
95
96 try {
97 $this->db = new DatabasePostgres(
98 $this->getVar( 'wgDBserver' ),
99 $this->getVar( '_InstallUser' ),
100 $this->getVar( '_InstallPassword' ),
101 $this->getVar( 'wgDBname' ) );
102 $status->value = $this->db;
103 } catch ( DBConnectionError $e ) {
104 $status->fatal( 'config-connection-error', $e->getMessage() );
105 }
106 return $status;
107 }
108
109 function getSettingsForm() {
110 return false;
111 }
112
113 function submitSettingsForm() {
114 return Status::newGood();
115 }
116
117 function setupDatabase() {
118 }
119
120 function createTables() {
121 $status = $this->getConnection();
122 if ( !$status->isOK() ) {
123 return $status;
124 }
125 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
126
127 global $IP;
128 $err = $this->db->sourceFile( "$IP/maintenance/postgres/tables.sql" );
129 if ( $err !== true ) {
130 //@todo or...?
131 $this->db->reportQueryError( $err, 0, $sql, __FUNCTION__ );
132 }
133 return Status::newGood();
134 }
135
136 function getLocalSettings() {
137 $port = $this->getVar( 'wgDBport' );
138 $schema = $this->getVar( 'wgDBmwschema' );
139 $ts2 = $this->getVar( 'wgDBts2schema' );
140 return
141 "# Postgres specific settings
142 \$wgDBport = \"{$port}\";
143 \$wgDBmwschema = \"{$schema}\";
144 \$wgDBts2schema = \"{$ts2}\";";
145 }
146 }