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