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