Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
68.18% covered (warning)
68.18%
30 / 44
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialCreateTopicPage
68.18% covered (warning)
68.18%
30 / 44
60.00% covered (warning)
60.00%
3 / 5
16.64
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 execute
65.22% covered (warning)
65.22%
15 / 23
0.00% covered (danger)
0.00%
0 / 1
9.06
 showAlreadyExistsMessage
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 showNoOrInvalidTitleGivenMessage
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 showTitleInputWithMessage
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace ArticlePlaceholder\Specials;
4
5use MediaWiki\HTMLForm\HTMLForm;
6use MediaWiki\Message\Message;
7use MediaWiki\Permissions\PermissionManager;
8use MediaWiki\SpecialPage\UnlistedSpecialPage;
9use MediaWiki\Title\Title;
10use PermissionsError;
11use Wikimedia\Stats\StatsFactory;
12
13/**
14 * The CreateTopicPage SpecialPage for the ArticlePlaceholder extension
15 *
16 * @ingroup Extensions
17 * @license GPL-2.0-or-later
18 * @author Florian Schmidt
19 */
20class SpecialCreateTopicPage extends UnlistedSpecialPage {
21
22    private PermissionManager $permissionManager;
23    private StatsFactory $statsFactory;
24
25    public function __construct(
26        PermissionManager $permissionManager,
27        StatsFactory $statsFactory
28    ) {
29        parent::__construct( 'CreateTopicPage' );
30        $this->permissionManager = $permissionManager;
31        $this->statsFactory = $statsFactory;
32    }
33
34    /**
35     * @param string|null $par
36     */
37    public function execute( $par ) {
38        $out = $this->getOutput();
39        $this->setHeaders();
40        if ( $this->getRequest()->getRawVal( 'ref' ) === 'button' ) {
41            $this->statsFactory->getCounter( 'ArticlePlaceholder_button_createArticle_total' )
42                ->copyToStatsdAt( 'wikibase.articleplaceholder.button.createArticle' )
43                ->increment();
44        }
45        $page = $this->getRequest()->getVal( 'wptitleinput', $par );
46        if ( $page === '' || $page === null ) {
47            $this->showNoOrInvalidTitleGivenMessage();
48            return;
49        }
50        $title = Title::newFromText( $page );
51        if ( $title === null ) {
52            $this->showNoOrInvalidTitleGivenMessage( 'invalid' );
53            return;
54        }
55        $out->setPageTitleMsg( $this->msg( 'articleplaceholder-createpage-title', $title->getText() ) );
56        if ( $title->exists() ) {
57            $this->showAlreadyExistsMessage( $title );
58            return;
59        }
60
61        $permissionErrors = $this->permissionManager
62            ->getPermissionErrors( 'edit', $this->getUser(), $title );
63        if ( $permissionErrors ) {
64            throw new PermissionsError( 'edit', $permissionErrors );
65        }
66
67        $out->redirect( $title->getLocalURL( [ 'action' => 'edit' ] ) );
68    }
69
70    /**
71     * Displays a form that gives the user the information, that the page (with
72     * the currently tried title) already exists and that he should
73     * choose another title.
74     *
75     * @param Title $title
76     */
77    private function showAlreadyExistsMessage( Title $title ) {
78        $this->showTitleInputWithMessage(
79            $this->msg( 'articleplaceholder-createpage-alreadyexists', $title->getText() )
80        );
81    }
82
83    /**
84     * Displays a form that gives the user the information, that the page title, he wants to
85     * create, is invalid or none is given and that he should provide one.
86     *
87     * @param string $invalid
88     */
89    private function showNoOrInvalidTitleGivenMessage( $invalid = 'missing' ) {
90        $this->showTitleInputWithMessage(
91            $this->msg( $invalid === 'invalid' ?
92                'articleplaceholder-createpage-invalidtitleprovided' :
93                'articleplaceholder-createpage-notitleprovided'
94            )
95        );
96    }
97
98    /**
99     * Displays a form that with the information that he should
100     * choose another title. The given message key is used as a
101     * reason why he need to do this.
102     *
103     * @param Message $msg
104     */
105    private function showTitleInputWithMessage( Message $msg ) {
106        HTMLForm::factory(
107            'ooui',
108            [ 'titleinput' => [ 'type' => 'text' ] ],
109            $this->getContext()
110        )->setMethod( 'get' )
111            ->setWrapperLegendMsg( $msg )
112            ->setSubmitTextMsg( 'create' )
113            ->prepareForm()
114            ->displayForm( false );
115    }
116
117}