Allow the $result parameter of the isValidPassword hook to be an array if the message...
[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 $link = DatabaseBase::newFromType( $type )->getSoftwareLink();
388 $dbSupport .= wfMsgNoTrans( "config-support-$type", $link ) . "\n";
389 }
390 $this->addHTML( $this->parent->getInfoBox(
391 wfMsg( 'config-support-info', $dbSupport ) ) );
392
393 foreach ( $this->parent->getVar( '_CompiledDBs' ) as $type ) {
394 $installer = $this->parent->getDBInstaller( $type );
395 $types .=
396 '<li>' .
397 Xml::radioLabel(
398 $installer->getReadableName(),
399 'DBType',
400 $type,
401 "DBType_$type",
402 $type == $defaultType,
403 array( 'class' => 'dbRadio', 'rel' => "DB_wrapper_$type" )
404 ) .
405 "</li>\n";
406
407 $settings .=
408 Html::openElement( 'div', array( 'id' => 'DB_wrapper_' . $type, 'class' => 'dbWrapper' ) ) .
409 Html::element( 'h3', array(), wfMsg( 'config-header-' . $type ) ) .
410 $installer->getConnectForm() .
411 "</div>\n";
412 }
413 $types .= "</ul><br clear=\"left\"/>\n";
414
415 $this->addHTML(
416 $this->parent->label( 'config-db-type', false, $types ) .
417 $settings
418 );
419
420 $this->endForm();
421 }
422
423 public function submit() {
424 $r = $this->parent->request;
425 $type = $r->getVal( 'DBType' );
426 $this->setVar( 'wgDBtype', $type );
427 $installer = $this->parent->getDBInstaller( $type );
428 if ( !$installer ) {
429 return Status::newFatal( 'config-invalid-db-type' );
430 }
431 return $installer->submitConnectForm();
432 }
433
434 }
435
436 class WebInstaller_Upgrade extends WebInstallerPage {
437
438 public function execute() {
439 if ( $this->getVar( '_UpgradeDone' ) ) {
440 // Allow regeneration of LocalSettings.php, unless we are working
441 // from a pre-existing LocalSettings.php file and we want to avoid
442 // leaking its contents
443 if ( $this->parent->request->wasPosted() && !$this->getVar( '_ExistingDBSettings' ) ) {
444 // Done message acknowledged
445 return 'continue';
446 } else {
447 // Back button click
448 // Show the done message again
449 // Make them click back again if they want to do the upgrade again
450 $this->showDoneMessage();
451 return 'output';
452 }
453 }
454
455 // wgDBtype is generally valid here because otherwise the previous page
456 // (connect) wouldn't have declared its happiness
457 $type = $this->getVar( 'wgDBtype' );
458 $installer = $this->parent->getDBInstaller( $type );
459
460 if ( !$installer->needsUpgrade() ) {
461 return 'skip';
462 }
463
464 if ( $this->parent->request->wasPosted() ) {
465 $installer->preUpgrade();
466 $this->addHTML(
467 '<div id="config-spinner" style="display:none;"><img src="../skins/common/images/ajax-loader.gif" /></div>' .
468 '<script>jQuery( "#config-spinner" )[0].style.display = "block";</script>' .
469 '<textarea id="config-update-log" name="UpdateLog" rows="10" readonly="readonly">'
470 );
471 $this->parent->output->flush();
472 $result = $installer->doUpgrade();
473 $this->addHTML( '</textarea>
474 <script>jQuery( "#config-spinner" )[0].style.display = "none";</script>' );
475 $this->parent->output->flush();
476 if ( $result ) {
477 $this->setVar( '_UpgradeDone', true );
478 $this->showDoneMessage();
479 return 'output';
480 }
481 }
482
483 $this->startForm();
484 $this->addHTML( $this->parent->getInfoBox(
485 wfMsgNoTrans( 'config-can-upgrade', $GLOBALS['wgVersion'] ) ) );
486 $this->endForm();
487 }
488
489 public function showDoneMessage() {
490 $this->startForm();
491 $regenerate = !$this->getVar( '_ExistingDBSettings' );
492 if ( $regenerate ) {
493 $msg = 'config-upgrade-done';
494 } else {
495 $msg = 'config-upgrade-done-no-regenerate';
496 }
497 $this->parent->disableLinkPopups();
498 $this->addHTML(
499 $this->parent->getInfoBox(
500 wfMsgNoTrans( $msg,
501 $GLOBALS['wgServer'] .
502 $this->getVar( 'wgScriptPath' ) . '/index' .
503 $this->getVar( 'wgScriptExtension' )
504 ), 'tick-32.png'
505 )
506 );
507 $this->parent->restoreLinkPopups();
508 $this->endForm( $regenerate ? 'regenerate' : false, false );
509 }
510
511 }
512
513 class WebInstaller_DBSettings extends WebInstallerPage {
514
515 public function execute() {
516 $installer = $this->parent->getDBInstaller( $this->getVar( 'wgDBtype' ) );
517
518 $r = $this->parent->request;
519 if ( $r->wasPosted() ) {
520 $status = $installer->submitSettingsForm();
521 if ( $status === false ) {
522 return 'skip';
523 } elseif ( $status->isGood() ) {
524 return 'continue';
525 } else {
526 $this->parent->showStatusBox( $status );
527 }
528 }
529
530 $form = $installer->getSettingsForm();
531 if ( $form === false ) {
532 return 'skip';
533 }
534
535 $this->startForm();
536 $this->addHTML( $form );
537 $this->endForm();
538 }
539
540 }
541
542 class WebInstaller_Name extends WebInstallerPage {
543
544 public function execute() {
545 $r = $this->parent->request;
546 if ( $r->wasPosted() ) {
547 if ( $this->submit() ) {
548 return 'continue';
549 }
550 }
551
552 $this->startForm();
553
554 if ( $this->getVar( 'wgSitename' ) == $GLOBALS['wgSitename'] ) {
555 $this->setVar( 'wgSitename', '' );
556 }
557
558 // Set wgMetaNamespace to something valid before we show the form.
559 // $wgMetaNamespace defaults to $wgSiteName which is 'MediaWiki'
560 $metaNS = $this->getVar( 'wgMetaNamespace' );
561 $this->setVar( 'wgMetaNamespace', wfMsgForContent( 'config-ns-other-default' ) );
562
563 $this->addHTML(
564 $this->parent->getTextBox( array(
565 'var' => 'wgSitename',
566 'label' => 'config-site-name',
567 'help' => $this->parent->getHelpBox( 'config-site-name-help' )
568 ) ) .
569 $this->parent->getRadioSet( array(
570 'var' => '_NamespaceType',
571 'label' => 'config-project-namespace',
572 'itemLabelPrefix' => 'config-ns-',
573 'values' => array( 'site-name', 'generic', 'other' ),
574 'commonAttribs' => array( 'class' => 'enableForOther', 'rel' => 'config_wgMetaNamespace' ),
575 'help' => $this->parent->getHelpBox( 'config-project-namespace-help' )
576 ) ) .
577 $this->parent->getTextBox( array(
578 'var' => 'wgMetaNamespace',
579 'label' => '', //TODO: Needs a label?
580 'attribs' => array( 'readonly' => 'readonly', 'class' => 'enabledByOther' ),
581
582 ) ) .
583 $this->getFieldSetStart( 'config-admin-box' ) .
584 $this->parent->getTextBox( array(
585 'var' => '_AdminName',
586 'label' => 'config-admin-name',
587 'help' => $this->parent->getHelpBox( 'config-admin-help' )
588 ) ) .
589 $this->parent->getPasswordBox( array(
590 'var' => '_AdminPassword',
591 'label' => 'config-admin-password',
592 ) ) .
593 $this->parent->getPasswordBox( array(
594 'var' => '_AdminPassword2',
595 'label' => 'config-admin-password-confirm'
596 ) ) .
597 $this->parent->getTextBox( array(
598 'var' => '_AdminEmail',
599 'label' => 'config-admin-email',
600 'help' => $this->parent->getHelpBox( 'config-admin-email-help' )
601 ) ) .
602 $this->parent->getCheckBox( array(
603 'var' => '_Subscribe',
604 'label' => 'config-subscribe',
605 'help' => $this->parent->getHelpBox( 'config-subscribe-help' )
606 ) ) .
607 $this->getFieldSetEnd() .
608 $this->parent->getInfoBox( wfMsg( 'config-almost-done' ) ) .
609 $this->parent->getRadioSet( array(
610 'var' => '_SkipOptional',
611 'itemLabelPrefix' => 'config-optional-',
612 'values' => array( 'continue', 'skip' )
613 ) )
614 );
615
616 // Restore the default value
617 $this->setVar( 'wgMetaNamespace', $metaNS );
618
619 $this->endForm();
620 return 'output';
621 }
622
623 public function submit() {
624 $retVal = true;
625 $this->parent->setVarsFromRequest( array( 'wgSitename', '_NamespaceType',
626 '_AdminName', '_AdminPassword', '_AdminPassword2', '_AdminEmail',
627 '_Subscribe', '_SkipOptional' ) );
628
629 // Validate site name
630 if ( strval( $this->getVar( 'wgSitename' ) ) === '' ) {
631 $this->parent->showError( 'config-site-name-blank' );
632 $retVal = false;
633 }
634
635 // Fetch namespace
636 $nsType = $this->getVar( '_NamespaceType' );
637 if ( $nsType == 'site-name' ) {
638 $name = $this->getVar( 'wgSitename' );
639 // Sanitize for namespace
640 // This algorithm should match the JS one in WebInstallerOutput.php
641 $name = preg_replace( '/[\[\]\{\}|#<>%+? ]/', '_', $name );
642 $name = str_replace( '&', '&amp;', $name );
643 $name = preg_replace( '/__+/', '_', $name );
644 $name = ucfirst( trim( $name, '_' ) );
645 } elseif ( $nsType == 'generic' ) {
646 $name = wfMsg( 'config-ns-generic' );
647 } else { // other
648 $name = $this->getVar( 'wgMetaNamespace' );
649 }
650
651 // Validate namespace
652 if ( strpos( $name, ':' ) !== false ) {
653 $good = false;
654 } else {
655 // Title-style validation
656 $title = Title::newFromText( $name );
657 if ( !$title ) {
658 $good = $nsType == 'site-name';
659 } else {
660 $name = $title->getDBkey();
661 $good = true;
662 }
663 }
664 if ( !$good ) {
665 $this->parent->showError( 'config-ns-invalid', $name );
666 $retVal = false;
667 }
668 $this->setVar( 'wgMetaNamespace', $name );
669
670 // Validate username for creation
671 $name = $this->getVar( '_AdminName' );
672 if ( strval( $name ) === '' ) {
673 $this->parent->showError( 'config-admin-name-blank' );
674 $cname = $name;
675 $retVal = false;
676 } else {
677 $cname = User::getCanonicalName( $name, 'creatable' );
678 if ( $cname === false ) {
679 $this->parent->showError( 'config-admin-name-invalid', $name );
680 $retVal = false;
681 } else {
682 $this->setVar( '_AdminName', $cname );
683 }
684 }
685
686 // Validate password
687 $msg = false;
688 $valid = false;
689 $pwd = $this->getVar( '_AdminPassword' );
690 $user = User::newFromName( $cname );
691 $valid = $user && $user->getPasswordValidity( $pwd );
692 if ( strval( $pwd ) === '' ) {
693 # $user->getPasswordValidity just checks for $wgMinimalPasswordLength.
694 # This message is more specific and helpful.
695 $msg = 'config-admin-password-blank';
696 } elseif ( $pwd !== $this->getVar( '_AdminPassword2' ) ) {
697 $msg = 'config-admin-password-mismatch';
698 } elseif ( $valid !== true ) {
699 # As of writing this will only catch the username being e.g. 'FOO' and
700 # the password 'foo'
701 $msg = $valid;
702 }
703 if ( $msg !== false ) {
704 call_user_func_array( array( $this->parent, 'showError' ), (array)$msg );
705 $this->setVar( '_AdminPassword', '' );
706 $this->setVar( '_AdminPassword2', '' );
707 $retVal = false;
708 }
709
710 // Validate e-mail if provided
711 $email = $this->getVar( '_AdminEmail' );
712 if( $email && !User::isValidEmailAddr( $email ) ) {
713 $this->parent->showError( 'config-admin-error-bademail' );
714 $retVal = false;
715 }
716
717 return $retVal;
718 }
719
720 }
721
722 class WebInstaller_Options extends WebInstallerPage {
723
724 public function execute() {
725 if ( $this->getVar( '_SkipOptional' ) == 'skip' ) {
726 return 'skip';
727 }
728 if ( $this->parent->request->wasPosted() ) {
729 if ( $this->submit() ) {
730 return 'continue';
731 }
732 }
733
734 $this->startForm();
735 $this->addHTML(
736 # User Rights
737 $this->parent->getRadioSet( array(
738 'var' => '_RightsProfile',
739 'label' => 'config-profile',
740 'itemLabelPrefix' => 'config-profile-',
741 'values' => array_keys( $this->parent->rightsProfiles ),
742 ) ) .
743 $this->parent->getHelpBox( 'config-profile-help' ) .
744
745 # Licensing
746 $this->parent->getRadioSet( array(
747 'var' => '_LicenseCode',
748 'label' => 'config-license',
749 'itemLabelPrefix' => 'config-license-',
750 'values' => array_keys( $this->parent->licenses ),
751 'commonAttribs' => array( 'class' => 'licenseRadio' ),
752 ) ) .
753 $this->getCCChooser() .
754 $this->parent->getHelpBox( 'config-license-help' ) .
755
756 # E-mail
757 $this->getFieldSetStart( 'config-email-settings' ) .
758 $this->parent->getCheckBox( array(
759 'var' => 'wgEnableEmail',
760 'label' => 'config-enable-email',
761 'attribs' => array( 'class' => 'showHideRadio', 'rel' => 'emailwrapper' ),
762 ) ) .
763 $this->parent->getHelpBox( 'config-enable-email-help' ) .
764 "<div id=\"emailwrapper\">" .
765 $this->parent->getTextBox( array(
766 'var' => 'wgPasswordSender',
767 'label' => 'config-email-sender'
768 ) ) .
769 $this->parent->getHelpBox( 'config-email-sender-help' ) .
770 $this->parent->getCheckBox( array(
771 'var' => 'wgEnableUserEmail',
772 'label' => 'config-email-user',
773 ) ) .
774 $this->parent->getHelpBox( 'config-email-user-help' ) .
775 $this->parent->getCheckBox( array(
776 'var' => 'wgEnotifUserTalk',
777 'label' => 'config-email-usertalk',
778 ) ) .
779 $this->parent->getHelpBox( 'config-email-usertalk-help' ) .
780 $this->parent->getCheckBox( array(
781 'var' => 'wgEnotifWatchlist',
782 'label' => 'config-email-watchlist',
783 ) ) .
784 $this->parent->getHelpBox( 'config-email-watchlist-help' ) .
785 $this->parent->getCheckBox( array(
786 'var' => 'wgEmailAuthentication',
787 'label' => 'config-email-auth',
788 ) ) .
789 $this->parent->getHelpBox( 'config-email-auth-help' ) .
790 "</div>" .
791 $this->getFieldSetEnd()
792 );
793
794 $extensions = $this->parent->findExtensions();
795
796 if( $extensions ) {
797 $extHtml = $this->getFieldSetStart( 'config-extensions' );
798
799 foreach( $extensions as $ext ) {
800 $extHtml .= $this->parent->getCheckBox( array(
801 'var' => "ext-$ext",
802 'rawtext' => $ext,
803 ) );
804 }
805
806 $extHtml .= $this->parent->getHelpBox( 'config-extensions-help' ) .
807 $this->getFieldSetEnd();
808 $this->addHTML( $extHtml );
809 }
810
811 // Having / in paths in Windows looks funny :)
812 $this->setVar( 'wgDeletedDirectory',
813 str_replace(
814 '/', DIRECTORY_SEPARATOR,
815 $this->getVar( 'wgDeletedDirectory' )
816 )
817 );
818
819 $this->addHTML(
820 # Uploading
821 $this->getFieldSetStart( 'config-upload-settings' ) .
822 $this->parent->getCheckBox( array(
823 'var' => 'wgEnableUploads',
824 'label' => 'config-upload-enable',
825 'attribs' => array( 'class' => 'showHideRadio', 'rel' => 'uploadwrapper' ),
826 'help' => $this->parent->getHelpBox( 'config-upload-help' )
827 ) ) .
828 '<div id="uploadwrapper" style="display: none;">' .
829 $this->parent->getTextBox( array(
830 'var' => 'wgDeletedDirectory',
831 'label' => 'config-upload-deleted',
832 'help' => $this->parent->getHelpBox( 'config-upload-deleted-help' )
833 ) ) .
834 '</div>' .
835 $this->parent->getTextBox( array(
836 'var' => 'wgLogo',
837 'label' => 'config-logo',
838 'help' => $this->parent->getHelpBox( 'config-logo-help' )
839 ) )
840 );
841 $this->addHTML(
842 $this->parent->getCheckBox( array(
843 'var' => 'wgUseInstantCommons',
844 'label' => 'config-instantcommons',
845 'help' => $this->parent->getHelpBox( 'config-instantcommons-help' )
846 ) ) .
847 $this->getFieldSetEnd()
848 );
849
850 $caches = array( 'none' );
851 if( count( $this->getVar( '_Caches' ) ) ) {
852 $caches[] = 'accel';
853 }
854 $caches[] = 'memcached';
855
856 $this->addHTML(
857 # Advanced settings
858 $this->getFieldSetStart( 'config-advanced-settings' ) .
859 # Object cache settings
860 $this->parent->getRadioSet( array(
861 'var' => 'wgMainCacheType',
862 'label' => 'config-cache-options',
863 'itemLabelPrefix' => 'config-cache-',
864 'values' => $caches,
865 'value' => 'none',
866 ) ) .
867 $this->parent->getHelpBox( 'config-cache-help' ) .
868 '<div id="config-memcachewrapper">' .
869 $this->parent->getTextBox( array(
870 'var' => '_MemCachedServers',
871 'label' => 'config-memcached-servers',
872 'help' => $this->parent->getHelpBox( 'config-memcached-help' )
873 ) ) .
874 '</div>' .
875 $this->getFieldSetEnd()
876 );
877 $this->endForm();
878 }
879
880 public function getCCPartnerUrl() {
881 global $wgServer;
882 $exitUrl = $wgServer . $this->parent->getUrl( array(
883 'page' => 'Options',
884 'SubmitCC' => 'indeed',
885 'config__LicenseCode' => 'cc',
886 'config_wgRightsUrl' => '[license_url]',
887 'config_wgRightsText' => '[license_name]',
888 'config_wgRightsIcon' => '[license_button]',
889 ) );
890 $styleUrl = $wgServer . dirname( dirname( $this->parent->getUrl() ) ) .
891 '/skins/common/config-cc.css';
892 $iframeUrl = 'http://creativecommons.org/license/?' .
893 wfArrayToCGI( array(
894 'partner' => 'MediaWiki',
895 'exit_url' => $exitUrl,
896 'lang' => $this->getVar( '_UserLang' ),
897 'stylesheet' => $styleUrl,
898 ) );
899 return $iframeUrl;
900 }
901
902 public function getCCChooser() {
903 $iframeAttribs = array(
904 'class' => 'config-cc-iframe',
905 'name' => 'config-cc-iframe',
906 'id' => 'config-cc-iframe',
907 'frameborder' => 0,
908 'width' => '100%',
909 'height' => '100%',
910 );
911 if ( $this->getVar( '_CCDone' ) ) {
912 $iframeAttribs['src'] = $this->parent->getUrl( array( 'ShowCC' => 'yes' ) );
913 } else {
914 $iframeAttribs['src'] = $this->getCCPartnerUrl();
915 }
916
917 return
918 "<div class=\"config-cc-wrapper\" id=\"config-cc-wrapper\" style=\"display: none;\">\n" .
919 Html::element( 'iframe', $iframeAttribs, '', false /* not short */ ) .
920 "</div>\n";
921 }
922
923 public function getCCDoneBox() {
924 $js = "parent.document.getElementById('config-cc-wrapper').style.height = '$1';";
925 // If you change this height, also change it in config.css
926 $expandJs = str_replace( '$1', '54em', $js );
927 $reduceJs = str_replace( '$1', '70px', $js );
928 return
929 '<p>'.
930 Html::element( 'img', array( 'src' => $this->getVar( 'wgRightsIcon' ) ) ) .
931 '&#160;&#160;' .
932 htmlspecialchars( $this->getVar( 'wgRightsText' ) ) .
933 "</p>\n" .
934 "<p style=\"text-align: center\">" .
935 Html::element( 'a',
936 array(
937 'href' => $this->getCCPartnerUrl(),
938 'onclick' => $expandJs,
939 ),
940 wfMsg( 'config-cc-again' )
941 ) .
942 "</p>\n" .
943 "<script type=\"text/javascript\">\n" .
944 # Reduce the wrapper div height
945 htmlspecialchars( $reduceJs ) .
946 "\n" .
947 "</script>\n";
948 }
949
950 public function submitCC() {
951 $newValues = $this->parent->setVarsFromRequest(
952 array( 'wgRightsUrl', 'wgRightsText', 'wgRightsIcon' ) );
953 if ( count( $newValues ) != 3 ) {
954 $this->parent->showError( 'config-cc-error' );
955 return;
956 }
957 $this->setVar( '_CCDone', true );
958 $this->addHTML( $this->getCCDoneBox() );
959 }
960
961 public function submit() {
962 $this->parent->setVarsFromRequest( array( '_RightsProfile', '_LicenseCode',
963 'wgEnableEmail', 'wgPasswordSender', 'wgEnableUploads', 'wgLogo',
964 'wgEnableUserEmail', 'wgEnotifUserTalk', 'wgEnotifWatchlist',
965 'wgEmailAuthentication', 'wgMainCacheType', '_MemCachedServers',
966 'wgUseInstantCommons' ) );
967
968 if ( !in_array( $this->getVar( '_RightsProfile' ),
969 array_keys( $this->parent->rightsProfiles ) ) )
970 {
971 reset( $this->parent->rightsProfiles );
972 $this->setVar( '_RightsProfile', key( $this->parent->rightsProfiles ) );
973 }
974
975 $code = $this->getVar( '_LicenseCode' );
976 if ( $code == 'cc-choose' ) {
977 if ( !$this->getVar( '_CCDone' ) ) {
978 $this->parent->showError( 'config-cc-not-chosen' );
979 return false;
980 }
981 } elseif ( in_array( $code, array_keys( $this->parent->licenses ) ) ) {
982 $entry = $this->parent->licenses[$code];
983 if ( isset( $entry['text'] ) ) {
984 $this->setVar( 'wgRightsText', $entry['text'] );
985 } else {
986 $this->setVar( 'wgRightsText', wfMsg( 'config-license-' . $code ) );
987 }
988 $this->setVar( 'wgRightsUrl', $entry['url'] );
989 $this->setVar( 'wgRightsIcon', $entry['icon'] );
990 } else {
991 $this->setVar( 'wgRightsText', '' );
992 $this->setVar( 'wgRightsUrl', '' );
993 $this->setVar( 'wgRightsIcon', '' );
994 }
995
996 $extsAvailable = $this->parent->findExtensions();
997 $extsToInstall = array();
998 foreach( $extsAvailable as $ext ) {
999 if( $this->parent->request->getCheck( 'config_ext-' . $ext ) ) {
1000 $extsToInstall[] = $ext;
1001 }
1002 }
1003 $this->parent->setVar( '_Extensions', $extsToInstall );
1004 return true;
1005 }
1006
1007 }
1008
1009 class WebInstaller_Install extends WebInstallerPage {
1010
1011 public function execute() {
1012 if( $this->parent->request->wasPosted() ) {
1013 return 'continue';
1014 } elseif( $this->getVar( '_InstallDone' ) ) {
1015 $this->startForm();
1016 $status = new Status();
1017 $status->warning( 'config-install-alreadydone' );
1018 $this->parent->showStatusBox( $status );
1019 } elseif( $this->getVar( '_UpgradeDone' ) ) {
1020 return 'skip';
1021 } else {
1022 $this->startForm();
1023 $this->addHTML("<ul>");
1024 $this->parent->performInstallation(
1025 array( $this, 'startStage'),
1026 array( $this, 'endStage' )
1027 );
1028 $this->addHTML("</ul>");
1029 }
1030 $this->endForm();
1031 return true;
1032 }
1033
1034 public function startStage( $step ) {
1035 $this->addHTML( "<li>" . wfMsgHtml( "config-install-$step" ) . wfMsg( 'ellipsis') );
1036 }
1037
1038 public function endStage( $step, $status ) {
1039 $msg = $status->isOk() ? 'config-install-step-done' : 'config-install-step-failed';
1040 $html = wfMsgHtml( 'word-separator' ) . wfMsgHtml( $msg );
1041 if ( !$status->isOk() ) {
1042 $html = "<span class=\"error\">$html</span>";
1043 }
1044 $this->addHTML( $html . "</li>\n" );
1045 if( !$status->isGood() ) {
1046 $this->parent->showStatusBox( $status );
1047 }
1048 }
1049
1050 }
1051
1052 class WebInstaller_Complete extends WebInstallerPage {
1053
1054 public function execute() {
1055 // Pop up a dialog box, to make it difficult for the user to forget
1056 // to download the file
1057 $lsUrl = $GLOBALS['wgServer'] . $this->parent->getURL( array( 'localsettings' => 1 ) );
1058 $this->parent->request->response()->header( "Refresh: 0;url=$lsUrl" );
1059
1060 $this->startForm();
1061 $this->parent->disableLinkPopups();
1062 $this->addHTML(
1063 $this->parent->getInfoBox(
1064 wfMsgNoTrans( 'config-install-done',
1065 $lsUrl,
1066 $GLOBALS['wgServer'] .
1067 $this->getVar( 'wgScriptPath' ) . '/index' .
1068 $this->getVar( 'wgScriptExtension' ),
1069 '<downloadlink/>'
1070 ), 'tick-32.png'
1071 )
1072 );
1073 $this->parent->restoreLinkPopups();
1074 $this->endForm( false, false );
1075 }
1076 }
1077
1078 class WebInstaller_Restart extends WebInstallerPage {
1079
1080 public function execute() {
1081 $r = $this->parent->request;
1082 if ( $r->wasPosted() ) {
1083 $really = $r->getVal( 'submit-restart' );
1084 if ( $really ) {
1085 $this->parent->session = array();
1086 $this->parent->happyPages = array();
1087 $this->parent->settings = array();
1088 }
1089 return 'continue';
1090 }
1091
1092 $this->startForm();
1093 $s = $this->parent->getWarningBox( wfMsgNoTrans( 'config-help-restart' ) );
1094 $this->addHTML( $s );
1095 $this->endForm( 'restart' );
1096 }
1097
1098 }
1099
1100 abstract class WebInstaller_Document extends WebInstallerPage {
1101
1102 protected abstract function getFileName();
1103
1104 public function execute() {
1105 $text = $this->getFileContents();
1106 $text = $this->formatTextFile( $text );
1107 $this->parent->output->addWikiText( $text );
1108 $this->startForm();
1109 $this->endForm( false );
1110 }
1111
1112 public function getFileContents() {
1113 return file_get_contents( dirname( __FILE__ ) . '/../../' . $this->getFileName() );
1114 }
1115
1116 protected function formatTextFile( $text ) {
1117 // Use Unix line endings, escape some wikitext stuff
1118 $text = str_replace( array( '<', '{{', '[[', "\r" ),
1119 array( '&lt;', '&#123;&#123;', '&#91;&#91;', '' ), $text );
1120 // join word-wrapped lines into one
1121 do {
1122 $prev = $text;
1123 $text = preg_replace( "/\n([\\*#\t])([^\n]*?)\n([^\n#\\*:]+)/", "\n\\1\\2 \\3", $text );
1124 } while ( $text != $prev );
1125 // Replace tab indents with colons
1126 $text = preg_replace( '/^\t\t/m', '::', $text );
1127 $text = preg_replace( '/^\t/m', ':', $text );
1128 // turn (bug nnnn) into links
1129 $text = preg_replace_callback('/bug (\d+)/', array( $this, 'replaceBugLinks' ), $text );
1130 // add links to manual to every global variable mentioned
1131 $text = preg_replace_callback('/(\$wg[a-z0-9_]+)/i', array( $this, 'replaceConfigLinks' ), $text );
1132 return $text;
1133 }
1134
1135 private function replaceBugLinks( $matches ) {
1136 return '<span class="config-plainlink">[https://bugzilla.wikimedia.org/' .
1137 $matches[1] . ' bug ' . $matches[1] . ']</span>';
1138 }
1139
1140 private function replaceConfigLinks( $matches ) {
1141 return '<span class="config-plainlink">[http://www.mediawiki.org/wiki/Manual:' .
1142 $matches[1] . ' ' . $matches[1] . ']</span>';
1143 }
1144
1145 }
1146
1147 class WebInstaller_Readme extends WebInstaller_Document {
1148 protected function getFileName() { return 'README'; }
1149 }
1150
1151 class WebInstaller_ReleaseNotes extends WebInstaller_Document {
1152 protected function getFileName() { return 'RELEASE-NOTES'; }
1153 }
1154
1155 class WebInstaller_UpgradeDoc extends WebInstaller_Document {
1156 protected function getFileName() { return 'UPGRADE'; }
1157 }
1158
1159 class WebInstaller_Copying extends WebInstaller_Document {
1160 protected function getFileName() { return 'COPYING'; }
1161 }
1162