Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CodeEditorHooks
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 2
20
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
 onCodeEditorGetPageLanguage
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Extension\Gadgets;
4
5use MediaWiki\Config\Config;
6use MediaWiki\Extension\CodeEditor\Hooks\CodeEditorGetPageLanguageHook;
7use MediaWiki\Title\Title;
8
9/**
10 * Hooks for optional integration with the CodeEditor extension.
11 */
12class CodeEditorHooks implements CodeEditorGetPageLanguageHook {
13
14    private readonly bool $useCodeEditor;
15
16    public function __construct(
17        Config $config
18    ) {
19        $this->useCodeEditor = $config->get( 'GadgetsDefinitionsUseCodeEditor' );
20    }
21
22    /**
23     * Set the CodeEditor language for GadgetDefinition pages.
24     *
25     * The CodeEditor extension sets the default syntax highlight language based
26     * on the content model (not page title), so while gadget definitions have ".json"
27     * page titles, the fact that we use a more specific subclass as content model,
28     * means we must explicitly opt-in to JSON syntax highlighting.
29     *
30     * @param Title $title
31     * @param string|null &$lang
32     * @param string $model
33     * @param string $format
34     * @return bool
35     */
36    public function onCodeEditorGetPageLanguage( Title $title, ?string &$lang, string $model, string $format ) {
37        if ( $title->hasContentModel( 'GadgetDefinition' ) && $this->useCodeEditor ) {
38            $lang = 'json';
39            return false;
40        }
41
42        return true;
43    }
44
45}