2011-04-09 00:39:40 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* MediaWiki math extension
|
|
|
|
*
|
2018-04-13 14:18:16 +00:00
|
|
|
* @copyright 2002-2015 various MediaWiki contributors
|
|
|
|
* @license GPL-2.0-or-later
|
2011-04-09 00:39:40 +00:00
|
|
|
*/
|
|
|
|
|
2021-04-07 22:22:05 +00:00
|
|
|
namespace MediaWiki\Extension\Math;
|
|
|
|
|
2023-05-22 20:17:58 +00:00
|
|
|
use ConfigException;
|
2021-04-07 22:22:05 +00:00
|
|
|
use DatabaseUpdater;
|
|
|
|
use Exception;
|
2021-06-21 11:32:26 +00:00
|
|
|
use ExtensionRegistry;
|
2021-04-07 22:22:05 +00:00
|
|
|
use Maintenance;
|
2021-04-02 18:30:13 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-05-22 20:17:58 +00:00
|
|
|
use MediaWiki\Settings\SettingsBuilder;
|
2021-04-07 22:22:05 +00:00
|
|
|
use RequestContext;
|
2015-04-14 19:42:48 +00:00
|
|
|
|
2021-04-07 22:22:05 +00:00
|
|
|
class Hooks {
|
2017-07-10 09:40:12 +00:00
|
|
|
|
2023-05-22 20:17:58 +00:00
|
|
|
/**
|
|
|
|
* Extension registration callback, used to apply dynamic defaults for configuration variables.
|
|
|
|
*/
|
|
|
|
public static function onConfig( array $extInfo, SettingsBuilder $settings ) {
|
|
|
|
$config = $settings->getConfig();
|
|
|
|
|
|
|
|
$fullRestbaseUrl = $config->get( 'MathFullRestbaseURL' );
|
|
|
|
$internalRestbaseURL = $config->get( 'MathInternalRestbaseURL' );
|
|
|
|
$useInternalRestbasePath = $config->get( 'MathUseInternalRestbasePath' );
|
|
|
|
$virtualRestConfig = $config->get( 'VirtualRestConfig' );
|
|
|
|
|
|
|
|
if ( !$fullRestbaseUrl ) {
|
|
|
|
if ( $config->has( 'VisualEditorFullRestbaseURL' ) ) {
|
|
|
|
$settings->warning( "MathFullRestbaseURL is falling back to using VisualEditorFullRestbaseURL. " .
|
|
|
|
"Please configure the Mathoid API URL explicitly." );
|
|
|
|
|
|
|
|
$fullRestbaseUrl = $config->get( 'VisualEditorFullRestbaseURL' );
|
|
|
|
$settings->overrideConfigValue( 'MathFullRestbaseURL', $fullRestbaseUrl );
|
|
|
|
} else {
|
|
|
|
throw new ConfigException(
|
|
|
|
'Math extension can not find Restbase URL. Please specify $wgMathFullRestbaseURL.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$useInternalRestbasePath ) {
|
|
|
|
if ( $internalRestbaseURL ) {
|
|
|
|
$settings->warning( "The MathInternalRestbaseURL setting will be ignored " .
|
|
|
|
"because MathUseInternalRestbasePath is set to false." );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force the use of the external URL for internal calls as well.
|
|
|
|
$settings->overrideConfigValue( 'MathInternalRestbaseURL', $fullRestbaseUrl );
|
|
|
|
} elseif ( !$internalRestbaseURL ) {
|
|
|
|
if ( isset( $virtualRestConfig['modules']['restbase'] ) ) {
|
|
|
|
$settings->warning( "The MathInternalRestbaseURL is falling back to " .
|
|
|
|
"VirtualRestConfig. Please set MathInternalRestbaseURL explicitly." );
|
|
|
|
|
|
|
|
$restBaseUrl = $virtualRestConfig['modules']['restbase']['url'];
|
|
|
|
$restBaseUrl = rtrim( $restBaseUrl, '/' );
|
|
|
|
|
|
|
|
$restBaseDomain = $virtualRestConfig['modules']['restbase']['domain'] ?? 'localhost';
|
|
|
|
|
|
|
|
// Ensure the correct domain format: strip protocol, port,
|
|
|
|
// and trailing slash if present. This lets us use
|
|
|
|
// $wgCanonicalServer as a default value, which is very convenient.
|
|
|
|
// XXX: This was copied from RestbaseVirtualRESTService. Use UrlUtils::parse instead?
|
|
|
|
$restBaseDomain = preg_replace(
|
|
|
|
'/^((https?:)?\/\/)?([^\/:]+?)(:\d+)?\/?$/',
|
|
|
|
'$3',
|
|
|
|
$restBaseDomain
|
|
|
|
);
|
|
|
|
|
2023-06-08 01:05:08 +00:00
|
|
|
$internalRestbaseURL = "$restBaseUrl/$restBaseDomain/";
|
2023-05-22 20:17:58 +00:00
|
|
|
} else {
|
|
|
|
// Default to using the external URL for internal calls as well.
|
|
|
|
$internalRestbaseURL = $fullRestbaseUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
$settings->overrideConfigValue( 'MathInternalRestbaseURL', $internalRestbaseURL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-09 19:57:35 +00:00
|
|
|
/**
|
|
|
|
* MaintenanceRefreshLinksInit handler; optimize settings for refreshLinks batch job.
|
|
|
|
*
|
|
|
|
* @param Maintenance $maint
|
|
|
|
*/
|
2018-11-02 17:52:06 +00:00
|
|
|
public static function onMaintenanceRefreshLinksInit( $maint ) {
|
2020-09-27 01:02:10 +00:00
|
|
|
$user = RequestContext::getMain()->getUser();
|
2011-04-09 19:57:35 +00:00
|
|
|
|
2020-09-28 12:50:50 +00:00
|
|
|
// Don't parse LaTeX to improve performance
|
2021-04-02 18:30:13 +00:00
|
|
|
MediaWikiServices::getInstance()->getUserOptionsManager()
|
2021-08-11 18:15:35 +00:00
|
|
|
->setOption( $user, 'math', MathConfig::MODE_SOURCE );
|
2011-04-09 19:57:35 +00:00
|
|
|
}
|
2011-04-22 21:37:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* LoadExtensionSchemaUpdates handler; set up math table on install/upgrade.
|
|
|
|
*
|
2019-11-25 05:31:29 +00:00
|
|
|
* @param DatabaseUpdater $updater
|
2015-01-10 01:55:54 +00:00
|
|
|
* @throws Exception
|
2011-04-22 21:37:16 +00:00
|
|
|
*/
|
2019-11-25 05:31:29 +00:00
|
|
|
public static function onLoadExtensionSchemaUpdates( DatabaseUpdater $updater ) {
|
2021-08-11 18:15:35 +00:00
|
|
|
$type = $updater->getDB()->getType();
|
2023-06-06 12:35:12 +00:00
|
|
|
if ( !in_array( $type, [ 'mysql', 'sqlite', 'postgres' ], true ) ) {
|
2022-06-01 08:39:29 +00:00
|
|
|
throw new Exception( "Math extension does not currently support $type database." );
|
2015-01-22 18:56:13 +00:00
|
|
|
}
|
2022-06-01 08:39:29 +00:00
|
|
|
|
|
|
|
foreach ( [ 'mathoid', 'mathlatexml' ] as $mode ) {
|
|
|
|
$updater->addExtensionTable(
|
|
|
|
$mode,
|
2022-06-10 20:11:28 +00:00
|
|
|
__DIR__ . "/../sql/$type/$mode.sql"
|
2022-06-01 08:39:29 +00:00
|
|
|
);
|
2011-04-22 21:37:16 +00:00
|
|
|
}
|
2015-01-22 18:56:13 +00:00
|
|
|
|
2022-06-01 08:39:29 +00:00
|
|
|
if ( $type === 'mysql' ) {
|
|
|
|
$updater->addExtensionField(
|
|
|
|
'mathoid',
|
|
|
|
'math_png',
|
2022-06-10 20:11:28 +00:00
|
|
|
__DIR__ . '/../sql/' . $type . '/patch-mathoid.add_png.sql'
|
2022-06-01 08:39:29 +00:00
|
|
|
);
|
|
|
|
}
|
2011-04-22 21:37:16 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 08:54:09 +00:00
|
|
|
/**
|
|
|
|
* Remove Special:MathWikibase if the Wikibase client extension isn't loaded
|
|
|
|
*
|
|
|
|
* @param array &$list
|
|
|
|
*/
|
2021-06-21 11:32:26 +00:00
|
|
|
public static function onSpecialPageInitList( &$list ) {
|
|
|
|
if ( !ExtensionRegistry::getInstance()->isLoaded( 'WikibaseClient' ) ) {
|
2019-12-28 08:54:09 +00:00
|
|
|
unset( $list['MathWikibase'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-09 00:39:40 +00:00
|
|
|
}
|