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