diff --git a/app/Jobs/WikiEntityImportJob.php b/app/Jobs/WikiEntityImportJob.php index 6b0af4736..932a6f17b 100644 --- a/app/Jobs/WikiEntityImportJob.php +++ b/app/Jobs/WikiEntityImportJob.php @@ -2,6 +2,7 @@ namespace App\Jobs; +use App\Services\MediaWikiHostResolver; use App\Wiki; use App\WikiEntityImport; use App\WikiEntityImportStatus; @@ -20,6 +21,8 @@ class WikiEntityImportJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + private MediaWikiHostResolver $mwHostResolver; + /** * Create a new job instance. */ @@ -28,7 +31,10 @@ public function __construct( public string $sourceWikiUrl, public array $entityIds, public int $importId, - ) {} + MediaWikiHostResolver $mwHostResolver = null, + ) { + $this->mwHostResolver = $mwHostResolver ?? new MediaWikiHostResolver(); + } private string $targetWikiUrl; @@ -77,9 +83,9 @@ private static function domainToOrigin(string $domain): string { : 'https://' . $domain; } - private static function acquireCredentials(string $wikiDomain): OAuthCredentials { + private function acquireCredentials(string $wikiDomain): OAuthCredentials { $response = Http::withHeaders(['host' => $wikiDomain])->asForm()->post( - getenv('PLATFORM_MW_BACKEND_HOST') . '/w/api.php?action=wbstackPlatformOauthGet&format=json', + $this->mwHostResolver->getBackendHostForDomain($wikiDomain) . '/w/api.php?action=wbstackPlatformOauthGet&format=json', [ 'consumerName' => 'WikiEntityImportJob', 'ownerOnly' => '1', diff --git a/app/Services/MediaWikiHostResolver.php b/app/Services/MediaWikiHostResolver.php new file mode 100644 index 000000000..54d5072ad --- /dev/null +++ b/app/Services/MediaWikiHostResolver.php @@ -0,0 +1,37 @@ + 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? + return sprintf('mediawiki-%s-backend.default.svc.cluster.local', $this->getMwVersionForDomain($domain)); + } + + public function getMwVersionForDomain(string $domain): string + { + $dbVersion = Wiki::where('domain', $domain) + ->whereNull('deleted_at') + ->leftJoin('wiki_dbs', 'wiki_id', '=', 'wikis.id') + ->pluck('version') + ->first(); + + return self::DB_VERSION_TO_MW_VERSION[$dbVersion]; + } +}