1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
|
<?php
/**
* MySQL-specific installer.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Deployment
*/
/**
* Class for setting up the MediaWiki database using MySQL.
*
* @ingroup Deployment
* @since 1.17
*/
class MysqlInstaller extends DatabaseInstaller {
protected $globalNames = array(
'wgDBserver',
'wgDBname',
'wgDBuser',
'wgDBpassword',
'wgDBprefix',
'wgDBTableOptions',
'wgDBmysql5',
);
protected $internalDefaults = array(
'_MysqlEngine' => 'InnoDB',
'_MysqlCharset' => 'binary',
'_InstallUser' => 'root',
);
public $supportedEngines = array( 'InnoDB', 'MyISAM' );
public $minimumVersion = '5.0.3';
public $webUserPrivs = array(
'DELETE',
'INSERT',
'SELECT',
'UPDATE',
'CREATE TEMPORARY TABLES',
);
/**
* @return string
*/
public function getName() {
return 'mysql';
}
/**
* @return bool
*/
public function isCompiled() {
return self::checkExtension( 'mysql' ) || self::checkExtension( 'mysqli' );
}
/**
* @return string
*/
public function getConnectForm() {
return $this->getTextBox(
'wgDBserver',
'config-db-host',
array(),
$this->parent->getHelpBox( 'config-db-host-help' )
) .
Html::openElement( 'fieldset' ) .
Html::element( 'legend', array(), wfMessage( 'config-db-wiki-settings' )->text() ) .
$this->getTextBox( 'wgDBname', 'config-db-name', array( 'dir' => 'ltr' ),
$this->parent->getHelpBox( 'config-db-name-help' ) ) .
$this->getTextBox( 'wgDBprefix', 'config-db-prefix', array( 'dir' => 'ltr' ),
$this->parent->getHelpBox( 'config-db-prefix-help' ) ) .
Html::closeElement( 'fieldset' ) .
$this->getInstallUserBox();
}
public function submitConnectForm() {
// Get variables from the request.
$newValues = $this->setVarsFromRequest( array(
'wgDBserver', 'wgDBname', 'wgDBprefix', '_InstallUser', '_InstallPassword'
) );
// Validate them.
$status = Status::newGood();
if ( !strlen( $newValues['wgDBserver'] ) ) {
$status->fatal( 'config-missing-db-host' );
}
if ( !strlen( $newValues['wgDBname'] ) ) {
$status->fatal( 'config-missing-db-name' );
} elseif ( !preg_match( '/^[a-z0-9+_-]+$/i', $newValues['wgDBname'] ) ) {
$status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
}
if ( !preg_match( '/^[a-z0-9_-]*$/i', $newValues['wgDBprefix'] ) ) {
$status->fatal( 'config-invalid-db-prefix', $newValues['wgDBprefix'] );
}
if ( !strlen( $newValues['_InstallUser'] ) ) {
$status->fatal( 'config-db-username-empty' );
}
if ( !strlen( $newValues['_InstallPassword'] ) ) {
$status->fatal( 'config-db-password-empty', $newValues['_InstallUser'] );
}
if ( !$status->isOK() ) {
return $status;
}
// Submit user box
$status = $this->submitInstallUserBox();
if ( !$status->isOK() ) {
return $status;
}
// Try to connect
$status = $this->getConnection();
if ( !$status->isOK() ) {
return $status;
}
/**
* @var $conn DatabaseBase
*/
$conn = $status->value;
// Check version
$version = $conn->getServerVersion();
if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
return Status::newFatal( 'config-mysql-old', $this->minimumVersion, $version );
}
return $status;
}
/**
* @return Status
*/
public function openConnection() {
$status = Status::newGood();
try {
$db = DatabaseBase::factory( 'mysql', array(
'host' => $this->getVar( 'wgDBserver' ),
'user' => $this->getVar( '_InstallUser' ),
'password' => $this->getVar( '_InstallPassword' ),
'dbname' => false,
'flags' => 0,
'tablePrefix' => $this->getVar( 'wgDBprefix' ) ) );
$status->value = $db;
} catch ( DBConnectionError $e ) {
$status->fatal( 'config-connection-error', $e->getMessage() );
}
return $status;
}
public function preUpgrade() {
global $wgDBuser, $wgDBpassword;
$status = $this->getConnection();
if ( !$status->isOK() ) {
$this->parent->showStatusError( $status );
return;
}
/**
* @var $conn DatabaseBase
*/
$conn = $status->value;
$conn->selectDB( $this->getVar( 'wgDBname' ) );
# Determine existing default character set
if ( $conn->tableExists( "revision", __METHOD__ ) ) {
$revision = $conn->buildLike( $this->getVar( 'wgDBprefix' ) . 'revision' );
$res = $conn->query( "SHOW TABLE STATUS $revision", __METHOD__ );
$row = $conn->fetchObject( $res );
if ( !$row ) {
$this->parent->showMessage( 'config-show-table-status' );
$existingSchema = false;
$existingEngine = false;
} else {
if ( preg_match( '/^latin1/', $row->Collation ) ) {
$existingSchema = 'latin1';
} elseif ( preg_match( '/^utf8/', $row->Collation ) ) {
$existingSchema = 'utf8';
} elseif ( preg_match( '/^binary/', $row->Collation ) ) {
$existingSchema = 'binary';
} else {
$existingSchema = false;
$this->parent->showMessage( 'config-unknown-collation' );
}
if ( isset( $row->Engine ) ) {
$existingEngine = $row->Engine;
} else {
$existingEngine = $row->Type;
}
}
} else {
$existingSchema = false;
$existingEngine = false;
}
if ( $existingSchema && $existingSchema != $this->getVar( '_MysqlCharset' ) ) {
$this->setVar( '_MysqlCharset', $existingSchema );
}
if ( $existingEngine && $existingEngine != $this->getVar( '_MysqlEngine' ) ) {
$this->setVar( '_MysqlEngine', $existingEngine );
}
# Normal user and password are selected after this step, so for now
# just copy these two
$wgDBuser = $this->getVar( '_InstallUser' );
$wgDBpassword = $this->getVar( '_InstallPassword' );
}
/**
* Get a list of storage engines that are available and supported
*
* @return array
*/
public function getEngines() {
$status = $this->getConnection();
/**
* @var $conn DatabaseBase
*/
$conn = $status->value;
$engines = array();
$res = $conn->query( 'SHOW ENGINES', __METHOD__ );
foreach ( $res as $row ) {
if ( $row->Support == 'YES' || $row->Support == 'DEFAULT' ) {
$engines[] = $row->Engine;
}
}
$engines = array_intersect( $this->supportedEngines, $engines );
return $engines;
}
/**
* Get a list of character sets that are available and supported
*
* @return array
*/
public function getCharsets() {
return array( 'binary', 'utf8' );
}
/**
* Return true if the install user can create accounts
*
* @return bool
*/
public function canCreateAccounts() {
$status = $this->getConnection();
if ( !$status->isOK() ) {
return false;
}
/** @var $conn DatabaseBase */
$conn = $status->value;
// Get current account name
$currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__ );
$parts = explode( '@', $currentName );
if ( count( $parts ) != 2 ) {
return false;
}
$quotedUser = $conn->addQuotes( $parts[0] ) .
'@' . $conn->addQuotes( $parts[1] );
// The user needs to have INSERT on mysql.* to be able to CREATE USER
// The grantee will be double-quoted in this query, as required
$res = $conn->select( 'INFORMATION_SCHEMA.USER_PRIVILEGES', '*',
array( 'GRANTEE' => $quotedUser ), __METHOD__ );
$insertMysql = false;
$grantOptions = array_flip( $this->webUserPrivs );
foreach ( $res as $row ) {
if ( $row->PRIVILEGE_TYPE == 'INSERT' ) {
$insertMysql = true;
}
if ( $row->IS_GRANTABLE ) {
unset( $grantOptions[$row->PRIVILEGE_TYPE] );
}
}
// Check for DB-specific privs for mysql.*
if ( !$insertMysql ) {
$row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
array(
'GRANTEE' => $quotedUser,
'TABLE_SCHEMA' => 'mysql',
'PRIVILEGE_TYPE' => 'INSERT',
), __METHOD__ );
if ( $row ) {
$insertMysql = true;
}
}
if ( !$insertMysql ) {
return false;
}
// Check for DB-level grant options
$res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
array(
'GRANTEE' => $quotedUser,
'IS_GRANTABLE' => 1,
), __METHOD__ );
foreach ( $res as $row ) {
$regex = $conn->likeToRegex( $row->TABLE_SCHEMA );
if ( preg_match( $regex, $this->getVar( 'wgDBname' ) ) ) {
unset( $grantOptions[$row->PRIVILEGE_TYPE] );
}
}
if ( count( $grantOptions ) ) {
// Can't grant everything
return false;
}
return true;
}
/**
* @return string
*/
public function getSettingsForm() {
if ( $this->canCreateAccounts() ) {
$noCreateMsg = false;
} else {
$noCreateMsg = 'config-db-web-no-create-privs';
}
$s = $this->getWebUserBox( $noCreateMsg );
// Do engine selector
$engines = $this->getEngines();
// If the current default engine is not supported, use an engine that is
if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
$this->setVar( '_MysqlEngine', reset( $engines ) );
}
$s .= Xml::openElement( 'div', array(
'id' => 'dbMyisamWarning'
) );
$myisamWarning = 'config-mysql-myisam-dep';
if ( count( $engines ) === 1 ) {
$myisamWarning = 'config-mysql-only-myisam-dep';
}
$s .= $this->parent->getWarningBox( wfMessage( $myisamWarning )->text() );
$s .= Xml::closeElement( 'div' );
if ( $this->getVar( '_MysqlEngine' ) != 'MyISAM' ) {
$s .= Xml::openElement( 'script', array( 'type' => 'text/javascript' ) );
$s .= '$(\'#dbMyisamWarning\').hide();';
$s .= Xml::closeElement( 'script' );
}
if ( count( $engines ) >= 2 ) {
// getRadioSet() builds a set of labeled radio buttons.
// For grep: The following messages are used as the item labels:
// config-mysql-innodb, config-mysql-myisam
$s .= $this->getRadioSet( array(
'var' => '_MysqlEngine',
'label' => 'config-mysql-engine',
'itemLabelPrefix' => 'config-mysql-',
'values' => $engines,
'itemAttribs' => array(
'MyISAM' => array(
'class' => 'showHideRadio',
'rel' => 'dbMyisamWarning'
),
'InnoDB' => array(
'class' => 'hideShowRadio',
'rel' => 'dbMyisamWarning'
)
)
) );
$s .= $this->parent->getHelpBox( 'config-mysql-engine-help' );
}
// If the current default charset is not supported, use a charset that is
$charsets = $this->getCharsets();
if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
$this->setVar( '_MysqlCharset', reset( $charsets ) );
}
// Do charset selector
if ( count( $charsets ) >= 2 ) {
// getRadioSet() builds a set of labeled radio buttons.
// For grep: The following messages are used as the item labels:
// config-mysql-binary, config-mysql-utf8
$s .= $this->getRadioSet( array(
'var' => '_MysqlCharset',
'label' => 'config-mysql-charset',
'itemLabelPrefix' => 'config-mysql-',
'values' => $charsets
) );
$s .= $this->parent->getHelpBox( 'config-mysql-charset-help' );
}
return $s;
}
/**
* @return Status
*/
public function submitSettingsForm() {
$this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
$status = $this->submitWebUserBox();
if ( !$status->isOK() ) {
return $status;
}
// Validate the create checkbox
$canCreate = $this->canCreateAccounts();
if ( !$canCreate ) {
$this->setVar( '_CreateDBAccount', false );
$create = false;
} else {
$create = $this->getVar( '_CreateDBAccount' );
}
if ( !$create ) {
// Test the web account
try {
DatabaseBase::factory( 'mysql', array(
'host' => $this->getVar( 'wgDBserver' ),
'user' => $this->getVar( 'wgDBuser' ),
'password' => $this->getVar( 'wgDBpassword' ),
'dbname' => false,
'flags' => 0,
'tablePrefix' => $this->getVar( 'wgDBprefix' )
) );
} catch ( DBConnectionError $e ) {
return Status::newFatal( 'config-connection-error', $e->getMessage() );
}
}
// Validate engines and charsets
// This is done pre-submit already so it's just for security
$engines = $this->getEngines();
if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
$this->setVar( '_MysqlEngine', reset( $engines ) );
}
$charsets = $this->getCharsets();
if ( !in_array( $this->getVar( '_MysqlCharset' ), $charsets ) ) {
$this->setVar( '_MysqlCharset', reset( $charsets ) );
}
return Status::newGood();
}
public function preInstall() {
# Add our user callback to installSteps, right before the tables are created.
$callback = array(
'name' => 'user',
'callback' => array( $this, 'setupUser' ),
);
$this->parent->addInstallStep( $callback, 'tables' );
}
/**
* @return Status
*/
public function setupDatabase() {
$status = $this->getConnection();
if ( !$status->isOK() ) {
return $status;
}
/** @var DatabaseBase $conn */
$conn = $status->value;
$dbName = $this->getVar( 'wgDBname' );
if ( !$conn->selectDB( $dbName ) ) {
$conn->query(
"CREATE DATABASE " . $conn->addIdentifierQuotes( $dbName ) . "CHARACTER SET utf8",
__METHOD__
);
$conn->selectDB( $dbName );
}
$this->setupSchemaVars();
return $status;
}
/**
* @return Status
*/
public function setupUser() {
$dbUser = $this->getVar( 'wgDBuser' );
if ( $dbUser == $this->getVar( '_InstallUser' ) ) {
return Status::newGood();
}
$status = $this->getConnection();
if ( !$status->isOK() ) {
return $status;
}
$this->setupSchemaVars();
$dbName = $this->getVar( 'wgDBname' );
$this->db->selectDB( $dbName );
$server = $this->getVar( 'wgDBserver' );
$password = $this->getVar( 'wgDBpassword' );
$grantableNames = array();
if ( $this->getVar( '_CreateDBAccount' ) ) {
// Before we blindly try to create a user that already has access,
try { // first attempt to connect to the database
DatabaseBase::factory( 'mysql', array(
'host' => $server,
'user' => $dbUser,
'password' => $password,
'dbname' => false,
'flags' => 0,
'tablePrefix' => $this->getVar( 'wgDBprefix' )
) );
$grantableNames[] = $this->buildFullUserName( $dbUser, $server );
$tryToCreate = false;
} catch ( DBConnectionError $e ) {
$tryToCreate = true;
}
} else {
$grantableNames[] = $this->buildFullUserName( $dbUser, $server );
$tryToCreate = false;
}
if ( $tryToCreate ) {
$createHostList = array(
$server,
'localhost',
'localhost.localdomain',
'%'
);
$createHostList = array_unique( $createHostList );
$escPass = $this->db->addQuotes( $password );
foreach ( $createHostList as $host ) {
$fullName = $this->buildFullUserName( $dbUser, $host );
if ( !$this->userDefinitelyExists( $dbUser, $host ) ) {
try {
$this->db->begin( __METHOD__ );
$this->db->query( "CREATE USER $fullName IDENTIFIED BY $escPass", __METHOD__ );
$this->db->commit( __METHOD__ );
$grantableNames[] = $fullName;
} catch ( DBQueryError $dqe ) {
if ( $this->db->lastErrno() == 1396 /* ER_CANNOT_USER */ ) {
// User (probably) already exists
$this->db->rollback( __METHOD__ );
$status->warning( 'config-install-user-alreadyexists', $dbUser );
$grantableNames[] = $fullName;
break;
} else {
// If we couldn't create for some bizzare reason and the
// user probably doesn't exist, skip the grant
$this->db->rollback( __METHOD__ );
$status->warning( 'config-install-user-create-failed', $dbUser, $dqe->getText() );
}
}
} else {
$status->warning( 'config-install-user-alreadyexists', $dbUser );
$grantableNames[] = $fullName;
break;
}
}
}
// Try to grant to all the users we know exist or we were able to create
$dbAllTables = $this->db->addIdentifierQuotes( $dbName ) . '.*';
foreach ( $grantableNames as $name ) {
try {
$this->db->begin( __METHOD__ );
$this->db->query( "GRANT ALL PRIVILEGES ON $dbAllTables TO $name", __METHOD__ );
$this->db->commit( __METHOD__ );
} catch ( DBQueryError $dqe ) {
$this->db->rollback( __METHOD__ );
$status->fatal( 'config-install-user-grant-failed', $dbUser, $dqe->getText() );
}
}
return $status;
}
/**
* Return a formal 'User'@'Host' username for use in queries
* @param string $name Username, quotes will be added
* @param string $host Hostname, quotes will be added
* @return string
*/
private function buildFullUserName( $name, $host ) {
return $this->db->addQuotes( $name ) . '@' . $this->db->addQuotes( $host );
}
/**
* Try to see if the user account exists. Our "superuser" may not have
* access to mysql.user, so false means "no" or "maybe"
* @param string $host Hostname to check
* @param string $user Username to check
* @return bool
*/
private function userDefinitelyExists( $host, $user ) {
try {
$res = $this->db->selectRow( 'mysql.user', array( 'Host', 'User' ),
array( 'Host' => $host, 'User' => $user ), __METHOD__ );
return (bool)$res;
} catch ( DBQueryError $dqe ) {
return false;
}
}
/**
* Return any table options to be applied to all tables that don't
* override them.
*
* @return string
*/
protected function getTableOptions() {
$options = array();
if ( $this->getVar( '_MysqlEngine' ) !== null ) {
$options[] = "ENGINE=" . $this->getVar( '_MysqlEngine' );
}
if ( $this->getVar( '_MysqlCharset' ) !== null ) {
$options[] = 'DEFAULT CHARSET=' . $this->getVar( '_MysqlCharset' );
}
return implode( ', ', $options );
}
/**
* Get variables to substitute into tables.sql and the SQL patch files.
*
* @return array
*/
public function getSchemaVars() {
return array(
'wgDBTableOptions' => $this->getTableOptions(),
'wgDBname' => $this->getVar( 'wgDBname' ),
'wgDBuser' => $this->getVar( 'wgDBuser' ),
'wgDBpassword' => $this->getVar( 'wgDBpassword' ),
);
}
public function getLocalSettings() {
$dbmysql5 = wfBoolToStr( $this->getVar( 'wgDBmysql5', true ) );
$prefix = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgDBprefix' ) );
$tblOpts = LocalSettingsGenerator::escapePhpString( $this->getTableOptions() );
return "# MySQL specific settings
\$wgDBprefix = \"{$prefix}\";
# MySQL table options to use during installation or update
\$wgDBTableOptions = \"{$tblOpts}\";
# Experimental charset support for MySQL 5.0.
\$wgDBmysql5 = {$dbmysql5};";
}
}
|