Skip to content

Commit 5d0ef9e

Browse files
committed
feat: very basic support for wiki farms (#9)
1 parent c8570b6 commit 5d0ef9e

4 files changed

Lines changed: 141 additions & 0 deletions

File tree

config/Defaults.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109

110110
# Load other configuration
111111

112+
require_once 'Farm/MWCFarm.php';
112113
require_once 'MWCConfig.php';
113114
require_once 'MWCExtensions.php';
114115
require_once 'MWCFunctions.php';

config/Farm/MWCFarm.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace MediaWikiConfig\Farm;
4+
5+
use MediaWiki\Config\SiteConfiguration;
6+
use MediaWikiConfig\MediaWikiConfig;
7+
8+
class MWCFarm {
9+
10+
/**
11+
* @param array<string, string> $wikis
12+
* @param array $settings
13+
* @param string $defaultWiki The wiki that will be used for maintenance scripts by default
14+
*/
15+
public function __construct(
16+
private readonly array $wikis,
17+
private array $settings,
18+
private readonly string $defaultWiki,
19+
) {
20+
}
21+
22+
public function apply( MediaWikiConfig $mwc ): void {
23+
$port = $mwc->env( 'MW_DOCKER_PORT' );
24+
$serverVals = [];
25+
foreach ( $this->wikis as $subdomain => $dbname ) {
26+
$serverVals[$dbname] = "http://$subdomain.localhost:$port";
27+
}
28+
$this->settings['wgServer'] = $serverVals;
29+
30+
if ( defined( 'MW_DB' ) ) {
31+
$wikiId = MW_DB;
32+
} elseif ( MW_ENTRY_POINT === 'cli' ) {
33+
$wikiId = $this->defaultWiki;
34+
} else {
35+
$subdomain = explode( '.', $_SERVER['SERVER_NAME'] )[0];
36+
if ( !array_key_exists( $subdomain, $this->wikis ) ) {
37+
$this->showWikiMap();
38+
} else {
39+
$wikiId = $this->wikis[$subdomain];
40+
}
41+
}
42+
43+
$siteConfiguration = new SiteConfiguration();
44+
$siteConfiguration->wikis = array_values( array_unique( $this->wikis ) );
45+
$mwc
46+
->conf( 'wgLocalDatabases', $siteConfiguration->wikis )
47+
->conf( 'wgDBname', $wikiId );
48+
$siteConfiguration->suffixes = [ 'wiki' ];
49+
$siteConfiguration->settings = $this->settings;
50+
51+
foreach ( $siteConfiguration->getAll( $wikiId ) as $key => $value ) {
52+
$mwc->conf( $key, $value );
53+
}
54+
55+
$mwc->conf( 'wgConf', $siteConfiguration );
56+
}
57+
58+
private function showWikiMap(): never {
59+
require_once __DIR__ . '/NotFound.php';
60+
die( 1 );
61+
}
62+
63+
/**
64+
* @return array<string, string>
65+
*/
66+
public function getWikis(): array {
67+
return $this->wikis;
68+
}
69+
70+
}

config/Farm/NotFound.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
use MediaWiki\Html\Html;
4+
use MediaWikiConfig\Farm\MWCFarm;
5+
6+
global $wgMwcFarm;
7+
/** @var MWCFarm $wgMwcFarm */
8+
9+
if ( MW_ENTRY_POINT !== 'cli' ) {
10+
$wikiLinks = '';
11+
// TODO bad
12+
$port = getenv( 'MW_DOCKER_PORT' );
13+
$path = parse_url( $_SERVER['REQUEST_URI'] ?? '|', PHP_URL_PATH ) ?? '';
14+
foreach ( $wgMwcFarm->getWikis() as $subdomain => $dbName ) {
15+
$link = Html::element(
16+
'a',
17+
[
18+
// TODO un-hardcode
19+
'href' => "http://$subdomain.localhost:$port$path",
20+
'class' => 'button',
21+
],
22+
$dbName,
23+
);
24+
$wikiLinks .= Html::rawElement( 'li', [], $link );
25+
}
26+
27+
// TODO include simplecss in repo?
28+
$output = <<<EOF
29+
<!DOCTYPE html>
30+
<html lang="en">
31+
<head>
32+
<meta charset="utf-8" />
33+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
34+
<title>Wiki not found</title>
35+
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
36+
</head>
37+
<body>
38+
<header>
39+
<h1>Wiki not found</h1>
40+
</header>
41+
<main>
42+
No wiki was found at the current subdomain.
43+
<h2>Available wikis</h2>
44+
<ul>$wikiLinks</ul>
45+
</main>
46+
<footer>
47+
<p>
48+
Powered by mw-dev-kit
49+
(<a href="https://github.com/SomeMWDev/mw-dev-kit" target="_blank">GitHub</a>)
50+
</p>
51+
</footer>
52+
</body>
53+
</html>
54+
EOF;
55+
header( 'Content-length: ' . strlen( $output ) );
56+
http_response_code( 404 );
57+
echo $output;
58+
die( 1 );
59+
} else {
60+
echo "The wiki database '{$this->getConf('wgDBName')}' was not found." . PHP_EOL;
61+
}

config/MWCConfig.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace MediaWikiConfig;
44

5+
use MediaWikiConfig\Farm\MWCFarm;
6+
57
trait MWCConfig {
68

79
public function allowExternalImages(): self {
@@ -87,4 +89,11 @@ public function setMaxArticleSize( int $amount, int $unit ): self {
8789
return $this->conf( 'wgMaxArticleSize', $kibibytes );
8890
}
8991

92+
public function setupFarm( MWCFarm $farm ): self {
93+
global $wgMwcFarm;
94+
$wgMwcFarm = $farm;
95+
$farm->apply( $this );
96+
return $this;
97+
}
98+
9099
}

0 commit comments

Comments
 (0)