Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
14 / 15
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
CampaignContentHooks
93.33% covered (success)
93.33%
14 / 15
80.00% covered (warning)
80.00%
4 / 5
9.02
0.00% covered (danger)
0.00%
0 / 1
 newFromGlobalState
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 onContentModelCanBeUsedOn
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 onEditPage__showEditForm_initial
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 onCodeEditorGetPageLanguage
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2// phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName
3
4namespace MediaWiki\Extension\MediaUploader\Hooks;
5
6use EditPage;
7use ExtensionRegistry;
8use MediaWiki\Content\Hook\ContentModelCanBeUsedOnHook;
9use MediaWiki\Hook\EditPage__showEditForm_initialHook;
10use OutputPage;
11use Title;
12
13/**
14 * Hooks related directly to the campaign content model.
15 */
16class CampaignContentHooks implements
17    ContentModelCanBeUsedOnHook,
18    EditPage__showEditForm_initialHook
19{
20    /** @var ExtensionRegistry */
21    private $extensionRegistry;
22
23    /**
24     * Factory method to inject ExtensionRegistry.
25     *
26     * @return static
27     */
28    public static function newFromGlobalState(): self {
29        return new static( ExtensionRegistry::getInstance() );
30    }
31
32    /**
33     * @param ExtensionRegistry $extensionRegistry
34     */
35    public function __construct( ExtensionRegistry $extensionRegistry ) {
36        $this->extensionRegistry = $extensionRegistry;
37    }
38
39    /**
40     * 'Campaign' content model must be used in, and only in, the 'Campaign' namespace.
41     *
42     * @param string $contentModel
43     * @param Title $title
44     * @param bool &$ok
45     * @return bool
46     */
47    public function onContentModelCanBeUsedOn( $contentModel, $title, &$ok ): bool {
48        $isCampaignModel = $contentModel === CONTENT_MODEL_CAMPAIGN;
49        $isCampaignNamespace = $title->inNamespace( NS_CAMPAIGN );
50        if ( $isCampaignModel !== $isCampaignNamespace ) {
51            $ok = false;
52            return false;
53        }
54
55        return true;
56    }
57
58    /**
59     * Load an additional module when editing campaigns with CodeEditor.
60     *
61     * @param EditPage $editor
62     * @param OutputPage $out
63     *
64     * @return bool
65     */
66    public function onEditPage__showEditForm_initial( $editor, $out ) {
67        if ( $this->extensionRegistry->isLoaded( 'CodeEditor' ) &&
68            $editor->getContextTitle()->getNamespace() === NS_CAMPAIGN
69        ) {
70            $out->addModules( 'ext.mediaUploader.campaignEditor' );
71        }
72
73        return true;
74    }
75
76    /**
77     * Declares YAML as the code editor language for Campaign: pages.
78     * This hook only runs if the CodeEditor extension is enabled.
79     *
80     * @param Title $title
81     * @param string|null &$lang Page language.
82     *
83     * @return bool
84     */
85    public static function onCodeEditorGetPageLanguage( Title $title, ?string &$lang ): bool {
86        if ( $title->inNamespace( NS_CAMPAIGN ) ) {
87            $lang = 'yaml';
88        }
89        return true;
90    }
91}