Skip to content

Commit ea35d0a

Browse files
outdooracorntarrow
authored andcommitted
Introduce MediaWikiHostResolver
Co-authored-by: Ollie <oliver.hyde@wikimedia.de> Bug: T409085 Bug: T408624
1 parent ca359b5 commit ea35d0a

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
use App\Wiki;
6+
7+
class MediaWikiHostResolver
8+
{
9+
// TODO: Move this mapping to a config file that doesn't require updating this code when doing a MW update?
10+
/** @var array<string, string> Map of DB version strings to MediaWiki backend version strings */
11+
private const DB_VERSION_TO_MW_VERSION = [
12+
'mw1.39-wbs1' => '139-app',
13+
'mw1.43-wbs1' => '143-app'
14+
];
15+
16+
// This service could have other methods in future, e.g. getBackendHostForWiki()
17+
// public function getBackendHostForWiki(Wiki $wiki): string {
18+
// return $this->getBackendHostForDomain($wiki->domain);
19+
// }
20+
21+
public function getBackendHostForDomain(string $domain): string
22+
{
23+
// TODO: should 'backend.default.svc.cluster.local' be an env var e.g. PLATFORM_MW_BACKEND_HOST_SUFFIX?
24+
return sprintf('mediawiki-%s-backend.default.svc.cluster.local', $this->getMwVersionForDomain($domain));
25+
}
26+
27+
public function getMwVersionForDomain(string $domain): string
28+
{
29+
$dbVersion = Wiki::where('domain', $domain)
30+
->whereNull('deleted_at')
31+
->leftJoin('wiki_dbs', 'wiki_id', '=', 'wikis.id')
32+
->pluck('version')
33+
->first();
34+
35+
return self::DB_VERSION_TO_MW_VERSION[$dbVersion];
36+
}
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use App\Services\MediaWikiHostResolver;
6+
use App\Wiki;
7+
use App\WikiDb;
8+
use App\WikiDomain;
9+
use Faker\Factory;
10+
use Faker\Generator;
11+
use Illuminate\Foundation\Testing\RefreshDatabase;
12+
13+
class MediaWikiHostResolverTest extends TestCase
14+
{
15+
use RefreshDatabase;
16+
public function testResolverRoutesToCorrectHost(): void
17+
{
18+
$domain = (new Factory())->create()->unique()->text(30);
19+
$this->createWiki($domain, 'mw1.39-wbs1');
20+
$resolver = new MediaWikiHostResolver();
21+
$this->assertEquals(
22+
'mediawiki-139-app-backend.default.svc.cluster.local',
23+
$resolver->getBackendHostForDomain($domain)
24+
);
25+
}
26+
27+
private function createWiki(string $domain, string $version) {
28+
$wiki = Wiki::factory()->create(['domain' => $domain]);
29+
WikiDb::create([
30+
'name' => $domain,
31+
'user' => 'someUser',
32+
'password' => 'somePassword',
33+
'version' => $version,
34+
'prefix' => 'somePrefix',
35+
'wiki_id' => $wiki->id,
36+
]);
37+
}
38+
39+
public function testResolverThrowsIfUnableToFindHostInMap(): void
40+
{
41+
$this->assertTrue(true);
42+
}
43+
}

0 commit comments

Comments
 (0)