[FileRepo] Lowered negative caching duration.
[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 abstract public 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 return '';
426 }
427
428 }
429
430 class WebInstaller_DBConnect extends WebInstallerPage {
431
432 public function execute() {
433 if ( $this->getVar( '_ExistingDBSettings' ) ) {
434 return 'skip';
435 }
436
437 $r = $this->parent->request;
438 if ( $r->wasPosted() ) {
439 $status = $this->submit();
440
441 if ( $status->isGood() ) {
442 $this->setVar( '_UpgradeDone', false );
443 return 'continue';
444 } else {
445 $this->parent->showStatusBox( $status );
446 }
447 }
448
449 $this->startForm();
450
451 $types = "<ul class=\"config-settings-block\">\n";
452 $settings = '';
453 $defaultType = $this->getVar( 'wgDBtype' );
454
455 $dbSupport = '';
456 foreach( $this->parent->getDBTypes() as $type ) {
457 $link = DatabaseBase::factory( $type )->getSoftwareLink();
458 $dbSupport .= wfMessage( "config-support-$type", $link )->plain() . "\n";
459 }
460 $this->addHTML( $this->parent->getInfoBox(
461 wfMessage( 'config-support-info', trim( $dbSupport ) )->text() ) );
462
463 foreach ( $this->parent->getVar( '_CompiledDBs' ) as $type ) {
464 $installer = $this->parent->getDBInstaller( $type );
465 $types .=
466 '<li>' .
467 Xml::radioLabel(
468 $installer->getReadableName(),
469 'DBType',
470 $type,
471 "DBType_$type",
472 $type == $defaultType,
473 array( 'class' => 'dbRadio', 'rel' => "DB_wrapper_$type" )
474 ) .
475 "</li>\n";
476
477 $settings .=
478 Html::openElement( 'div', array( 'id' => 'DB_wrapper_' . $type,
479 'class' => 'dbWrapper' ) ) .
480 Html::element( 'h3', array(), wfMessage( 'config-header-' . $type )->text() ) .
481 $installer->getConnectForm() .
482 "</div>\n";
483 }
484 $types .= "</ul><br style=\"clear: left\"/>\n";
485
486 $this->addHTML(
487 $this->parent->label( 'config-db-type', false, $types ) .
488 $settings
489 );
490
491 $this->endForm();
492 }
493
494 public function submit() {
495 $r = $this->parent->request;
496 $type = $r->getVal( 'DBType' );
497 $this->setVar( 'wgDBtype', $type );
498 $installer = $this->parent->getDBInstaller( $type );
499 if ( !$installer ) {
500 return Status::newFatal( 'config-invalid-db-type' );
501 }
502 return $installer->submitConnectForm();
503 }
504
505 }
506
507 class WebInstaller_Upgrade extends WebInstallerPage {
508 public function isSlow() {
509 return true;
510 }
511
512 public function execute() {
513 if ( $this->getVar( '_UpgradeDone' ) ) {
514 // Allow regeneration of LocalSettings.php, unless we are working
515 // from a pre-existing LocalSettings.php file and we want to avoid
516 // leaking its contents
517 if ( $this->parent->request->wasPosted() && !$this->getVar( '_ExistingDBSettings' ) ) {
518 // Done message acknowledged
519 return 'continue';
520 } else {
521 // Back button click
522 // Show the done message again
523 // Make them click back again if they want to do the upgrade again
524 $this->showDoneMessage();
525 return 'output';
526 }
527 }
528
529 // wgDBtype is generally valid here because otherwise the previous page
530 // (connect) wouldn't have declared its happiness
531 $type = $this->getVar( 'wgDBtype' );
532 $installer = $this->parent->getDBInstaller( $type );
533
534 if ( !$installer->needsUpgrade() ) {
535 return 'skip';
536 }
537
538 if ( $this->parent->request->wasPosted() ) {
539 $installer->preUpgrade();
540
541 $this->startLiveBox();
542 $result = $installer->doUpgrade();
543 $this->endLiveBox();
544
545 if ( $result ) {
546 // If they're going to possibly regenerate LocalSettings, we
547 // need to create the upgrade/secret keys. Bug 26481
548 if( !$this->getVar( '_ExistingDBSettings' ) ) {
549 $this->parent->generateKeys();
550 }
551 $this->setVar( '_UpgradeDone', true );
552 $this->showDoneMessage();
553 return 'output';
554 }
555 }
556
557 $this->startForm();
558 $this->addHTML( $this->parent->getInfoBox(
559 wfMessage( 'config-can-upgrade', $GLOBALS['wgVersion'] )->plain() ) );
560 $this->endForm();
561 }
562
563 public function showDoneMessage() {
564 $this->startForm();
565 $regenerate = !$this->getVar( '_ExistingDBSettings' );
566 if ( $regenerate ) {
567 $msg = 'config-upgrade-done';
568 } else {
569 $msg = 'config-upgrade-done-no-regenerate';
570 }
571 $this->parent->disableLinkPopups();
572 $this->addHTML(
573 $this->parent->getInfoBox(
574 wfMessage( $msg,
575 $this->getVar( 'wgServer' ) .
576 $this->getVar( 'wgScriptPath' ) . '/index' .
577 $this->getVar( 'wgScriptExtension' )
578 )->plain(), 'tick-32.png'
579 )
580 );
581 $this->parent->restoreLinkPopups();
582 $this->endForm( $regenerate ? 'regenerate' : false, false );
583 }
584
585 }
586
587 class WebInstaller_DBSettings extends WebInstallerPage {
588
589 public function execute() {
590 $installer = $this->parent->getDBInstaller( $this->getVar( 'wgDBtype' ) );
591
592 $r = $this->parent->request;
593 if ( $r->wasPosted() ) {
594 $status = $installer->submitSettingsForm();
595 if ( $status === false ) {
596 return 'skip';
597 } elseif ( $status->isGood() ) {
598 return 'continue';
599 } else {
600 $this->parent->showStatusBox( $status );
601 }
602 }
603
604 $form = $installer->getSettingsForm();
605 if ( $form === false ) {
606 return 'skip';
607 }
608
609 $this->startForm();
610 $this->addHTML( $form );
611 $this->endForm();
612 }
613
614 }
615
616 class WebInstaller_Name extends WebInstallerPage {
617
618 public function execute() {
619 $r = $this->parent->request;
620 if ( $r->wasPosted() ) {
621 if ( $this->submit() ) {
622 return 'continue';
623 }
624 }
625
626 $this->startForm();
627
628 // Encourage people to not name their site 'MediaWiki' by blanking the
629 // field. I think that was the intent with the original $GLOBALS['wgSitename']
630 // but these two always were the same so had the effect of making the
631 // installer forget $wgSitename when navigating back to this page.
632 if ( $this->getVar( 'wgSitename' ) == 'MediaWiki' ) {
633 $this->setVar( 'wgSitename', '' );
634 }
635
636 // Set wgMetaNamespace to something valid before we show the form.
637 // $wgMetaNamespace defaults to $wgSiteName which is 'MediaWiki'
638 $metaNS = $this->getVar( 'wgMetaNamespace' );
639 $this->setVar(
640 'wgMetaNamespace',
641 wfMessage( 'config-ns-other-default' )->inContentLanguage()->text()
642 );
643
644 $this->addHTML(
645 $this->parent->getTextBox( array(
646 'var' => 'wgSitename',
647 'label' => 'config-site-name',
648 'help' => $this->parent->getHelpBox( 'config-site-name-help' )
649 ) ) .
650 $this->parent->getRadioSet( array(
651 'var' => '_NamespaceType',
652 'label' => 'config-project-namespace',
653 'itemLabelPrefix' => 'config-ns-',
654 'values' => array( 'site-name', 'generic', 'other' ),
655 'commonAttribs' => array( 'class' => 'enableForOther',
656 'rel' => 'config_wgMetaNamespace' ),
657 'help' => $this->parent->getHelpBox( 'config-project-namespace-help' )
658 ) ) .
659 $this->parent->getTextBox( array(
660 'var' => 'wgMetaNamespace',
661 'label' => '', //TODO: Needs a label?
662 'attribs' => array( 'readonly' => 'readonly', 'class' => 'enabledByOther' ),
663
664 ) ) .
665 $this->getFieldSetStart( 'config-admin-box' ) .
666 $this->parent->getTextBox( array(
667 'var' => '_AdminName',
668 'label' => 'config-admin-name',
669 'help' => $this->parent->getHelpBox( 'config-admin-help' )
670 ) ) .
671 $this->parent->getPasswordBox( array(
672 'var' => '_AdminPassword',
673 'label' => 'config-admin-password',
674 ) ) .
675 $this->parent->getPasswordBox( array(
676 'var' => '_AdminPassword2',
677 'label' => 'config-admin-password-confirm'
678 ) ) .
679 $this->parent->getTextBox( array(
680 'var' => '_AdminEmail',
681 'label' => 'config-admin-email',
682 'help' => $this->parent->getHelpBox( 'config-admin-email-help' )
683 ) ) .
684 $this->parent->getCheckBox( array(
685 'var' => '_Subscribe',
686 'label' => 'config-subscribe',
687 'help' => $this->parent->getHelpBox( 'config-subscribe-help' )
688 ) ) .
689 $this->getFieldSetEnd() .
690 $this->parent->getInfoBox( wfMessage( 'config-almost-done' )->text() ) .
691 $this->parent->getRadioSet( array(
692 'var' => '_SkipOptional',
693 'itemLabelPrefix' => 'config-optional-',
694 'values' => array( 'continue', 'skip' )
695 ) )
696 );
697
698 // Restore the default value
699 $this->setVar( 'wgMetaNamespace', $metaNS );
700
701 $this->endForm();
702 return 'output';
703 }
704
705 public function submit() {
706 $retVal = true;
707 $this->parent->setVarsFromRequest( array( 'wgSitename', '_NamespaceType',
708 '_AdminName', '_AdminPassword', '_AdminPassword2', '_AdminEmail',
709 '_Subscribe', '_SkipOptional', 'wgMetaNamespace' ) );
710
711 // Validate site name
712 if ( strval( $this->getVar( 'wgSitename' ) ) === '' ) {
713 $this->parent->showError( 'config-site-name-blank' );
714 $retVal = false;
715 }
716
717 // Fetch namespace
718 $nsType = $this->getVar( '_NamespaceType' );
719 if ( $nsType == 'site-name' ) {
720 $name = $this->getVar( 'wgSitename' );
721 // Sanitize for namespace
722 // This algorithm should match the JS one in WebInstallerOutput.php
723 $name = preg_replace( '/[\[\]\{\}|#<>%+? ]/', '_', $name );
724 $name = str_replace( '&', '&amp;', $name );
725 $name = preg_replace( '/__+/', '_', $name );
726 $name = ucfirst( trim( $name, '_' ) );
727 } elseif ( $nsType == 'generic' ) {
728 $name = wfMessage( 'config-ns-generic' )->text();
729 } else { // other
730 $name = $this->getVar( 'wgMetaNamespace' );
731 }
732
733 // Validate namespace
734 if ( strpos( $name, ':' ) !== false ) {
735 $good = false;
736 } else {
737 // Title-style validation
738 $title = Title::newFromText( $name );
739 if ( !$title ) {
740 $good = $nsType == 'site-name';
741 } else {
742 $name = $title->getDBkey();
743 $good = true;
744 }
745 }
746 if ( !$good ) {
747 $this->parent->showError( 'config-ns-invalid', $name );
748 $retVal = false;
749 }
750
751 // Make sure it won't conflict with any existing namespaces
752 global $wgContLang;
753 $nsIndex = $wgContLang->getNsIndex( $name );
754 if( $nsIndex !== false && $nsIndex !== NS_PROJECT ) {
755 $this->parent->showError( 'config-ns-conflict', $name );
756 $retVal = false;
757 }
758
759 $this->setVar( 'wgMetaNamespace', $name );
760
761 // Validate username for creation
762 $name = $this->getVar( '_AdminName' );
763 if ( strval( $name ) === '' ) {
764 $this->parent->showError( 'config-admin-name-blank' );
765 $cname = $name;
766 $retVal = false;
767 } else {
768 $cname = User::getCanonicalName( $name, 'creatable' );
769 if ( $cname === false ) {
770 $this->parent->showError( 'config-admin-name-invalid', $name );
771 $retVal = false;
772 } else {
773 $this->setVar( '_AdminName', $cname );
774 }
775 }
776
777 // Validate password
778 $msg = false;
779 $pwd = $this->getVar( '_AdminPassword' );
780 $user = User::newFromName( $cname );
781 $valid = $user && $user->getPasswordValidity( $pwd );
782 if ( strval( $pwd ) === '' ) {
783 # $user->getPasswordValidity just checks for $wgMinimalPasswordLength.
784 # This message is more specific and helpful.
785 $msg = 'config-admin-password-blank';
786 } elseif ( $pwd !== $this->getVar( '_AdminPassword2' ) ) {
787 $msg = 'config-admin-password-mismatch';
788 } elseif ( $valid !== true ) {
789 # As of writing this will only catch the username being e.g. 'FOO' and
790 # the password 'foo'
791 $msg = $valid;
792 }
793 if ( $msg !== false ) {
794 call_user_func_array( array( $this->parent, 'showError' ), (array)$msg );
795 $this->setVar( '_AdminPassword', '' );
796 $this->setVar( '_AdminPassword2', '' );
797 $retVal = false;
798 }
799
800 // Validate e-mail if provided
801 $email = $this->getVar( '_AdminEmail' );
802 if( $email && !Sanitizer::validateEmail( $email ) ) {
803 $this->parent->showError( 'config-admin-error-bademail' );
804 $retVal = false;
805 }
806 // If they asked to subscribe to mediawiki-announce but didn't give
807 // an e-mail, show an error. Bug 29332
808 if( !$email && $this->getVar( '_Subscribe' ) ) {
809 $this->parent->showError( 'config-subscribe-noemail' );
810 $retVal = false;
811 }
812
813 return $retVal;
814 }
815
816 }
817
818 class WebInstaller_Options extends WebInstallerPage {
819
820 public function execute() {
821 if ( $this->getVar( '_SkipOptional' ) == 'skip' ) {
822 return 'skip';
823 }
824 if ( $this->parent->request->wasPosted() ) {
825 if ( $this->submit() ) {
826 return 'continue';
827 }
828 }
829
830 $emailwrapperStyle = $this->getVar( 'wgEnableEmail' ) ? '' : 'display: none';
831 $this->startForm();
832 $this->addHTML(
833 # User Rights
834 $this->parent->getRadioSet( array(
835 'var' => '_RightsProfile',
836 'label' => 'config-profile',
837 'itemLabelPrefix' => 'config-profile-',
838 'values' => array_keys( $this->parent->rightsProfiles ),
839 ) ) .
840 $this->parent->getInfoBox( wfMessage( 'config-profile-help' )->plain() ) .
841
842 # Licensing
843 $this->parent->getRadioSet( array(
844 'var' => '_LicenseCode',
845 'label' => 'config-license',
846 'itemLabelPrefix' => 'config-license-',
847 'values' => array_keys( $this->parent->licenses ),
848 'commonAttribs' => array( 'class' => 'licenseRadio' ),
849 ) ) .
850 $this->getCCChooser() .
851 $this->parent->getHelpBox( 'config-license-help' ) .
852
853 # E-mail
854 $this->getFieldSetStart( 'config-email-settings' ) .
855 $this->parent->getCheckBox( array(
856 'var' => 'wgEnableEmail',
857 'label' => 'config-enable-email',
858 'attribs' => array( 'class' => 'showHideRadio', 'rel' => 'emailwrapper' ),
859 ) ) .
860 $this->parent->getHelpBox( 'config-enable-email-help' ) .
861 "<div id=\"emailwrapper\" style=\"$emailwrapperStyle\">" .
862 $this->parent->getTextBox( array(
863 'var' => 'wgPasswordSender',
864 'label' => 'config-email-sender'
865 ) ) .
866 $this->parent->getHelpBox( 'config-email-sender-help' ) .
867 $this->parent->getCheckBox( array(
868 'var' => 'wgEnableUserEmail',
869 'label' => 'config-email-user',
870 ) ) .
871 $this->parent->getHelpBox( 'config-email-user-help' ) .
872 $this->parent->getCheckBox( array(
873 'var' => 'wgEnotifUserTalk',
874 'label' => 'config-email-usertalk',
875 ) ) .
876 $this->parent->getHelpBox( 'config-email-usertalk-help' ) .
877 $this->parent->getCheckBox( array(
878 'var' => 'wgEnotifWatchlist',
879 'label' => 'config-email-watchlist',
880 ) ) .
881 $this->parent->getHelpBox( 'config-email-watchlist-help' ) .
882 $this->parent->getCheckBox( array(
883 'var' => 'wgEmailAuthentication',
884 'label' => 'config-email-auth',
885 ) ) .
886 $this->parent->getHelpBox( 'config-email-auth-help' ) .
887 "</div>" .
888 $this->getFieldSetEnd()
889 );
890
891 $extensions = $this->parent->findExtensions();
892
893 if( $extensions ) {
894 $extHtml = $this->getFieldSetStart( 'config-extensions' );
895
896 foreach( $extensions as $ext ) {
897 $extHtml .= $this->parent->getCheckBox( array(
898 'var' => "ext-$ext",
899 'rawtext' => $ext,
900 ) );
901 }
902
903 $extHtml .= $this->parent->getHelpBox( 'config-extensions-help' ) .
904 $this->getFieldSetEnd();
905 $this->addHTML( $extHtml );
906 }
907
908 // Having / in paths in Windows looks funny :)
909 $this->setVar( 'wgDeletedDirectory',
910 str_replace(
911 '/', DIRECTORY_SEPARATOR,
912 $this->getVar( 'wgDeletedDirectory' )
913 )
914 );
915
916 $uploadwrapperStyle = $this->getVar( 'wgEnableUploads' ) ? '' : 'display: none';
917 $this->addHTML(
918 # Uploading
919 $this->getFieldSetStart( 'config-upload-settings' ) .
920 $this->parent->getCheckBox( array(
921 'var' => 'wgEnableUploads',
922 'label' => 'config-upload-enable',
923 'attribs' => array( 'class' => 'showHideRadio', 'rel' => 'uploadwrapper' ),
924 'help' => $this->parent->getHelpBox( 'config-upload-help' )
925 ) ) .
926 '<div id="uploadwrapper" style="' . $uploadwrapperStyle . '">' .
927 $this->parent->getTextBox( array(
928 'var' => 'wgDeletedDirectory',
929 'label' => 'config-upload-deleted',
930 'attribs' => array( 'dir' => 'ltr' ),
931 'help' => $this->parent->getHelpBox( 'config-upload-deleted-help' )
932 ) ) .
933 '</div>' .
934 $this->parent->getTextBox( array(
935 'var' => 'wgLogo',
936 'label' => 'config-logo',
937 'attribs' => array( 'dir' => 'ltr' ),
938 'help' => $this->parent->getHelpBox( 'config-logo-help' )
939 ) )
940 );
941 $this->addHTML(
942 $this->parent->getCheckBox( array(
943 'var' => 'wgUseInstantCommons',
944 'label' => 'config-instantcommons',
945 'help' => $this->parent->getHelpBox( 'config-instantcommons-help' )
946 ) ) .
947 $this->getFieldSetEnd()
948 );
949
950 $caches = array( 'none' );
951 if( count( $this->getVar( '_Caches' ) ) ) {
952 $caches[] = 'accel';
953 }
954 $caches[] = 'memcached';
955
956 // We'll hide/show this on demand when the value changes, see config.js.
957 $cacheval = $this->getVar( 'wgMainCacheType' );
958 if (!$cacheval) {
959 // We need to set a default here; but don't hardcode it
960 // or we lose it every time we reload the page for validation
961 // or going back!
962 $cacheval = 'none';
963 }
964 $hidden = ($cacheval == 'memcached') ? '' : 'display: none';
965 $this->addHTML(
966 # Advanced settings
967 $this->getFieldSetStart( 'config-advanced-settings' ) .
968 # Object cache settings
969 $this->parent->getRadioSet( array(
970 'var' => 'wgMainCacheType',
971 'label' => 'config-cache-options',
972 'itemLabelPrefix' => 'config-cache-',
973 'values' => $caches,
974 'value' => $cacheval,
975 ) ) .
976 $this->parent->getHelpBox( 'config-cache-help' ) .
977 "<div id=\"config-memcachewrapper\" style=\"$hidden\">" .
978 $this->parent->getTextArea( array(
979 'var' => '_MemCachedServers',
980 'label' => 'config-memcached-servers',
981 'help' => $this->parent->getHelpBox( 'config-memcached-help' )
982 ) ) .
983 '</div>' .
984 $this->getFieldSetEnd()
985 );
986 $this->endForm();
987 }
988
989 /**
990 * @return string
991 */
992 public function getCCPartnerUrl() {
993 $server = $this->getVar( 'wgServer' );
994 $exitUrl = $server . $this->parent->getUrl( array(
995 'page' => 'Options',
996 'SubmitCC' => 'indeed',
997 'config__LicenseCode' => 'cc',
998 'config_wgRightsUrl' => '[license_url]',
999 'config_wgRightsText' => '[license_name]',
1000 'config_wgRightsIcon' => '[license_button]',
1001 ) );
1002 $styleUrl = $server . dirname( dirname( $this->parent->getUrl() ) ) .
1003 '/skins/common/config-cc.css';
1004 $iframeUrl = 'http://creativecommons.org/license/?' .
1005 wfArrayToCGI( array(
1006 'partner' => 'MediaWiki',
1007 'exit_url' => $exitUrl,
1008 'lang' => $this->getVar( '_UserLang' ),
1009 'stylesheet' => $styleUrl,
1010 ) );
1011 return $iframeUrl;
1012 }
1013
1014 public function getCCChooser() {
1015 $iframeAttribs = array(
1016 'class' => 'config-cc-iframe',
1017 'name' => 'config-cc-iframe',
1018 'id' => 'config-cc-iframe',
1019 'frameborder' => 0,
1020 'width' => '100%',
1021 'height' => '100%',
1022 );
1023 if ( $this->getVar( '_CCDone' ) ) {
1024 $iframeAttribs['src'] = $this->parent->getUrl( array( 'ShowCC' => 'yes' ) );
1025 } else {
1026 $iframeAttribs['src'] = $this->getCCPartnerUrl();
1027 }
1028 $wrapperStyle = ($this->getVar('_LicenseCode') == 'cc-choose') ? '' : 'display: none';
1029
1030 return
1031 "<div class=\"config-cc-wrapper\" id=\"config-cc-wrapper\" style=\"$wrapperStyle\">\n" .
1032 Html::element( 'iframe', $iframeAttribs, '', false /* not short */ ) .
1033 "</div>\n";
1034 }
1035
1036 public function getCCDoneBox() {
1037 $js = "parent.document.getElementById('config-cc-wrapper').style.height = '$1';";
1038 // If you change this height, also change it in config.css
1039 $expandJs = str_replace( '$1', '54em', $js );
1040 $reduceJs = str_replace( '$1', '70px', $js );
1041 return
1042 '<p>'.
1043 Html::element( 'img', array( 'src' => $this->getVar( 'wgRightsIcon' ) ) ) .
1044 '&#160;&#160;' .
1045 htmlspecialchars( $this->getVar( 'wgRightsText' ) ) .
1046 "</p>\n" .
1047 "<p style=\"text-align: center\">" .
1048 Html::element( 'a',
1049 array(
1050 'href' => $this->getCCPartnerUrl(),
1051 'onclick' => $expandJs,
1052 ),
1053 wfMessage( 'config-cc-again' )->text()
1054 ) .
1055 "</p>\n" .
1056 "<script type=\"text/javascript\">\n" .
1057 # Reduce the wrapper div height
1058 htmlspecialchars( $reduceJs ) .
1059 "\n" .
1060 "</script>\n";
1061 }
1062
1063 public function submitCC() {
1064 $newValues = $this->parent->setVarsFromRequest(
1065 array( 'wgRightsUrl', 'wgRightsText', 'wgRightsIcon' ) );
1066 if ( count( $newValues ) != 3 ) {
1067 $this->parent->showError( 'config-cc-error' );
1068 return;
1069 }
1070 $this->setVar( '_CCDone', true );
1071 $this->addHTML( $this->getCCDoneBox() );
1072 }
1073
1074 public function submit() {
1075 $this->parent->setVarsFromRequest( array( '_RightsProfile', '_LicenseCode',
1076 'wgEnableEmail', 'wgPasswordSender', 'wgEnableUploads', 'wgLogo',
1077 'wgEnableUserEmail', 'wgEnotifUserTalk', 'wgEnotifWatchlist',
1078 'wgEmailAuthentication', 'wgMainCacheType', '_MemCachedServers',
1079 'wgUseInstantCommons' ) );
1080
1081 if ( !in_array( $this->getVar( '_RightsProfile' ),
1082 array_keys( $this->parent->rightsProfiles ) ) )
1083 {
1084 reset( $this->parent->rightsProfiles );
1085 $this->setVar( '_RightsProfile', key( $this->parent->rightsProfiles ) );
1086 }
1087
1088 $code = $this->getVar( '_LicenseCode' );
1089 if ( $code == 'cc-choose' ) {
1090 if ( !$this->getVar( '_CCDone' ) ) {
1091 $this->parent->showError( 'config-cc-not-chosen' );
1092 return false;
1093 }
1094 } elseif ( in_array( $code, array_keys( $this->parent->licenses ) ) ) {
1095 $entry = $this->parent->licenses[$code];
1096 if ( isset( $entry['text'] ) ) {
1097 $this->setVar( 'wgRightsText', $entry['text'] );
1098 } else {
1099 $this->setVar( 'wgRightsText', wfMessage( 'config-license-' . $code )->text() );
1100 }
1101 $this->setVar( 'wgRightsUrl', $entry['url'] );
1102 $this->setVar( 'wgRightsIcon', $entry['icon'] );
1103 } else {
1104 $this->setVar( 'wgRightsText', '' );
1105 $this->setVar( 'wgRightsUrl', '' );
1106 $this->setVar( 'wgRightsIcon', '' );
1107 }
1108
1109 $extsAvailable = $this->parent->findExtensions();
1110 $extsToInstall = array();
1111 foreach( $extsAvailable as $ext ) {
1112 if( $this->parent->request->getCheck( 'config_ext-' . $ext ) ) {
1113 $extsToInstall[] = $ext;
1114 }
1115 }
1116 $this->parent->setVar( '_Extensions', $extsToInstall );
1117
1118 if( $this->getVar( 'wgMainCacheType' ) == 'memcached' ) {
1119 $memcServers = explode( "\n", $this->getVar( '_MemCachedServers' ) );
1120 if( !$memcServers ) {
1121 $this->parent->showError( 'config-memcache-needservers' );
1122 return false;
1123 }
1124
1125 foreach( $memcServers as $server ) {
1126 $memcParts = explode( ":", $server, 2 );
1127 if ( !isset( $memcParts[0] )
1128 || ( !IP::isValid( $memcParts[0] )
1129 && ( gethostbyname( $memcParts[0] ) == $memcParts[0] ) ) ) {
1130 $this->parent->showError( 'config-memcache-badip', $memcParts[0] );
1131 return false;
1132 } elseif( !isset( $memcParts[1] ) ) {
1133 $this->parent->showError( 'config-memcache-noport', $memcParts[0] );
1134 return false;
1135 } elseif( $memcParts[1] < 1 || $memcParts[1] > 65535 ) {
1136 $this->parent->showError( 'config-memcache-badport', 1, 65535 );
1137 return false;
1138 }
1139 }
1140 }
1141 return true;
1142 }
1143
1144 }
1145
1146 class WebInstaller_Install extends WebInstallerPage {
1147 public function isSlow() {
1148 return true;
1149 }
1150
1151 public function execute() {
1152 if( $this->getVar( '_UpgradeDone' ) ) {
1153 return 'skip';
1154 } elseif( $this->getVar( '_InstallDone' ) ) {
1155 return 'continue';
1156 } elseif( $this->parent->request->wasPosted() ) {
1157 $this->startForm();
1158 $this->addHTML("<ul>");
1159 $results = $this->parent->performInstallation(
1160 array( $this, 'startStage'),
1161 array( $this, 'endStage' )
1162 );
1163 $this->addHTML("</ul>");
1164 // PerformInstallation bails on a fatal, so make sure the last item
1165 // completed before giving 'next.' Likewise, only provide back on failure
1166 $lastStep = end( $results );
1167 $continue = $lastStep->isOK() ? 'continue' : false;
1168 $back = $lastStep->isOK() ? false : 'back';
1169 $this->endForm( $continue, $back );
1170 } else {
1171 $this->startForm();
1172 $this->addHTML( $this->parent->getInfoBox( wfMessage( 'config-install-begin' )->plain() ) );
1173 $this->endForm();
1174 }
1175 return true;
1176 }
1177
1178 public function startStage( $step ) {
1179 $this->addHTML( "<li>" . wfMessage( "config-install-$step" )->escaped() . wfMessage( 'ellipsis')->escaped() );
1180 if ( $step == 'extension-tables' ) {
1181 $this->startLiveBox();
1182 }
1183 }
1184
1185 /**
1186 * @param $step
1187 * @param $status Status
1188 */
1189 public function endStage( $step, $status ) {
1190 if ( $step == 'extension-tables' ) {
1191 $this->endLiveBox();
1192 }
1193 $msg = $status->isOk() ? 'config-install-step-done' : 'config-install-step-failed';
1194 $html = wfMessage( 'word-separator' )->escaped() . wfMessage( $msg )->escaped();
1195 if ( !$status->isOk() ) {
1196 $html = "<span class=\"error\">$html</span>";
1197 }
1198 $this->addHTML( $html . "</li>\n" );
1199 if( !$status->isGood() ) {
1200 $this->parent->showStatusBox( $status );
1201 }
1202 }
1203
1204 }
1205
1206 class WebInstaller_Complete extends WebInstallerPage {
1207
1208 public function execute() {
1209 // Pop up a dialog box, to make it difficult for the user to forget
1210 // to download the file
1211 $lsUrl = $this->getVar( 'wgServer' ) . $this->parent->getURL( array( 'localsettings' => 1 ) );
1212 if ( isset( $_SERVER['HTTP_USER_AGENT'] ) &&
1213 strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE' ) !== false ) {
1214 // JS appears the only method that works consistently with IE7+
1215 $this->addHtml( "\n<script type=\"" . $GLOBALS['wgJsMimeType'] .
1216 '">jQuery( document ).ready( function() { document.location=' .
1217 Xml::encodeJsVar( $lsUrl) . "; } );</script>\n" );
1218 } else {
1219 $this->parent->request->response()->header( "Refresh: 0;url=$lsUrl" );
1220 }
1221
1222 $this->startForm();
1223 $this->parent->disableLinkPopups();
1224 $this->addHTML(
1225 $this->parent->getInfoBox(
1226 wfMessage( 'config-install-done',
1227 $lsUrl,
1228 $this->getVar( 'wgServer' ) .
1229 $this->getVar( 'wgScriptPath' ) . '/index' .
1230 $this->getVar( 'wgScriptExtension' ),
1231 '<downloadlink/>'
1232 )->plain(), 'tick-32.png'
1233 )
1234 );
1235 $this->parent->restoreLinkPopups();
1236 $this->endForm( false, false );
1237 }
1238 }
1239
1240 class WebInstaller_Restart extends WebInstallerPage {
1241
1242 public function execute() {
1243 $r = $this->parent->request;
1244 if ( $r->wasPosted() ) {
1245 $really = $r->getVal( 'submit-restart' );
1246 if ( $really ) {
1247 $this->parent->reset();
1248 }
1249 return 'continue';
1250 }
1251
1252 $this->startForm();
1253 $s = $this->parent->getWarningBox( wfMessage( 'config-help-restart' )->plain() );
1254 $this->addHTML( $s );
1255 $this->endForm( 'restart' );
1256 }
1257
1258 }
1259
1260 abstract class WebInstaller_Document extends WebInstallerPage {
1261
1262 abstract protected function getFileName();
1263
1264 public function execute() {
1265 $text = $this->getFileContents();
1266 $text = InstallDocFormatter::format( $text );
1267 $this->parent->output->addWikiText( $text );
1268 $this->startForm();
1269 $this->endForm( false );
1270 }
1271
1272 public function getFileContents() {
1273 $file = __DIR__ . '/../../' . $this->getFileName();
1274 if( ! file_exists( $file ) ) {
1275 return wfMessage( 'config-nofile', $file )->plain();
1276 }
1277 return file_get_contents( $file );
1278 }
1279
1280 }
1281
1282 class WebInstaller_Readme extends WebInstaller_Document {
1283 protected function getFileName() { return 'README'; }
1284 }
1285
1286 class WebInstaller_ReleaseNotes extends WebInstaller_Document {
1287 protected function getFileName() {
1288 global $wgVersion;
1289
1290 if(! preg_match( '/^(\d+)\.(\d+).*/i', $wgVersion, $result ) ) {
1291 throw new MWException('Variable $wgVersion has an invalid value.');
1292 }
1293
1294 return 'RELEASE-NOTES-' . $result[1] . '.' . $result[2];
1295 }
1296 }
1297
1298 class WebInstaller_UpgradeDoc extends WebInstaller_Document {
1299 protected function getFileName() { return 'UPGRADE'; }
1300 }
1301
1302 class WebInstaller_Copying extends WebInstaller_Document {
1303 protected function getFileName() { return 'COPYING'; }
1304 }
1305