Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
17.65% covered (danger)
17.65%
9 / 51
57.14% covered (warning)
57.14%
4 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
CiteHooks
17.65% covered (danger)
17.65%
9 / 51
57.14% covered (warning)
57.14%
4 / 7
123.47
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 onContentHandlerDefaultModelFor
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 onMakeGlobalVariablesScript
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 onResourceLoaderGetConfigVars
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 onResourceLoaderRegisterModules
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
6
 onAPIQuerySiteInfoGeneralInfo
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 onEditPage__showEditForm_initial
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
4 * @license MIT
5 */
6
7namespace Cite\Hooks;
8
9use ApiQuerySiteinfo;
10use Cite\ReferencePreviews\ReferencePreviewsContext;
11use ExtensionRegistry;
12use MediaWiki\Api\Hook\APIQuerySiteInfoGeneralInfoHook;
13use MediaWiki\Config\Config;
14use MediaWiki\EditPage\EditPage;
15use MediaWiki\Hook\EditPage__showEditForm_initialHook;
16use MediaWiki\Output\Hook\MakeGlobalVariablesScriptHook;
17use MediaWiki\Output\OutputPage;
18use MediaWiki\ResourceLoader\Hook\ResourceLoaderGetConfigVarsHook;
19use MediaWiki\ResourceLoader\Hook\ResourceLoaderRegisterModulesHook;
20use MediaWiki\ResourceLoader\ResourceLoader;
21use MediaWiki\Revision\Hook\ContentHandlerDefaultModelForHook;
22use MediaWiki\Title\Title;
23use MediaWiki\User\Options\UserOptionsLookup;
24
25/**
26 * @license GPL-2.0-or-later
27 * @phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName
28 */
29class CiteHooks implements
30    ContentHandlerDefaultModelForHook,
31    MakeGlobalVariablesScriptHook,
32    ResourceLoaderGetConfigVarsHook,
33    ResourceLoaderRegisterModulesHook,
34    APIQuerySiteInfoGeneralInfoHook,
35    EditPage__showEditForm_initialHook
36{
37
38    private UserOptionsLookup $userOptionsLookup;
39
40    public function __construct( UserOptionsLookup $userOptionsLookup ) {
41        $this->userOptionsLookup = $userOptionsLookup;
42    }
43
44    /**
45     * Convert the content model of a message that is actually JSON to JSON. This
46     * only affects validation and UI when saving and editing, not loading the
47     * content.
48     *
49     * @param Title $title
50     * @param string &$model
51     */
52    public function onContentHandlerDefaultModelFor( $title, &$model ) {
53        if (
54            $title->inNamespace( NS_MEDIAWIKI ) &&
55            (
56                $title->getText() == 'Visualeditor-cite-tool-definition.json' ||
57                $title->getText() == 'Cite-tool-definition.json'
58            )
59        ) {
60            $model = CONTENT_MODEL_JSON;
61        }
62    }
63
64    /**
65     * @param array &$vars
66     * @param OutputPage $out
67     */
68    public function onMakeGlobalVariablesScript( &$vars, $out ): void {
69        $referencePreviewsContext = new ReferencePreviewsContext(
70            $out->getConfig(),
71            $this->userOptionsLookup
72        );
73        $vars['wgCiteReferencePreviews'] = $referencePreviewsContext->isReferencePreviewsEnabled(
74            $out->getUser(),
75            $out->getSkin()
76        );
77    }
78
79    /**
80     * Adds extra variables to the global config
81     * @param array &$vars `[ variable name => value ]`
82     * @param string $skin
83     * @param Config $config
84     */
85    public function onResourceLoaderGetConfigVars( array &$vars, $skin, Config $config ): void {
86        $vars['wgCiteVisualEditorOtherGroup'] = $config->get( 'CiteVisualEditorOtherGroup' );
87        $vars['wgCiteResponsiveReferences'] = $config->get( 'CiteResponsiveReferences' );
88        $vars['wgCiteBookReferencing'] = $config->get( 'CiteBookReferencing' );
89    }
90
91    /**
92     * @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderRegisterModules
93     */
94    public function onResourceLoaderRegisterModules( ResourceLoader $resourceLoader ): void {
95        if ( ExtensionRegistry::getInstance()->isLoaded( 'Popups' ) ) {
96            $dir = dirname( __DIR__, 2 ) . '/modules/';
97            $resourceLoader->register( [
98                'ext.cite.referencePreviews' => [
99                    'localBasePath' => $dir . '/ext.cite.referencePreviews',
100                    'remoteExtPath' => 'Cite/modules/ext.cite.referencePreviews',
101                    'dependencies' => [
102                        'ext.popups.main',
103                    ],
104                    'styles' => [
105                        'referencePreview.less',
106                    ],
107                    'packageFiles' => [
108                        'index.js',
109                        'constants.js',
110                        'createReferenceGateway.js',
111                        'createReferencePreview.js',
112                        'isReferencePreviewsEnabled.js',
113                        'referencePreviewsInstrumentation.js'
114                    ]
115                ]
116            ] );
117        }
118    }
119
120    /**
121     * Hook: APIQuerySiteInfoGeneralInfo
122     *
123     * Expose configs via action=query&meta=siteinfo
124     *
125     * @param ApiQuerySiteinfo $module
126     * @param array &$results
127     */
128    public function onAPIQuerySiteInfoGeneralInfo( $module, &$results ) {
129        $results['citeresponsivereferences'] = $module->getConfig()->get( 'CiteResponsiveReferences' );
130    }
131
132    /**
133     * Hook: EditPage::showEditForm:initial
134     *
135     * Add the module for WikiEditor
136     *
137     * @param EditPage $editPage the current EditPage object.
138     * @param OutputPage $outputPage object.
139     */
140    public function onEditPage__showEditForm_initial( $editPage, $outputPage ) {
141        $extensionRegistry = ExtensionRegistry::getInstance();
142        $allowedContentModels = array_merge(
143            [ CONTENT_MODEL_WIKITEXT ],
144            $extensionRegistry->getAttribute( 'CiteAllowedContentModels' )
145        );
146        if ( !in_array( $editPage->contentModel, $allowedContentModels ) ) {
147            return;
148        }
149
150        $wikiEditorEnabled = $extensionRegistry->isLoaded( 'WikiEditor' );
151
152        $user = $editPage->getContext()->getUser();
153
154        if (
155            $wikiEditorEnabled &&
156            $this->userOptionsLookup->getBoolOption( $user, 'usebetatoolbar' )
157        ) {
158            $outputPage->addModules( 'ext.cite.wikiEditor' );
159        }
160    }
161
162}