MediaWiki  master
WebInstallerName.php
Go to the documentation of this file.
1 <?php
23 
25 
29  public function execute() {
30  $r = $this->parent->request;
31  if ( $r->wasPosted() && $this->submit() ) {
32  return 'continue';
33  }
34 
35  $this->startForm();
36 
37  // Encourage people to not name their site 'MediaWiki' by blanking the
38  // field. I think that was the intent with the original $GLOBALS['wgSitename']
39  // but these two always were the same so had the effect of making the
40  // installer forget $wgSitename when navigating back to this page.
41  if ( $this->getVar( 'wgSitename' ) == 'MediaWiki' ) {
42  $this->setVar( 'wgSitename', '' );
43  }
44 
45  // Set wgMetaNamespace to something valid before we show the form.
46  // $wgMetaNamespace defaults to $wgSiteName which is 'MediaWiki'
47  $metaNS = $this->getVar( 'wgMetaNamespace' );
48  $this->setVar(
49  'wgMetaNamespace',
50  wfMessage( 'config-ns-other-default' )->inContentLanguage()->text()
51  );
52 
53  // Database isn't available in config yet, so take it
54  // from the installer
55  $pingbackConf = new HashConfig( [
56  'DBtype' => $this->getVar( 'wgDBtype' ),
57  ] );
58  $pingbackInfo = Pingback::getSystemInfo( $pingbackConf );
59 
60  $this->addHTML(
61  // @phan-suppress-next-line SecurityCheck-DoubleEscaped taint cannot track the helpbox from the rest
62  $this->parent->getTextBox( [
63  'var' => 'wgSitename',
64  'label' => 'config-site-name',
65  'help' => $this->parent->getHelpBox( 'config-site-name-help' )
66  ] ) .
67  // getRadioSet() builds a set of labeled radio buttons.
68  // For grep: The following messages are used as the item labels:
69  // config-ns-site-name, config-ns-generic, config-ns-other
70  // @phan-suppress-next-line SecurityCheck-DoubleEscaped taint cannot track the helpbox from the rest
71  $this->parent->getRadioSet( [
72  'var' => '_NamespaceType',
73  'label' => 'config-project-namespace',
74  'itemLabelPrefix' => 'config-ns-',
75  'values' => [ 'site-name', 'generic', 'other' ],
76  'commonAttribs' => [ 'class' => 'enableForOther',
77  'rel' => 'config_wgMetaNamespace' ],
78  'help' => $this->parent->getHelpBox( 'config-project-namespace-help' )
79  ] ) .
80  $this->parent->getTextBox( [
81  'var' => 'wgMetaNamespace',
82  'label' => '', // @todo Needs a label?
83  'attribs' => [ 'class' => 'enabledByOther' ]
84  ] ) .
85  $this->getFieldsetStart( 'config-admin-box' ) .
86  // @phan-suppress-next-line SecurityCheck-DoubleEscaped taint cannot track the helpbox from the rest
87  $this->parent->getTextBox( [
88  'var' => '_AdminName',
89  'label' => 'config-admin-name',
90  'help' => $this->parent->getHelpBox( 'config-admin-help' )
91  ] ) .
92  $this->parent->getPasswordBox( [
93  'var' => '_AdminPassword',
94  'label' => 'config-admin-password',
95  ] ) .
96  $this->parent->getPasswordBox( [
97  'var' => '_AdminPasswordConfirm',
98  'label' => 'config-admin-password-confirm'
99  ] ) .
100  // @phan-suppress-next-line SecurityCheck-DoubleEscaped taint cannot track the helpbox from the rest
101  $this->parent->getTextBox( [
102  'var' => '_AdminEmail',
103  'attribs' => [
104  'dir' => 'ltr',
105  ],
106  'label' => 'config-admin-email',
107  'help' => $this->parent->getHelpBox( 'config-admin-email-help' )
108  ] ) .
109  // @phan-suppress-next-line SecurityCheck-DoubleEscaped taint cannot track the helpbox from the rest
110  $this->parent->getCheckBox( [
111  'var' => '_Subscribe',
112  'label' => 'config-subscribe',
113  'help' => $this->parent->getHelpBox( 'config-subscribe-help' )
114  ] ) .
115  // @phan-suppress-next-line SecurityCheck-DoubleEscaped taint cannot track the helpbox from the rest
116  $this->parent->getCheckBox( [
117  'var' => 'wgPingback',
118  'label' => 'config-pingback',
119  'help' => $this->parent->getHelpBox(
120  'config-pingback-help',
121  FormatJson::encode( $pingbackInfo, true )
122  ),
123  'value' => true,
124  ] ) .
125  $this->getFieldsetEnd() .
126  $this->parent->getInfoBox( wfMessage( 'config-almost-done' )->plain() ) .
127  // getRadioSet() builds a set of labeled radio buttons.
128  // For grep: The following messages are used as the item labels:
129  // config-optional-continue, config-optional-skip
130  $this->parent->getRadioSet( [
131  'var' => '_SkipOptional',
132  'itemLabelPrefix' => 'config-optional-',
133  'values' => [ 'continue', 'skip' ]
134  ] )
135  );
136 
137  // Restore the default value
138  $this->setVar( 'wgMetaNamespace', $metaNS );
139 
140  $this->endForm();
141 
142  return 'output';
143  }
144 
148  public function submit() {
149  global $wgPasswordPolicy;
150 
151  $retVal = true;
152  $this->parent->setVarsFromRequest( [ 'wgSitename', '_NamespaceType',
153  '_AdminName', '_AdminPassword', '_AdminPasswordConfirm', '_AdminEmail',
154  '_Subscribe', '_SkipOptional', 'wgMetaNamespace', 'wgPingback' ] );
155 
156  // Validate site name
157  if ( strval( $this->getVar( 'wgSitename' ) ) === '' ) {
158  $this->parent->showError( 'config-site-name-blank' );
159  $retVal = false;
160  }
161 
162  // Fetch namespace
163  $nsType = $this->getVar( '_NamespaceType' );
164  if ( $nsType == 'site-name' ) {
165  $name = $this->getVar( 'wgSitename' );
166  // Sanitize for namespace
167  // This algorithm should match the JS one in WebInstallerOutput.php
168  $name = preg_replace( '/[\[\]\{\}|#<>%+? ]/', '_', $name );
169  $name = str_replace( '&', '&amp;', $name );
170  $name = preg_replace( '/__+/', '_', $name );
171  $name = ucfirst( trim( $name, '_' ) );
172  } elseif ( $nsType == 'generic' ) {
173  $name = wfMessage( 'config-ns-generic' )->text();
174  } else { // other
175  $name = $this->getVar( 'wgMetaNamespace' );
176  }
177 
178  // Validate namespace
179  if ( strpos( $name, ':' ) !== false ) {
180  $good = false;
181  } else {
182  // Title-style validation
183  $title = Title::newFromText( $name );
184  if ( !$title ) {
185  $good = $nsType == 'site-name';
186  } else {
187  $name = $title->getDBkey();
188  $good = true;
189  }
190  }
191  if ( !$good ) {
192  $this->parent->showError( 'config-ns-invalid', $name );
193  $retVal = false;
194  }
195 
196  // Make sure it won't conflict with any existing namespaces
197  $nsIndex = MediaWikiServices::getInstance()->getContentLanguage()->getNsIndex( $name );
198  if ( $nsIndex !== false && $nsIndex !== NS_PROJECT ) {
199  $this->parent->showError( 'config-ns-conflict', $name );
200  $retVal = false;
201  }
202 
203  $this->setVar( 'wgMetaNamespace', $name );
204 
205  // Validate username for creation
206  $name = $this->getVar( '_AdminName' );
207  if ( strval( $name ) === '' ) {
208  $this->parent->showError( 'config-admin-name-blank' );
209  $cname = $name;
210  $retVal = false;
211  } else {
212  $cname = User::getCanonicalName( $name, 'creatable' );
213  if ( $cname === false ) {
214  $this->parent->showError( 'config-admin-name-invalid', $name );
215  $retVal = false;
216  } else {
217  $this->setVar( '_AdminName', $cname );
218  }
219  }
220 
221  // Validate password
222  $msg = false;
223  $pwd = $this->getVar( '_AdminPassword' );
224  $user = User::newFromName( $cname );
225  if ( $user ) {
226  $upp = new UserPasswordPolicy(
227  $wgPasswordPolicy['policies'],
228  $wgPasswordPolicy['checks']
229  );
230  $status = $upp->checkUserPasswordForGroups(
231  $user,
232  $pwd,
233  [ 'bureaucrat', 'sysop', 'interface-admin' ] // per Installer::createSysop()
234  );
235  $valid = $status->isGood() ? true : $status->getMessage();
236  } else {
237  $valid = 'config-admin-name-invalid';
238  }
239  if ( strval( $pwd ) === '' ) {
240  // Provide a more specific and helpful message if password field is left blank
241  $msg = 'config-admin-password-blank';
242  } elseif ( $pwd !== $this->getVar( '_AdminPasswordConfirm' ) ) {
243  $msg = 'config-admin-password-mismatch';
244  } elseif ( $valid !== true ) {
245  $msg = $valid;
246  }
247  if ( $msg !== false ) {
248  call_user_func( [ $this->parent, 'showError' ], $msg );
249  $this->setVar( '_AdminPassword', '' );
250  $this->setVar( '_AdminPasswordConfirm', '' );
251  $retVal = false;
252  }
253 
254  // Validate e-mail if provided
255  $email = $this->getVar( '_AdminEmail' );
256  if ( $email && !Sanitizer::validateEmail( $email ) ) {
257  $this->parent->showError( 'config-admin-error-bademail' );
258  $retVal = false;
259  }
260  // If they asked to subscribe to mediawiki-announce but didn't give
261  // an e-mail, show an error. T31332
262  if ( !$email && $this->getVar( '_Subscribe' ) ) {
263  $this->parent->showError( 'config-subscribe-noemail' );
264  $retVal = false;
265  }
266 
267  return $retVal;
268  }
269 
270 }
WebInstallerPage\startForm
startForm()
Definition: WebInstallerPage.php:69
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:361
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:166
HashConfig
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
true
return true
Definition: router.php:90
Sanitizer\validateEmail
static validateEmail( $addr)
Does a string look like an e-mail address?
Definition: Sanitizer.php:1711
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:558
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1230
WebInstallerPage\endForm
endForm( $continue='continue', $back='back')
Definition: WebInstallerPage.php:86
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:115
NS_PROJECT
const NS_PROJECT
Definition: Defines.php:67
WebInstallerPage\getVar
getVar( $var, $default=null)
Definition: WebInstallerPage.php:152
$title
$title
Definition: testCompression.php:38
UserPasswordPolicy
Check if a user's password complies with any password policies that apply to that user,...
Definition: UserPasswordPolicy.php:28
WebInstallerName
Definition: WebInstallerName.php:24
WebInstallerPage\addHTML
addHTML( $html)
Definition: WebInstallerPage.php:65
WebInstallerName\submit
submit()
Definition: WebInstallerName.php:148
Pingback\getSystemInfo
static getSystemInfo(Config $config)
Collect basic data about this MediaWiki installation and return it as an associative array conforming...
Definition: Pingback.php:190
WebInstallerPage\setVar
setVar( $name, $value)
Definition: WebInstallerPage.php:160
User\getCanonicalName
static getCanonicalName( $name, $validate='valid')
Given unvalidated user input, return a canonical username, or false if the username is invalid.
Definition: User.php:1147
WebInstallerPage
Abstract class to define pages for the web installer.
Definition: WebInstallerPage.php:30
$wgPasswordPolicy
$wgPasswordPolicy
Password policy for the wiki.
Definition: DefaultSettings.php:4889
WebInstallerName\execute
execute()
Definition: WebInstallerName.php:29