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