Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialRunFunction
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 6
110
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
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isListed
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 userCanExecute
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 execute
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * WikiLambda Special:RunFunction page
4 *
5 * @file
6 * @ingroup Extensions
7 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
8 * @license MIT
9 */
10
11namespace MediaWiki\Extension\WikiLambda\Special;
12
13use MediaWiki\Extension\WikiLambda\Registry\ZLangRegistry;
14use MediaWiki\Extension\WikiLambda\UIUtils;
15use MediaWiki\Html\Html;
16use MediaWiki\SpecialPage\SpecialPage;
17use MediaWiki\User\User;
18
19class SpecialRunFunction extends SpecialPage {
20
21    public function __construct() {
22        parent::__construct( 'RunFunction', 'wikilambda-execute' );
23    }
24
25    /**
26     * @inheritDoc
27     */
28    protected function getGroupName() {
29        // Triggers use of message specialpages-group-wikilambda
30        return 'wikilambda';
31    }
32
33    /**
34     * @inheritDoc
35     */
36    public function getDescription() {
37        return $this->msg( 'wikilambda-special-runfunction' );
38    }
39
40    /** @inheritDoc */
41    public function isListed() {
42        // No usage allowed on client-mode wikis.
43        return $this->getConfig()->get( 'WikiLambdaEnableRepoMode' );
44    }
45
46    /**
47     * @inheritDoc
48     *
49     * @param User $user
50     * @return bool
51     */
52    public function userCanExecute( User $user ) {
53        if ( !$this->getConfig()->get( 'WikiLambdaEnableRepoMode' ) ) {
54            // No usage allowed on client-mode wikis.
55            return false;
56        }
57
58        $block = $user->getBlock();
59
60        return (
61            // Does the user have the relevant right (wikilambda-execute, as set above)?
62            parent::userCanExecute( $user ) &&
63            // If they're blocked in some way, is it site-wide?
64            ( !$block || !$block->isSitewide() )
65        );
66    }
67
68    /**
69     * @inheritDoc
70     */
71    public function execute( $subPage ) {
72        if ( !$this->userCanExecute( $this->getUser() ) ) {
73            $this->displayRestrictionError();
74        }
75
76        // TODO (T359573): Use $subPage to extract and pre-fill target Z8?
77
78        $this->setHeaders();
79        $this->outputHeader( 'wikilambda-special-runfunction-summary' );
80
81        $output = $this->getOutput();
82        $output->addModules( [ 'ext.wikilambda.app', 'mediawiki.special' ] );
83        $output->addModuleStyles( [ 'ext.wikilambda.special.styles' ] );
84
85        $output->addWikiMsg(
86            'wikilambda-special-runfunction-intro',
87            'Special:MyLanguage/Wikifunctions:List_of_policies_and_guidelines'
88        );
89
90        $this->addHelpLink( 'Help:Wikifunctions/Run function' );
91
92        // TODO (T362240): Can we re-use parts of ZObjectEditingPageTrait rather than re-use?
93        $userLang = $this->getLanguage();
94
95        // Fallback no-JS notice.
96        $output->addHtml( Html::rawElement(
97            'noscript',
98            [],
99            $this->msg( 'wikilambda-nojs' )->inLanguage( $userLang )->parse()
100        ) );
101        // Vue app element with Codex progress indicator
102        $loadingMessage = $this->msg( 'wikilambda-loading' )->inLanguage( $userLang )->text();
103        $output->addHtml( Html::rawElement(
104            'div',
105            [ 'id' => 'ext-wikilambda-app' ],
106            UIUtils::createCodexProgressIndicator( $loadingMessage )
107        ) );
108
109        $userLangCode = $userLang->getCode();
110
111        $zLangRegistry = ZLangRegistry::singleton();
112        // If the userLang isn't recognised (e.g. it's qqx, or a language we don't support yet, or it's
113        // nonsense), then fall back to English.
114        $userLangZid = $zLangRegistry->getLanguageZidFromCode( $userLangCode, true );
115        // Normalise our used language code from what the Language object says
116        $userLangCode = $zLangRegistry->getLanguageCodeFromZid( $userLangZid );
117
118        $editingData = [
119            'zlang' => $userLangCode,
120            'zlangZid' => $userLangZid,
121            'createNewPage' => false,
122            'runFunction' => true,
123            'viewmode' => false
124        ];
125
126        $output->addJsConfigVars( 'wgWikiLambda', $editingData );
127    }
128}