Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 104
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialIncubatorFirstSteps
0.00% covered (danger)
0.00%
0 / 104
0.00% covered (danger)
0.00%
0 / 8
756
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 showHeader
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 addLanguageSelector
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
90
 showSignup
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
20
 showTestwikiSetting
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 showUserpage
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
 showStartWiki
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Makes Special:IncubatorFirstSteps to guide users
4 * through the process of starting a new wiki.
5 *
6 * Based on the concept of FirstSteps of the Translate extension.
7 *
8 * @file
9 * @author Robin Pepermans
10 * @author Niklas Laxström
11 * @license GPL-2.0-or-later
12 * @ingroup SpecialPage
13 */
14
15namespace MediaWiki\Extension\WikimediaIncubator;
16
17use MediaWiki\Html\Html;
18use MediaWiki\Languages\LanguageNameUtils;
19use MediaWiki\Message\Message;
20use MediaWiki\SpecialPage\SpecialPage;
21use MediaWiki\SpecialPage\UnlistedSpecialPage;
22use MediaWiki\Title\Title;
23
24class SpecialIncubatorFirstSteps extends UnlistedSpecialPage {
25
26    /**
27     * @var array
28     */
29    protected $wikiprefix;
30
31    /** @var LanguageNameUtils */
32    private $languageNameUtils;
33
34    /**
35     * @param LanguageNameUtils $languageNameUtils
36     */
37    public function __construct(
38        LanguageNameUtils $languageNameUtils
39    ) {
40        parent::__construct( 'IncubatorFirstSteps' );
41        $this->languageNameUtils = $languageNameUtils;
42    }
43
44    /**
45     * @param string|null $params
46     */
47    public function execute( $params ) {
48        $this->wikiprefix = WikimediaIncubator::analyzePrefix( WikimediaIncubator::displayPrefix() );
49
50        $this->getOutput()->addWikiMsg( 'wminc-fs-intro' );
51        $step = false;
52
53        $this->addLanguageSelector();
54
55        $step = $this->showSignup( $step );
56        $step = $this->showTestwikiSetting( $step );
57        $step = $this->showUserpage( $step );
58        $step = $this->showStartWiki( $step );
59
60        $title = $this->msg( 'wminc-fs-pagetitle', $step->text() )->escaped();
61        $this->getOutput()->setPageTitle( $title );
62    }
63
64    /**
65     * @param Message $msg Message object
66     * @param bool $opaque
67     * @param bool $done
68     */
69    protected function showHeader( $msg, $opaque = true, $done = false ) {
70        $attrs = [];
71        if ( $opaque ) {
72            $attrs['class'] = 'mw-special-incubatorfirststeps-header';
73        }
74        $content = $msg->plain();
75        if ( $done ) {
76            $content .= $this->msg( 'wminc-fs-pagetitle-done' )->text();
77        }
78        $this->getOutput()->addHtml( Html::element( 'h3', $attrs, $content ) );
79    }
80
81    /**
82     * Adds an inline list of languages that change the language with uselang=xx
83     */
84    protected function addLanguageSelector() {
85        $getLangCodes = $this->msg( 'wminc-fs-langselect-langs' )->inContentLanguage();
86        $currentLangCode = $this->getLanguage()->getCode();
87
88        if ( $this->getUser()->isNamed() || $getLangCodes->isBlank() ) {
89            return;
90        }
91
92        # Make a list of selectable languages, based on language codes
93        # in a MediaWiki message and on the browser language(s)
94        $getLangCodes = array_flip( explode( ',', $getLangCodes->text() ) );
95        $names = $this->languageNameUtils->getLanguageNames();
96        $names_keys = array_keys( $names );
97        $browserLanguages = array_keys( $this->getRequest()->getAcceptLang() );
98
99        foreach ( $browserLanguages as $browserLanguage ) {
100            if ( in_array( $browserLanguage, $names_keys ) ) {
101                // add the language to the list if it is supported in MediaWiki
102                $getLangCodes[$browserLanguage] = true;
103            }
104        }
105
106        # add the language of the "testwiki" URL param if set
107        $urlTestWiki = WikimediaIncubator::getUrlParam();
108        if ( $urlTestWiki ) {
109            $getLangCodes[$urlTestWiki['lang']] = true;
110        }
111
112        ksort( $getLangCodes ); // sorting by language code is not ideal, but well
113
114        $showLanguages = [];
115        $linkRenderer = $this->getLinkRenderer();
116        foreach ( $getLangCodes as $code => $nothing ) {
117            $code = trim( $code );
118            if ( !isset( $names[$code] ) || $code === $currentLangCode ) {
119                # language code not recognised, or is current interface language
120                continue;
121            }
122            $linkParams = [ 'uselang' => $code,
123                'testwiki' => $this->getRequest()->getVal( 'testwiki' ) ];
124            $showLanguages[] = $linkRenderer->makeKnownLink( $this->getPageTitle(),
125                $names[$code], [], $linkParams );
126        }
127
128        # Show list
129        $this->getOutput()->addHtml( '<div id="mw-special-incubatorfirststeps-langselect">' .
130            $this->msg( 'wminc-fs-langselect', $names[$currentLangCode] )->rawParams(
131            $this->getLanguage()->pipeList( $showLanguages ) )->escaped() .
132        '</div>' );
133    }
134
135    /**
136     * @param Message|false $step
137     * @return Message|false
138     */
139    protected function showSignup( $step ) {
140        $step_msg = $this->msg( 'wminc-fs-signup-title' );
141        if ( $step ) {
142            $this->showHeader( $step_msg, true, false );
143            return $step;
144        } elseif ( $this->getUser()->isNamed() ) {
145            $this->showHeader( $step_msg, true, true );
146            return $step;
147        }
148        $this->showHeader( $step_msg, false, false );
149
150        # Login / create account links
151        $link = SpecialPage::getTitleFor( 'Userlogin' );
152        $query = [ 'returnto' => $this->getPageTitle(),
153            'uselang' => $this->getRequest()->getVal( 'uselang' ) ];
154        $urlTestWiki = WikimediaIncubator::getUrlParam();
155        if ( $urlTestWiki ) {
156            // set preferences automatically, based on the "testwiki" URL param
157            $query['testwikiproject'] = $urlTestWiki['project'];
158            $query['testwikicode'] = $urlTestWiki['lang'];
159        }
160        $login = $link->getFullUrl( $query );
161        $signup = $link->getFullUrl( $query + [ 'type' => 'signup' ] );
162
163        $this->getOutput()->addWikiMsg( 'wminc-fs-signup-text', $login, $signup );
164
165        return $step_msg;
166    }
167
168    /**
169     * @param Message|false $step
170     * @return Message|false
171     */
172    protected function showTestwikiSetting( $step ) {
173        $step_msg = $this->msg( 'wminc-fs-settings-title' );
174        if ( $step ) {
175            $this->showHeader( $step_msg, true, false );
176            return $step;
177        } elseif ( !$this->wikiprefix['error'] ) {
178            $this->showHeader( $step_msg, true, true );
179            return $step;
180        }
181        $this->showHeader( $step_msg, false, false );
182
183        $this->getOutput()->addWikiMsg( 'wminc-fs-settings-image' );
184        $this->getOutput()->addWikiMsg( 'wminc-fs-settings-text' );
185
186        return $step_msg;
187    }
188
189    /**
190     * @param Message|false $step
191     * @return Message|false
192     */
193    protected function showUserpage( $step ) {
194        $step_msg = $this->msg( 'wminc-fs-userpage-title' );
195
196        if ( $step ) {
197            $this->showHeader( $step_msg, true, false );
198            return $step;
199        } elseif ( $this->getUser()->getUserPage()->isKnown() ) {
200            $this->showHeader( $step_msg, true, true );
201            return $step;
202        }
203        $this->showHeader( $step_msg, false, false );
204
205        $linkRenderer = $this->getLinkRenderer();
206        $link = $linkRenderer->makeLink( $this->getUser()->getUserPage(), $this->getUser()->getName() );
207        $this->getOutput()->addHtml( $this->msg( 'wminc-fs-userpage-text' )->rawParams( $link )->escaped() );
208
209        return $step_msg;
210    }
211
212    /**
213     * @param Message|false $step
214     * @return Message
215     */
216    protected function showStartWiki( $step ) {
217        $step_msg = $this->msg( 'wminc-fs-startwiki-title' );
218
219        if ( $step ) {
220            $this->showHeader( $step_msg, true, false );
221            return $step;
222        }
223        $this->showHeader( $step_msg, false, false );
224
225        $mainpage = WikimediaIncubator::getMainPage(
226            $this->wikiprefix['lang'], $this->wikiprefix['prefix'] );
227
228        $linkRenderer = $this->getLinkRenderer();
229        $prefix = $linkRenderer->makeKnownLink( Title::newFromText( $this->wikiprefix['prefix'] ) );
230        $link = $linkRenderer->makeLink( $mainpage, $mainpage->getText() );
231        $this->getOutput()->addHtml( $this->msg( $mainpage->exists() ?
232            'wminc-fs-startwiki-exists-text' : 'wminc-fs-startwiki-text' )
233            ->rawParams( $prefix, $link )->parse() );
234
235        return $step_msg;
236    }
237}