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