-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.php
320 lines (273 loc) · 8.93 KB
/
module.php
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
<?php
use LithiumHosting\WebApps\MediaWiki\Handler;
use Module\Support\Webapps;
use Module\Support\Webapps\DatabaseGenerator;
use Module\Support\Webapps\PhpWrapper;
use Module\Support\Webapps\VersionFetcher\Github;
use Opcenter\Auth\Password;
use Opcenter\Versioning;
/**
* MediaWiki management
*
* @package core
*/
class MediaWiki_Module extends Webapps
{
const APP_NAME = Handler::NAME;
const DEFAULT_VERSION_LOCK = 'minor';
protected $aclList = [
'min' => [
'images'
],
'max' => [
'images'
],
];
/**
* Install MediaWiki into a pre-existing location
*
* @param string $hostname domain or subdomain to install MediaWiki
* @param string $path optional path under hostname
* @param array $opts additional installation options
*
* @return bool
*/
public function install(string $hostname, string $path = '', array $opts = []): bool
{
if (! $this->parseInstallOptions($opts, $hostname, $path)) {
return false;
}
if (! version_compare($this->php_version(), '7', '>=')) {
return error('MediaWiki requires PHP7.3 or higher.');
}
if (version_compare($this->php_version(), '7.4.0', '>=') && version_compare($this->php_version(), '7.4.2', '<=')) {
return error('MediaWiki is not compatible with PHP 7.4.0 - 7.4.2 due to an upstream bug. Use PHP 7.4.3+ instead.');
}
if (version_compare($opts['version'], '1.38.4', '<') && version_compare($this->php_version(), '8', '>=')) {
return error('If using PHP8 we recommend using MediaWiki 1.38.4+');
}
if (! ($docroot = $this->getDocumentRoot($hostname, $path))) {
return error("failed to normalize path for `%s'", $hostname);
}
$args['version'] = $opts['version'];
$oldex = Error_Reporter::exception_upgrade(Error_Reporter::E_ERROR);
$approot = $this->getAppRoot($hostname, $path);
try {
$this->downloadPackage($approot, $args['version']);
$db = DatabaseGenerator::mysql($this->getAuthContext(), $hostname);
$db->connectionLimit = max($db->connectionLimit, 15);
if (! $db->create()) {
return false;
}
if (! isset($opts['sitename'])) {
$opts['sitename'] = 'My Wiki';
info("setting sitename to default of `%s'", $opts['sitename']);
}
if (! isset($opts['adminuser'])) {
$opts['adminuser'] = 'wikiadmin';
info("auto generated admin username `%s'", $opts['adminuser']);
}
$opts['adminpassword'] = Password::generate();
info("autogenerated password `%s'", $opts['adminpassword']);
$opts['email'] ?? $this->common_get_email();
$installArgs = [
'path' => $approot,
'dbserver' => $db->hostname,
'dbname' => $db->database,
'dbuser' => $db->username,
'dbpass' => $db->password,
'passwd' => $opts['adminpassword'],
'server' => $opts['ssl'] ? "https://$hostname" : "http://$hostname",
'sitename' => $opts['sitename'],
'adminuser' => $opts['adminuser']
];
// https://www.mediawiki.org/wiki/Manual:Install.php
$installCommand = 'install.php '.
'--dbserver %(dbserver)s '.
'--dbname %(dbname)s '.
'--dbuser %(dbuser)s '.
'--dbpass %(dbpass)s '.
'--installdbuser %(dbuser)s '.
'--installdbpass %(dbpass)s '.
'--pass %(passwd)s '.
'--server %(server)s '.
'--scriptpath "" '.
'"%(sitename)s" %(adminuser)s';
$ret = PhpWrapper::instantiateContexted($this->getAuthContextFromDocroot($approot))->exec("$approot/maintenance", $installCommand, $installArgs);
if (! $ret['success']) {
return error("failed to install %(app)s: %(err)s", [
'app' => static::APP_NAME,
'approot' => $approot,
'err' => $ret['stderr']
]);
}
} catch (apnscpException $e) {
$this->file_delete($docroot, true);
$db->rollback();
return error('Failed to install %s: %s', static::APP_NAME, $e->getMessage());
} finally {
Error_Reporter::exception_upgrade($oldex);
}
$this->initializeMeta($docroot, $opts);
$this->fortify($hostname, $path, Handler::DEFAULT_FORTIFICATION);
$this->notifyInstalled($hostname, $path, $opts);
return info('%(app)s installed - confirmation email with login info sent to %(email)s',
['app' => static::APP_NAME, 'email' => $opts['email']]);
}
/**
* Get all available MediaWiki versions
*
* @return array
*/
public function get_versions(): array
{
$versions = $this->_getVersions();
return array_column(array_filter($versions, static function($meta) {
return false === strpos($meta['version'], '-');
}), 'version');
}
/**
* Get all current major versions
*
* @return array
*/
private function _getVersions(): array
{
$key = 'mediawiki.versions';
$cache = Cache_Super_Global::spawn();
if (false !== ($ver = $cache->get($key))) {
return (array) $ver;
}
$versions = (new Github)->setMode('tags')->fetch('wikimedia/mediawiki');
$cache->set($key, $versions, 43200);
return $versions;
}
/**
* Get installed version
*
* @param string $hostname
* @param string $path
*
* @return string version number
*/
public function get_version(string $hostname, string $path = ''): ?string
{
$approot = $this->getAppRoot($hostname, $path);
if (! $this->valid($hostname, $path)) {
return null;
}
$contents = $this->file_get_file_contents("$approot/includes/Defines.php");
if (preg_match_all("/^.*MW_VERSION.*\$/m", $contents, $lines)) {
if (0 !== preg_match('/\d{1,2}\.\d{1,2}\.\d{1,3}/', $lines[0][0], $matches)) {
return $matches[0];
}
return null;
}
return null;
}
/**
* Location is a valid MediaWiki install
*
* @param string $hostname or $docroot
* @param string $path
*
* @return bool
*/
public function valid(string $hostname, string $path = ''): bool
{
if ($hostname[0] === '/') {
$approot = $hostname;
} else {
$approot = $this->getAppRoot($hostname, $path);
if (! $approot) {
return false;
}
}
return $this->file_exists($approot.'/includes/MediaWiki.php');
}
/**
* @inheritDoc
*/
public function update_all(string $hostname, string $path = '', string $version = null): bool
{
return $this->update($hostname, $path, $version);
}
/**
* Update MediaWiki to latest version
*
* @param string $hostname domain or subdomain under which WP is installed
* @param string $path optional subdirectory
* @param string $version version to upgrade
*
* @return bool
*/
public function update(string $hostname, string $path = '', string $version = null): bool
{
$approot = $this->getAppRoot($hostname, $path);
if (! $approot) {
return error('update failed');
}
$oldversion = $this->get_version($hostname, $path);
$ret = serial(function() use ($approot, $version, $hostname, $path) {
if (! $version) {
$version = Versioning::nextVersion($this->get_versions(),
$this->get_version($hostname, $path));
} else {
if (! Versioning::valid($version)) {
return error('invalid version number, %s', $version);
}
}
$this->downloadPackage($approot, $version);
// https://www.mediawiki.org/wiki/Manual:Update.php
$ret = PhpWrapper::instantiateContexted($this->getAuthContextFromDocroot($approot))->exec("$approot/maintenance", 'update.php --quick');
if (! $ret['success']) {
return error("Failed to upgrade %s: %s", static::APP_NAME, $ret['stdout']);
}
return $this->fortify($hostname, $path, data_get($this->getOptions($approot), 'fortify') ?: 'max');
});
$this->setInfo($approot, [
'version' => $ret ? $version : $oldversion,
'failed' => ! $ret
]);
return (bool) $ret;
}
/**
* @inheritdoc
*/
public function db_config(string $hostname, string $path = '')
{
$approot = $this->getAppRoot($hostname, $path);
if (! $this->file_exists($approot.'/LocalSettings.php')) {
return false;
}
$code = 'ob_start(); function wfLoadSkin(){}; function wfLoadExtension(){}; define(\'MEDIAWIKI\', TRUE); include("./LocalSettings.php"); file_put_contents("php://fd/3", serialize(["db" => $wgDBname, "user" => $wgDBuser, "host" => $wgDBserver, "prefix" => $wgDBprefix, "password" => $wgDBpassword])); ';
$cmd = 'cd %(path)s && php -d mysqli.default_socket=%(socket)s -r %(code)s 3>&1-';
$ret = $this->pman_run($cmd, [
'path' => $approot,
'code' => $code,
'socket' => ini_get('mysqli.default_socket')
]);
if (! $ret['success']) {
return error("failed to obtain %(app)s configuration for `%(approot)s': %(err)s", [
'app' => static::APP_NAME,
'approot' => $approot,
'err' => $ret['stderr']
]);
}
return \Util_PHP::unserialize(trim($ret['stdout']));
}
/**
* Download remote MediaWiki version and Extract into directory
*
* @param string $approot
* @param string $version
*
* @return bool
*/
private function downloadPackage(string $approot, string $version): bool
{
$minor = Versioning::asMinor($version);
$this->download("https://releases.wikimedia.org/mediawiki/$minor/mediawiki-$version.zip", $approot);
return $this->file_copy("$approot/mediawiki-$version/", $approot) && $this->file_delete("$approot/mediawiki-$version", true);
}
}