MwddSettings.php 6.86 KB
Newer Older
Addshore's avatar
Addshore committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php

################################
# MWDD START
################################

# Protect against web entry
if ( !defined( 'MEDIAWIKI' ) ) {
	exit;
}

################################
# MWDD Setup
################################

# When used via CLI, use the default DB if no MW_DB is specified
17
# Maintenance scripts with --wiki passed will set MW_DB
Addshore's avatar
Addshore committed
18
19
20
21
if ( PHP_SAPI === 'cli' && !defined( 'MW_DB' ) ) {
    define( 'MW_DB', 'default' );
}

22
23
24
# Detect usage of update.php, so we can turn of replication https://phabricator.wikimedia.org/T283417
$dockerIsRunningUpdate = basename( $_SERVER['argv'][0] ) === 'update.php';

Addshore's avatar
Addshore committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Must be above WebRequest::detectServer.
# mwdd uses a proxy server with no default ports.
$wgAssumeProxiesUseDefaultProtocolPorts = false;

# Either use the MW_DB env var, or get the DB from the request
if ( defined( "MW_DB" ) ) {
    $dockerDb = MW_DB;
    $wgServer = "//$dockerDb.mediawiki.mwdd.localhost:80";
} elseif( array_key_exists( 'SERVER_NAME', $_SERVER ) ) {
    $dockerHostParts = explode( '.', $_SERVER['SERVER_NAME'] );
    $dockerDb = $dockerHostParts[0];
    $wgServer = WebRequest::detectServer();
} else {
    die( 'Unable to decide which MediaWiki DB to use (from env or request).' );
}

# Only use "advanced" services if they can be seen, and if we are not in tests
$mwddServices = [
Addshore's avatar
Addshore committed
43
	'mysql' => gethostbyname('mysql') !== 'mysql',
44
	'mysql-replica' => gethostbyname('mysql-replica') !== 'mysql-replica' && !defined( 'MW_PHPUNIT_TEST' ) && !$dockerIsRunningUpdate,
Addshore's avatar
Addshore committed
45
	'redis' => gethostbyname('redis') !== 'redis' && !defined( 'MW_PHPUNIT_TEST' ),
Addshore's avatar
Addshore committed
46
	'graphite' => gethostbyname('graphite') !== 'graphite' && !defined( 'MW_PHPUNIT_TEST' ),
Addshore's avatar
Addshore committed
47
48
49
50
51
];

################################
# MWDD Database
################################
52
53
// TODO cache the DB existance checks somehow so as not to run on every request...

Addshore's avatar
Addshore committed
54
55
56
// Figure out if we are using sqlite, or if this should be mysql..?
if( file_exists( $IP . '/data/' . $dockerDb . '.sqlite' ) ) {
	$dockerDbType = 'sqlite';
57
58
59
60
}

// Figure out if we are using mysql
if(!isset($dockerDbType)){
Addshore's avatar
Addshore committed
61
	try{
62
63
64
65
66
67
68
69
		$mysqlPdo = new PDO(
			"mysql:host=mysql;dbname=" . $dockerDb,
			'root',
			'toor',
			[
				PDO::ATTR_TIMEOUT => 1, // in seconds
			]
		);
70
71
72
73
74
75
		$mysqlCheck = $mysqlPdo->query("SHOW DATABASES LIKE \"" . $dockerDb . "\"");
		if($mysqlCheck === false) {
			var_dump(json_encode($mysqlPdo->errorInfo()));
			die("Failed to get mysql databases list looking for {$dockerDb}.");
		}
		if(count($mysqlCheck->fetchAll()) === 1){
Addshore's avatar
Addshore committed
76
77
78
			$dockerDbType = 'mysql';
		}
	} catch ( Exception $e ) {
79
		// TODO set the timeout on connection to be much shorter, so that when mysql doesnt exist, it doenst hang for a while
Addshore's avatar
Addshore committed
80
81
		// do nothing
	}
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
}

// Figure out if we are using postgres
if(!isset($dockerDbType)){
	$pvars = [
		'dbname' => $dockerDb,
		'user' => 'root',
		'password' => 'toor',
		'host' => 'postgres',
	];
	$pconnString = '';
	foreach ( $pvars as $name => $value ) {
		$pconnString .= "$name='" . str_replace( "'", "\\'", $value ) . "' ";
	}
	$postgresConn = @pg_connect( $pconnString . ' connect_timeout=1', PGSQL_CONNECT_FORCE_NEW );
	if($postgresConn !== false){
Addshore's avatar
Addshore committed
98
99
		$dockerDbType = 'postgres';
	}
Addshore's avatar
Addshore committed
100
101
}

102
103
104
105
106
// Otherwise something must be wrong
if(!isset($dockerDbType)){
	die("Is your database running and wiki database created / installed?");
}

Addshore's avatar
Addshore committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
$wgDBname = $dockerDb;

