Skip to content

Commit 680a0c4

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

2 files changed

Lines changed: 45 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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
private const array DB_VERSION_TO_MW_VERSION = [
11+
'mw1.39-wbs1' => '139-app',
12+
'mw1.43-wbs1' => '143-app'
13+
];
14+
15+
// This service could have other methods in future, e.g. getBackendHostForWiki()
16+
// public function getBackendHostForWiki(Wiki $wiki): string {
17+
// return $this->getBackendHostForDomain($wiki->domain);
18+
// }
19+
20+
public function getBackendHostForDomain(string $domain): string
21+
{
22+
// TODO: should 'backend.default.svc.cluster.local' be an env var e.g. PLATFORM_MW_BACKEND_HOST_SUFFIX?
23+
return sprintf('mediawiki-%s-backend.default.svc.cluster.local', $this->getMwVersionForDomain($domain));
24+
}
25+
26+
public function getMwVersionForDomain(string $domain): string
27+
{
28+
$dbVersion = Wiki::where('domain', $domain)
29+
->whereNull('deleted_at')
30+
->leftJoin('wiki_dbs', 'wiki_id', '=', 'wikis.id')
31+
->pluck('version')
32+
->first();
33+
34+
return self::DB_VERSION_TO_MW_VERSION[$dbVersion];
35+
}
36+
}

0 commit comments

Comments
 (0)