From 96464e6b94a8ac3bbf9cb9c2b254a0a02ad00ec6 Mon Sep 17 00:00:00 2001 From: dena Date: Fri, 31 Oct 2025 14:23:44 +0100 Subject: [PATCH 1/3] add getVersionedMediawikiBackendHost() --- public_html/php/WbstackMagnusOauth.php | 51 +++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/public_html/php/WbstackMagnusOauth.php b/public_html/php/WbstackMagnusOauth.php index a186cba..abbee40 100644 --- a/public_html/php/WbstackMagnusOauth.php +++ b/public_html/php/WbstackMagnusOauth.php @@ -3,6 +3,7 @@ class WbstackMagnusOauth { public const platformIngressHostAndPort = "platform-nginx.default.svc.cluster.local:8080"; + public const platformApiBackendHost = "api-app-backend.default.svc.cluster.local"; /** * @var bool @@ -29,6 +30,53 @@ class WbstackMagnusOauth { */ private static $callbackUrlTail; + public static function getVersionedMediawikiBackendHost() { + $host = getenv('PLATFORM_MW_BACKEND_HOST'); // default fallback + + $headers = [ + 'Host: ' . $_SERVER['SERVER_NAME'], + ]; + + $client = curl_init('http://' . self::platformApiBackendHost . '/backend/ingress/getWikiVersionForDomain?domain=' . $_SERVER['SERVER_NAME']); + curl_setopt($client, CURLOPT_HTTPHEADER, $headers); + curl_setopt( $client, CURLOPT_USERAGENT, "WBStack - " . self::$consumerName . " - WbstackMagnusOauth::getMediawikiHostByDomain" ); + curl_setopt($client, CURLOPT_RETURNTRANSFER, true); + curl_setopt($client, CURLOPT_HEADER, true); + + $response = curl_exec($client); + + $headerSize = curl_getinfo($client, CURLINFO_HEADER_SIZE); + $headerString = substr($response, 0, $headerSize); + $body = substr($response, $headerSize); + + if ($body == '1') { // success + $headerArray = explode("\r\n", $headerString); + $wikiVersion = null; + + foreach ($headerArray as $header) { + if (stripos($header, 'X-Version:') !== false) { + $valueStart = strpos($header, ':') + 1; + $wikiVersion = substr($header, $valueStart); + $wikiVersion = trim($wikiVersion); + break; + } + } + + // mapping like in https://github.com/wmde/wbaas-deploy/blob/main/k8s/helmfile/env/local/platform-nginx.nginx.conf#L4 + // ideally Platform API could give us this someday + switch ($wikiVersion) { + case 'mw1.39-wbs1': + $host = 'mediawiki-139-app-backend.default.svc.cluster.local'; + break; + case 'mw1.43-wbs1': + $host = 'mediawiki-143-app-backend.default.svc.cluster.local'; + break; + } + } + + return $host; + } + /** * Calling this means that oauth.php will just do the right thing in terms of wbstack. * Not calling this before MW_OAuth is used in a magnus tool will result in an error. @@ -104,7 +152,8 @@ public static function parse_ini_file($filename) { 'callbackUrlTail' => self::$callbackUrlTail, ]; - $client = curl_init('http://' . getenv( 'PLATFORM_MW_BACKEND_HOST' ) . '/w/api.php?action=wbstackPlatformOauthGet&format=json'); + //$client = curl_init('http://' . getenv( 'PLATFORM_MW_BACKEND_HOST' ) . '/w/api.php?action=wbstackPlatformOauthGet&format=json'); + $client = curl_init('http://' . self::getVersionedMediawikiBackendHost() . '/w/api.php?action=wbstackPlatformOauthGet&format=json'); curl_setopt($client, CURLOPT_HTTPHEADER, $headers); curl_setopt( $client, CURLOPT_USERAGENT, "WBStack - " . self::$consumerName . " - WbstackMagnusOauth::parse_ini_file" ); curl_setopt($client, CURLOPT_RETURNTRANSFER, true); From aab47f80399e47abf0057691989a25d16ff662b8 Mon Sep 17 00:00:00 2001 From: dena <91744937+deer-wmde@users.noreply.github.com> Date: Tue, 4 Nov 2025 09:56:40 +0100 Subject: [PATCH 2/3] Update public_html/php/WbstackMagnusOauth.php Co-authored-by: Thomas Arrow --- public_html/php/WbstackMagnusOauth.php | 1 - 1 file changed, 1 deletion(-) diff --git a/public_html/php/WbstackMagnusOauth.php b/public_html/php/WbstackMagnusOauth.php index abbee40..866dd71 100644 --- a/public_html/php/WbstackMagnusOauth.php +++ b/public_html/php/WbstackMagnusOauth.php @@ -152,7 +152,6 @@ public static function parse_ini_file($filename) { 'callbackUrlTail' => self::$callbackUrlTail, ]; - //$client = curl_init('http://' . getenv( 'PLATFORM_MW_BACKEND_HOST' ) . '/w/api.php?action=wbstackPlatformOauthGet&format=json'); $client = curl_init('http://' . self::getVersionedMediawikiBackendHost() . '/w/api.php?action=wbstackPlatformOauthGet&format=json'); curl_setopt($client, CURLOPT_HTTPHEADER, $headers); curl_setopt( $client, CURLOPT_USERAGENT, "WBStack - " . self::$consumerName . " - WbstackMagnusOauth::parse_ini_file" ); From 95546e3eb3dd293c250b7ea0b041570d1185cc6a Mon Sep 17 00:00:00 2001 From: dena Date: Tue, 4 Nov 2025 11:41:19 +0100 Subject: [PATCH 3/3] simplify getVersionedMediawikiBackendHost() --- public_html/php/WbstackMagnusOauth.php | 57 +++++++++----------------- 1 file changed, 20 insertions(+), 37 deletions(-) diff --git a/public_html/php/WbstackMagnusOauth.php b/public_html/php/WbstackMagnusOauth.php index 866dd71..694549e 100644 --- a/public_html/php/WbstackMagnusOauth.php +++ b/public_html/php/WbstackMagnusOauth.php @@ -33,45 +33,28 @@ class WbstackMagnusOauth { public static function getVersionedMediawikiBackendHost() { $host = getenv('PLATFORM_MW_BACKEND_HOST'); // default fallback - $headers = [ - 'Host: ' . $_SERVER['SERVER_NAME'], - ]; - - $client = curl_init('http://' . self::platformApiBackendHost . '/backend/ingress/getWikiVersionForDomain?domain=' . $_SERVER['SERVER_NAME']); - curl_setopt($client, CURLOPT_HTTPHEADER, $headers); - curl_setopt( $client, CURLOPT_USERAGENT, "WBStack - " . self::$consumerName . " - WbstackMagnusOauth::getMediawikiHostByDomain" ); - curl_setopt($client, CURLOPT_RETURNTRANSFER, true); - curl_setopt($client, CURLOPT_HEADER, true); - - $response = curl_exec($client); - - $headerSize = curl_getinfo($client, CURLINFO_HEADER_SIZE); - $headerString = substr($response, 0, $headerSize); - $body = substr($response, $headerSize); - - if ($body == '1') { // success - $headerArray = explode("\r\n", $headerString); - $wikiVersion = null; - - foreach ($headerArray as $header) { - if (stripos($header, 'X-Version:') !== false) { - $valueStart = strpos($header, ':') + 1; - $wikiVersion = substr($header, $valueStart); - $wikiVersion = trim($wikiVersion); - break; + $requestUrl = 'http://' + .self::platformApiBackendHost + .'/backend/ingress/getWikiVersionForDomain?domain=' + .$_SERVER['SERVER_NAME']; + + $headers = get_headers($requestUrl, true); + + if (is_array($headers)) { + if (isset($headers['x-version'])) { + $wikiVersion = $headers['x-version']; + + // mapping like in https://github.com/wmde/wbaas-deploy/blob/main/k8s/helmfile/env/local/platform-nginx.nginx.conf#L4 + // TODO https://phabricator.wikimedia.org/T409078 + switch ($wikiVersion) { + case 'mw1.39-wbs1': + $host = 'mediawiki-139-app-backend.default.svc.cluster.local'; + break; + case 'mw1.43-wbs1': + $host = 'mediawiki-143-app-backend.default.svc.cluster.local'; + break; } } - - // mapping like in https://github.com/wmde/wbaas-deploy/blob/main/k8s/helmfile/env/local/platform-nginx.nginx.conf#L4 - // ideally Platform API could give us this someday - switch ($wikiVersion) { - case 'mw1.39-wbs1': - $host = 'mediawiki-139-app-backend.default.svc.cluster.local'; - break; - case 'mw1.43-wbs1': - $host = 'mediawiki-143-app-backend.default.svc.cluster.local'; - break; - } } return $host;