Skip to content

Commit df69f2e

Browse files
committed
[PoC] Route to correct MW version
Bug: T407504
1 parent 1562a97 commit df69f2e

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

app/Jobs/WikiEntityImportJob.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Jobs;
44

5+
use App\Services\MediaWikiHostResolver;
56
use App\Wiki;
67
use App\WikiEntityImport;
78
use App\WikiEntityImportStatus;
@@ -20,6 +21,8 @@
2021
class WikiEntityImportJob implements ShouldQueue {
2122
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2223

24+
private MediaWikiHostResolver $mwHostResolver;
25+
2326
/**
2427
* Create a new job instance.
2528
*/
@@ -28,7 +31,10 @@ public function __construct(
2831
public string $sourceWikiUrl,
2932
public array $entityIds,
3033
public int $importId,
31-
) {}
34+
MediaWikiHostResolver $mwHostResolver = null,
35+
) {
36+
$this->mwHostResolver = $mwHostResolver ?? new MediaWikiHostResolver();
37+
}
3238

3339
private string $targetWikiUrl;
3440

@@ -77,9 +83,9 @@ private static function domainToOrigin(string $domain): string {
7783
: 'https://' . $domain;
7884
}
7985

80-
private static function acquireCredentials(string $wikiDomain): OAuthCredentials {
86+
private function acquireCredentials(string $wikiDomain): OAuthCredentials {
8187
$response = Http::withHeaders(['host' => $wikiDomain])->asForm()->post(
82-
getenv('PLATFORM_MW_BACKEND_HOST') . '/w/api.php?action=wbstackPlatformOauthGet&format=json',
88+
$this->mwHostResolver->getBackendHostForDomain($wikiDomain) . '/w/api.php?action=wbstackPlatformOauthGet&format=json',
8389
[
8490
'consumerName' => 'WikiEntityImportJob',
8591
'ownerOnly' => '1',
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+
}

0 commit comments

Comments
 (0)