Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 7
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 / 7
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 onCodeEditorGetPageLanguage
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace JsonConfig;
4
5use MediaWiki\Config\Config;
6use MediaWiki\Content\IContentHandlerFactory;
7use MediaWiki\Extension\CodeEditor\Hooks\CodeEditorGetPageLanguageHook;
8use MediaWiki\Title\Title;
9
10/**
11 * Hook handlers for JsonConfig extension.
12 * All hooks from the CodeEditor extension which is optional to use with this extension.
13 *
14 * @file
15 * @ingroup Extensions
16 * @ingroup JsonConfig
17 * @license GPL-2.0-or-later
18 */
19class CodeEditorHooks implements
20    CodeEditorGetPageLanguageHook
21{
22    private Config $config;
23    private IContentHandlerFactory $contentHandlerFactory;
24
25    public function __construct(
26        Config $config,
27        IContentHandlerFactory $contentHandlerFactory
28    ) {
29        $this->config = $config;
30        $this->contentHandlerFactory = $contentHandlerFactory;
31    }
32
33    /**
34     * Declares JSON as the code editor language for Config: pages.
35     * This hook only runs if the CodeEditor extension is enabled.
36     * @param Title $title The title the language is for
37     * @param string|null &$lang The language to use
38     * @param string $model The content model of the title
39     * @param string $format The content format of the title
40     * @return bool|void True or no return value to continue or false to abort
41     */
42    public function onCodeEditorGetPageLanguage( Title $title, ?string &$lang, string $model, string $format ) {
43        if ( !JCHooks::jsonConfigIsStorage( $this->config ) ) {
44            return;
45        }
46
47        // todo/fixme? We should probably add 'json' lang to only those pages that pass parseTitle()
48        $handler = $this->contentHandlerFactory->getContentHandler( $title->getContentModel() );
49        if ( $handler->getDefaultFormat() === CONTENT_FORMAT_JSON || JCSingleton::parseTitle( $title ) ) {
50            $lang = 'json';
51        }
52    }
53}