|
| 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 | +} |
0 commit comments