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