2bea111b97e37ba50b7ee680c4c3cd6c9bcad787
[lhc/web/wiklou.git] / includes / installer / Ibm_db2Installer.php
1 <?php
2 /**
3 * IBM_DB2-specific installer.
4 *
5 * @file
6 * @ingroup Deployment
7 */
8
9 /**
10 * Class for setting up the MediaWiki database using IBM_DB2.
11 *
12 * @ingroup Deployment
13 * @since 1.17
14 */
15 class Ibm_db2Installer extends DatabaseInstaller {
16
17
18 protected $globalNames = array(
19 'wgDBserver',
20 'wgDBport',
21 'wgDBname',
22 'wgDBuser',
23 'wgDBpassword',
24 'wgDBmwschema',
25 );
26
27 /**
28 * Get the DB2 database extension name
29 * @return string
30 */
31 public function getName(){
32 return 'ibm_db2';
33 }
34
35 /**
36 * Determine whether the DB2 database extension is currently available in PHP
37 * @return boolean
38 */
39 public function isCompiled() {
40 return self::checkExtension( 'ibm_db2' );
41 }
42
43 /**
44 * Generate a connection form for a DB2 database
45 * @return string
46 */
47 public function getConnectForm() {
48 return
49 $this->getTextBox( 'wgDBserver', 'config-db-host', array(), $this->parent->getHelpBox( 'config-db-host-help' ) ) .
50 $this->getTextBox( 'wgDBport', 'config-db-port', array(), $this->parent->getHelpBox( 'config-db-port' ) ) .
51 Html::openElement( 'fieldset' ) .
52 Html::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
53 $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent->getHelpBox( 'config-db-name-help' ) ) .
54 $this->getTextBox( 'wgDBmwschema', 'config-db-schema', array(), $this->parent->getHelpBox( 'config-db-schema-help' ) ) .
55 Html::closeElement( 'fieldset' ) .
56 $this->getInstallUserBox();
57 }
58
59 /**
60 * Validate and then execute the connection form for a DB2 database
61 * @return Status
62 */
63 public function submitConnectForm() {
64 // Get variables from the request
65 $newValues = $this->setVarsFromRequest(
66 array( 'wgDBserver', 'wgDBport', 'wgDBname',
67 'wgDBmwschema', 'wgDBuser', 'wgDBpassword' ) );
68
69 // Validate them
70 $status = Status::newGood();
71 if ( !strlen( $newValues['wgDBname'] ) ) {
72 $status->fatal( 'config-missing-db-name' );
73 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
74 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
75 }
76 if ( !strlen( $newValues['wgDBmwschema'] ) ) {
77 $status->fatal( 'config-invalid-schema' );
78 }
79 elseif ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) {
80 $status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] );
81 }
82 if ( !strlen( $newValues['wgDBport'] ) ) {
83 $status->fatal( 'config-invalid-port' );
84 }
85 elseif ( !preg_match( '/^[0-9_]*$/', $newValues['wgDBport'] ) ) {
86 $status->fatal( 'config-invalid-port', $newValues['wgDBport'] );
87 }
88
89 // Submit user box
90 if ( $status->isOK() ) {
91 $status->merge( $this->submitInstallUserBox() );
92 }
93 if ( !$status->isOK() ) {
94 return $status;
95 }
96
97 global $wgDBport;
98 $wgDBport = $newValues['wgDBport'];
99
100 // Try to connect
101 $status->merge( $this->getConnection() );
102 if ( !$status->isOK() ) {
103 return $status;
104 }
105
106 $this->parent->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
107 $this->parent->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
108
109 return $status;
110
111 }
112
113 /**
114 * Open a DB2 database connection
115 * @return Status
116 */
117 public function openConnection() {
118 $status = Status::newGood();
119 try {
120 $db = new DatabaseIbm_db2(
121 $this->getVar( 'wgDBserver' ),
122 $this->getVar( '_InstallUser' ),
123 $this->getVar( '_InstallPassword' ),
124 $this->getVar( 'wgDBname' ),
125 0,
126 $this->getVar( 'wgDBmwschema' )
127 );
128 $status->value = $db;
129 } catch ( DBConnectionError $e ) {
130 $status->fatal( 'config-connection-error', $e->getMessage() );
131 }
132 return $status;
133 }
134
135 /**
136 * Create a DB2 database for MediaWiki
137 * @return Status
138 */
139 public function setupDatabase() {
140 $status = $this->getConnection();
141 if ( !$status->isOK() ) {
142 return $status;
143 }
144 $conn = $status->value;
145 $dbName = $this->getVar( 'wgDBname' );
146 if( !$conn->selectDB( $dbName ) ) {
147 $conn->query( "CREATE DATABASE "
148 . $conn->addIdentifierQuotes( $dbName )
149 . " AUTOMATIC STORAGE YES"
150 . " USING CODESET UTF-8 TERRITORY US COLLATE USING SYSTEM"
151 . " PAGESIZE 32768", __METHOD__ );
152 $conn->selectDB( $dbName );
153 }
154 $this->setupSchemaVars();
155 return $status;
156 }
157
158 /**
159 * Create tables from scratch.
160 * First check if pagesize >= 32k.
161 *
162 * @return Status
163 */
164 public function createTables() {
165 $status = $this->getConnection();
166 if ( !$status->isOK() ) {
167 return $status;
168 }
169 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
170
171 if( $this->db->tableExists( 'user' ) ) {
172 $status->warning( 'config-install-tables-exist' );
173 return $status;
174 }
175
176 /* Check for pagesize */
177 $status = $this->checkPageSize();
178 if ( !$status->isOK() ) {
179 return $status;
180 }
181
182 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
183 $this->db->begin( __METHOD__ );
184
185 $error = $this->db->sourceFile( $this->db->getSchema() );
186 if( $error !== true ) {
187 $this->db->reportQueryError( $error, 0, '', __METHOD__ );
188 $this->db->rollback( __METHOD__ );
189 $status->fatal( 'config-install-tables-failed', $error );
190 } else {
191 $this->db->commit( __METHOD__ );
192 }
193 // Resume normal operations
194 if( $status->isOk() ) {
195 $this->enableLB();
196 }
197 return $status;
198 }
199
200 /**
201 * Check if database has a tablspace with pagesize >= 32k.
202 *
203 * @return Status
204 */
205 public function checkPageSize() {
206 $status = $this->getConnection();
207 if ( !$status->isOK() ) {
208 return $status;
209 }
210 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
211
212 try {
213 $result = $this->db->query( 'SELECT PAGESIZE FROM SYSCAT.TABLESPACES' );
214 if( $result == false ) {
215 $status->fatal( 'config-connection-error', '' );
216 }
217 else {
218 $nRows = $this->db->numRows( $result );
219 while ( $row = $row = $this->db->fetchRow( $result ) ) {
220 if( $row[0] >= 32768 ) {
221 return $status;
222 }
223 }
224 $status->fatal( 'config-ibm_db2-low-db-pagesize', '' );
225 }
226 } catch ( DBUnexpectedError $e ) {
227 $status->fatal( 'config-connection-error', $e->getMessage() );
228 }
229
230 return $status;
231 }
232
233 /**
234 * Generate the code to store the DB2-specific settings defined by the configuration form
235 * @return string
236 */
237 public function getLocalSettings() {
238 $schema = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgDBmwschema' ) );
239 $port = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgDBport' ) );
240 return
241 "# IBM_DB2 specific settings
242 \$wgDBmwschema = \"{$schema}\";
243 \$wgDBport = \"{$port}\";";
244 }
245
246 public function __construct($parent) {
247 parent::__construct($parent);
248 }
249 }
250 ?>