summaryrefslogtreecommitdiff
path: root/includes/installer/MssqlInstaller.php
blob: 46bb86c01009416d8413616767df41f66f9ff920 (plain)
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
<?php
/**
 * Microsoft SQL Server-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 Microsoft SQL Server.
 *
 * @ingroup Deployment
 * @since 1.23
 */
class MssqlInstaller extends DatabaseInstaller {

	protected $globalNames = array(
		'wgDBserver',
		'wgDBname',
		'wgDBuser',
		'wgDBpassword',
		'wgDBmwschema',
		'wgDBprefix',
		'wgDBWindowsAuthentication',
	);

	protected $internalDefaults = array(
		'_InstallUser' => 'sa',
		'_InstallWindowsAuthentication' => 'sqlauth',
		'_WebWindowsAuthentication' => 'sqlauth',
	);

	// SQL Server 2005 RTM
	// @todo Are SQL Express version numbers different?)
	public $minimumVersion = '9.00.1399';

	// These are schema-level privs
	// Note: the web user will be created will full permissions if possible, this permission
	// list is only used if we are unable to grant full permissions.
	public $webUserPrivs = array(
		'DELETE',
		'INSERT',
		'SELECT',
		'UPDATE',
		'EXECUTE',
	);

	/**
	 * @return string
	 */
	public function getName() {
		return 'mssql';
	}

	/**
	 * @return bool
	 */
	public function isCompiled() {
		return self::checkExtension( 'sqlsrv' );
	}

	/**
	 * @return string
	 */
	public function getConnectForm() {
		if ( $this->getVar( '_InstallWindowsAuthentication' ) == 'windowsauth' ) {
			$displayStyle = 'display: none;';
		} else {
			$displayStyle = 'display: block;';
		}

		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( 'wgDBmwschema', 'config-db-schema', array( 'dir' => 'ltr' ),
				$this->parent->getHelpBox( 'config-db-schema-help' ) ) .
			$this->getTextBox( 'wgDBprefix', 'config-db-prefix', array( 'dir' => 'ltr' ),
				$this->parent->getHelpBox( 'config-db-prefix-help' ) ) .
			Html::closeElement( 'fieldset' ) .
			Html::openElement( 'fieldset' ) .
			Html::element( 'legend', array(), wfMessage( 'config-db-install-account' )->text() ) .
			$this->getRadioSet( array(
				'var' => '_InstallWindowsAuthentication',
				'label' => 'config-mssql-auth',
				'itemLabelPrefix' => 'config-mssql-',
				'values' => array( 'sqlauth', 'windowsauth' ),
				'itemAttribs' => array(
					'sqlauth' => array(
						'class' => 'showHideRadio',
						'rel' => 'dbCredentialBox',
					),
					'windowsauth' => array(
						'class' => 'hideShowRadio',
						'rel' => 'dbCredentialBox',
					)
				),
				'help' => $this->parent->getHelpBox( 'config-mssql-install-auth' )
			) ) .
			Html::openElement( 'div', array( 'id' => 'dbCredentialBox', 'style' => $displayStyle ) ) .
			$this->getTextBox(
				'_InstallUser',
				'config-db-username',
				array( 'dir' => 'ltr' ),
				$this->parent->getHelpBox( 'config-db-install-username' )
			) .
			$this->getPasswordBox(
				'_InstallPassword',
				'config-db-password',
				array( 'dir' => 'ltr' ),
				$this->parent->getHelpBox( 'config-db-install-password' )
			) .
			Html::closeElement( 'div' ) .
			Html::closeElement( 'fieldset' );
	}

