Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
42 changes: 42 additions & 0 deletions app/Services/MediaWikiHostResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Services;

use App\Wiki;
use Exception;

class MediaWikiHostResolver {
// TODO: Move this mapping to a config file that doesn't require updating this code when doing a MW update?
Comment thread
tarrow marked this conversation as resolved.
Outdated
/** @var array<string, string> Map of DB version strings to MediaWiki backend version strings */
private const DB_VERSION_TO_MW_VERSION = [
'mw1.39-wbs1' => '139-app',
'mw1.43-wbs1' => '143-app',
];

// This service could have other methods in future, e.g. getBackendHostForWiki()
// public function getBackendHostForWiki(Wiki $wiki): string {
// return $this->getBackendHostForDomain($wiki->domain);
// }

public function getBackendHostForDomain(string $domain): string {
// TODO: should 'backend.default.svc.cluster.local' be an env var e.g. PLATFORM_MW_BACKEND_HOST_SUFFIX?
Comment thread
tarrow marked this conversation as resolved.
Outdated
return sprintf('mediawiki-%s-backend.default.svc.cluster.local', $this->getMwVersionForDomain($domain));
}

public function getMwVersionForDomain(string $domain): string {
$dbVersion = Wiki::where('domain', $domain)
->first()
->wikiDb
->version;

if (array_key_exists($dbVersion, self::DB_VERSION_TO_MW_VERSION)) {
return self::DB_VERSION_TO_MW_VERSION[$dbVersion];
}
throw new UnknownDBVersionException("Unknown DB version '{$dbVersion}' for domain '{$domain}'.");
}
}

Comment thread
tarrow marked this conversation as resolved.
Outdated
/**
* Exception thrown when a database version is not recognized in MediaWikiHostResolver.
*/
class UnknownDBVersionException extends Exception {}
Comment thread
tarrow marked this conversation as resolved.
Outdated
46 changes: 46 additions & 0 deletions tests/Services/MediaWikiHostResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Tests;

use App\Services\MediaWikiHostResolver;
use App\Services\UnknownDBVersionException;
use App\Wiki;
use App\WikiDb;
use Faker\Factory;
use Illuminate\Foundation\Testing\RefreshDatabase;

class MediaWikiHostResolverTest extends TestCase {
use RefreshDatabase;

public function testResolverRoutesToCorrectHost(): void {
$domain = (new Factory)->create()->unique()->text(30);
$this->createWiki($domain, 'mw1.39-wbs1');
$resolver = new MediaWikiHostResolver;
$this->assertEquals(
'mediawiki-139-app-backend.default.svc.cluster.local',
$resolver->getBackendHostForDomain($domain)
);
}

private function createWiki(string $domain, string $version) {
$wiki = Wiki::factory()->create(['domain' => $domain]);
WikiDb::create([
'name' => $domain,
'user' => 'someUser',
'password' => 'somePassword',
'version' => $version,
'prefix' => 'somePrefix',
'wiki_id' => $wiki->id,
]);
}

public function testResolverThrowsIfUnableToFindHostInMap(): void {
$domain = (new Factory)->create()->unique()->text(30);
$this->createWiki($domain, 'mw1.39-unmapped');
$resolver = new MediaWikiHostResolver;
$this->assertThrows(
fn () => $resolver->getBackendHostForDomain($domain),
UnknownDBVersionException::class
);
}
}
Loading