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