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