Merge "Fix support for TestSwarm on SpecialJavaScriptTest/qunit"
[lhc/web/wiklou.git] / includes / installer / WebInstallerPage.php
1 <?php
2 /**
3 * Base code for web installer pages.
4 *
5 * @file
6 * @ingroup Deployment
7 */
8
9 /**
10 * Abstract class to define pages for the web installer.
11 *
12 * @ingroup Deployment
13 * @since 1.17
14 */
15 abstract class WebInstallerPage {
16
17 /**
18 * The WebInstaller object this WebInstallerPage belongs to.
19 *
20 * @var WebInstaller
21 */
22 public $parent;
23
24 public abstract function execute();
25
26 /**
27 * Constructor.
28 *
29 * @param $parent WebInstaller
30 */
31 public function __construct( WebInstaller $parent ) {
32 $this->parent = $parent;
33 }
34
35 /**
36 * Is this a slow-running page in the installer? If so, WebInstaller will
37 * set_time_limit(0) before calling execute(). Right now this only applies
38 * to Install and Upgrade pages
39 * @return bool
40 */
41 public function isSlow() {
42 return false;
43 }
44
45 public function addHTML( $html ) {
46 $this->parent->output->addHTML( $html );
47 }
48
49 public function startForm() {
50 $this->addHTML(
51 "<div class=\"config-section\">\n" .
52 Html::openElement(
53 'form',
54 array(
55 'method' => 'post',
56 'action' => $this->parent->getUrl( array( 'page' => $this->getName() ) )
57 )
58 ) . "\n"
59 );
60 }
61
62 public function endForm( $continue = 'continue', $back = 'back' ) {
63 $s = "<div class=\"config-submit\">\n";
64 $id = $this->getId();
65
66 if ( $id === false ) {
67 $s .= Html::hidden( 'lastPage', $this->parent->request->getVal( 'lastPage' ) );
68 }
69
70 if ( $continue ) {
71 // Fake submit button for enter keypress (bug 26267)
72 $s .= Xml::submitButton( wfMsg( "config-$continue" ),
73 array( 'name' => "enter-$continue", 'style' =>
74 'visibility:hidden;overflow:hidden;width:1px;margin:0' ) ) . "\n";
75 }
76
77 if ( $back ) {
78 $s .= Xml::submitButton( wfMsg( "config-$back" ),
79 array(
80 'name' => "submit-$back",
81 'tabindex' => $this->parent->nextTabIndex()
82 ) ) . "\n";
83 }
84
85 if ( $continue ) {
86 $s .= Xml::submitButton( wfMsg( "config-$continue" ),
87 array(
88 'name' => "submit-$continue",
89 'tabindex' => $this->parent->nextTabIndex(),
90 ) ) . "\n";
91 }
92
93 $s .= "</div></form></div>\n";
94 $this->addHTML( $s );
95 }
96
97 public function getName() {
98 return str_replace( 'WebInstaller_', '', get_class( $this ) );
99 }
100
101 protected function getId() {
102 return array_search( $this->getName(), $this->parent->pageSequence );
103 }
104
105 public function getVar( $var ) {
106 return $this->parent->getVar( $var );
107 }
108
109 public function setVar( $name, $value ) {
110 $this->parent->setVar( $name, $value );
111 }
112
113 /**
114 * Get the starting tags of a fieldset.
115 *
116 * @param $legend String: message name
117 *
118 * @return string
119 */
120 protected function getFieldsetStart( $legend ) {
121 return "\n<fieldset><legend>" . wfMsgHtml( $legend ) . "</legend>\n";
122 }
123
124 /**
125 * Get the end tag of a fieldset.
126 *
127 * @return string
128 */
129 protected function getFieldsetEnd() {
130 return "</fieldset>\n";
131 }
132
133 /**
134 * Opens a textarea used to display the progress of a long operation
135 */
136 protected function startLiveBox() {
137 $this->addHTML(
138 '<div id="config-spinner" style="display:none;">' .
139 '<img src="../skins/common/images/ajax-loader.gif" /></div>' .
140 '<script>jQuery( "#config-spinner" ).show();</script>' .
141 '<div id="config-live-log">' .
142 '<textarea name="LiveLog" rows="10" cols="30" readonly="readonly">'
143 );
144 $this->parent->output->flush();
145 }
146
147 /**
148 * Opposite to startLiveBox()
149 */
150 protected function endLiveBox() {
151 $this->addHTML( '</textarea></div>
152 <script>jQuery( "#config-spinner" ).hide()</script>' );
153 $this->parent->output->flush();
154 }
155 }
156
157 class WebInstaller_Language extends WebInstallerPage {
158
159 public function execute() {
160 global $wgLang;
161 $r = $this->parent->request;
162 $userLang = $r->getVal( 'uselang' );
163 $contLang = $r->getVal( 'ContLang' );
164
165 $languages = Language::fetchLanguageNames();
166 $lifetime = intval( ini_get( 'session.gc_maxlifetime' ) );
167 if ( !$lifetime ) {
168 $lifetime = 1440; // PHP default
169 }
170
171 if ( $r->wasPosted() ) {
172 # Do session test
173 if ( $this->parent->getSession( 'test' ) === null ) {
174 $requestTime = $r->getVal( 'LanguageRequestTime' );
175 if ( !$requestTime ) {
176 // The most likely explanation is that the user was knocked back
177 // from another page on POST due to session expiry
178 $msg = 'config-session-expired';
179 } elseif ( time() - $requestTime > $lifetime ) {
180 $msg = 'config-session-expired';
181 } else {
182 $msg = 'config-no-session';
183 }
184 $this->parent->showError( $msg, $wgLang->formatTimePeriod( $lifetime ) );
185 } else {
186 if ( isset( $languages[$userLang] ) ) {
187 $this->setVar( '_UserLang', $userLang );
188 }
189 if ( isset( $languages[$contLang] ) ) {
190 $this->setVar( 'wgLanguageCode', $contLang );
191 }
192 return 'continue';
193 }
194 } elseif ( $this->parent->showSessionWarning ) {
195 # The user was knocked back from another page to the start
196 # This probably indicates a session expiry
197 $this->parent->showError( 'config-session-expired',
198 $wgLang->formatTimePeriod( $lifetime ) );
199 }
200
201 $this->parent->setSession( 'test', true );
202
203 if ( !isset( $languages[$userLang] ) ) {
204 $userLang = $this->getVar( '_UserLang', 'en' );
205 }
206 if ( !isset( $languages[$contLang] ) ) {
207 $contLang = $this->getVar( 'wgLanguageCode', 'en' );
208 }
209 $this->startForm();
210 $s = Html::hidden( 'LanguageRequestTime', time() ) .
211 $this->getLanguageSelector( 'uselang', 'config-your-language', $userLang,
212 $this->parent->getHelpBox( 'config-your-language-help' ) ) .
213 $this->getLanguageSelector( 'ContLang', 'config-wiki-language', $contLang,
214 $this->parent->getHelpBox( 'config-wiki-language-help' ) );
215 $this->addHTML( $s );
216 $this->endForm( 'continue', false );
217 }
218
219 /**
220 * Get a <select> for selecting languages.
221 *
222 * @param $name
223 * @param $label
224 * @param $selectedCode
225 * @param $helpHtml string
226 * @return string
227 */
228 public function getLanguageSelector( $name, $label, $selectedCode, $helpHtml = '' ) {
229 global $wgDummyLanguageCodes;
230
231 $s = $helpHtml;
232
233 $s .= Html::openElement( 'select', array( 'id' => $name, 'name' => $name,
234 'tabindex' => $this->parent->nextTabIndex() ) ) . "\n";
235
236 $languages = Language::fetchLanguageNames();
237 ksort( $languages );
238 foreach ( $languages as $code => $lang ) {
239 if ( isset( $wgDummyLanguageCodes[$code] ) ) continue;
240 $s .= "\n" . Xml::option( "$code - $lang", $code, $code == $selectedCode );
241 }
242 $s .= "\n</select>\n";
243 return $this->parent->label( $label, $name, $s );
244 }
245
246 }
247
248 class WebInstaller_ExistingWiki extends WebInstallerPage {
249 public function execute() {
250 // If there is no LocalSettings.php, continue to the installer welcome page
251 $vars = Installer::getExistingLocalSettings();
252 if ( !$vars ) {
253 return 'skip';
254 }
255
256 // Check if the upgrade key supplied to the user has appeared in LocalSettings.php
257 if ( $vars['wgUpgradeKey'] !== false
258 && $this->getVar( '_UpgradeKeySupplied' )
259 && $this->getVar( 'wgUpgradeKey' ) === $vars['wgUpgradeKey'] )
260 {
261 // It's there, so the user is authorized
262 $status = $this->handleExistingUpgrade( $vars );
263 if ( $status->isOK() ) {
264 return 'skip';
265 } else {
266 $this->startForm();
267 $this->parent->showStatusBox( $status );
268 $this->endForm( 'continue' );
269 return 'output';
270 }
271 }
272
273 // If there is no $wgUpgradeKey, tell the user to add one to LocalSettings.php
274 if ( $vars['wgUpgradeKey'] === false ) {
275 if ( $this->getVar( 'wgUpgradeKey', false ) === false ) {
276 $secretKey = $this->getVar( 'wgSecretKey' ); // preserve $wgSecretKey
277 $this->parent->generateKeys();
278 $this->setVar( 'wgSecretKey', $secretKey );
279 $this->setVar( '_UpgradeKeySupplied', true );
280 }
281 $this->startForm();
282 $this->addHTML( $this->parent->getInfoBox(
283 wfMsgNoTrans( 'config-upgrade-key-missing',
284 "<pre dir=\"ltr\">\$wgUpgradeKey = '" . $this->getVar( 'wgUpgradeKey' ) . "';</pre>" )
285 ) );
286 $this->endForm( 'continue' );
287 return 'output';
288 }
289
290 // If there is an upgrade key, but it wasn't supplied, prompt the user to enter it
291
292 $r = $this->parent->request;
293 if ( $r->wasPosted() ) {
294 $key = $r->getText( 'config_wgUpgradeKey' );
295 if( !$key || $key !== $vars['wgUpgradeKey'] ) {
296 $this->parent->showError( 'config-localsettings-badkey' );
297 $this->showKeyForm();
298 return 'output';
299 }
300 // Key was OK
301 $status = $this->handleExistingUpgrade( $vars );
302 if ( $status->isOK() ) {
303 return 'continue';
304 } else {
305 $this->parent->showStatusBox( $status );
306 $this->showKeyForm();
307 return 'output';
308 }
309 } else {
310 $this->showKeyForm();
311 return 'output';
312 }
313 }
314
315 /**
316 * Show the "enter key" form
317 */
318 protected function showKeyForm() {
319 $this->startForm();
320 $this->addHTML(
321 $this->parent->getInfoBox( wfMsgNoTrans( 'config-localsettings-upgrade' ) ).
322 '<br />' .
323 $this->parent->getTextBox( array(
324 'var' => 'wgUpgradeKey',
325 'label' => 'config-localsettings-key',
326 'attribs' => array( 'autocomplete' => 'off' ),
327 ) )
328 );
329 $this->endForm( 'continue' );
330 }
331
332 protected function importVariables( $names, $vars ) {
333 $status = Status::newGood();
334 foreach ( $names as $name ) {
335 if ( !isset( $vars[$name] ) ) {
336 $status->fatal( 'config-localsettings-incomplete', $name );
337 }
338 $this->setVar( $name, $vars[$name] );
339 }
340 return $status;
341 }
342
343 /**
344 * Initiate an upgrade of the existing database
345 * @param $vars array Variables from LocalSettings.php and AdminSettings.php
346 * @return Status
347 */
348 protected function handleExistingUpgrade( $vars ) {
349 // Check $wgDBtype
350 if ( !isset( $vars['wgDBtype'] ) ||
351 !in_array( $vars['wgDBtype'], Installer::getDBTypes() ) ) {
352 return Status::newFatal( 'config-localsettings-connection-error', '' );
353 }
354
355 // Set the relevant variables from LocalSettings.php
356 $requiredVars = array( 'wgDBtype' );
357 $status = $this->importVariables( $requiredVars , $vars );
358 $installer = $this->parent->getDBInstaller();
359 $status->merge( $this->importVariables( $installer->getGlobalNames(), $vars ) );
360 if ( !$status->isOK() ) {
361 return $status;
362 }
363
364 if ( isset( $vars['wgDBadminuser'] ) ) {
365 $this->setVar( '_InstallUser', $vars['wgDBadminuser'] );
366 } else {
367 $this->setVar( '_InstallUser', $vars['wgDBuser'] );
368 }
369 if ( isset( $vars['wgDBadminpassword'] ) ) {
370 $this->setVar( '_InstallPassword', $vars['wgDBadminpassword'] );
371 } else {
372 $this->setVar( '_InstallPassword', $vars['wgDBpassword'] );
373 }
374
375 // Test the database connection
376 $status = $installer->getConnection();
377 if ( !$status->isOK() ) {
378 // Adjust the error message to explain things correctly
379 $status->replaceMessage( 'config-connection-error',
380 'config-localsettings-connection-error' );
381 return $status;
382 }
383
384 // All good
385 $this->setVar( '_ExistingDBSettings', true );
386 return $status;
387 }
388 }
389
390 class WebInstaller_Welcome extends WebInstallerPage {
391
392 public function execute() {
393 if ( $this->parent->request->wasPosted() ) {
394 if ( $this->getVar( '_Environment' ) ) {
395 return 'continue';
396 }
397 }
398 $this->parent->output->addWikiText( wfMsgNoTrans( 'config-welcome' ) );
399 $status = $this->parent->doEnvironmentChecks();
400 if ( $status->isGood() ) {
401 $this->parent->output->addHTML( '<span class="success-message">' .
402 wfMsgHtml( 'config-env-good' ) . '</span>' );
403 $this->parent->output->addWikiText( wfMsgNoTrans( 'config-copyright',
404 SpecialVersion::getCopyrightAndAuthorList() ) );
405 $this->startForm();
406 $this->endForm();
407 } else {
408 $this->parent->showStatusMessage( $status );
409 }
410 }
411
412 }
413
414 class WebInstaller_DBConnect extends WebInstallerPage {
415
416 public function execute() {
417 if ( $this->getVar( '_ExistingDBSettings' ) ) {
418 return 'skip';
419 }
420
421 $r = $this->parent->request;
422 if ( $r->wasPosted() ) {
423 $status = $this->submit();
424
425 if ( $status->isGood() ) {
426 $this->setVar( '_UpgradeDone', false );
427 return 'continue';
428 } else {
429 $this->parent->showStatusBox( $status );
430 }
431 }
432
433 $this->startForm();
434
435 $types = "<ul class=\"config-settings-block\">\n";
436 $settings = '';
437 $defaultType = $this->getVar( 'wgDBtype' );
438
439 $dbSupport = '';
440 foreach( $this->parent->getDBTypes() as $type ) {
441 $link = DatabaseBase::factory( $type )->getSoftwareLink();
442 $dbSupport .= wfMsgNoTrans( "config-support-$type", $link ) . "\n";
443 }
444 $this->addHTML( $this->parent->getInfoBox(
445 wfMsg( 'config-support-info', $dbSupport ) ) );
446
447 foreach ( $this->parent->getVar( '_CompiledDBs' ) as $type ) {
448 $installer = $this->parent->getDBInstaller( $type );
449 $types .=
450 '<li>' .
451 Xml::radioLabel(
452 $installer->getReadableName(),
453 'DBType',
454 $type,
455 "DBType_$type",
456 $type == $defaultType,
457 array( 'class' => 'dbRadio', 'rel' => "DB_wrapper_$type" )
458 ) .
459 "</li>\n";
460
461 $settings .=
462 Html::openElement( 'div', array( 'id' => 'DB_wrapper_' . $type,
463 'class' => 'dbWrapper' ) ) .
464 Html::element( 'h3', array(), wfMsg( 'config-header-' . $type ) ) .
465 $installer->getConnectForm() .
466 "</div>\n";
467 }
468 $types .= "</ul><br style=\"clear: left\"/>\n";
469
470 $this->addHTML(
471 $this->parent->label( 'config-db-type', false, $types ) .
472 $settings
473 );
474
475 $this->endForm();
476 }
477
478 public function submit() {
479 $r = $this->parent->request;
480 $type = $r->getVal( 'DBType' );
481 $this->setVar( 'wgDBtype', $type );
482 $installer = $this->parent->getDBInstaller( $type );
483 if ( !$installer ) {
484 return Status::newFatal( 'config-invalid-db-type' );
485 }
486 return $installer->submitConnectForm();
487 }
488
489 }
490
491 class WebInstaller_Upgrade extends WebInstallerPage {
492 public function isSlow() {
493 return true;
494 }
495
496 public function execute() {
497 if ( $this->getVar( '_UpgradeDone' ) ) {
498 // Allow regeneration of LocalSettings.php, unless we are working
499 // from a pre-existing LocalSettings.php file and we want to avoid
500 // leaking its contents
501 if ( $this->parent->request->wasPosted() && !$this->getVar( '_ExistingDBSettings' ) ) {
502 // Done message acknowledged
503 return 'continue';
504 } else {
505 // Back button click
506 // Show the done message again
507 // Make them click back again if they want to do the upgrade again
508 $this->showDoneMessage();
509 return 'output';
510 }
511 }
512
513 // wgDBtype is generally valid here because otherwise the previous page
514 // (connect) wouldn't have declared its happiness
515 $type = $this->getVar( 'wgDBtype' );
516 $installer = $this->parent->getDBInstaller( $type );
517
518 if ( !$installer->needsUpgrade() ) {
519 return 'skip';
520 }
521
522 if ( $this->parent->request->wasPosted() ) {
523 $installer->preUpgrade();
524
525 $this->startLiveBox();
526 $result = $installer->doUpgrade();
527 $this->endLiveBox();
528
529 if ( $result ) {
530 // If they're going to possibly regenerate LocalSettings, we
531 // need to create the upgrade/secret keys. Bug 26481
532 if( !$this->getVar( '_ExistingDBSettings' ) ) {
533 $this->parent->generateKeys();
534 }
535 $this->setVar( '_UpgradeDone', true );
536 $this->showDoneMessage();
537 return 'output';
538 }
539 }
540
541 $this->startForm();
542 $this->addHTML( $this->parent->getInfoBox(
543 wfMsgNoTrans( 'config-can-upgrade', $GLOBALS['wgVersion'] ) ) );
544 $this->endForm();
545 }
546
547 public function showDoneMessage() {
548 $this->startForm();
549 $regenerate = !$this->getVar( '_ExistingDBSettings' );
550 if ( $regenerate ) {
551 $msg = 'config-upgrade-done';
552 } else {
553 $msg = 'config-upgrade-done-no-regenerate';
554 }
555 $this->parent->disableLinkPopups();
556 $this->addHTML(
557 $this->parent->getInfoBox(
558 wfMsgNoTrans( $msg,
559 $this->getVar( 'wgServer' ) .
560 $this->getVar( 'wgScriptPath' ) . '/index' .
561 $this->getVar( 'wgScriptExtension' )
562 ), 'tick-32.png'
563 )
564 );
565 $this->parent->restoreLinkPopups();
566 $this->endForm( $regenerate ? 'regenerate' : false, false );
567 }
568
569 }
570
571 class WebInstaller_DBSettings extends WebInstallerPage {
572
573 public function execute() {
574 $installer = $this->parent->getDBInstaller( $this->getVar( 'wgDBtype' ) );
575
576 $r = $this->parent->request;
577 if ( $r->wasPosted() ) {
578 $status = $installer->submitSettingsForm();
579 if ( $status === false ) {
580 return 'skip';
581 } elseif ( $status->isGood() ) {
582 return 'continue';
583 } else {
584 $this->parent->showStatusBox( $status );
585 }
586 }
587
588 $form = $installer->getSettingsForm();
589 if ( $form === false ) {
590 return 'skip';
591 }
592
593 $this->startForm();
594 $this->addHTML( $form );
595 $this->endForm();
596 }
597
598 }
599
600 class WebInstaller_Name extends WebInstallerPage {
601
602 public function execute() {
603 $r = $this->parent->request;
604 if ( $r->wasPosted() ) {
605 if ( $this->submit() ) {
606 return 'continue';
607 }
608 }
609
610 $this->startForm();
611
612 // Encourage people to not name their site 'MediaWiki' by blanking the
613 // field. I think that was the intent with the original $GLOBALS['wgSitename']
614 // but these two always were the same so had the effect of making the
615 // installer forget $wgSitename when navigating back to this page.
616 if ( $this->getVar( 'wgSitename' ) == 'MediaWiki' ) {
617 $this->setVar( 'wgSitename', '' );
618 }
619
620 // Set wgMetaNamespace to something valid before we show the form.
621 // $wgMetaNamespace defaults to $wgSiteName which is 'MediaWiki'
622 $metaNS = $this->getVar( 'wgMetaNamespace' );
623 $this->setVar( 'wgMetaNamespace', wfMsgForContent( 'config-ns-other-default' ) );
624
625 $this->addHTML(
626 $this->parent->getTextBox( array(
627 'var' => 'wgSitename',
628 'label' => 'config-site-name',
629 'help' => $this->parent->getHelpBox( 'config-site-name-help' )
630 ) ) .
631 $this->parent->getRadioSet( array(
632 'var' => '_NamespaceType',
633 'label' => 'config-project-namespace',
634 'itemLabelPrefix' => 'config-ns-',
635 'values' => array( 'site-name', 'generic', 'other' ),
636 'commonAttribs' => array( 'class' => 'enableForOther',
637 'rel' => 'config_wgMetaNamespace' ),
638 'help' => $this->parent->getHelpBox( 'config-project-namespace-help' )
639 ) ) .
640 $this->parent->getTextBox( array(
641 'var' => 'wgMetaNamespace',
642 'label' => '', //TODO: Needs a label?
643 'attribs' => array( 'readonly' => 'readonly', 'class' => 'enabledByOther' ),
644
645 ) ) .
646 $this->getFieldSetStart( 'config-admin-box' ) .
647 $this->parent->getTextBox( array(
648 'var' => '_AdminName',
649 'label' => 'config-admin-name',
650 'help' => $this->parent->getHelpBox( 'config-admin-help' )
651 ) ) .
652 $this->parent->getPasswordBox( array(
653 'var' => '_AdminPassword',
654 'label' => 'config-admin-password',
655 ) ) .
656 $this->parent->getPasswordBox( array(
657 'var' => '_AdminPassword2',
658 'label' => 'config-admin-password-confirm'
659 ) ) .
660 $this->parent->getTextBox( array(
661 'var' => '_AdminEmail',
662 'label' => 'config-admin-email',
663 'help' => $this->parent->getHelpBox( 'config-admin-email-help' )
664 ) ) .
665 $this->parent->getCheckBox( array(
666 'var' => '_Subscribe',
667 'label' => 'config-subscribe',
668 'help' => $this->parent->getHelpBox( 'config-subscribe-help' )
669 ) ) .
670 $this->getFieldSetEnd() .
671 $this->parent->getInfoBox( wfMsg( 'config-almost-done' ) ) .
672 $this->parent->getRadioSet( array(
673 'var' => '_SkipOptional',
674 'itemLabelPrefix' => 'config-optional-',
675 'values' => array( 'continue', 'skip' )
676 ) )
677 );
678
679 // Restore the default value
680 $this->setVar( 'wgMetaNamespace', $metaNS );
681
682 $this->endForm();
683 return 'output';
684 }
685
686 public function submit() {
687 $retVal = true;
688 $this->parent->setVarsFromRequest( array( 'wgSitename', '_NamespaceType',
689 '_AdminName', '_AdminPassword', '_AdminPassword2', '_AdminEmail',
690 '_Subscribe', '_SkipOptional', 'wgMetaNamespace' ) );
691
692 // Validate site name
693 if ( strval( $this->getVar( 'wgSitename' ) ) === '' ) {
694 $this->parent->showError( 'config-site-name-blank' );
695 $retVal = false;
696 }
697
698 // Fetch namespace
699 $nsType = $this->getVar( '_NamespaceType' );
700 if ( $nsType == 'site-name' ) {
701 $name = $this->getVar( 'wgSitename' );
702 // Sanitize for namespace
703 // This algorithm should match the JS one in WebInstallerOutput.php
704 $name = preg_replace( '/[\[\]\{\}|#<>%+? ]/', '_', $name );
705 $name = str_replace( '&', '&amp;', $name );
706 $name = preg_replace( '/__+/', '_', $name );
707 $name = ucfirst( trim( $name, '_' ) );
708 } elseif ( $nsType == 'generic' ) {
709 $name = wfMsg( 'config-ns-generic' );
710 } else { // other
711 $name = $this->getVar( 'wgMetaNamespace' );
712 }
713
714 // Validate namespace
715 if ( strpos( $name, ':' ) !== false ) {
716 $good = false;
717 } else {
718 // Title-style validation
719 $title = Title::newFromText( $name );
720 if ( !$title ) {
721 $good = $nsType == 'site-name';
722 } else {
723 $name = $title->getDBkey();
724 $good = true;
725 }
726 }
727 if ( !$good ) {
728 $this->parent->showError( 'config-ns-invalid', $name );
729 $retVal = false;
730 }
731
732 // Make sure it won't conflict with any existing namespaces
733 global $wgContLang;
734 $nsIndex = $wgContLang->getNsIndex( $name );
735 if( $nsIndex !== false && $nsIndex !== NS_PROJECT ) {
736 $this->parent->showError( 'config-ns-conflict', $name );
737 $retVal = false;
738 }
739
740 $this->setVar( 'wgMetaNamespace', $name );
741
742 // Validate username for creation
743 $name = $this->getVar( '_AdminName' );
744 if ( strval( $name ) === '' ) {
745 $this->parent->showError( 'config-admin-name-blank' );
746 $cname = $name;
747 $retVal = false;
748 } else {
749 $cname = User::getCanonicalName( $name, 'creatable' );
750 if ( $cname === false ) {
751 $this->parent->showError( 'config-admin-name-invalid', $name );
752 $retVal = false;
753 } else {
754 $this->setVar( '_AdminName', $cname );
755 }
756 }
757
758 // Validate password
759 $msg = false;
760 $pwd = $this->getVar( '_AdminPassword' );
761 $user = User::newFromName( $cname );
762 $valid = $user && $user->getPasswordValidity( $pwd );
763 if ( strval( $pwd ) === '' ) {
764 # $user->getPasswordValidity just checks for $wgMinimalPasswordLength.
765 # This message is more specific and helpful.
766 $msg = 'config-admin-password-blank';
767 } elseif ( $pwd !== $this->getVar( '_AdminPassword2' ) ) {
768 $msg = 'config-admin-password-mismatch';
769 } elseif ( $valid !== true ) {
770 # As of writing this will only catch the username being e.g. 'FOO' and
771 # the password 'foo'
772 $msg = $valid;
773 }
774 if ( $msg !== false ) {
775 call_user_func_array( array( $this->parent, 'showError' ), (array)$msg );
776 $this->setVar( '_AdminPassword', '' );
777 $this->setVar( '_AdminPassword2', '' );
778 $retVal = false;
779 }
780
781 // Validate e-mail if provided
782 $email = $this->getVar( '_AdminEmail' );
783 if( $email && !Sanitizer::validateEmail( $email ) ) {
784 $this->parent->showError( 'config-admin-error-bademail' );
785 $retVal = false;
786 }
787 // If they asked to subscribe to mediawiki-announce but didn't give
788 // an e-mail, show an error. Bug 29332
789 if( !$email && $this->getVar( '_Subscribe' ) ) {
790 $this->parent->showError( 'config-subscribe-noemail' );
791 $retVal = false;
792 }
793
794 return $retVal;
795 }
796
797 }
798
799 class WebInstaller_Options extends WebInstallerPage {
800
801 public function execute() {
802 if ( $this->getVar( '_SkipOptional' ) == 'skip' ) {
803 return 'skip';
804 }
805 if ( $this->parent->request->wasPosted() ) {
806 if ( $this->submit() ) {
807 return 'continue';
808 }
809 }
810
811 $emailwrapperStyle = $this->getVar( 'wgEnableEmail' ) ? '' : 'display: none';
812 $this->startForm();
813 $this->addHTML(
814 # User Rights
815 $this->parent->getRadioSet( array(
816 'var' => '_RightsProfile',
817 'label' => 'config-profile',
818 'itemLabelPrefix' => 'config-profile-',
819 'values' => array_keys( $this->parent->rightsProfiles ),
820 ) ) .
821 $this->parent->getInfoBox( wfMsgNoTrans( 'config-profile-help' ) ) .
822
823 # Licensing
824 $this->parent->getRadioSet( array(
825 'var' => '_LicenseCode',
826 'label' => 'config-license',
827 'itemLabelPrefix' => 'config-license-',
828 'values' => array_keys( $this->parent->licenses ),
829 'commonAttribs' => array( 'class' => 'licenseRadio' ),
830 ) ) .
831 $this->getCCChooser() .
832 $this->parent->getHelpBox( 'config-license-help' ) .
833
834 # E-mail
835 $this->getFieldSetStart( 'config-email-settings' ) .
836 $this->parent->getCheckBox( array(
837 'var' => 'wgEnableEmail',
838 'label' => 'config-enable-email',
839 'attribs' => array( 'class' => 'showHideRadio', 'rel' => 'emailwrapper' ),
840 ) ) .
841 $this->parent->getHelpBox( 'config-enable-email-help' ) .
842 "<div id=\"emailwrapper\" style=\"$emailwrapperStyle\">" .
843 $this->parent->getTextBox( array(
844 'var' => 'wgPasswordSender',
845 'label' => 'config-email-sender'
846 ) ) .
847 $this->parent->getHelpBox( 'config-email-sender-help' ) .
848 $this->parent->getCheckBox( array(
849 'var' => 'wgEnableUserEmail',
850 'label' => 'config-email-user',
851 ) ) .
852 $this->parent->getHelpBox( 'config-email-user-help' ) .
853 $this->parent->getCheckBox( array(
854 'var' => 'wgEnotifUserTalk',
855 'label' => 'config-email-usertalk',
856 ) ) .
857 $this->parent->getHelpBox( 'config-email-usertalk-help' ) .
858 $this->parent->getCheckBox( array(
859 'var' => 'wgEnotifWatchlist',
860 'label' => 'config-email-watchlist',
861 ) ) .
862 $this->parent->getHelpBox( 'config-email-watchlist-help' ) .
863 $this->parent->getCheckBox( array(
864 'var' => 'wgEmailAuthentication',
865 'label' => 'config-email-auth',
866 ) ) .
867 $this->parent->getHelpBox( 'config-email-auth-help' ) .
868 "</div>" .
869 $this->getFieldSetEnd()
870 );
871
872 $extensions = $this->parent->findExtensions();
873
874 if( $extensions ) {
875 $extHtml = $this->getFieldSetStart( 'config-extensions' );
876
877 foreach( $extensions as $ext ) {
878 $extHtml .= $this->parent->getCheckBox( array(
879 'var' => "ext-$ext",
880 'rawtext' => $ext,
881 ) );
882 }
883
884 $extHtml .= $this->parent->getHelpBox( 'config-extensions-help' ) .
885 $this->getFieldSetEnd();
886 $this->addHTML( $extHtml );
887 }
888
889 // Having / in paths in Windows looks funny :)
890 $this->setVar( 'wgDeletedDirectory',
891 str_replace(
892 '/', DIRECTORY_SEPARATOR,
893 $this->getVar( 'wgDeletedDirectory' )
894 )
895 );
896
897 $uploadwrapperStyle = $this->getVar( 'wgEnableUploads' ) ? '' : 'display: none';
898 $this->addHTML(
899 # Uploading
900 $this->getFieldSetStart( 'config-upload-settings' ) .
901 $this->parent->getCheckBox( array(
902 'var' => 'wgEnableUploads',
903 'label' => 'config-upload-enable',
904 'attribs' => array( 'class' => 'showHideRadio', 'rel' => 'uploadwrapper' ),
905 'help' => $this->parent->getHelpBox( 'config-upload-help' )
906 ) ) .
907 '<div id="uploadwrapper" style="' . $uploadwrapperStyle . '">' .
908 $this->parent->getTextBox( array(
909 'var' => 'wgDeletedDirectory',
910 'label' => 'config-upload-deleted',
911 'attribs' => array( 'dir' => 'ltr' ),
912 'help' => $this->parent->getHelpBox( 'config-upload-deleted-help' )
913 ) ) .
914 '</div>' .
915 $this->parent->getTextBox( array(
916 'var' => 'wgLogo',
917 'label' => 'config-logo',
918 'attribs' => array( 'dir' => 'ltr' ),
919 'help' => $this->parent->getHelpBox( 'config-logo-help' )
920 ) )
921 );
922 $this->addHTML(
923 $this->parent->getCheckBox( array(
924 'var' => 'wgUseInstantCommons',
925 'label' => 'config-instantcommons',
926 'help' => $this->parent->getHelpBox( 'config-instantcommons-help' )
927 ) ) .
928 $this->getFieldSetEnd()
929 );
930
931 $caches = array( 'none' );
932 if( count( $this->getVar( '_Caches' ) ) ) {
933 $caches[] = 'accel';
934 }
935 $caches[] = 'memcached';
936
937 // We'll hide/show this on demand when the value changes, see config.js.
938 $cacheval = $this->getVar( 'wgMainCacheType' );
939 if (!$cacheval) {
940 // We need to set a default here; but don't hardcode it
941 // or we lose it every time we reload the page for validation
942 // or going back!
943 $cacheval = 'none';
944 }
945 $hidden = ($cacheval == 'memcached') ? '' : 'display: none';
946 $this->addHTML(
947 # Advanced settings
948 $this->getFieldSetStart( 'config-advanced-settings' ) .
949 # Object cache settings
950 $this->parent->getRadioSet( array(
951 'var' => 'wgMainCacheType',
952 'label' => 'config-cache-options',
953 'itemLabelPrefix' => 'config-cache-',
954 'values' => $caches,
955 'value' => $cacheval,
956 ) ) .
957 $this->parent->getHelpBox( 'config-cache-help' ) .
958 "<div id=\"config-memcachewrapper\" style=\"$hidden\">" .
959 $this->parent->getTextArea( array(
960 'var' => '_MemCachedServers',
961 'label' => 'config-memcached-servers',
962 'help' => $this->parent->getHelpBox( 'config-memcached-help' )
963 ) ) .
964 '</div>' .
965 $this->getFieldSetEnd()
966 );
967 $this->endForm();
968 }
969
970 /**
971 * @return string
972 */
973 public function getCCPartnerUrl() {
974 $server = $this->getVar( 'wgServer' );
975 $exitUrl = $server . $this->parent->getUrl( array(
976 'page' => 'Options',
977 'SubmitCC' => 'indeed',
978 'config__LicenseCode' => 'cc',
979 'config_wgRightsUrl' => '[license_url]',
980 'config_wgRightsText' => '[license_name]',
981 'config_wgRightsIcon' => '[license_button]',
982 ) );
983 $styleUrl = $server . dirname( dirname( $this->parent->getUrl() ) ) .
984 '/skins/common/config-cc.css';
985 $iframeUrl = 'http://creativecommons.org/license/?' .
986 wfArrayToCGI( array(
987 'partner' => 'MediaWiki',
988 'exit_url' => $exitUrl,
989 'lang' => $this->getVar( '_UserLang' ),
990 'stylesheet' => $styleUrl,
991 ) );
992 return $iframeUrl;
993 }
994
995 public function getCCChooser() {
996 $iframeAttribs = array(
997 'class' => 'config-cc-iframe',
998 'name' => 'config-cc-iframe',
999 'id' => 'config-cc-iframe',
1000 'frameborder' => 0,
1001 'width' => '100%',
1002 'height' => '100%',
1003 );
1004 if ( $this->getVar( '_CCDone' ) ) {
1005 $iframeAttribs['src'] = $this->parent->getUrl( array( 'ShowCC' => 'yes' ) );
1006 } else {
1007 $iframeAttribs['src'] = $this->getCCPartnerUrl();
1008 }
1009 $wrapperStyle = ($this->getVar('_LicenseCode') == 'cc-choose') ? '' : 'display: none';
1010
1011 return
1012 "<div class=\"config-cc-wrapper\" id=\"config-cc-wrapper\" style=\"$wrapperStyle\">\n" .
1013 Html::element( 'iframe', $iframeAttribs, '', false /* not short */ ) .
1014 "</div>\n";
1015 }
1016
1017 public function getCCDoneBox() {
1018 $js = "parent.document.getElementById('config-cc-wrapper').style.height = '$1';";
1019 // If you change this height, also change it in config.css
1020 $expandJs = str_replace( '$1', '54em', $js );
1021 $reduceJs = str_replace( '$1', '70px', $js );
1022 return
1023 '<p>'.
1024 Html::element( 'img', array( 'src' => $this->getVar( 'wgRightsIcon' ) ) ) .
1025 '&#160;&#160;' .
1026 htmlspecialchars( $this->getVar( 'wgRightsText' ) ) .
1027 "</p>\n" .
1028 "<p style=\"text-align: center\">" .
1029 Html::element( 'a',
1030 array(
1031 'href' => $this->getCCPartnerUrl(),
1032 'onclick' => $expandJs,
1033 ),
1034 wfMsg( 'config-cc-again' )
1035 ) .
1036 "</p>\n" .
1037 "<script type=\"text/javascript\">\n" .
1038 # Reduce the wrapper div height
1039 htmlspecialchars( $reduceJs ) .
1040 "\n" .
1041 "</script>\n";
1042 }
1043
1044 public function submitCC() {
1045 $newValues = $this->parent->setVarsFromRequest(
1046 array( 'wgRightsUrl', 'wgRightsText', 'wgRightsIcon' ) );
1047 if ( count( $newValues ) != 3 ) {
1048 $this->parent->showError( 'config-cc-error' );
1049 return;
1050 }
1051 $this->setVar( '_CCDone', true );
1052 $this->addHTML( $this->getCCDoneBox() );
1053 }
1054
1055 public function submit() {
1056 $this->parent->setVarsFromRequest( array( '_RightsProfile', '_LicenseCode',
1057 'wgEnableEmail', 'wgPasswordSender', 'wgEnableUploads', 'wgLogo',
1058 'wgEnableUserEmail', 'wgEnotifUserTalk', 'wgEnotifWatchlist',
1059 'wgEmailAuthentication', 'wgMainCacheType', '_MemCachedServers',
1060 'wgUseInstantCommons' ) );
1061
1062 if ( !in_array( $this->getVar( '_RightsProfile' ),
1063 array_keys( $this->parent->rightsProfiles ) ) )
1064 {
1065 reset( $this->parent->rightsProfiles );
1066 $this->setVar( '_RightsProfile', key( $this->parent->rightsProfiles ) );
1067 }
1068
1069 $code = $this->getVar( '_LicenseCode' );
1070 if ( $code == 'cc-choose' ) {
1071 if ( !$this->getVar( '_CCDone' ) ) {
1072 $this->parent->showError( 'config-cc-not-chosen' );
1073 return false;
1074 }
1075 } elseif ( in_array( $code, array_keys( $this->parent->licenses ) ) ) {
1076 $entry = $this->parent->licenses[$code];
1077 if ( isset( $entry['text'] ) ) {
1078 $this->setVar( 'wgRightsText', $entry['text'] );
1079 } else {
1080 $this->setVar( 'wgRightsText', wfMsg( 'config-license-' . $code ) );
1081 }
1082 $this->setVar( 'wgRightsUrl', $entry['url'] );
1083 $this->setVar( 'wgRightsIcon', $entry['icon'] );
1084 } else {
1085 $this->setVar( 'wgRightsText', '' );
1086 $this->setVar( 'wgRightsUrl', '' );
1087 $this->setVar( 'wgRightsIcon', '' );
1088 }
1089
1090 $extsAvailable = $this->parent->findExtensions();
1091 $extsToInstall = array();
1092 foreach( $extsAvailable as $ext ) {
1093 if( $this->parent->request->getCheck( 'config_ext-' . $ext ) ) {
1094 $extsToInstall[] = $ext;
1095 }
1096 }
1097 $this->parent->setVar( '_Extensions', $extsToInstall );
1098
1099 if( $this->getVar( 'wgMainCacheType' ) == 'memcached' ) {
1100 $memcServers = explode( "\n", $this->getVar( '_MemCachedServers' ) );
1101 if( !$memcServers ) {
1102 $this->parent->showError( 'config-memcache-needservers' );
1103 return false;
1104 }
1105
1106 foreach( $memcServers as $server ) {
1107 $memcParts = explode( ":", $server, 2 );
1108 if ( !isset( $memcParts[0] )
1109 || ( !IP::isValid( $memcParts[0] )
1110 && ( gethostbyname( $memcParts[0] ) == $memcParts[0] ) ) ) {
1111 $this->parent->showError( 'config-memcache-badip', $memcParts[0] );
1112 return false;
1113 } elseif( !isset( $memcParts[1] ) ) {
1114 $this->parent->showError( 'config-memcache-noport', $memcParts[0] );
1115 return false;
1116 } elseif( $memcParts[1] < 1 || $memcParts[1] > 65535 ) {
1117 $this->parent->showError( 'config-memcache-badport', 1, 65535 );
1118 return false;
1119 }
1120 }
1121 }
1122 return true;
1123 }
1124
1125 }
1126
1127 class WebInstaller_Install extends WebInstallerPage {
1128 public function isSlow() {
1129 return true;
1130 }
1131
1132 public function execute() {
1133 if( $this->getVar( '_UpgradeDone' ) ) {
1134 return 'skip';
1135 } elseif( $this->getVar( '_InstallDone' ) ) {
1136 return 'continue';
1137 } elseif( $this->parent->request->wasPosted() ) {
1138 $this->startForm();
1139 $this->addHTML("<ul>");
1140 $results = $this->parent->performInstallation(
1141 array( $this, 'startStage'),
1142 array( $this, 'endStage' )
1143 );
1144 $this->addHTML("</ul>");
1145 // PerformInstallation bails on a fatal, so make sure the last item
1146 // completed before giving 'next.' Likewise, only provide back on failure
1147 $lastStep = end( $results );
1148 $continue = $lastStep->isOK() ? 'continue' : false;
1149 $back = $lastStep->isOK() ? false : 'back';
1150 $this->endForm( $continue, $back );
1151 } else {
1152 $this->startForm();
1153 $this->addHTML( $this->parent->getInfoBox( wfMsgNoTrans( 'config-install-begin' ) ) );
1154 $this->endForm();
1155 }
1156 return true;
1157 }
1158
1159 public function startStage( $step ) {
1160 $this->addHTML( "<li>" . wfMsgHtml( "config-install-$step" ) . wfMsg( 'ellipsis') );
1161 if ( $step == 'extension-tables' ) {
1162 $this->startLiveBox();
1163 }
1164 }
1165
1166 /**
1167 * @param $step
1168 * @param $status Status
1169 */
1170 public function endStage( $step, $status ) {
1171 if ( $step == 'extension-tables' ) {
1172 $this->endLiveBox();
1173 }
1174 $msg = $status->isOk() ? 'config-install-step-done' : 'config-install-step-failed';
1175 $html = wfMsgHtml( 'word-separator' ) . wfMsgHtml( $msg );
1176 if ( !$status->isOk() ) {
1177 $html = "<span class=\"error\">$html</span>";
1178 }
1179 $this->addHTML( $html . "</li>\n" );
1180 if( !$status->isGood() ) {
1181 $this->parent->showStatusBox( $status );
1182 }
1183 }
1184
1185 }
1186
1187 class WebInstaller_Complete extends WebInstallerPage {
1188
1189 public function execute() {
1190 // Pop up a dialog box, to make it difficult for the user to forget
1191 // to download the file
1192 $lsUrl = $this->getVar( 'wgServer' ) . $this->parent->getURL( array( 'localsettings' => 1 ) );
1193 if ( isset( $_SERVER['HTTP_USER_AGENT'] ) &&
1194 strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE' ) !== false ) {
1195 // JS appears the only method that works consistently with IE7+
1196 $this->addHtml( "\n<script type=\"" . $GLOBALS['wgJsMimeType'] .
1197 '">jQuery( document ).ready( function() { document.location=' .
1198 Xml::encodeJsVar( $lsUrl) . "; } );</script>\n" );
1199 } else {
1200 $this->parent->request->response()->header( "Refresh: 0;url=$lsUrl" );
1201 }
1202
1203 $this->startForm();
1204 $this->parent->disableLinkPopups();
1205 $this->addHTML(
1206 $this->parent->getInfoBox(
1207 wfMsgNoTrans( 'config-install-done',
1208 $lsUrl,
1209 $this->getVar( 'wgServer' ) .
1210 $this->getVar( 'wgScriptPath' ) . '/index' .
1211 $this->getVar( 'wgScriptExtension' ),
1212 '<downloadlink/>'
1213 ), 'tick-32.png'
1214 )
1215 );
1216 $this->parent->restoreLinkPopups();
1217 $this->endForm( false, false );
1218 }
1219 }
1220
1221 class WebInstaller_Restart extends WebInstallerPage {
1222
1223 public function execute() {
1224 $r = $this->parent->request;
1225 if ( $r->wasPosted() ) {
1226 $really = $r->getVal( 'submit-restart' );
1227 if ( $really ) {
1228 $this->parent->reset();
1229 }
1230 return 'continue';
1231 }
1232
1233 $this->startForm();
1234 $s = $this->parent->getWarningBox( wfMsgNoTrans( 'config-help-restart' ) );
1235 $this->addHTML( $s );
1236 $this->endForm( 'restart' );
1237 }
1238
1239 }
1240
1241 abstract class WebInstaller_Document extends WebInstallerPage {
1242
1243 protected abstract function getFileName();
1244
1245 public function execute() {
1246 $text = $this->getFileContents();
1247 $text = InstallDocFormatter::format( $text );
1248 $this->parent->output->addWikiText( $text );
1249 $this->startForm();
1250 $this->endForm( false );
1251 }
1252
1253 public function getFileContents() {
1254 $file = dirname( __FILE__ ) . '/../../' . $this->getFileName();
1255 if( ! file_exists( $file ) ) {
1256 return wfMsgNoTrans( 'config-nofile', $file );
1257 }
1258 return file_get_contents( $file );
1259 }
1260
1261 }
1262
1263 class WebInstaller_Readme extends WebInstaller_Document {
1264 protected function getFileName() { return 'README'; }
1265 }
1266
1267 class WebInstaller_ReleaseNotes extends WebInstaller_Document {
1268 protected function getFileName() {
1269 global $wgVersion;
1270
1271 if(! preg_match( '/^(\d+)\.(\d+).*/i', $wgVersion, $result ) ) {
1272 throw new MWException('Variable $wgVersion has an invalid value.');
1273 }
1274
1275 return 'RELEASE-NOTES-' . $result[1] . '.' . $result[2];
1276 }
1277 }
1278
1279 class WebInstaller_UpgradeDoc extends WebInstaller_Document {
1280 protected function getFileName() { return 'UPGRADE'; }
1281 }
1282
1283 class WebInstaller_Copying extends WebInstaller_Document {
1284 protected function getFileName() { return 'COPYING'; }
1285 }
1286