File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace App \Services ;
4+
5+ use App \Wiki ;
6+ use Exception ;
7+
8+ /**
9+ * Exception thrown when a database version is not recognized in MediaWikiHostResolver.
10+ */
11+ class UnknownDBVersionException extends Exception {}
12+
13+ class MediaWikiHostResolver {
14+ // TODO: Move this mapping to a config file so that MW updates do not require code changes here.
15+ /** @var array<string, string> Map of DB version strings to MediaWiki backend version strings */
16+ private const DB_VERSION_TO_MW_VERSION = [
17+ 'mw1.39-wbs1 ' => '139-app ' ,
18+ 'mw1.43-wbs1 ' => '143-app ' ,
19+ ];
20+
21+ // This service could have other methods in future, e.g. getBackendHostForWiki()
22+ // public function getBackendHostForWiki(Wiki $wiki): string {
23+ // return $this->getBackendHostForDomain($wiki->domain);
24+ // }
25+
26+ public function getBackendHostForDomain (string $ domain ): string {
27+ // TODO: Move 'backend.default.svc.cluster.local' to an env variable (e.g. PLATFORM_MW_BACKEND_HOST_SUFFIX) for flexibility.
28+ return sprintf ('mediawiki-%s-backend.default.svc.cluster.local ' , $ this ->getMwVersionForDomain ($ domain ));
29+ }
30+
31+ public function getMwVersionForDomain (string $ domain ): string {
32+ $ dbVersion = Wiki::where ('domain ' , $ domain )
33+ ->first ()
34+ ->wikiDb
35+ ->version ;
36+
37+ if (array_key_exists ($ dbVersion , self ::DB_VERSION_TO_MW_VERSION )) {
38+ return self ::DB_VERSION_TO_MW_VERSION [$ dbVersion ];
39+ }
40+ throw new UnknownDBVersionException ("Unknown DB version ' {$ dbVersion }' for domain ' {$ domain }'. " );
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Tests ;
4+
5+ use App \Services \MediaWikiHostResolver ;
6+ use App \Services \UnknownDBVersionException ;
7+ use App \Wiki ;
8+ use App \WikiDb ;
9+ use Faker \Factory ;
10+ use Illuminate \Foundation \Testing \RefreshDatabase ;
11+
12+ class MediaWikiHostResolverTest extends TestCase {
13+ use RefreshDatabase;
14+
15+ public function testResolverRoutesToCorrectHost (): void {
16+ $ domain = (new Factory )->create ()->unique ()->text (30 );
17+ $ this ->createWiki ($ domain , 'mw1.39-wbs1 ' );
18+ $ resolver = new MediaWikiHostResolver ;
19+ $ this ->assertEquals (
20+ 'mediawiki-139-app-backend.default.svc.cluster.local ' ,
21+ $ resolver ->getBackendHostForDomain ($ domain )
22+ );
23+ }
24+
25+ private function createWiki (string $ domain , string $ version ) {
26+ $ wiki = Wiki::factory ()->create (['domain ' => $ domain ]);
27+ WikiDb::create ([
28+ 'name ' => $ domain ,
29+ 'user ' => 'someUser ' ,
30+ 'password ' => 'somePassword ' ,
31+ 'version ' => $ version ,
32+ 'prefix ' => 'somePrefix ' ,
33+ 'wiki_id ' => $ wiki ->id ,
34+ ]);
35+ }
36+
37+ public function testResolverThrowsIfUnableToFindHostInMap (): void {
38+ $ domain = (new Factory )->create ()->unique ()->text (30 );
39+ $ this ->createWiki ($ domain , 'mw1.39-unmapped ' );
40+ $ resolver = new MediaWikiHostResolver ;
41+ $ this ->assertThrows (
42+ fn () => $ resolver ->getBackendHostForDomain ($ domain ),
43+ UnknownDBVersionException::class
44+ );
45+ }
46+ }
You can’t perform that action at this time.
0 commit comments