Skip to content
Closed
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
12 changes: 9 additions & 3 deletions app/Jobs/WikiEntityImportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Jobs;

use App\Services\MediaWikiHostResolver;
use App\Wiki;
use App\WikiEntityImport;
use App\WikiEntityImportStatus;
Expand All @@ -20,6 +21,8 @@
class WikiEntityImportJob implements ShouldQueue {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

private MediaWikiHostResolver $mwHostResolver;

/**
* Create a new job instance.
*/
Expand All @@ -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;

Expand Down Expand Up @@ -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',
Expand Down
37 changes: 37 additions & 0 deletions app/Services/MediaWikiHostResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Services;
Copy link
Contributor

@deer-wmde deer-wmde Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unclear about the introduction of this Service namespace and location, this could be confused with the Laravel concept of Service Providers https://laravel.com/docs/10.x/providers

I think App\Helper would be more appropriate https://github.com/wbstack/api/tree/main/app/Helper

I got confused, Service Container is the pattern used here https://laravel.com/docs/10.x/container


use App\Wiki;

class MediaWikiHostResolver
{
// TODO: Move this mapping to a config file that doesn't require updating this code when doing a MW update?
/** @var array<string, string> 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];
}
}
Loading