Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
MobileFrontendEditorHooks
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 4
342
0.00% covered (danger)
0.00%
0 / 1
 getContentLanguageMessages
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 onMakeGlobalVariablesScript
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 onCustomEditor
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
42
 isSupportedEditRequest
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2
3use MediaWiki\Actions\Hook\CustomEditorHook;
4use MediaWiki\Config\Config;
5use MediaWiki\Context\IContextSource;
6use MediaWiki\MediaWikiServices;
7use MediaWiki\Output\Hook\MakeGlobalVariablesScriptHook;
8use MediaWiki\Output\OutputPage;
9use MediaWiki\Page\Article;
10use MediaWiki\Registration\ExtensionRegistry;
11use MediaWiki\ResourceLoader\Context;
12use MediaWiki\User\User;
13
14class MobileFrontendEditorHooks implements
15    CustomEditorHook,
16    MakeGlobalVariablesScriptHook
17{
18
19    /**
20     * Return messages in content language, for use in a ResourceLoader module.
21     *
22     * @param Context $context
23     * @param Config $config
24     * @param array $messagesKeys
25     * @return array
26     */
27    public static function getContentLanguageMessages(
28        Context $context, Config $config, array $messagesKeys = []
29    ): array {
30        return array_combine(
31            $messagesKeys,
32            array_map( static function ( $key ) {
33                return wfMessage( $key )->inContentLanguage()->text();
34            }, $messagesKeys )
35        );
36    }
37
38    /**
39     * Handler for MakeGlobalVariablesScript hook.
40     * For values that depend on the current page, user or request state.
41     *
42     * @see https://www.mediawiki.org/wiki/Manual:Hooks/MakeGlobalVariablesScript
43     * @param array &$vars Variables to be added into the output
44     * @param OutputPage $out OutputPage instance calling the hook
45     */
46    public function onMakeGlobalVariablesScript( &$vars, $out ): void {
47        /** @var MobileContext $mobileContext */
48        $mobileContext = MediaWikiServices::getInstance()->getService( 'MobileFrontend.Context' );
49
50        if ( $mobileContext->shouldDisplayMobileView() ) {
51            // mobile.init
52            $vars['wgMFIsSupportedEditRequest'] = self::isSupportedEditRequest( $out->getContext() );
53        }
54    }
55
56    /**
57     * Decide whether to bother showing the wikitext editor at all.
58     * If not, we expect the editor initialisation JS to activate.
59     *
60     * @param Article $article The article being viewed.
61     * @param User $user The user-specific settings.
62     * @return bool Whether to show the wikitext editor or not.
63     */
64    public function onCustomEditor( $article, $user ) {
65        $req = $article->getContext()->getRequest();
66        $title = $article->getTitle();
67        if (
68            !$req->getVal( 'mfnoscript' ) &&
69            self::isSupportedEditRequest( $article->getContext() )
70        ) {
71            $params = $req->getValues();
72            $params['mfnoscript'] = '1';
73            $url = wfScript() . '?' . wfArrayToCgi( $params );
74
75            $out = $article->getContext()->getOutput();
76            $titleMsg = $title->exists() ? 'editing' : 'creating';
77            $out->setPageTitleMsg( wfMessage( $titleMsg, $title->getPrefixedText() ) );
78
79            $msgParams = [];
80            if ( $title->inNamespace( NS_FILE ) && !$title->exists() ) {
81                // Is a new file page (enable upload image only) T60311
82                $msg = 'mobile-frontend-editor-uploadenable';
83            } else {
84                $msg = 'mobile-frontend-editor-toload';
85                $urlUtils = MediaWikiServices::getInstance()->getUrlUtils();
86                $msgParams[] = $urlUtils->expand( $url, PROTO_CURRENT );
87            }
88            // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Only null for invalid URL, shouldn't happen
89            $out->showPendingTakeover( $url, $msg, ...$msgParams );
90
91            $out->setRevisionId( $req->getInt( 'oldid', $article->getRevIdFetched() ) );
92            return false;
93        }
94        return true;
95    }
96
97    /**
98     * Whether the custom editor override should occur
99     *
100     * @param IContextSource $context
101     * @return bool Whether the frontend JS should try to display an editor
102     */
103    protected static function isSupportedEditRequest( IContextSource $context ) {
104        /** @var MobileContext $mobileContext */
105        $mobileContext = MediaWikiServices::getInstance()->getService( 'MobileFrontend.Context' );
106        if ( !$mobileContext->shouldDisplayMobileView() ) {
107            return false;
108        }
109
110        $extensionRegistry = ExtensionRegistry::getInstance();
111        $editorAvailableSkins = $extensionRegistry->getAttribute( 'MobileFrontendEditorAvailableSkins' );
112        if ( !in_array( $context->getSkin()->getSkinName(), $editorAvailableSkins ) ) {
113            // Mobile editor commonly doesn't work well with other skins than Minerva (it looks horribly
114            // broken without some styles that are only defined by Minerva). So we only enable it for the
115            // skin that wants it.
116            return false;
117        }
118
119        $req = $context->getRequest();
120        $title = $context->getTitle();
121
122        // Various things fall back to WikiEditor
123        if ( $req->getRawVal( 'action' ) === 'submit' ) {
124            // Don't try to take over if the form has already been submitted
125            return false;
126        }
127        if ( $title->inNamespace( NS_SPECIAL ) ) {
128            return false;
129        }
130        if ( !$title->hasContentModel( CONTENT_MODEL_WIKITEXT ) ) {
131            // Only load the wikitext editor on wikitext. Otherwise we'll rely on the fallback behaviour
132            // (You can test this on MediaWiki:Common.css) ?action=edit url (T173800)
133            return false;
134        }
135        if ( $req->getCheck( 'undo' ) || $req->getCheck( 'undoafter' ) ) {
136            // Undo needs to show a diff above the editor
137            return false;
138        }
139        if ( $req->getRawVal( 'section' ) === 'new' ) {
140            // New sections need a title field
141            return false;
142        }
143        return true;
144    }
145
146}