Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.22% covered (warning)
72.22%
26 / 36
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
72.22% covered (warning)
72.22%
26 / 36
0.00% covered (danger)
0.00%
0 / 5
15.09
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onConfig
89.66% covered (warning)
89.66%
26 / 29
0.00% covered (danger)
0.00%
0 / 1
6.04
 onExtensionFunctions
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 onMaintenanceRefreshLinksInit
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 onSpecialPage_initList
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * MediaWiki math extension
4 *
5 * @copyright 2002-2015 various MediaWiki contributors
6 * @license GPL-2.0-or-later
7 */
8
9namespace MediaWiki\Extension\Math;
10
11// phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName
12
13use MediaWiki\Config\ConfigException;
14use MediaWiki\Context\RequestContext;
15use MediaWiki\Hook\MaintenanceRefreshLinksInitHook;
16use MediaWiki\Maintenance\Maintenance;
17use MediaWiki\Registration\ExtensionRegistry;
18use MediaWiki\Settings\SettingsBuilder;
19use MediaWiki\SpecialPage\Hook\SpecialPage_initListHook;
20use MediaWiki\User\Options\UserOptionsManager;
21
22class Hooks implements
23    SpecialPage_initListHook,
24    MaintenanceRefreshLinksInitHook
25{
26    private UserOptionsManager $userOptionsManager;
27
28    public function __construct(
29        UserOptionsManager $userOptionsManager
30    ) {
31        $this->userOptionsManager = $userOptionsManager;
32    }
33
34    /**
35     * Extension registration callback, used to apply dynamic defaults for configuration variables.
36     */
37    public static function onConfig( array $extInfo, SettingsBuilder $settings ) {
38        $config = $settings->getConfig();
39
40        // Documentation of MathRestbaseInterface::getUrl() should be updated when this is changed.
41
42        $fullRestbaseUrl = $config->get( 'MathFullRestbaseURL' );
43        $internalRestbaseURL = $config->get( 'MathInternalRestbaseURL' );
44        $useInternalRestbasePath = $config->get( 'MathUseInternalRestbasePath' );
45        $virtualRestConfig = $config->get( 'VirtualRestConfig' );
46
47        if ( !$fullRestbaseUrl ) {
48            throw new ConfigException(
49                'Math extension can not find Restbase URL. Please specify $wgMathFullRestbaseURL.'
50            );
51        }
52
53        if ( !$useInternalRestbasePath ) {
54            if ( $internalRestbaseURL ) {
55                $settings->warning( "The MathInternalRestbaseURL setting will be ignored " .
56                    "because MathUseInternalRestbasePath is set to false." );
57            }
58
59            // Force the use of the external URL for internal calls as well.
60            $settings->overrideConfigValue( 'MathInternalRestbaseURL', $fullRestbaseUrl );
61        } elseif ( !$internalRestbaseURL ) {
62            if ( isset( $virtualRestConfig['modules']['restbase'] ) ) {
63                $settings->warning( "The MathInternalRestbaseURL is falling back to " .
64                    "VirtualRestConfig. Please set MathInternalRestbaseURL explicitly." );
65
66                $restBaseUrl = $virtualRestConfig['modules']['restbase']['url'];
67                $restBaseUrl = rtrim( $restBaseUrl, '/' );
68
69                $restBaseDomain = $virtualRestConfig['modules']['restbase']['domain'] ?? 'localhost';
70
71                // Ensure the correct domain format: strip protocol, port,
72                // and trailing slash if present.  This lets us use
73                // $wgCanonicalServer as a default value, which is very convenient.
74                // XXX: This was copied from RestbaseVirtualRESTService. Use UrlUtils::parse instead?
75                $restBaseDomain = preg_replace(
76                    '/^((https?:)?\/\/)?([^\/:]+?)(:\d+)?\/?$/',
77                    '$3',
78                    $restBaseDomain
79                );
80
81                $internalRestbaseURL = "$restBaseUrl/$restBaseDomain/";
82            } else {
83                // Default to using the external URL for internal calls as well.
84                $internalRestbaseURL = $fullRestbaseUrl;
85            }
86
87            $settings->overrideConfigValue( 'MathInternalRestbaseURL', $internalRestbaseURL );
88        }
89    }
90
91    public static function onExtensionFunctions() {
92        if ( ExtensionRegistry::getInstance()->isLoaded( 'WikibaseClient' ) ) {
93            global $wgRestAPIAdditionalRouteFiles;
94            $wgRestAPIAdditionalRouteFiles[] = dirname( __DIR__ ) . '/popupRestRoutes.json';
95        }
96    }
97
98    /**
99     * MaintenanceRefreshLinksInit handler; optimize settings for refreshLinks batch job.
100     *
101     * @param Maintenance $maint
102     */
103    public function onMaintenanceRefreshLinksInit( $maint ) {
104        $user = RequestContext::getMain()->getUser();
105
106        // Don't parse LaTeX to improve performance
107        $this->userOptionsManager->setOption( $user, 'math', MathConfig::MODE_SOURCE );
108    }
109
110    /**
111     * Remove Special:MathWikibase if the Wikibase client extension isn't loaded
112     *
113     * @param array &$list
114     */
115    public function onSpecialPage_initList( &$list ) {
116        if ( !ExtensionRegistry::getInstance()->isLoaded( 'WikibaseClient' ) ) {
117            unset( $list['MathWikibase'] );
118        }
119    }
120
121}