Skip to content

Commit f6235ee

Browse files
committed
extend mediawikihostresolver (#994)
1 parent b7a2ec7 commit f6235ee

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

app/Services/MediaWikiHostResolver.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
*/
1111
class UnknownDBVersionException extends Exception {}
1212

13+
/**
14+
* Exception thrown when a wiki is not found by domain in MediaWikiHostResolver.
15+
*/
16+
class UnknownWikiDomainException extends Exception {}
17+
1318
class MediaWikiHostResolver {
1419
// TODO: Move this mapping to a config file so that MW updates do not require code changes here.
1520
/** @var array<string, string> Map of DB version strings to MediaWiki backend version strings */
@@ -18,6 +23,7 @@ class MediaWikiHostResolver {
1823
'mw1.43-wbs1' => '143-app',
1924
];
2025

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

3137
public function getMwVersionForDomain(string $domain): string {
32-
$dbVersion = Wiki::where('domain', $domain)
33-
->first()
34-
->wikiDb
35-
->version;
38+
$wiki = Wiki::where('domain', $domain)->first();
39+
40+
if (!$wiki) {
41+
throw new UnknownWikiDomainException("Unknown Wiki Domain '{$domain}'.");
42+
}
43+
44+
$dbVersion = $wiki->wikiDb->version;
3645

3746
if (array_key_exists($dbVersion, self::DB_VERSION_TO_MW_VERSION)) {
3847
return self::DB_VERSION_TO_MW_VERSION[$dbVersion];

tests/Services/MediaWikiHostResolverTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Services\MediaWikiHostResolver;
66
use App\Services\UnknownDBVersionException;
7+
use App\Services\UnknownWikiDomainException;
78
use App\Wiki;
89
use App\WikiDb;
910
use Faker\Factory;
@@ -43,4 +44,13 @@ public function testResolverThrowsIfUnableToFindHostInMap(): void {
4344
UnknownDBVersionException::class
4445
);
4546
}
47+
48+
public function testResolverThrowsIfUnableToFindWiki(): void {
49+
$domain = (new Factory)->create()->unique()->text(30);
50+
$resolver = new MediaWikiHostResolver;
51+
$this->assertThrows(
52+
fn () => $resolver->getBackendHostForDomain($domain),
53+
UnknownWikiDomainException::class
54+
);
55+
}
4656
}

0 commit comments

Comments
 (0)