Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialCreateObject
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 7
156
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 / 10
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 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 displayNotAvailableError
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * WikiLambda Special:CreateObject 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\Exception\ErrorPageError;
14use MediaWiki\Extension\WikiLambda\Registry\ZTypeRegistry;
15use MediaWiki\Extension\WikiLambda\ZObjectEditingPageTrait;
16use MediaWiki\SpecialPage\SpecialPage;
17use MediaWiki\User\User;
18
19class SpecialCreateObject extends SpecialPage {
20    use ZObjectEditingPageTrait;
21
22    public function __construct() {
23        parent::__construct( 'CreateObject', 'wikilambda-create' );
24    }
25
26    /**
27     * @inheritDoc
28     */
29    protected function getGroupName() {
30        // Triggers use of message specialpages-group-wikilambda
31        return 'wikilambda';
32    }
33
34    /**
35     * @inheritDoc
36     */
37    public function getDescription() {
38        // we do not know which object type will be created, so we need to be generic here
39        $request = $this->getRequest();
40        $zid = $request->getText( 'zid' );
41
42        $description = match ( $zid ) {
43            ZTypeRegistry::Z_TYPE => $this->msg( 'wikilambda-special-create-type' ),
44            ZTypeRegistry::Z_FUNCTION => $this->msg( 'wikilambda-special-create-function' ),
45            ZTypeRegistry::Z_IMPLEMENTATION => $this->msg( 'wikilambda-special-create-implementation' ),
46            ZTypeRegistry::Z_TESTER => $this->msg( 'wikilambda-special-create-test' ),
47            default => $this->msg( 'wikilambda-special-createobject' ),
48        };
49
50        return $description;
51    }
52
53    /**
54     * @inheritDoc
55     */
56    public function isListed() {
57        // No usage allowed on client-mode wikis.
58        return $this->getConfig()->get( 'WikiLambdaEnableRepoMode' );
59    }
60
61    /**
62     * @inheritDoc
63     */
64    public function userCanExecute( User $user ) {
65        $block = $user->getBlock();
66
67        return (
68            // Does the user have the relevant right (wikilambda-create, as set above)?
69            parent::userCanExecute( $user ) &&
70            // If they're blocked in some way, does it block page creations or is site-wide?
71            ( !$block || !$block->appliesToRight( 'createpage' ) || !$block->isSitewide() )
72        );
73    }
74
75    /**
76     * Output an error message telling the user that the Repo Mode is not enabled
77     *
78     * @throws ErrorPageError
79     */
80    private function displayNotAvailableError(): never {
81        $titleMessage = $this->msg( 'wikilambda-special-create-zobject-not-enabled-title' );
82        $errorMessage = $this->msg( 'wikilambda-special-create-zobject-not-enabled' );
83        throw new ErrorPageError( $titleMessage, $errorMessage );
84    }
85
86    /**
87     * @inheritDoc
88     */
89    public function execute( $subPage ) {
90        // Throw ErrorPageError if Abtract Mode is not enabled
91        if ( !$this->getConfig()->get( 'WikiLambdaEnableRepoMode' ) ) {
92            $this->displayNotAvailableError();
93        }
94
95        // Throw PermissionsError if user doesn't have the necessary rights
96        if ( !$this->userCanExecute( $this->getUser() ) ) {
97            $this->displayRestrictionError();
98        }
99
100        // NOTE: We ignore $subPage server-side, and extract and pre-fill type/etc. in Vue
101
102        $this->setHeaders();
103
104        $output = $this->getOutput();
105
106        $output->addModules( [ 'ext.wikilambda.app', 'mediawiki.special' ] );
107        $output->addModuleStyles( [ 'ext.wikilambda.special.styles' ] );
108
109        $output->addWikiMsg(
110            'wikilambda-special-createobject-intro',
111            'Special:MyLanguage/Wikifunctions:List_of_policies_and_guidelines'
112        );
113
114        $this->addHelpLink( 'Extension:WikiLambda/Creating Objects' );
115
116        $this->generateZObjectPayload( $this->getContext(), $output, [
117            'createNewPage' => true,
118        ] );
119    }
120}