Merge "Follow-up I0b781c11 (2a55449): use User::getAutomaticGroups()."
[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( wfMessage( "config-$continue" )->text(),
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( wfMessage( "config-$back" )->text(),
94 array(
95 'name' => "submit-$back",
96 'tabindex' => $this->parent->nextTabIndex()
97 ) ) . "\n";
98 }
99
100 if ( $continue ) {
101 $s .= Xml::submitButton( wfMessage( "config-$continue" )->text(),
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>" . wfMessage( $legend )->escaped() . "</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 wfMessage( 'config-upgrade-key-missing', "<pre dir=\"ltr\">\$wgUpgradeKey = '" .
299 $this->getVar( 'wgUpgradeKey' ) . "';</pre>" )->plain()
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( wfMessage( 'config-localsettings-upgrade' )->plain() ).
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( wfMessage( 'config-welcome' )->plain() );
414 $status = $this->parent->doEnvironmentChecks();
415 if ( $status->isGood() ) {
416 $this->parent->output->addHTML( '<span class="success-message">' .
417 wfMessage( 'config-env-good' )->escaped() . '</span>' );
418 $this->parent->output->addWikiText( wfMessage( 'config-copyright',
419 SpecialVersion::getCopyrightAndAuthorList() )->plain() );
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 .= wfMessage( "config-support-$type", $link )->plain() . "\n";
458 }
459 $this->addHTML( $this->parent->getInfoBox(
460 wfMessage( 'config-support-info', trim( $dbSupport ) )->text() ) );
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(), wfMessage( 'config-header-' . $type )->text() ) .
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 wfMessage( 'config-can-upgrade', $GLOBALS['wgVersion'] )->plain() ) );
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 wfMessage( $msg,
574 $this->getVar( 'wgServer' ) .
575 $this->getVar( 'wgScriptPath' ) . '/index' .
576 $this->getVar( 'wgScriptExtension' )
577 )->plain(), '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(
639 'wgMetaNamespace',
640 wfMessage( 'config-ns-other-default' )->inContentLanguage()->text()
641 );
642
643 $this->addHTML(
644 $this->parent->getTextBox( array(
645 'var' => 'wgSitename',
646 'label' => 'config-site-name',
647 'help' => $this->parent->getHelpBox( 'config-site-name-help' )
648 ) ) .
649 $this->parent->getRadioSet( array(
650 'var' => '_NamespaceType',
651 'label' => 'config-project-namespace',
652 'itemLabelPrefix' => 'config-ns-',
653 'values' => array( 'site-name', 'generic', 'other' ),
654 'commonAttribs' => array( 'class' => 'enableForOther',
655 'rel' => 'config_wgMetaNamespace' ),
656 'help' => $this->parent->getHelpBox( 'config-project-namespace-help' )
657 ) ) .
658 $this->parent->getTextBox( array(
659 'var' => 'wgMetaNamespace',
660 'label' => '', //TODO: Needs a label?
661 'attribs' => array( 'readonly' => 'readonly', 'class' => 'enabledByOther' ),
662
663 ) ) .
664 $this->getFieldSetStart( 'config-admin-box' ) .
665 $this->parent->getTextBox( array(
666 'var' => '_AdminName',
667 'label' => 'config-admin-name',
668 'help' => $this->parent->getHelpBox( 'config-admin-help' )
669 ) ) .
670 $this->parent->getPasswordBox( array(
671 'var' => '_AdminPassword',
672 'label' => 'config-admin-password',
673 ) ) .
674 $this->parent->getPasswordBox( array(
675 'var' => '_AdminPassword2',
676 'label' => 'config-admin-password-confirm'
677 ) ) .
678 $this->parent->getTextBox( array(
679 'var' => '_AdminEmail',
680 'label' => 'config-admin-email',
681 'help' => $this->parent->getHelpBox( 'config-admin-email-help' )
682 ) ) .
683 $this->parent->getCheckBox( array(
684 'var' => '_Subscribe',
685 'label' => 'config-subscribe',
686 'help' => $this->parent->getHelpBox( 'config-subscribe-help' )
687 ) ) .
688 $this->getFieldSetEnd() .
689 $this->parent->getInfoBox( wfMessage( 'config-almost-done' )->text() ) .
690 $this->parent->getRadioSet( array(
691 'var' => '_SkipOptional',
692 'itemLabelPrefix' => 'config-optional-',
693 'values' => array( 'continue', 'skip' )
694 ) )
695 );
696
697 // Restore the default value
698 $this->setVar( 'wgMetaNamespace', $metaNS );
699
700 $this->endForm();
701 return 'output';
702 }
703
704 public function submit() {
705 $retVal = true;
706 $this->parent->setVarsFromRequest( array( 'wgSitename', '_NamespaceType',
707 '_AdminName', '_AdminPassword', '_AdminPassword2', '_AdminEmail',
708 '_Subscribe', '_SkipOptional', 'wgMetaNamespace' ) );
709
710 // Validate site name
711 if ( strval( $this->getVar( 'wgSitename' ) ) === '' ) {
712 $this->parent->showError( 'config-site-name-blank' );
713 $retVal = false;
714 }
715
716 // Fetch namespace
717 $nsType = $this->getVar( '_NamespaceType' );
718 if ( $nsType == 'site-name' ) {
719 $name = $this->getVar( 'wgSitename' );
720 // Sanitize for namespace
721 // This algorithm should match the JS one in WebInstallerOutput.php
722 $name = preg_replace( '/[\[\]\{\}|#<>%+? ]/', '_', $name );
723 $name = str_replace( '&', '&amp;', $name );
724 $name = preg_replace( '/__+/', '_', $name );
725 $name = ucfirst( trim( $name, '_' ) );
726 } elseif ( $nsType == 'generic' ) {
727 $name = wfMessage( 'config-ns-generic' )->text();
728 } else { // other
729 $name = $this->getVar( 'wgMetaNamespace' );
730 }
731
732 // Validate namespace
733 if ( strpos( $name, ':' ) !== false ) {
734 $good = false;
735 } else {
736 // Title-style validation
737 $title = Title::newFromText( $name );
738 if ( !$title ) {
739 $good = $nsType == 'site-name';
740 } else {
741 $name = $title->getDBkey();
742 $good = true;
743 }
744 }
745 if ( !$good ) {
746 $this->parent->showError( 'config-ns-invalid', $name );
747 $retVal = false;
748 }
749
750 // Make sure it won't conflict with any existing namespaces
751 global $wgContLang;
752 $nsIndex = $wgContLang->getNsIndex( $name );
753 if( $nsIndex !== false && $nsIndex !== NS_PROJECT ) {
754 $this->parent->showError( 'config-ns-conflict', $name );
755 $retVal = false;
756 }
757
758 $this->setVar( 'wgMetaNamespace', $name );
759
760 // Validate username for creation
761 $name = $this->getVar( '_AdminName' );
762 if ( strval( $name ) === '' ) {
763 $this->parent->showError( 'config-admin-name-blank' );
764 $cname = $name;
765 $retVal = false;
766 } else {
767 $cname = User::getCanonicalName( $name, 'creatable' );
768 if ( $cname === false ) {
769 $this->parent->showError( 'config-admin-name-invalid', $name );
770 $retVal = false;
771 } else {
772 $this->setVar( '_AdminName', $cname );
773 }
774 }
775
776 // Validate password
777 $msg = false;
778 $pwd = $this->getVar( '_AdminPassword' );
779 $user = User::newFromName( $cname );
780 $valid = $user && $user->getPasswordValidity( $pwd );
781 if ( strval( $pwd ) === '' ) {
782 # $user->getPasswordValidity just checks for $wgMinimalPasswordLength.
783 # This message is more specific and helpful.
784 $msg = 'config-admin-password-blank';
785 } elseif ( $pwd !== $this->getVar( '_AdminPassword2' ) ) {
786 $msg = 'config-admin-password-mismatch';
787 } elseif ( $valid !== true ) {
788 # As of writing this will only catch the username being e.g. 'FOO' and
789 # the password 'foo'
790 $msg = $valid;
791 }
792 if ( $msg !== false ) {
793 call_user_func_array( array( $this->parent, 'showError' ), (array)$msg );
794 $this->setVar( '_AdminPassword', '' );
795 $this->setVar( '_AdminPassword2', '' );
796 $retVal = false;
797 }
798
799 // Validate e-mail if provided
800 $email = $this->getVar( '_AdminEmail' );
801 if( $email && !Sanitizer::validateEmail( $email ) ) {
802 $this->parent->showError( 'config-admin-error-bademail' );
803 $retVal = false;
804 }
805 // If they asked to subscribe to mediawiki-announce but didn't give
806 // an e-mail, show an error. Bug 29332
807 if( !$email && $this->getVar( '_Subscribe' ) ) {
808 $this->parent->showError( 'config-subscribe-noemail' );
809 $retVal = false;
810 }
811
812 return $retVal;
813 }
814
815 }
816
817 class WebInstaller_Options extends WebInstallerPage {
818
819 public function execute() {
820 if ( $this->getVar( '_SkipOptional' ) == 'skip' ) {
821 return 'skip';
822 }
823 if ( $this->parent->request->wasPosted() ) {
824 if ( $this->submit() ) {
825 return 'continue';
826 }
827 }
828
829 $emailwrapperStyle = $this->getVar( 'wgEnableEmail' ) ? '' : 'display: none';
830 $this->startForm();
831 $this->addHTML(
832 # User Rights
833 $this->parent->getRadioSet( array(
834 'var' => '_RightsProfile',
835 'label' => 'config-profile',
836 'itemLabelPrefix' => 'config-profile-',
837 'values' => array_keys( $this->parent->rightsProfiles ),
838 ) ) .
839 $this->parent->getInfoBox( wfMessage( 'config-profile-help' )->plain() ) .
840
841 # Licensing
842 $this->parent->getRadioSet( array(
843 'var' => '_LicenseCode',
844 'label' => 'config-license',
845 'itemLabelPrefix' => 'config-license-',
846 'values' => array_keys( $this->parent->licenses ),
847 'commonAttribs' => array( 'class' => 'licenseRadio' ),
848 ) ) .
849 $this->getCCChooser() .
850 $this->parent->getHelpBox( 'config-license-help' ) .
851
852 # E-mail
853 $this->getFieldSetStart( 'config-email-settings' ) .
854 $this->parent->getCheckBox( array(
855 'var' => 'wgEnableEmail',
856 'label' => 'config-enable-email',
857 'attribs' => array( 'class' => 'showHideRadio', 'rel' => 'emailwrapper' ),
858 ) ) .
859 $this->parent->getHelpBox( 'config-enable-email-help' ) .
860 "<div id=\"emailwrapper\" style=\"$emailwrapperStyle\">" .
861 $this->parent->getTextBox( array(
862 'var' => 'wgPasswordSender',
863 'label' => 'config-email-sender'
864 ) ) .
865 $this->parent->getHelpBox( 'config-email-sender-help' ) .
866 $this->parent->getCheckBox( array(
867 'var' => 'wgEnableUserEmail',
868 'label' => 'config-email-user',
869 ) ) .
870 $this->parent->getHelpBox( 'config-email-user-help' ) .
871 $this->parent->getCheckBox( array(
872 'var' => 'wgEnotifUserTalk',
873 'label' => 'config-email-usertalk',
874 ) ) .
875 $this->parent->getHelpBox( 'config-email-usertalk-help' ) .
876 $this->parent->getCheckBox( array(
877 'var' => 'wgEnotifWatchlist',
878 'label' => 'config-email-watchlist',
879 ) ) .
880 $this->parent->getHelpBox( 'config-email-watchlist-help' ) .
881 $this->parent->getCheckBox( array(
882 'var' => 'wgEmailAuthentication',
883 'label' => 'config-email-auth',
884 ) ) .
885 $this->parent->getHelpBox( 'config-email-auth-help' ) .
886 "</div>" .
887 $this->getFieldSetEnd()
888 );
889
890 $extensions = $this->parent->findExtensions();
891
892 if( $extensions ) {
893 $extHtml = $this->getFieldSetStart( 'config-extensions' );
894
895 foreach( $extensions as $ext ) {
896 $extHtml .= $this->parent->getCheckBox( array(
897 'var' => "ext-$ext",
898 'rawtext' => $ext,
899 ) );
900 }
901
902 $extHtml .= $this->parent->getHelpBox( 'config-extensions-help' ) .
903 $this->getFieldSetEnd();
904 $this->addHTML( $extHtml );
905 }
906
907 // Having / in paths in Windows looks funny :)
908 $this->setVar( 'wgDeletedDirectory',
909 str_replace(
910 '/', DIRECTORY_SEPARATOR,
911 $this->getVar( 'wgDeletedDirectory' )
912 )
913 );
914
915 $uploadwrapperStyle = $this->getVar( 'wgEnableUploads' ) ? '' : 'display: none';
916 $this->addHTML(
917 # Uploading
918 $this->getFieldSetStart( 'config-upload-settings' ) .
919 $this->parent->getCheckBox( array(
920 'var' => 'wgEnableUploads',
921 'label' => 'config-upload-enable',
922 'attribs' => array( 'class' => 'showHideRadio', 'rel' => 'uploadwrapper' ),
923 'help' => $this->parent->getHelpBox( 'config-upload-help' )
924 ) ) .
925 '<div id="uploadwrapper" style="' . $uploadwrapperStyle . '">' .
926 $this->parent->getTextBox( array(
927 'var' => 'wgDeletedDirectory',
928 'label' => 'config-upload-deleted',
929 'attribs' => array( 'dir' => 'ltr' ),
930 'help' => $this->parent->getHelpBox( 'config-upload-deleted-help' )
931 ) ) .
932 '</div>' .
933 $this->parent->getTextBox( array(
934 'var' => 'wgLogo',
935 'label' => 'config-logo',
936 'attribs' => array( 'dir' => 'ltr' ),
937 'help' => $this->parent->getHelpBox( 'config-logo-help' )
938 ) )
939 );
940 $this->addHTML(
941 $this->parent->getCheckBox( array(
942 'var' => 'wgUseInstantCommons',
943 'label' => 'config-instantcommons',
944 'help' => $this->parent->getHelpBox( 'config-instantcommons-help' )
945 ) ) .
946 $this->getFieldSetEnd()
947 );
948
949 $caches = array( 'none' );
950 if( count( $this->getVar( '_Caches' ) ) ) {
951 $caches[] = 'accel';
952 }
953 $caches[] = 'memcached';
954
955 // We'll hide/show this on demand when the value changes, see config.js.
956 $cacheval = $this->getVar( 'wgMainCacheType' );
957 if (!$cacheval) {
958 // We need to set a default here; but don't hardcode it
959 // or we lose it every time we reload the page for validation
960 // or going back!
961 $cacheval = 'none';
962 }
963 $hidden = ($cacheval == 'memcached') ? '' : 'display: none';
964 $this->addHTML(
965 # Advanced settings
966 $this->getFieldSetStart( 'config-advanced-settings' ) .
967 # Object cache settings
968 $this->parent->getRadioSet( array(
969 'var' => 'wgMainCacheType',
970 'label' => 'config-cache-options',
971 'itemLabelPrefix' => 'config-cache-',
972 'values' => $caches,
973 'value' => $cacheval,
974 ) ) .
975 $this->parent->getHelpBox( 'config-cache-help' ) .
976 "<div id=\"config-memcachewrapper\" style=\"$hidden\">" .
977 $this->parent->getTextArea( array(
978 'var' => '_MemCachedServers',
979 'label' => 'config-memcached-servers',
980 'help' => $this->parent->getHelpBox( 'config-memcached-help' )
981 ) ) .
982 '</div>' .
983 $this->getFieldSetEnd()
984 );
985 $this->endForm();
986 }
987
988 /**
989 * @return string
990 */
991 public function getCCPartnerUrl() {
992 $server = $this->getVar( 'wgServer' );
993 $exitUrl = $server . $this->parent->getUrl( array(
994 'page' => 'Options',
995 'SubmitCC' => 'indeed',
996 'config__LicenseCode' => 'cc',
997 'config_wgRightsUrl' => '[license_url]',
998 'config_wgRightsText' => '[license_name]',
999 'config_wgRightsIcon' => '[license_button]',
1000 ) );
1001 $styleUrl = $server . dirname( dirname( $this->parent->getUrl() ) ) .
1002 '/skins/common/config-cc.css';
1003 $iframeUrl = 'http://creativecommons.org/license/?' .
1004 wfArrayToCGI( array(
1005 'partner' => 'MediaWiki',
1006 'exit_url' => $exitUrl,
1007 'lang' => $this->getVar( '_UserLang' ),
1008 'stylesheet' => $styleUrl,
1009 ) );
1010 return $iframeUrl;
1011 }
1012
1013 public function getCCChooser() {
1014 $iframeAttribs = array(
1015 'class' => 'config-cc-iframe',
1016 'name' => 'config-cc-iframe',
1017 'id' => 'config-cc-iframe',
1018 'frameborder' => 0,
1019 'width' => '100%',
1020 'height' => '100%',
1021 );
1022 if ( $this->getVar( '_CCDone' ) ) {
1023 $iframeAttribs['src'] = $this->parent->getUrl( array( 'ShowCC' => 'yes' ) );
1024 } else {
1025 $iframeAttribs['src'] = $this->getCCPartnerUrl();
1026 }
1027 $wrapperStyle = ($this->getVar('_LicenseCode') == 'cc-choose') ? '' : 'display: none';
1028
1029 return
1030 "<div class=\"config-cc-wrapper\" id=\"config-cc-wrapper\" style=\"$wrapperStyle\">\n" .
1031 Html::element( 'iframe', $iframeAttribs, '', false /* not short */ ) .
1032 "</div>\n";
1033 }
1034
1035 public function getCCDoneBox() {
1036 $js = "parent.document.getElementById('config-cc-wrapper').style.height = '$1';";
1037 // If you change this height, also change it in config.css
1038 $expandJs = str_replace( '$1', '54em', $js );
1039 $reduceJs = str_replace( '$1', '70px', $js );
1040 return
1041 '<p>'.
1042 Html::element( 'img', array( 'src' => $this->getVar( 'wgRightsIcon' ) ) ) .
1043 '&#160;&#160;' .
1044 htmlspecialchars( $this->getVar( 'wgRightsText' ) ) .
1045 "</p>\n" .
1046 "<p style=\"text-align: center\">" .
1047 Html::element( 'a',
1048 array(
1049 'href' => $this->getCCPartnerUrl(),
1050 'onclick' => $expandJs,
1051 ),
1052 wfMessage( 'config-cc-again' )->text()
1053 ) .
1054 "</p>\n" .
1055 "<script type=\"text/javascript\">\n" .
1056 # Reduce the wrapper div height
1057 htmlspecialchars( $reduceJs ) .
1058 "\n" .
1059 "</script>\n";
1060 }
1061
1062 public function submitCC() {
1063 $newValues = $this->parent->setVarsFromRequest(
1064 array( 'wgRightsUrl', 'wgRightsText', 'wgRightsIcon' ) );
1065 if ( count( $newValues ) != 3 ) {
1066 $this->parent->showError( 'config-cc-error' );
1067 return;
1068 }
1069 $this->setVar( '_CCDone', true );
1070 $this->addHTML( $this->getCCDoneBox() );
1071 }
1072
1073 public function submit() {
1074 $this->parent->setVarsFromRequest( array( '_RightsProfile', '_LicenseCode',
1075 'wgEnableEmail', 'wgPasswordSender', 'wgEnableUploads', 'wgLogo',
1076 'wgEnableUserEmail', 'wgEnotifUserTalk', 'wgEnotifWatchlist',
1077 'wgEmailAuthentication', 'wgMainCacheType', '_MemCachedServers',
1078 'wgUseInstantCommons' ) );
1079
1080 if ( !in_array( $this->getVar( '_RightsProfile' ),
1081 array_keys( $this->parent->rightsProfiles ) ) )
1082 {
1083 reset( $this->parent->rightsProfiles );
1084 $this->setVar( '_RightsProfile', key( $this->parent->rightsProfiles ) );
1085 }
1086
1087 $code = $this->getVar( '_LicenseCode' );
1088 if ( $code == 'cc-choose' ) {
1089 if ( !$this->getVar( '_CCDone' ) ) {
1090 $this->parent->showError( 'config-cc-not-chosen' );
1091 return false;
1092 }
1093 } elseif ( in_array( $code, array_keys( $this->parent->licenses ) ) ) {
1094 $entry = $this->parent->licenses[$code];
1095 if ( isset( $entry['text'] ) ) {
1096 $this->setVar( 'wgRightsText', $entry['text'] );
1097 } else {
1098 $this->setVar( 'wgRightsText', wfMessage( 'config-license-' . $code )->text() );
1099 }
1100 $this->setVar( 'wgRightsUrl', $entry['url'] );
1101 $this->setVar( 'wgRightsIcon', $entry['icon'] );
1102 } else {
1103 $this->setVar( 'wgRightsText', '' );
1104 $this->setVar( 'wgRightsUrl', '' );
1105 $this->setVar( 'wgRightsIcon', '' );
1106 }
1107
1108 $extsAvailable = $this->parent->findExtensions();
1109 $extsToInstall = array();
1110 foreach( $extsAvailable as $ext ) {
1111 if( $this->parent->request->getCheck( 'config_ext-' . $ext ) ) {
1112 $extsToInstall[] = $ext;
1113 }
1114 }
1115 $this->parent->setVar( '_Extensions', $extsToInstall );
1116
1117 if( $this->getVar( 'wgMainCacheType' ) == 'memcached' ) {
1118 $memcServers = explode( "\n", $this->getVar( '_MemCachedServers' ) );
1119 if( !$memcServers ) {
1120 $this->parent->showError( 'config-memcache-needservers' );
1121 return false;
1122 }
1123
1124 foreach( $memcServers as $server ) {
1125 $memcParts = explode( ":", $server, 2 );
1126 if ( !isset( $memcParts[0] )
1127 || ( !IP::isValid( $memcParts[0] )
1128 && ( gethostbyname( $memcParts[0] ) == $memcParts[0] ) ) ) {
1129 $this->parent->showError( 'config-memcache-badip', $memcParts[0] );
1130 return false;
1131 } elseif( !isset( $memcParts[1] ) ) {
1132 $this->parent->showError( 'config-memcache-noport', $memcParts[0] );
1133 return false;
1134 } elseif( $memcParts[1] < 1 || $memcParts[1] > 65535 ) {
1135 $this->parent->showError( 'config-memcache-badport', 1, 65535 );
1136 return false;
1137 }
1138 }
1139 }
1140 return true;
1141 }
1142
1143 }
1144
1145 class WebInstaller_Install extends WebInstallerPage {
1146 public function isSlow() {
1147 return true;
1148 }
1149
1150 public function execute() {
1151 if( $this->getVar( '_UpgradeDone' ) ) {
1152 return 'skip';
1153 } elseif( $this->getVar( '_InstallDone' ) ) {
1154 return 'continue';
1155 } elseif( $this->parent->request->wasPosted() ) {
1156 $this->startForm();
1157 $this->addHTML("<ul>");
1158 $results = $this->parent->performInstallation(
1159 array( $this, 'startStage'),
1160 array( $this, 'endStage' )
1161 );
1162 $this->addHTML("</ul>");
1163 // PerformInstallation bails on a fatal, so make sure the last item
1164 // completed before giving 'next.' Likewise, only provide back on failure
1165 $lastStep = end( $results );
1166 $continue = $lastStep->isOK() ? 'continue' : false;
1167 $back = $lastStep->isOK() ? false : 'back';
1168 $this->endForm( $continue, $back );
1169 } else {
1170 $this->startForm();
1171 $this->addHTML( $this->parent->getInfoBox( wfMessage( 'config-install-begin' )->plain() ) );
1172 $this->endForm();
1173 }
1174 return true;
1175 }
1176
1177 public function startStage( $step ) {
1178 $this->addHTML( "<li>" . wfMessage( "config-install-$step" )->escaped() . wfMessage( 'ellipsis')->escaped() );
1179 if ( $step == 'extension-tables' ) {
1180 $this->startLiveBox();
1181 }
1182 }
1183
1184 /**
1185 * @param $step
1186 * @param $status Status
1187 */
1188 public function endStage( $step, $status ) {
1189 if ( $step == 'extension-tables' ) {
1190 $this->endLiveBox();
1191 }
1192 $msg = $status->isOk() ? 'config-install-step-done' : 'config-install-step-failed';
1193 $html = wfMessage( 'word-separator' )->escaped() . wfMessage( $msg )->escaped();
1194 if ( !$status->isOk() ) {
1195 $html = "<span class=\"error\">$html</span>";
1196 }
1197 $this->addHTML( $html . "</li>\n" );
1198 if( !$status->isGood() ) {
1199 $this->parent->showStatusBox( $status );
1200 }
1201 }
1202
1203 }
1204
1205 class WebInstaller_Complete extends WebInstallerPage {
1206
1207 public function execute() {
1208 // Pop up a dialog box, to make it difficult for the user to forget
1209 // to download the file
1210 $lsUrl = $this->getVar( 'wgServer' ) . $this->parent->getURL( array( 'localsettings' => 1 ) );
1211 if ( isset( $_SERVER['HTTP_USER_AGENT'] ) &&
1212 strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE' ) !== false ) {
1213 // JS appears the only method that works consistently with IE7+
1214 $this->addHtml( "\n<script type=\"" . $GLOBALS['wgJsMimeType'] .
1215 '">jQuery( document ).ready( function() { document.location=' .
1216 Xml::encodeJsVar( $lsUrl) . "; } );</script>\n" );
1217 } else {
1218 $this->parent->request->response()->header( "Refresh: 0;url=$lsUrl" );
1219 }
1220
1221 $this->startForm();
1222 $this->parent->disableLinkPopups();
1223 $this->addHTML(
1224 $this->parent->getInfoBox(
1225 wfMessage( 'config-install-done',
1226 $lsUrl,
1227 $this->getVar( 'wgServer' ) .
1228 $this->getVar( 'wgScriptPath' ) . '/index' .
1229 $this->getVar( 'wgScriptExtension' ),
1230 '<downloadlink/>'
1231 )->plain(), 'tick-32.png'
1232 )
1233 );
1234 $this->parent->restoreLinkPopups();
1235 $this->endForm( false, false );
1236 }
1237 }
1238
1239 class WebInstaller_Restart extends WebInstallerPage {
1240
1241 public function execute() {
1242 $r = $this->parent->request;
1243 if ( $r->wasPosted() ) {
1244 $really = $r->getVal( 'submit-restart' );
1245 if ( $really ) {
1246 $this->parent->reset();
1247 }
1248 return 'continue';
1249 }
1250
1251 $this->startForm();
1252 $s = $this->parent->getWarningBox( wfMessage( 'config-help-restart' )->plain() );
1253 $this->addHTML( $s );
1254 $this->endForm( 'restart' );
1255 }
1256
1257 }
1258
1259 abstract class WebInstaller_Document extends WebInstallerPage {
1260
1261 protected abstract function getFileName();
1262
1263 public function execute() {
1264 $text = $this->getFileContents();
1265 $text = InstallDocFormatter::format( $text );
1266 $this->parent->output->addWikiText( $text );
1267 $this->startForm();
1268 $this->endForm( false );
1269 }
1270
1271 public function getFileContents() {
1272 $file = __DIR__ . '/../../' . $this->getFileName();
1273 if( ! file_exists( $file ) ) {
1274 return wfMessage( 'config-nofile', $file )->plain();
1275 }
1276 return file_get_contents( $file );
1277 }
1278
1279 }
1280
1281 class WebInstaller_Readme extends WebInstaller_Document {
1282 protected function getFileName() { return 'README'; }
1283 }
1284
1285 class WebInstaller_ReleaseNotes extends WebInstaller_Document {
1286 protected function getFileName() {
1287 global $wgVersion;
1288
1289 if(! preg_match( '/^(\d+)\.(\d+).*/i', $wgVersion, $result ) ) {
1290 throw new MWException('Variable $wgVersion has an invalid value.');
1291 }
1292
1293 return 'RELEASE-NOTES-' . $result[1] . '.' . $result[2];
1294 }
1295 }
1296
1297 class WebInstaller_UpgradeDoc extends WebInstaller_Document {
1298 protected function getFileName() { return 'UPGRADE'; }
1299 }
1300
1301 class WebInstaller_Copying extends WebInstaller_Document {
1302 protected function getFileName() { return 'COPYING'; }
1303 }
1304