Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialRunFunction
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 5
72
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
 userCanExecute
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 execute
0.00% covered (danger)
0.00%
0 / 27
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\Html\Html;
15use MediaWiki\SpecialPage\SpecialPage;
16use MediaWiki\User\User;
17
18class SpecialRunFunction extends SpecialPage {
19
20    public function __construct() {
21        parent::__construct( 'RunFunction', 'wikilambda-execute' );
22    }
23
24    /**
25     * @inheritDoc
26     */
27    protected function getGroupName() {
28        // Triggers use of message specialpages-group-wikilambda
29        return 'wikilambda';
30    }
31
32    /**
33     * @inheritDoc
34     */
35    public function getDescription() {
36        return $this->msg( 'wikilambda-special-runfunction' );
37    }
38
39    /**
40     * @inheritDoc
41     *
42     * @param User $user
43     * @return bool
44     */
45    public function userCanExecute( User $user ) {
46        $block = $user->getBlock();
47
48        return (
49            // Does the user have the relevant right (wikilambda-execute, as set above)?
50            parent::userCanExecute( $user ) &&
51            // If they're blocked in some way, is it site-wide?
52            ( !$block || !$block->isSitewide() )
53        );
54    }
55
56    /**
57     * @inheritDoc
58     */
59    public function execute( $subPage ) {
60        if ( !$this->userCanExecute( $this->getUser() ) ) {
61            $this->displayRestrictionError();
62        }
63
64        // TODO (T359573): Use $subPage to extract and pre-fill target Z8?
65
66        $this->setHeaders();
67        $this->outputHeader( 'wikilambda-special-runfunction-summary' );
68
69        $output = $this->getOutput();
70        $output->addModules( [ 'ext.wikilambda.edit', 'mediawiki.special' ] );
71
72        $output->addWikiMsg( 'wikilambda-special-runfunction-intro' );
73
74        $this->addHelpLink( 'Help:Wikifunctions/Run function' );
75
76        // TODO (T362240): Can we re-use parts of ZObjectEditingPageTrait rather than re-use?
77        $userLang = $this->getLanguage();
78
79        // Fallback no-JS notice.
80        $output->addHtml( Html::element(
81            'div',
82            [ 'class' => [ 'client-nojs', 'ext-wikilambda-editor-nojswarning' ] ],
83            $this->msg( 'wikilambda-special-runfunction-nojs' )->inLanguage( $userLang )->text()
84        ) );
85
86        $userLangCode = $userLang->getCode();
87
88        $zLangRegistry = ZLangRegistry::singleton();
89        // If the userLang isn't recognised (e.g. it's qqx, or a language we don't support yet, or it's
90        // nonsense), then fall back to English.
91        $userLangZid = $zLangRegistry->getLanguageZidFromCode( $userLangCode, true );
92        // Normalise our used language code from what the Language object says
93        $userLangCode = $zLangRegistry->getLanguageCodeFromZid( $userLangZid );
94
95        $editingData = [
96            'zlang' => $userLangCode,
97            'zlangZid' => $userLangZid,
98            'createNewPage' => false,
99            'runFunction' => true,
100            'viewmode' => false
101        ];
102
103        $output->addJsConfigVars( 'wgWikiLambda', $editingData );
104
105        // Vue app element
106        $output->addHtml( Html::element( 'div', [ 'id' => 'ext-wikilambda-app' ] ) );
107    }
108}