if( $dockerDbType === 'sqlite' ) {
	$wgDBservers = [
		[
			'dbDirectory' => $IP . '/data',
			'dbname' => $dockerDb,
			'type' => $dockerDbType,
			'flags' => DBO_DEFAULT,
			'load' => 1,
		],
	];
}

if( $dockerDbType === 'mysql' ) {
	$wgDBservers = [
		[
124
			'host' => "mysql",
Addshore's avatar
Addshore committed
125
126
127
128
129
			'dbname' => $dockerDb,
			'user' => 'root',
			'password' => 'toor',
			'type' => $dockerDbType,
			'flags' => DBO_DEFAULT,
130
			'load' => $mwddServices['mysql-replica'] ? 0 : 1,
Addshore's avatar
Addshore committed
131
132
		],
	];
133
	if($mwddServices['mysql-replica'] ) {
Addshore's avatar
Addshore committed
134
		$wgDBservers[] = [
135
			'host' => "mysql-replica",
Addshore's avatar
Addshore committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
			'dbname' => $dockerDb,
			'user' => 'root',
			'password' => 'toor',
			'type' => $dockerDbType,
			'flags' => DBO_DEFAULT,
			# Avoid switching to readonly too early (for example during update.php)
			'max lag' => 60,
			'load' => 1,
		];
	}

	// mysql only stuff (would need to change for sqlite?)
	$wgDBprefix = "";
	$wgDBTableOptions = "ENGINE=InnoDB, DEFAULT CHARSET=binary";
}

Addshore's avatar
Addshore committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
if( $dockerDbType === 'postgres' ) {
	$wgDBservers = [
		[
			'host' => "postgres",
			'dbname' => $dockerDb,
			'user' => 'root',
			'password' => 'toor',
			'type' => $dockerDbType,
			'flags' => DBO_DEFAULT,
			'load' => 1,
		],
	];
	// https://www.mediawiki.org/wiki/Manual:$wgDBmwschema
	$wgDBmwschema = "mediawiki";
}

Addshore's avatar
Addshore committed
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
################################
# MWDD Redis
################################
if(gethostbyname('redis') !== 'redis') {
	$wgObjectCaches['redis'] = [
		'class' => 'RedisBagOStuff',
		'servers' => [ 'redis:6379' ],
	];
}


################################
# MWDD Graphite & Statsd
################################
if(gethostbyname('graphite-statsd') !== 'graphite-statsd') {
	$wgStatsdServer = "graphite-statsd";
}

################################
# MWDD Special Page
################################
require_once __DIR__ . '/MwddSpecialPage.php';
$wgSpecialPages['Mwdd'] = MwddSpecial::class;
$wgExtensionMessagesFiles['Mwdd'] = __DIR__ . '/MwddSpecialPage-aliases.php';

################################
# MWDD Dev Settings
################################
$wgShowHostnames = true;

## Site settings
$wgScriptPath = "/w";
$wgSitename = "mwdd-$dockerDb";
$wgMetaNamespace = "Project"; // Set to "Project", instead of the default $wgSitename
// TODO re add favicon (removed porting to go)
//$wgFavicon = "{$wgScriptPath}/.docker/favicon.ico";

## Various directories
$wgUploadDirectory = "{$IP}/images/docker/{$dockerDb}";
$wgTmpDirectory = "{$wgUploadDirectory}/tmp";
$wgCacheDirectory = "{$wgUploadDirectory}/cache";

$wgUploadPath = "{$wgScriptPath}/images/docker/{$dockerDb}";

## Dev & Debug
$dockerLogDirectory = "/var/log/mediawiki";
$wgDebugLogFile = "$dockerLogDirectory/debug.log";

error_reporting( -1 );
ini_set( 'display_errors', 1 );
$wgShowExceptionDetails = true;
$wgShowSQLErrors = true;
$wgDebugDumpSql  = true;
$wgShowDBErrorBacktrace = true;
$wgDevelopmentWarnings = true;
$wgEnableJavaScriptTest = true;

## Email

# TODO use some mail catcher?
$wgEnableEmail = true;
$wgEmergencyContact = "mediawiki@$dockerDb";
$wgPasswordSender = "mediawiki@$dockerDb";
$wgEnableUserEmail = true;
$wgEmailAuthentication = true;

## Notifications, turned off as we don't send mail
$wgEnotifUserTalk = false;
$wgEnotifWatchlist = false;

## Files
$wgEnableUploads = true;
$wgAllowCopyUploads = true;
$wgUseInstantCommons = false;

## Keys
$wgUpgradeKey = "0j90sa0fjsa90jf0ajfaa";
$wgSecretKey = "j8093j903j902jfr9j109j109jf1093jf09j190jfj09fj1jfknnccnmxnmx";

## PHP Location
// TODO check me
$wgPhpCli = '/usr/local/bin/php';

################################
# MWDD END
################################

// TODO add auto loading of other LocalSetting.php files from a directory based on dbname -> file name...