diff --git a/config/Defaults.php b/config/Defaults.php index 176945b..d9d1e8e 100644 --- a/config/Defaults.php +++ b/config/Defaults.php @@ -109,6 +109,7 @@ # Load other configuration +require_once 'Farm/MWCFarm.php'; require_once 'MWCConfig.php'; require_once 'MWCExtensions.php'; require_once 'MWCFunctions.php'; diff --git a/config/Farm/MWCFarm.php b/config/Farm/MWCFarm.php new file mode 100644 index 0000000..565095d --- /dev/null +++ b/config/Farm/MWCFarm.php @@ -0,0 +1,70 @@ + $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 + */ + public function getWikis(): array { + return $this->wikis; + } + +} diff --git a/config/Farm/NotFound.php b/config/Farm/NotFound.php new file mode 100644 index 0000000..d843448 --- /dev/null +++ b/config/Farm/NotFound.php @@ -0,0 +1,61 @@ +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 = << + + + + + Wiki not found + + + +
+

Wiki not found

+
+
+ No wiki was found at the current subdomain. +

Available wikis

+
    $wikiLinks
+
+ + + + 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; +} diff --git a/config/MWCConfig.php b/config/MWCConfig.php index 438c4dc..133eb5b 100644 --- a/config/MWCConfig.php +++ b/config/MWCConfig.php @@ -2,6 +2,8 @@ namespace MediaWikiConfig; +use MediaWikiConfig\Farm\MWCFarm; + trait MWCConfig { public function allowExternalImages(): self { @@ -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; + } + }