Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 13 additions & 4 deletions app/Services/MediaWikiHostResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
*/
class UnknownDBVersionException extends Exception {}

/**
* Exception thrown when a wiki is not found by domain in MediaWikiHostResolver.
*/
class UnknownWikiDomainException extends Exception {}

class MediaWikiHostResolver {
// TODO: Move this mapping to a config file so that MW updates do not require code changes here.
/** @var array<string, string> Map of DB version strings to MediaWiki backend version strings */
Expand All @@ -18,6 +23,7 @@ class MediaWikiHostResolver {
'mw1.43-wbs1' => '143-app',
];

// https://phabricator.wikimedia.org/T409530
// This service could have other methods in future, e.g. getBackendHostForWiki()
// public function getBackendHostForWiki(Wiki $wiki): string {
// return $this->getBackendHostForDomain($wiki->domain);
Expand All @@ -29,10 +35,13 @@ public function getBackendHostForDomain(string $domain): string {
}

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

if (!$wiki) {
throw new UnknownWikiDomainException("Unknown Wiki Domain '{$domain}'.");
}

$dbVersion = $wiki->wikiDb->version;

if (array_key_exists($dbVersion, self::DB_VERSION_TO_MW_VERSION)) {
return self::DB_VERSION_TO_MW_VERSION[$dbVersion];
Expand Down
10 changes: 10 additions & 0 deletions tests/Services/MediaWikiHostResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Services\MediaWikiHostResolver;
use App\Services\UnknownDBVersionException;
use App\Services\UnknownWikiDomainException;
use App\Wiki;
use App\WikiDb;
use Faker\Factory;
Expand Down Expand Up @@ -43,4 +44,13 @@ public function testResolverThrowsIfUnableToFindHostInMap(): void {
UnknownDBVersionException::class
);
}

public function testResolverThrowsIfUnableToFindWiki(): void {
$domain = (new Factory)->create()->unique()->text(30);
$resolver = new MediaWikiHostResolver;
$this->assertThrows(
fn () => $resolver->getBackendHostForDomain($domain),
UnknownWikiDomainException::class
);
}
}
Loading