Make dependency to Wikibase client config optional

The MathEntitySelector gets its config from wbRepo which caused a hard
dependency on the Wikibase client.
If the Wikibase client config is not available a default value (wikidata)
is used to determine the URL of the foreign repo.
As the dropdown will never do write operations, we restrict the repo
access to anonymous read only.

Bug: T313143
Change-Id: Iba33dfd32a78f4ad7c2e99a1f56218458ab884b4
This commit is contained in:
Moritz Schubotz (physikerwelt) 2022-07-17 11:59:09 +02:00
parent 9bb379ad3f
commit 480d241f97
No known key found for this signature in database
GPG key ID: F803DB146DDF36C3
7 changed files with 123 additions and 17 deletions

View file

@ -24,7 +24,8 @@ return [
},
'Math.Config' => static function ( MediaWikiServices $services ): MathConfig {
return new MathConfig(
new ServiceOptions( MathConfig::CONSTRUCTOR_OPTIONS, $services->getMainConfig() )
new ServiceOptions( MathConfig::CONSTRUCTOR_OPTIONS, $services->getMainConfig() ),
ExtensionRegistry::getInstance()
);
},
'Math.RendererFactory' => static function ( MediaWikiServices $services ): RendererFactory {

View file

@ -42,6 +42,12 @@
"services": [
"Math.Config"
]
},
"ResourceLoaderConfigHandler": {
"class": "MediaWiki\\Extension\\Math\\HookHandlers\\ResourceLoaderConfigHandler",
"services": [
"Math.Config"
]
}
},
"Hooks": {
@ -54,7 +60,8 @@
"WikibaseClientDataTypes": "MediaWiki\\Extension\\Math\\WikibaseHook::onWikibaseClientDataTypes",
"WikibaseRepoDataTypes": "MediaWiki\\Extension\\Math\\WikibaseHook::onWikibaseRepoDataTypes",
"SpecialPage_initList": "MediaWiki\\Extension\\Math\\Hooks::onSpecialPageInitList",
"MaintenanceRefreshLinksInit": "MediaWiki\\Extension\\Math\\Hooks::onMaintenanceRefreshLinksInit"
"MaintenanceRefreshLinksInit": "MediaWiki\\Extension\\Math\\Hooks::onMaintenanceRefreshLinksInit",
"ResourceLoaderGetConfigVars": "ResourceLoaderConfigHandler"
},
"config": {
"MathDefaultLaTeXMLSetting": {
@ -90,6 +97,10 @@
"MathEnableExperimentalInputFormats": {
"value": false
},
"MathEntitySelectorFallbackUrl": {
"description": "Fallback value for wbEntitySelector if wbRepo is not configured. See https://www.mediawiki.org/wiki/Manual:CORS for cross wiki communication.",
"value": "https://www.wikidata.org/w/api.php"
},
"MathLaTeXMLTimeout": {
"value": 240
},
@ -218,7 +229,6 @@
"dependencies": [
"ext.visualEditor.mwcore",
"ext.math.styles",
"mw.config.values.wbRepo",
"oojs-ui.styles.icons-editing-advanced"
],
"messages": [

View file

@ -18,8 +18,9 @@
config = config || {};
this.api = config.api;
if ( !this.api ) {
var repoConfig = mw.config.get( 'wbRepo' ), url = repoConfig.url + repoConfig.scriptPath + '/api.php';
this.api = new mw.ForeignApi( url );
this.api = new mw.ForeignApi(
mw.config.get( 'wgMathEntitySelectorUrl' ),
{ anonymous: true } );
}
OO.ui.TextInputWidget.call( this, config );
OO.ui.mixin.LookupElement.call( this, config );

View file

@ -0,0 +1,28 @@
<?php
namespace MediaWiki\Extension\Math\HookHandlers;
use Config;
use MediaWiki\Extension\Math\MathConfig;
use MediaWiki\ResourceLoader\Hook\ResourceLoaderGetConfigVarsHook;
class ResourceLoaderConfigHandler implements
ResourceLoaderGetConfigVarsHook
{
/** @var MathConfig */
private $mathConfig;
/**
* @param MathConfig $mathConfig
*/
public function __construct(
MathConfig $mathConfig
) {
$this->mathConfig = $mathConfig;
}
public function onResourceLoaderGetConfigVars( array &$vars, $skin, Config $config ): void {
$vars['wgMathEntitySelectorUrl'] = $this->mathConfig->getMathEntitySelectorUrl();
}
}

View file

@ -2,8 +2,10 @@
namespace MediaWiki\Extension\Math;
use ExtensionRegistry;
use MediaWiki\Config\ServiceOptions;
use Message;
use Wikibase\Client\WikibaseClient;
class MathConfig {
@ -11,6 +13,7 @@ class MathConfig {
public const CONSTRUCTOR_OPTIONS = [
'MathDisableTexFilter',
'MathValidModes',
'MathEntitySelectorFallbackUrl'
];
/** @var string */
@ -54,15 +57,21 @@ class MathConfig {
/** @var ServiceOptions */
private $options;
/** @var ExtensionRegistry */
private $registry;
/**
* @param ServiceOptions $options
* @param ExtensionRegistry $registry
*/
public function __construct(
ServiceOptions $options
ServiceOptions $options,
ExtensionRegistry $registry
) {
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
$this->options = $options;
$this->registry = $registry;
}
/**
@ -161,4 +170,21 @@ class MathConfig {
}
return $default;
}
/**
* If the WikibaseClient is enabled the API url of that client is returned, otherwise the
* fallback url is used.
* @return string url of the Wikibase url
*/
public function getMathEntitySelectorUrl(): string {
// @see WikibaseSettings::isClientEnabled()
if ( $this->registry->isLoaded( 'WikibaseClient' ) ) {
$settings = WikibaseClient::getSettings();
return $settings->getSetting( 'repoUrl' ) .
$settings->getSetting( 'repoScriptPath' ) .
'/api.php';
}
return $this->options->get( 'MathEntitySelectorFallbackUrl' );
}
}

View file

@ -2,23 +2,33 @@
namespace MediaWiki\Extension\Math\Tests;
use ExtensionRegistry;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Extension\Math\MathConfig;
use MediaWikiUnitTestCase;
use PHPUnit\Framework\TestCase;
/**
* @covers \MediaWiki\Extension\Math\MathConfig
*/
class MathConfigTest extends MediaWikiUnitTestCase {
class MathConfigTest extends TestCase {
private const TEST_DEFAULT = 'test-default';
private const DUMMY_URL = 'https://example.com/api.php';
private function newMathConfig( array $configOverrides ): MathConfig {
private function newMathConfig(
array $configOverrides,
ExtensionRegistry $registry = null
): MathConfig {
if ( $registry === null ) {
$registry = ExtensionRegistry::getInstance();
}
return new MathConfig(
new ServiceOptions( MathConfig::CONSTRUCTOR_OPTIONS, $configOverrides + [
'MathDisableTexFilter' => MathConfig::ALWAYS,
'MathValidModes' => [ MathConfig::MODE_SOURCE ],
] )
'MathEntitySelectorFallbackUrl' => self::DUMMY_URL,
] ),
$registry
);
}
@ -65,10 +75,10 @@ class MathConfigTest extends MediaWikiUnitTestCase {
$mathConfig = $this->newMathConfig( [
'MathValidModes' => [ MathConfig::MODE_MATHML, 5, MathConfig::MODE_PNG, 'this will be converted to png', ],
] );
$this->assertArrayEquals(
[ MathConfig::MODE_MATHML, MathConfig::MODE_PNG ],
$mathConfig->getValidRenderingModes()
);
$actualModes = $mathConfig->getValidRenderingModes();
$this->assertCount( 2, $actualModes );
$this->assertContains( MathConfig::MODE_MATHML, $actualModes );
$this->assertContains( MathConfig::MODE_PNG, $actualModes );
}
public function provideIsValidRenderingMode() {
@ -91,6 +101,30 @@ class MathConfigTest extends MediaWikiUnitTestCase {
$mathConfig = $this->newMathConfig( [
'MathValidModes' => [ MathConfig::MODE_PNG ],
] );
$this->assertArrayEquals( [ 'mw_math_png' ], $mathConfig->getValidRenderingModeKeys() );
$this->assertEquals(
[ 'png' => 'mw_math_png' ],
$mathConfig->getValidRenderingModeKeys() );
}
public function testEntitySelectorUrlWikibase() {
$mathConfig = $this->newMathConfig( [] );
if ( ExtensionRegistry::getInstance()->isLoaded( 'WikibaseClient' ) ) {
$localUrl = $mathConfig->getMathEntitySelectorUrl();
$this->assertStringContainsString( 'api.php', $localUrl );
$this->assertStringNotContainsString( 'example.com', $localUrl );
} else {
$this->markTestSkipped( 'Wikibase Client is not loaded partially skipping test.' );
}
}
public function testEntitySelectorUrlFallback() {
$mathConfig = $this->newMathConfig( [], new class extends ExtensionRegistry {
public function isLoaded( $name, $constraint = '*' ): bool {
return false;
}
} );
$url = $mathConfig->getMathEntitySelectorUrl();
$this->assertEquals( self::DUMMY_URL, $url );
}
}

View file

@ -1,6 +1,7 @@
<?php
namespace MediaWiki\Extension\Math\Tests;
use ExtensionRegistry;
use HashConfig;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Extension\Math\Math;
@ -12,17 +13,22 @@ use MediaWikiUnitTestCase;
* @covers \MediaWiki\Extension\Math\Math
*/
class MathTest extends MediaWikiUnitTestCase {
private const DUMMY_URL = 'https://example.com/api.php';
public function testGetMathConfigService() {
$config = new HashConfig( [
'MathDisableTexFilter' => MathConfig::NEW,
'MathValidModes' => [ MathConfig::MODE_SOURCE ]
'MathValidModes' => [ MathConfig::MODE_SOURCE ],
'MathEntitySelectorFallbackUrl' => self::DUMMY_URL,
] );
$services = new MediaWikiServices( $config );
$services->defineService( 'Math.Config',
static function ( MediaWikiServices $services ){
return new MathConfig(
new ServiceOptions( MathConfig::CONSTRUCTOR_OPTIONS, $services->get( 'BootstrapConfig' ) )
new ServiceOptions(
MathConfig::CONSTRUCTOR_OPTIONS,
$services->get( 'BootstrapConfig' ) ),
ExtensionRegistry::getInstance()
);
}
);