Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@

# Load other configuration

require_once 'Farm/MWCFarm.php';
require_once 'MWCConfig.php';
require_once 'MWCExtensions.php';
require_once 'MWCFunctions.php';
Expand Down
70 changes: 70 additions & 0 deletions config/Farm/MWCFarm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace MediaWikiConfig\Farm;

use MediaWiki\Config\SiteConfiguration;
use MediaWikiConfig\MediaWikiConfig;

class MWCFarm {

/**
* @param array<string, string> $wikis
* @param array $settings
* @param string $defaultWiki The wiki that will be used for maintenance scripts by default
*/
public function __construct(
private readonly array $wikis,
private array $settings,
private readonly string $defaultWiki,
) {
}

public function apply( MediaWikiConfig $mwc ): void {
$port = $mwc->env( 'MW_DOCKER_PORT' );
$serverVals = [];
foreach ( $this->wikis as $subdomain => $dbname ) {
$serverVals[$dbname] = "http://$subdomain.localhost:$port";
}
$this->settings['wgServer'] = $serverVals;

if ( defined( 'MW_DB' ) ) {
$wikiId = MW_DB;
} elseif ( MW_ENTRY_POINT === 'cli' ) {
$wikiId = $this->defaultWiki;
} else {
$subdomain = explode( '.', $_SERVER['SERVER_NAME'] )[0];
if ( !array_key_exists( $subdomain, $this->wikis ) ) {
$this->showWikiMap();
} else {
$wikiId = $this->wikis[$subdomain];
}
}

$siteConfiguration = new SiteConfiguration();
$siteConfiguration->wikis = array_values( array_unique( $this->wikis ) );
$mwc
->conf( 'wgLocalDatabases', $siteConfiguration->wikis )
->conf( 'wgDBname', $wikiId );
$siteConfiguration->suffixes = [ 'wiki' ];
$siteConfiguration->settings = $this->settings;

foreach ( $siteConfiguration->getAll( $wikiId ) as $key => $value ) {
$mwc->conf( $key, $value );
}

$mwc->conf( 'wgConf', $siteConfiguration );
}

private function showWikiMap(): never {
require_once __DIR__ . '/NotFound.php';
die( 1 );
}

/**
* @return array<string, string>
*/
public function getWikis(): array {
return $this->wikis;
}

}
61 changes: 61 additions & 0 deletions config/Farm/NotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

use MediaWiki\Html\Html;
use MediaWikiConfig\Farm\MWCFarm;

global $wgMwcFarm;
/** @var MWCFarm $wgMwcFarm */

if ( MW_ENTRY_POINT !== 'cli' ) {
$wikiLinks = '';
// TODO bad
$port = getenv( 'MW_DOCKER_PORT' );
$path = parse_url( $_SERVER['REQUEST_URI'] ?? '|', PHP_URL_PATH ) ?? '';
foreach ( $wgMwcFarm->getWikis() as $subdomain => $dbName ) {
$link = Html::element(
'a',
[
// TODO un-hardcode
'href' => "http://$subdomain.localhost:$port$path",
'class' => 'button',
],
$dbName,
);
$wikiLinks .= Html::rawElement( 'li', [], $link );
}

// TODO include simplecss in repo?
$output = <<<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Wiki not found</title>
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
</head>
<body>
<header>
<h1>Wiki not found</h1>
</header>
<main>
No wiki was found at the current subdomain.
<h2>Available wikis</h2>
<ul>$wikiLinks</ul>
</main>
<footer>
<p>
Powered by mw-dev-kit
(<a href="https://github.com/SomeMWDev/mw-dev-kit" target="_blank">GitHub</a>)
</p>
</footer>
</body>
</html>
EOF;
header( 'Content-length: ' . strlen( $output ) );
http_response_code( 404 );
echo $output;
die( 1 );
} else {
echo "The wiki database '{$this->getConf('wgDBName')}' was not found." . PHP_EOL;
}
9 changes: 9 additions & 0 deletions config/MWCConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MediaWikiConfig;

use MediaWikiConfig\Farm\MWCFarm;

trait MWCConfig {

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

public function setupFarm( MWCFarm $farm ): self {
global $wgMwcFarm;
$wgMwcFarm = $farm;
$farm->apply( $this );
return $this;
}

}