	public function submitConnectForm() {
		// Get variables from the request.
		$newValues = $this->setVarsFromRequest( array(
			'wgDBserver',
			'wgDBname',
			'wgDBmwschema',
			'wgDBprefix'
		) );

		// 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['wgDBmwschema'] ) ) {
			$status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] );
		}
		if ( !preg_match( '/^[a-z0-9_]*$/i', $newValues['wgDBprefix'] ) ) {
			$status->fatal( 'config-invalid-db-prefix', $newValues['wgDBprefix'] );
		}
		if ( !$status->isOK() ) {
			return $status;
		}

		// Check for blank schema and remap to dbo
		if ( $newValues['wgDBmwschema'] === '' ) {
			$this->setVar( 'wgDBmwschema', 'dbo' );
		}

		// User box
		$this->setVarsFromRequest( array(
			'_InstallUser',
			'_InstallPassword',
			'_InstallWindowsAuthentication'
		) );

		// 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-mssql-old', $this->minimumVersion, $version );
		}

		return $status;
	}

	/**
	 * @return Status
	 */
	public function openConnection() {
		global $wgDBWindowsAuthentication;
		$status = Status::newGood();
		$user = $this->getVar( '_InstallUser' );
		$password = $this->getVar( '_InstallPassword' );

		if ( $this->getVar( '_InstallWindowsAuthentication' ) == 'windowsauth' ) {
			// Use Windows authentication for this connection
			$wgDBWindowsAuthentication = true;
		} else {
			$wgDBWindowsAuthentication = false;
		}

		try {
			$db = DatabaseBase::factory( 'mssql', array(
				'host' => $this->getVar( 'wgDBserver' ),
				'user' => $user,
				'password' => $password,
				'dbname' => false,
				'flags' => 0,
				'schema' => $this->getVar( 'wgDBmwschema' ),
				'tablePrefix' => $this->getVar( 'wgDBprefix' ) ) );
			$db->prepareStatements( false );
			$db->scrollableCursor( false );
			$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' ) );

		# Normal user and password are selected after this step, so for now
		# just copy these two
		$wgDBuser = $this->getVar( '_InstallUser' );
		$wgDBpassword = $this->getVar( '_InstallPassword' );
	}

	/**
	 * 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;

		// We need the server-level ALTER ANY LOGIN permission to create new accounts
		$res = $conn->query( "SELECT permission_name FROM sys.fn_my_permissions( NULL, 'SERVER' )" );
		$serverPrivs = array(
			'ALTER ANY LOGIN' => false,
			'CONTROL SERVER' => false,
		);

		foreach ( $res as $row ) {
			$serverPrivs[$row->permission_name] = true;
		}

		if ( !$serverPrivs['ALTER ANY LOGIN'] ) {
			return false;
		}

		// Check to ensure we can grant everything needed as well
		// We can't actually tell if we have WITH GRANT OPTION for a given permission, so we assume we do
		// and just check for the permission
		// http://technet.microsoft.com/en-us/library/ms178569.aspx
		// The following array sets up which permissions imply whatever permissions we specify
		$implied = array(
			// schema           database  server
			'DELETE'  => array( 'DELETE', 'CONTROL SERVER' ),
			'EXECUTE' => array( 'EXECUTE', 'CONTROL SERVER' ),
			'INSERT'  => array( 'INSERT', 'CONTROL SERVER' ),
			'SELECT'  => array( 'SELECT', 'CONTROL SERVER' ),
			'UPDATE'  => array( 'UPDATE', 'CONTROL SERVER' ),
		);

		$grantOptions = array_flip( $this->webUserPrivs );

		// Check for schema and db-level permissions, but only if the schema/db exists
		$schemaPrivs = $dbPrivs = array(
			'DELETE' => false,
			'EXECUTE' => false,
			'INSERT' => false,
			'SELECT' => false,
			'UPDATE' => false,
		);

		$dbPrivs['ALTER ANY USER'] = false;

		if ( $this->databaseExists( $this->getVar( 'wgDBname' ) ) ) {
			$conn->selectDB( $this->getVar( 'wgDBname' ) );
			$res = $conn->query( "SELECT permission_name FROM sys.fn_my_permissions( NULL, 'DATABASE' )" );

			foreach ( $res as $row ) {
				$dbPrivs[$row->permission_name] = true;
			}

			// If the db exists, we need ALTER ANY USER privs on it to make a new user
			if ( !$dbPrivs['ALTER ANY USER'] ) {
				return false;
			}

			if ( $this->schemaExists( $this->getVar( 'wgDBmwschema' ) ) ) {
				// wgDBmwschema is validated to only contain alphanumeric + underscore, so this is safe
				$res = $conn->query( "SELECT permission_name FROM sys.fn_my_permissions( "
					. "'{$this->getVar( 'wgDBmwschema' )}', 'SCHEMA' )" );

				foreach ( $res as $row ) {
					$schemaPrivs[$row->permission_name] = true;
				}
			}
		}

		// Now check all the grants we'll need to be doing to see if we can
		foreach ( $this->webUserPrivs as $permission ) {
			if ( ( isset( $schemaPrivs[$permission] ) && $schemaPrivs[$permission] )
					|| ( isset( $dbPrivs[$implied[$permission][0]] )
						&& $dbPrivs[$implied[$permission][0]] )
					|| ( isset( $serverPrivs[$implied[$permission][1]] )
						&& $serverPrivs[$implied[$permission][1]] )
			) {
				unset( $grantOptions[$permission] );
			}
		}

		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';
		}

		$wrapperStyle = $this->getVar( '_SameAccount' ) ? 'display: none' : '';
		$displayStyle = $this->getVar( '_WebWindowsAuthentication' ) == 'windowsauth'
			? 'display: none'
			: '';
		$s = Html::openElement( 'fieldset' ) .
			Html::element( 'legend', array(), wfMessage( 'config-db-web-account' )->text() ) .
			$this->getCheckBox(
				'_SameAccount', 'config-db-web-account-same',
				array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
			) .
			Html::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => $wrapperStyle ) ) .
			$this->getRadioSet( array(
				'var' => '_WebWindowsAuthentication',
				'label' => 'config-mssql-auth',
				'itemLabelPrefix' => 'config-mssql-',
				'values' => array( 'sqlauth', 'windowsauth' ),
				'itemAttribs' => array(
					'sqlauth' => array(
						'class' => 'showHideRadio',
						'rel' => 'dbCredentialBox',
					),
					'windowsauth' => array(
						'class' => 'hideShowRadio',
						'rel' => 'dbCredentialBox',
					)
				),
				'help' => $this->parent->getHelpBox( 'config-mssql-web-auth' )
			) ) .
			Html::openElement( 'div', array( 'id' => 'dbCredentialBox', 'style' => $displayStyle ) ) .
			$this->getTextBox( 'wgDBuser', 'config-db-username' ) .
			$this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
			Html::closeElement( 'div' );

		if ( $noCreateMsg ) {
			$s .= $this->parent->getWarningBox( wfMessage( $noCreateMsg )->plain() );
		} else {
			$s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
		}

		$s .= Html::closeElement( 'div' ) . Html::closeElement( 'fieldset' );

		return $s;
	}

	/**
	 * @return Status
	 */
	public function submitSettingsForm() {
		$this->setVarsFromRequest( array(
				'wgDBuser',
				'wgDBpassword',
				'_SameAccount',
				'_CreateDBAccount',
				'_WebWindowsAuthentication'
		) );

		if ( $this->getVar( '_SameAccount' ) ) {
			$this->setVar( '_WebWindowsAuthentication', $this->getVar( '_InstallWindowsAuthentication' ) );
			$this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
			$this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
		}

		if ( $this->getVar( '_WebWindowsAuthentication' ) == 'windowsauth' ) {
			$this->setVar( 'wgDBuser', '' );
			$this->setVar( 'wgDBpassword', '' );
			$this->setVar( 'wgDBWindowsAuthentication', true );
		} else {
			$this->setVar( 'wgDBWindowsAuthentication', false );
		}

		if ( $this->getVar( '_CreateDBAccount' )
			&& $this->getVar( '_WebWindowsAuthentication' ) == 'sqlauth'
			&& strval( $this->getVar( 'wgDBpassword' ) ) == ''
		) {
			return Status::newFatal( 'config-db-password-empty', $this->getVar( 'wgDBuser' ) );
		}

		// 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
			$user = $this->getVar( 'wgDBuser' );
			$password = $this->getVar( 'wgDBpassword' );

			if ( $this->getVar( '_WebWindowsAuthentication' ) == 'windowsauth' ) {
				$user = 'windowsauth';
				$password = 'windowsauth';
			}

			try {
				DatabaseBase::factory( 'mssql', array(
					'host' => $this->getVar( 'wgDBserver' ),
					'user' => $user,
					'password' => $password,
					'dbname' => false,
					'flags' => 0,
					'tablePrefix' => $this->getVar( 'wgDBprefix' ),
					'schema' => $this->getVar( 'wgDBmwschema' ),
				) );
			} catch ( DBConnectionError $e ) {
				return Status::newFatal( 'config-connection-error', $e->getMessage() );
			}
		}

		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' );
		$schemaName = $this->getVar( 'wgDBmwschema' );
		if ( !$this->databaseExists( $dbName ) ) {
			$conn->query(
				"CREATE DATABASE " . $conn->addIdentifierQuotes( $dbName ),
				__METHOD__
			);
			$conn->selectDB( $dbName );
			if ( !$this->schemaExists( $schemaName ) ) {
				$conn->query(
					"CREATE SCHEMA " . $conn->addIdentifierQuotes( $schemaName ),
					__METHOD__
				);
			}
			if ( !$this->catalogExists( $schemaName ) ) {
				$conn->query(
					"CREATE FULLTEXT CATALOG " . $conn->addIdentifierQuotes( $schemaName ),
					__METHOD__
				);
			}
		}
		$this->setupSchemaVars();

		return $status;
	}

	/**
	 * @return Status
	 */
	public function setupUser() {
		$dbUser = $this->getVar( 'wgDBuser' );
		if ( $dbUser == $this->getVar( '_InstallUser' )
				|| ( $this->getVar( '_InstallWindowsAuthentication' ) == 'windowsauth'
					&& $this->getVar( '_WebWindowsAuthentication' ) == 'windowsauth' ) ) {
			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' );
		$schemaName = $this->getVar( 'wgDBmwschema' );

		if ( $this->getVar( '_WebWindowsAuthentication' ) == 'windowsauth' ) {
			$dbUser = 'windowsauth';
			$password = 'windowsauth';
		}

		if ( $this->getVar( '_CreateDBAccount' ) ) {
			$tryToCreate = true;
		} else {
			$tryToCreate = false;
		}

		$escUser = $this->db->addIdentifierQuotes( $dbUser );
		$escDb = $this->db->addIdentifierQuotes( $dbName );
		$escSchema = $this->db->addIdentifierQuotes( $schemaName );
		$grantableNames = array();
		if ( $tryToCreate ) {
			$escPass = $this->db->addQuotes( $password );

			if ( !$this->loginExists( $dbUser ) ) {
				try {
					$this->db->begin();
					$this->db->selectDB( 'master' );
					$logintype = $this->getVar( '_WebWindowsAuthentication' ) == 'windowsauth'
						? 'FROM WINDOWS'
						: "WITH PASSWORD = $escPass";
					$this->db->query( "CREATE LOGIN $escUser $logintype" );
					$this->db->selectDB( $dbName );
					$this->db->query( "CREATE USER $escUser FOR LOGIN $escUser WITH DEFAULT_SCHEMA = $escSchema" );
					$this->db->commit();
					$grantableNames[] = $dbUser;
				} catch ( DBQueryError $dqe ) {
					$this->db->rollback();
					$status->warning( 'config-install-user-create-failed', $dbUser, $dqe->getText() );
				}
			} elseif ( !$this->userExists( $dbUser ) ) {
				try {
					$this->db->begin();
					$this->db->selectDB( $dbName );
					$this->db->query( "CREATE USER $escUser FOR LOGIN $escUser WITH DEFAULT_SCHEMA = $escSchema" );
					$this->db->commit();
					$grantableNames[] = $dbUser;
				} catch ( DBQueryError $dqe ) {
					$this->db->rollback();
					$status->warning( 'config-install-user-create-failed', $dbUser, $dqe->getText() );
				}
			} else {
				$status->warning( 'config-install-user-alreadyexists', $dbUser );
				$grantableNames[] = $dbUser;
			}
		}

		// Try to grant to all the users we know exist or we were able to create
		$this->db->selectDB( $dbName );
		foreach ( $grantableNames as $name ) {
			try {
				// First try to grant full permissions
				$fullPrivArr = array(
					'BACKUP DATABASE', 'BACKUP LOG', 'CREATE FUNCTION', 'CREATE PROCEDURE',
					'CREATE TABLE', 'CREATE VIEW', 'CREATE FULLTEXT CATALOG', 'SHOWPLAN'
				);
				$fullPrivList = implode( ', ', $fullPrivArr );
				$this->db->begin();
				$this->db->query( "GRANT $fullPrivList ON DATABASE :: $escDb TO $escUser", __METHOD__ );
				$this->db->query( "GRANT CONTROL ON SCHEMA :: $escSchema TO $escUser", __METHOD__ );
				$this->db->commit();
			} catch ( DBQueryError $dqe ) {
				// If that fails, try to grant the limited subset specified in $this->webUserPrivs
				try {
					$privList = implode( ', ', $this->webUserPrivs );
					$this->db->rollback();
					$this->db->begin();
					$this->db->query( "GRANT $privList ON SCHEMA :: $escSchema TO $escUser", __METHOD__ );
					$this->db->commit();
				} catch ( DBQueryError $dqe ) {
					$this->db->rollback();
					$status->fatal( 'config-install-user-grant-failed', $dbUser, $dqe->getText() );
				}
				// Also try to grant SHOWPLAN on the db, but don't fail if we can't
				// (just makes a couple things in mediawiki run slower since
				// we have to run SELECT COUNT(*) instead of getting the query plan)
				try {
					$this->db->query( "GRANT SHOWPLAN ON DATABASE :: $escDb TO $escUser", __METHOD__ );
				} catch ( DBQueryError $dqe ) {
				}
			}
		}

		return $status;
	}

	public function createTables() {
		$status = parent::createTables();

		// Do last-minute stuff like fulltext indexes (since they can't be inside a transaction)
		if ( $status->isOk() ) {
			$searchindex = $this->db->tableName( 'searchindex' );
			$schema = $this->db->addIdentifierQuotes( $this->getVar( 'wgDBmwschema' ) );
			try {
				$this->db->query( "CREATE FULLTEXT INDEX ON $searchindex (si_title, si_text) "
					. "KEY INDEX si_page ON $schema" );
			} catch ( DBQueryError $dqe ) {
				$status->fatal( 'config-install-tables-failed', $dqe->getText() );
			}
		}

		return $status;
	}

	public function getGlobalDefaults() {
		// The default $wgDBmwschema is null, which breaks Postgres and other DBMSes that require
		// the use of a schema, so we need to set it here
		return array(
			'wgDBmwschema' => 'mediawiki',
		);
	}

	/**
	 * Try to see if the login exists
	 * @param string $user Username to check
	 * @return bool
	 */
	private function loginExists( $user ) {
		$res = $this->db->selectField( 'sys.sql_logins', 1, array( 'name' => $user ) );
		return (bool)$res;
	}

	/**
	 * Try to see if the user account exists
	 * We assume we already have the appropriate database selected
	 * @param string $user Username to check
	 * @return bool
	 */
	private function userExists( $user ) {
		$res = $this->db->selectField( 'sys.sysusers', 1, array( 'name' => $user ) );
		return (bool)$res;
	}

	/**
	 * Try to see if a given database exists
	 * @param string $dbName Database name to check
	 * @return bool
	 */
	private function databaseExists( $dbName ) {
		$res = $this->db->selectField( 'sys.databases', 1, array( 'name' => $dbName ) );
		return (bool)$res;
	}

	/**
	 * Try to see if a given schema exists
	 * We assume we already have the appropriate database selected
	 * @param string $schemaName Schema name to check
	 * @return bool
	 */
	private function schemaExists( $schemaName ) {
		$res = $this->db->selectField( 'sys.schemas', 1, array( 'name' => $schemaName ) );
		return (bool)$res;
	}

	/**
	 * Try to see if a given fulltext catalog exists
	 * We assume we already have the appropriate database selected
	 * @param string $catalogName Catalog name to check
	 * @return bool
	 */
	private function catalogExists( $catalogName ) {
		$res = $this->db->selectField( 'sys.fulltext_catalogs', 1, array( 'name' => $catalogName ) );
		return (bool)$res;
	}

	/**
	 * Get variables to substitute into tables.sql and the SQL patch files.
	 *
	 * @return array
	 */
	public function getSchemaVars() {
		return array(
			'wgDBname' => $this->getVar( 'wgDBname' ),
			'wgDBmwschema' => $this->getVar( 'wgDBmwschema' ),
			'wgDBuser' => $this->getVar( 'wgDBuser' ),
			'wgDBpassword' => $this->getVar( 'wgDBpassword' ),
		);
	}

	public function getLocalSettings() {
		$schema = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgDBmwschema' ) );
		$prefix = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgDBprefix' ) );
		$windowsauth = $this->getVar( 'wgDBWindowsAuthentication' ) ? 'true' : 'false';

		return "# MSSQL specific settings
\$wgDBWindowsAuthentication = {$windowsauth};
\$wgDBmwschema = \"{$schema}\";
\$wgDBprefix = \"{$prefix}\";";
	}
}