3515469dcbcd608d88446a14d4a2abc1c65baea4
[lhc/web/wiklou.git] / includes / installer / WebInstaller.php
1 <?php
2 /**
3 * Core installer web interface.
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 * Class for the core installer web interface.
26 *
27 * @ingroup Deployment
28 * @since 1.17
29 */
30 class WebInstaller extends Installer {
31
32 /**
33 * @var WebInstallerOutput
34 */
35 public $output;
36
37 /**
38 * WebRequest object.
39 *
40 * @var WebRequest
41 */
42 public $request;
43
44 /**
45 * Cached session array.
46 *
47 * @var array[]
48 */
49 protected $session;
50
51 /**
52 * Captured PHP error text. Temporary.
53 *
54 * @var string[]
55 */
56 protected $phpErrors;
57
58 /**
59 * The main sequence of page names. These will be displayed in turn.
60 *
61 * To add a new installer page:
62 * * Add it to this WebInstaller::$pageSequence property
63 * * Add a "config-page-<name>" message
64 * * Add a "WebInstaller<name>" class
65 *
66 * @var string[]
67 */
68 public $pageSequence = array(
69 'Language',
70 'ExistingWiki',
71 'Welcome',
72 'DBConnect',
73 'Upgrade',
74 'DBSettings',
75 'Name',
76 'Options',
77 'Install',
78 'Complete',
79 );
80
81 /**
82 * Out of sequence pages, selectable by the user at any time.
83 *
84 * @var string[]
85 */
86 protected $otherPages = array(
87 'Restart',
88 'Readme',
89 'ReleaseNotes',
90 'Copying',
91 'UpgradeDoc', // Can't use Upgrade due to Upgrade step
92 );
93
94 /**
95 * Array of pages which have declared that they have been submitted, have validated
96 * their input, and need no further processing.
97 *
98 * @var bool[]
99 */
100 protected $happyPages;
101
102 /**
103 * List of "skipped" pages. These are pages that will automatically continue
104 * to the next page on any GET request. To avoid breaking the "back" button,
105 * they need to be skipped during a back operation.
106 *
107 * @var bool[]
108 */
109 protected $skippedPages;
110
111 /**
112 * Flag indicating that session data may have been lost.
113 *
114 * @var bool
115 */
116 public $showSessionWarning = false;
117
118 /**
119 * Numeric index of the page we're on
120 *
121 * @var int
122 */
123 protected $tabIndex = 1;
124
125 /**
126 * Name of the page we're on
127 *
128 * @var string
129 */
130 protected $currentPageName;
131
132 /**
133 * Constructor.
134 *
135 * @param WebRequest $request
136 */
137 public function __construct( WebRequest $request ) {
138 parent::__construct();
139 $this->output = new WebInstallerOutput( $this );
140 $this->request = $request;
141
142 // Add parser hooks
143 global $wgParser;
144 $wgParser->setHook( 'downloadlink', array( $this, 'downloadLinkHook' ) );
145 $wgParser->setHook( 'doclink', array( $this, 'docLink' ) );
146 }
147
148 /**
149 * Main entry point.
150 *
151 * @param array[] $session initial session array
152 *
153 * @return array[] New session array
154 */
155 public function execute( array $session ) {
156 $this->session = $session;
157
158 if ( isset( $session['settings'] ) ) {
159 $this->settings = $session['settings'] + $this->settings;
160 }
161
162 $this->exportVars();
163 $this->setupLanguage();
164
165 if ( ( $this->getVar( '_InstallDone' ) || $this->getVar( '_UpgradeDone' ) )
166 && $this->request->getVal( 'localsettings' )
167 ) {
168 $this->request->response()->header( 'Content-type: application/x-httpd-php' );
169 $this->request->response()->header(
170 'Content-Disposition: attachment; filename="LocalSettings.php"'
171 );
172
173 $ls = InstallerOverrides::getLocalSettingsGenerator( $this );
174 $rightsProfile = $this->rightsProfiles[$this->getVar( '_RightsProfile' )];
175 foreach ( $rightsProfile as $group => $rightsArr ) {
176 $ls->setGroupRights( $group, $rightsArr );
177 }
178 echo $ls->getText();
179
180 return $this->session;
181 }
182
183 $isCSS = $this->request->getVal( 'css' );
184 if ( $isCSS ) {
185 $this->outputCss();
186 return $this->session;
187 }
188
189 if ( isset( $session['happyPages'] ) ) {
190 $this->happyPages = $session['happyPages'];
191 } else {
192 $this->happyPages = array();
193 }
194
195 if ( isset( $session['skippedPages'] ) ) {
196 $this->skippedPages = $session['skippedPages'];
197 } else {
198 $this->skippedPages = array();
199 }
200
201 $lowestUnhappy = $this->getLowestUnhappy();
202
203 # Special case for Creative Commons partner chooser box.
204 if ( $this->request->getVal( 'SubmitCC' ) ) {
205 $page = $this->getPageByName( 'Options' );
206 $this->output->useShortHeader();
207 $this->output->allowFrames();
208 $page->submitCC();
209
210 return $this->finish();
211 }
212
213 if ( $this->request->getVal( 'ShowCC' ) ) {
214 $page = $this->getPageByName( 'Options' );
215 $this->output->useShortHeader();
216 $this->output->allowFrames();
217 $this->output->addHTML( $page->getCCDoneBox() );
218
219 return $this->finish();
220 }
221
222 # Get the page name.
223 $pageName = $this->request->getVal( 'page' );
224
225 if ( in_array( $pageName, $this->otherPages ) ) {
226 # Out of sequence
227 $pageId = false;
228 $page = $this->getPageByName( $pageName );
229 } else {
230 # Main sequence
231 if ( !$pageName || !in_array( $pageName, $this->pageSequence ) ) {
232 $pageId = $lowestUnhappy;
233 } else {
234 $pageId = array_search( $pageName, $this->pageSequence );
235 }
236
237 # If necessary, move back to the lowest-numbered unhappy page
238 if ( $pageId > $lowestUnhappy ) {
239 $pageId = $lowestUnhappy;
240 if ( $lowestUnhappy == 0 ) {
241 # Knocked back to start, possible loss of session data.
242 $this->showSessionWarning = true;
243 }
244 }
245
246 $pageName = $this->pageSequence[$pageId];
247 $page = $this->getPageByName( $pageName );
248 }
249
250 # If a back button was submitted, go back without submitting the form data.
251 if ( $this->request->wasPosted() && $this->request->getBool( 'submit-back' ) ) {
252 if ( $this->request->getVal( 'lastPage' ) ) {
253 $nextPage = $this->request->getVal( 'lastPage' );
254 } elseif ( $pageId !== false ) {
255 # Main sequence page
256 # Skip the skipped pages
257 $nextPageId = $pageId;
258
259 do {
260 $nextPageId--;
261 $nextPage = $this->pageSequence[$nextPageId];
262 } while ( isset( $this->skippedPages[$nextPage] ) );
263 } else {
264 $nextPage = $this->pageSequence[$lowestUnhappy];
265 }
266
267 $this->output->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
268
269 return $this->finish();
270 }
271
272 # Execute the page.
273 $this->currentPageName = $page->getName();
274 $this->startPageWrapper( $pageName );
275
276 if ( $page->isSlow() ) {
277 $this->disableTimeLimit();
278 }
279
280 $result = $page->execute();
281
282 $this->endPageWrapper();
283
284 if ( $result == 'skip' ) {
285 # Page skipped without explicit submission.
286 # Skip it when we click "back" so that we don't just go forward again.
287 $this->skippedPages[$pageName] = true;
288 $result = 'continue';
289 } else {
290 unset( $this->skippedPages[$pageName] );
291 }
292
293 # If it was posted, the page can request a continue to the next page.
294 if ( $result === 'continue' && !$this->output->headerDone() ) {
295 if ( $pageId !== false ) {
296 $this->happyPages[$pageId] = true;
297 }
298
299 $lowestUnhappy = $this->getLowestUnhappy();
300
301 if ( $this->request->getVal( 'lastPage' ) ) {
302 $nextPage = $this->request->getVal( 'lastPage' );
303 } elseif ( $pageId !== false ) {
304 $nextPage = $this->pageSequence[$pageId + 1];
305 } else {
306 $nextPage = $this->pageSequence[$lowestUnhappy];
307 }
308
309 if ( array_search( $nextPage, $this->pageSequence ) > $lowestUnhappy ) {
310 $nextPage = $this->pageSequence[$lowestUnhappy];
311 }
312
313 $this->output->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
314 }
315
316 return $this->finish();
317 }
318
319 /**
320 * Find the next page in sequence that hasn't been completed
321 * @return int
322 */
323 public function getLowestUnhappy() {
324 if ( count( $this->happyPages ) == 0 ) {
325 return 0;
326 } else {
327 return max( array_keys( $this->happyPages ) ) + 1;
328 }
329 }
330
331 /**
332 * Start the PHP session. This may be called before execute() to start the PHP session.
333 *
334 * @throws Exception
335 * @return bool
336 */
337 public function startSession() {
338 if ( wfIniGetBool( 'session.auto_start' ) || session_id() ) {
339 // Done already
340 return true;
341 }
342
343 $this->phpErrors = array();
344 set_error_handler( array( $this, 'errorHandler' ) );
345 try {
346 session_start();
347 } catch ( Exception $e ) {
348 restore_error_handler();
349 throw $e;
350 }
351 restore_error_handler();
352
353 if ( $this->phpErrors ) {
354 return false;
355 }
356
357 return true;
358 }
359
360 /**
361 * Get a hash of data identifying this MW installation.
362 *
363 * This is used by mw-config/index.php to prevent multiple installations of MW
364 * on the same cookie domain from interfering with each other.
365 *
366 * @return string
367 */
368 public function getFingerprint() {
369 // Get the base URL of the installation
370 $url = $this->request->getFullRequestURL();
371 if ( preg_match( '!^(.*\?)!', $url, $m ) ) {
372 // Trim query string
373 $url = $m[1];
374 }
375 if ( preg_match( '!^(.*)/[^/]*/[^/]*$!', $url, $m ) ) {
376 // This... seems to try to get the base path from
377 // the /mw-config/index.php. Kinda scary though?
378 $url = $m[1];
379 }
380
381 return md5( serialize( array(
382 'local path' => dirname( __DIR__ ),
383 'url' => $url,
384 'version' => $GLOBALS['wgVersion']
385 ) ) );
386 }
387
388 /**
389 * Show an error message in a box. Parameters are like wfMessage().
390 * @param string $msg
391 */
392 public function showError( $msg /*...*/ ) {
393 $args = func_get_args();
394 array_shift( $args );
395 $args = array_map( 'htmlspecialchars', $args );
396 $msg = wfMessage( $msg, $args )->useDatabase( false )->plain();
397 $this->output->addHTML( $this->getErrorBox( $msg ) );
398 }
399
400 /**
401 * Temporary error handler for session start debugging.
402 *
403 * @param int $errno Unused
404 * @param string $errstr
405 */
406 public function errorHandler( $errno, $errstr ) {
407 $this->phpErrors[] = $errstr;
408 }
409
410 /**
411 * Clean up from execute()
412 *
413 * @return array[]
414 */
415 public function finish() {
416 $this->output->output();
417
418 $this->session['happyPages'] = $this->happyPages;
419 $this->session['skippedPages'] = $this->skippedPages;
420 $this->session['settings'] = $this->settings;
421
422 return $this->session;
423 }
424
425 /**
426 * We're restarting the installation, reset the session, happyPages, etc
427 */
428 public function reset() {
429 $this->session = array();
430 $this->happyPages = array();
431 $this->settings = array();
432 }
433
434 /**
435 * Get a URL for submission back to the same script.
436 *
437 * @param string[] $query
438 *
439 * @return string
440 */
441 public function getUrl( $query = array() ) {
442 $url = $this->request->getRequestURL();
443 # Remove existing query
444 $url = preg_replace( '/\?.*$/', '', $url );
445
446 if ( $query ) {
447 $url .= '?' . wfArrayToCgi( $query );
448 }
449
450 return $url;
451 }
452
453 /**
454 * Get a WebInstallerPage by name.
455 *
456 * @param string $pageName
457 * @return WebInstallerPage
458 */
459 public function getPageByName( $pageName ) {
460 $pageClass = 'WebInstaller' . $pageName;
461
462 return new $pageClass( $this );
463 }
464
465 /**
466 * Get a session variable.
467 *
468 * @param string $name
469 * @param array $default
470 *
471 * @return array
472 */
473 public function getSession( $name, $default = null ) {
474 if ( !isset( $this->session[$name] ) ) {
475 return $default;
476 } else {
477 return $this->session[$name];
478 }
479 }
480
481 /**
482 * Set a session variable.
483 *
484 * @param string $name Key for the variable
485 * @param mixed $value
486 */
487 public function setSession( $name, $value ) {
488 $this->session[$name] = $value;
489 }
490
491 /**
492 * Get the next tabindex attribute value.
493 *
494 * @return int
495 */
496 public function nextTabIndex() {
497 return $this->tabIndex++;
498 }
499
500 /**
501 * Initializes language-related variables.
502 */
503 public function setupLanguage() {
504 global $wgLang, $wgContLang, $wgLanguageCode;
505
506 if ( $this->getSession( 'test' ) === null && !$this->request->wasPosted() ) {
507 $wgLanguageCode = $this->getAcceptLanguage();
508 $wgLang = $wgContLang = Language::factory( $wgLanguageCode );
509 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
510 $this->setVar( '_UserLang', $wgLanguageCode );
511 } else {
512 $wgLanguageCode = $this->getVar( 'wgLanguageCode' );
513 $wgContLang = Language::factory( $wgLanguageCode );
514 }
515 }
516
517 /**
518 * Retrieves MediaWiki language from Accept-Language HTTP header.
519 *
520 * @return string
521 */
522 public function getAcceptLanguage() {
523 global $wgLanguageCode, $wgRequest;
524
525 $mwLanguages = Language::fetchLanguageNames();
526 $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
527
528 foreach ( $headerLanguages as $lang ) {
529 if ( isset( $mwLanguages[$lang] ) ) {
530 return $lang;
531 }
532 }
533
534 return $wgLanguageCode;
535 }
536
537 /**
538 * Called by execute() before page output starts, to show a page list.
539 *
540 * @param string $currentPageName
541 */
542 private function startPageWrapper( $currentPageName ) {
543 $s = "<div class=\"config-page-wrapper\">\n";
544 $s .= "<div class=\"config-page\">\n";
545 $s .= "<div class=\"config-page-list\"><ul>\n";
546 $lastHappy = -1;
547
548 foreach ( $this->pageSequence as $id => $pageName ) {
549 $happy = !empty( $this->happyPages[$id] );
550 $s .= $this->getPageListItem(
551 $pageName,
552 $happy || $lastHappy == $id - 1,
553 $currentPageName
554 );
555
556 if ( $happy ) {
557 $lastHappy = $id;
558 }
559 }
560
561 $s .= "</ul><br/><ul>\n";
562 $s .= $this->getPageListItem( 'Restart', true, $currentPageName );
563 // End list pane
564 $s .= "</ul></div>\n";
565
566 // Messages:
567 // config-page-language, config-page-welcome, config-page-dbconnect, config-page-upgrade,
568 // config-page-dbsettings, config-page-name, config-page-options, config-page-install,
569 // config-page-complete, config-page-restart, config-page-readme, config-page-releasenotes,
570 // config-page-copying, config-page-upgradedoc, config-page-existingwiki
571 $s .= Html::element( 'h2', array(),
572 wfMessage( 'config-page-' . strtolower( $currentPageName ) )->text() );
573
574 $this->output->addHTMLNoFlush( $s );
575 }
576
577 /**
578 * Get a list item for the page list.
579 *
580 * @param string $pageName
581 * @param bool $enabled
582 * @param string $currentPageName
583 *
584 * @return string
585 */
586 private function getPageListItem( $pageName, $enabled, $currentPageName ) {
587 $s = "<li class=\"config-page-list-item\">";
588
589 // Messages:
590 // config-page-language, config-page-welcome, config-page-dbconnect, config-page-upgrade,
591 // config-page-dbsettings, config-page-name, config-page-options, config-page-install,
592 // config-page-complete, config-page-restart, config-page-readme, config-page-releasenotes,
593 // config-page-copying, config-page-upgradedoc, config-page-existingwiki
594 $name = wfMessage( 'config-page-' . strtolower( $pageName ) )->text();
595
596 if ( $enabled ) {
597 $query = array( 'page' => $pageName );
598
599 if ( !in_array( $pageName, $this->pageSequence ) ) {
600 if ( in_array( $currentPageName, $this->pageSequence ) ) {
601 $query['lastPage'] = $currentPageName;
602 }
603
604 $link = Html::element( 'a',
605 array(
606 'href' => $this->getUrl( $query )
607 ),
608 $name
609 );
610 } else {
611 $link = htmlspecialchars( $name );
612 }
613
614 if ( $pageName == $currentPageName ) {
615 $s .= "<span class=\"config-page-current\">$link</span>";
616 } else {
617 $s .= $link;
618 }
619 } else {
620 $s .= Html::element( 'span',
621 array(
622 'class' => 'config-page-disabled'
623 ),
624 $name
625 );
626 }
627
628 $s .= "</li>\n";
629
630 return $s;
631 }
632
633 /**
634 * Output some stuff after a page is finished.
635 */
636 private function endPageWrapper() {
637 $this->output->addHTMLNoFlush(
638 "<div class=\"visualClear\"></div>\n" .
639 "</div>\n" .
640 "<div class=\"visualClear\"></div>\n" .
641 "</div>" );
642 }
643
644 /**
645 * Get HTML for an error box with an icon.
646 *
647 * @param string $text Wikitext, get this with wfMessage()->plain()
648 *
649 * @return string
650 */
651 public function getErrorBox( $text ) {
652 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
653 }
654
655 /**
656 * Get HTML for a warning box with an icon.
657 *
658 * @param string $text Wikitext, get this with wfMessage()->plain()
659 *
660 * @return string
661 */
662 public function getWarningBox( $text ) {
663 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
664 }
665
666 /**
667 * Get HTML for an info box with an icon.
668 *
669 * @param string $text Wikitext, get this with wfMessage()->plain()
670 * @param string|bool $icon Icon name, file in skins/common/images. Default: false
671 * @param string|bool $class Additional class name to add to the wrapper div. Default: false.
672 *
673 * @return string
674 */
675 public function getInfoBox( $text, $icon = false, $class = false ) {
676 $text = $this->parse( $text, true );
677 $icon = ( $icon == false ) ?
678 '../skins/common/images/info-32.png' :
679 '../skins/common/images/' . $icon;
680 $alt = wfMessage( 'config-information' )->text();
681
682 return Html::infoBox( $text, $icon, $alt, $class, false );
683 }
684
685 /**
686 * Get small text indented help for a preceding form field.
687 * Parameters like wfMessage().
688 *
689 * @param string $msg
690 * @return string
691 */
692 public function getHelpBox( $msg /*, ... */ ) {
693 $args = func_get_args();
694 array_shift( $args );
695 $args = array_map( 'htmlspecialchars', $args );
696 $text = wfMessage( $msg, $args )->useDatabase( false )->plain();
697 $html = $this->parse( $text, true );
698
699 return "<div class=\"mw-help-field-container\">\n" .
700 "<span class=\"mw-help-field-hint\" title=\"" .
701 wfMessage( 'config-help-tooltip' )->escaped() . "\">" .
702 wfMessage( 'config-help' )->escaped() . "</span>\n" .
703 "<span class=\"mw-help-field-data\">" . $html . "</span>\n" .
704 "</div>\n";
705 }
706
707 /**
708 * Output a help box.
709 * @param string $msg Key for wfMessage()
710 */
711 public function showHelpBox( $msg /*, ... */ ) {
712 $args = func_get_args();
713 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
714 $this->output->addHTML( $html );
715 }
716
717 /**
718 * Show a short informational message.
719 * Output looks like a list.
720 *
721 * @param string $msg
722 */
723 public function showMessage( $msg /*, ... */ ) {
724 $args = func_get_args();
725 array_shift( $args );
726 $html = '<div class="config-message">' .
727 $this->parse( wfMessage( $msg, $args )->useDatabase( false )->plain() ) .
728 "</div>\n";
729 $this->output->addHTML( $html );
730 }
731
732 /**
733 * Label a control by wrapping a config-input div around it and putting a
734 * label before it.
735 *
736 * @param string $msg
737 * @param string $forId
738 * @param string $contents
739 * @param string $helpData
740 * @return string
741 */
742 public function label( $msg, $forId, $contents, $helpData = "" ) {
743 if ( strval( $msg ) == '' ) {
744 $labelText = '&#160;';
745 } else {
746 $labelText = wfMessage( $msg )->escaped();
747 }
748
749 $attributes = array( 'class' => 'config-label' );
750
751 if ( $forId ) {
752 $attributes['for'] = $forId;
753 }
754
755 return "<div class=\"config-block\">\n" .
756 " <div class=\"config-block-label\">\n" .
757 Xml::tags( 'label',
758 $attributes,
759 $labelText
760 ) . "\n" .
761 $helpData .
762 " </div>\n" .
763 " <div class=\"config-block-elements\">\n" .
764 $contents .
765 " </div>\n" .
766 "</div>\n";
767 }
768
769 /**
770 * Get a labelled text box to configure a variable.
771 *
772 * @param mixed[] $params
773 * Parameters are:
774 * var: The variable to be configured (required)
775 * label: The message name for the label (required)
776 * attribs: Additional attributes for the input element (optional)
777 * controlName: The name for the input element (optional)
778 * value: The current value of the variable (optional)
779 * help: The html for the help text (optional)
780 *
781 * @return string
782 */
783 public function getTextBox( $params ) {
784 if ( !isset( $params['controlName'] ) ) {
785 $params['controlName'] = 'config_' . $params['var'];
786 }
787
788 if ( !isset( $params['value'] ) ) {
789 $params['value'] = $this->getVar( $params['var'] );
790 }
791
792 if ( !isset( $params['attribs'] ) ) {
793 $params['attribs'] = array();
794 }
795 if ( !isset( $params['help'] ) ) {
796 $params['help'] = "";
797 }
798
799 return $this->label(
800 $params['label'],
801 $params['controlName'],
802 Xml::input(
803 $params['controlName'],
804 30, // intended to be overridden by CSS
805 $params['value'],
806 $params['attribs'] + array(
807 'id' => $params['controlName'],
808 'class' => 'config-input-text',
809 'tabindex' => $this->nextTabIndex()
810 )
811 ),
812 $params['help']
813 );
814 }
815
816 /**
817 * Get a labelled textarea to configure a variable
818 *
819 * @param mixed[] $params
820 * Parameters are:
821 * var: The variable to be configured (required)
822 * label: The message name for the label (required)
823 * attribs: Additional attributes for the input element (optional)
824 * controlName: The name for the input element (optional)
825 * value: The current value of the variable (optional)
826 * help: The html for the help text (optional)
827 *
828 * @return string
829 */
830 public function getTextArea( $params ) {
831 if ( !isset( $params['controlName'] ) ) {
832 $params['controlName'] = 'config_' . $params['var'];
833 }
834
835 if ( !isset( $params['value'] ) ) {
836 $params['value'] = $this->getVar( $params['var'] );
837 }
838
839 if ( !isset( $params['attribs'] ) ) {
840 $params['attribs'] = array();
841 }
842 if ( !isset( $params['help'] ) ) {
843 $params['help'] = "";
844 }
845
846 return $this->label(
847 $params['label'],
848 $params['controlName'],
849 Xml::textarea(
850 $params['controlName'],
851 $params['value'],
852 30,
853 5,
854 $params['attribs'] + array(
855 'id' => $params['controlName'],
856 'class' => 'config-input-text',
857 'tabindex' => $this->nextTabIndex()
858 )
859 ),
860 $params['help']
861 );
862 }
863
864 /**
865 * Get a labelled password box to configure a variable.
866 *
867 * Implements password hiding
868 * @param mixed[] $params
869 * Parameters are:
870 * var: The variable to be configured (required)
871 * label: The message name for the label (required)
872 * attribs: Additional attributes for the input element (optional)
873 * controlName: The name for the input element (optional)
874 * value: The current value of the variable (optional)
875 * help: The html for the help text (optional)
876 *
877 * @return string
878 */
879 public function getPasswordBox( $params ) {
880 if ( !isset( $params['value'] ) ) {
881 $params['value'] = $this->getVar( $params['var'] );
882 }
883
884 if ( !isset( $params['attribs'] ) ) {
885 $params['attribs'] = array();
886 }
887
888 $params['value'] = $this->getFakePassword( $params['value'] );
889 $params['attribs']['type'] = 'password';
890
891 return $this->getTextBox( $params );
892 }
893
894 /**
895 * Get a labelled checkbox to configure a boolean variable.
896 *
897 * @param mixed[] $params
898 * Parameters are:
899 * var: The variable to be configured (required)
900 * label: The message name for the label (required)
901 * attribs: Additional attributes for the input element (optional)
902 * controlName: The name for the input element (optional)
903 * value: The current value of the variable (optional)
904 * help: The html for the help text (optional)
905 *
906 * @return string
907 */
908 public function getCheckBox( $params ) {
909 if ( !isset( $params['controlName'] ) ) {
910 $params['controlName'] = 'config_' . $params['var'];
911 }
912
913 if ( !isset( $params['value'] ) ) {
914 $params['value'] = $this->getVar( $params['var'] );
915 }
916
917 if ( !isset( $params['attribs'] ) ) {
918 $params['attribs'] = array();
919 }
920 if ( !isset( $params['help'] ) ) {
921 $params['help'] = "";
922 }
923 if ( isset( $params['rawtext'] ) ) {
924 $labelText = $params['rawtext'];
925 } else {
926 $labelText = $this->parse( wfMessage( $params['label'] )->text() );
927 }
928
929 return "<div class=\"config-input-check\">\n" .
930 $params['help'] .
931 "<label>\n" .
932 Xml::check(
933 $params['controlName'],
934 $params['value'],
935 $params['attribs'] + array(
936 'id' => $params['controlName'],
937 'tabindex' => $this->nextTabIndex(),
938 )
939 ) .
940 $labelText . "\n" .
941 "</label>\n" .
942 "</div>\n";
943 }
944
945 /**
946 * Get a set of labelled radio buttons.
947 *
948 * @param mixed[] $params
949 * Parameters are:
950 * var: The variable to be configured (required)
951 * label: The message name for the label (required)
952 * itemLabelPrefix: The message name prefix for the item labels (required)
953 * itemLabels: List of message names to use for the item labels instead of itemLabelPrefix, keyed by values
954 * values: List of allowed values (required)
955 * itemAttribs: Array of attribute arrays, outer key is the value name (optional)
956 * commonAttribs: Attribute array applied to all items
957 * controlName: The name for the input element (optional)
958 * value: The current value of the variable (optional)
959 * help: The html for the help text (optional)
960 *
961 * @return string
962 */
963 public function getRadioSet( $params ) {
964 $items = $this->getRadioElements( $params );
965
966 if ( !isset( $params['label'] ) ) {
967 $label = '';
968 } else {
969 $label = $params['label'];
970 }
971
972 if ( !isset( $params['controlName'] ) ) {
973 $params['controlName'] = 'config_' . $params['var'];
974 }
975
976 if ( !isset( $params['help'] ) ) {
977 $params['help'] = "";
978 }
979
980 $s = "<ul>\n";
981 foreach ( $items as $value => $item ) {
982 $s .= "<li>$item</li>\n";
983 }
984 $s .= "</ul>\n";
985
986 return $this->label( $label, $params['controlName'], $s, $params['help'] );
987 }
988
989 /**
990 * Get a set of labelled radio buttons. You probably want to use getRadioSet(), not this.
991 *
992 * @see getRadioSet
993 *
994 * @return array
995 */
996 public function getRadioElements( $params ) {
997 if ( !isset( $params['controlName'] ) ) {
998 $params['controlName'] = 'config_' . $params['var'];
999 }
1000
1001 if ( !isset( $params['value'] ) ) {
1002 $params['value'] = $this->getVar( $params['var'] );
1003 }
1004
1005 $items = array();
1006
1007 foreach ( $params['values'] as $value ) {
1008 $itemAttribs = array();
1009
1010 if ( isset( $params['commonAttribs'] ) ) {
1011 $itemAttribs = $params['commonAttribs'];
1012 }
1013
1014 if ( isset( $params['itemAttribs'][$value] ) ) {
1015 $itemAttribs = $params['itemAttribs'][$value] + $itemAttribs;
1016 }
1017
1018 $checked = $value == $params['value'];
1019 $id = $params['controlName'] . '_' . $value;
1020 $itemAttribs['id'] = $id;
1021 $itemAttribs['tabindex'] = $this->nextTabIndex();
1022
1023 $items[$value] =
1024 Xml::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
1025 '&#160;' .
1026 Xml::tags( 'label', array( 'for' => $id ), $this->parse(
1027 isset( $params['itemLabels'] ) ?
1028 wfMessage( $params['itemLabels'][$value] )->plain() :
1029 wfMessage( $params['itemLabelPrefix'] . strtolower( $value ) )->plain()
1030 ) );
1031 }
1032
1033 return $items;
1034 }
1035
1036 /**
1037 * Output an error or warning box using a Status object.
1038 *
1039 * @param Status $status
1040 */
1041 public function showStatusBox( $status ) {
1042 if ( !$status->isGood() ) {
1043 $text = $status->getWikiText();
1044
1045 if ( $status->isOk() ) {
1046 $box = $this->getWarningBox( $text );
1047 } else {
1048 $box = $this->getErrorBox( $text );
1049 }
1050
1051 $this->output->addHTML( $box );
1052 }
1053 }
1054
1055 /**
1056 * Convenience function to set variables based on form data.
1057 * Assumes that variables containing "password" in the name are (potentially
1058 * fake) passwords.
1059 *
1060 * @param string[] $varNames
1061 * @param string $prefix The prefix added to variables to obtain form names
1062 *
1063 * @return string[]
1064 */
1065 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
1066 $newValues = array();
1067
1068 foreach ( $varNames as $name ) {
1069 $value = $this->request->getVal( $prefix . $name );
1070 // bug 30524, do not trim passwords
1071 if ( stripos( $name, 'password' ) === false ) {
1072 $value = trim( $value );
1073 }
1074 $newValues[$name] = $value;
1075
1076 if ( $value === null ) {
1077 // Checkbox?
1078 $this->setVar( $name, false );
1079 } else {
1080 if ( stripos( $name, 'password' ) !== false ) {
1081 $this->setPassword( $name, $value );
1082 } else {
1083 $this->setVar( $name, $value );
1084 }
1085 }
1086 }
1087
1088 return $newValues;
1089 }
1090
1091 /**
1092 * Helper for Installer::docLink()
1093 *
1094 * @param string $page
1095 *
1096 * @return string
1097 */
1098 protected function getDocUrl( $page ) {
1099 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1100
1101 if ( in_array( $this->currentPageName, $this->pageSequence ) ) {
1102 $url .= '&lastPage=' . urlencode( $this->currentPageName );
1103 }
1104
1105 return $url;
1106 }
1107
1108 /**
1109 * Extension tag hook for a documentation link.
1110 *
1111 * @param string $linkText
1112 * @param string[] $attribs
1113 * @param Parser $parser Unused
1114 *
1115 * @return string
1116 */
1117 public function docLink( $linkText, $attribs, $parser ) {
1118 $url = $this->getDocUrl( $attribs['href'] );
1119
1120 return '<a href="' . htmlspecialchars( $url ) . '">' .
1121 htmlspecialchars( $linkText ) .
1122 '</a>';
1123 }
1124
1125 /**
1126 * Helper for "Download LocalSettings" link on WebInstall_Complete
1127 *
1128 * @param string $text Unused
1129 * @param string[] $attribs Unused
1130 * @param Parser $parser Unused
1131 *
1132 * @return string Html for download link
1133 */
1134 public function downloadLinkHook( $text, $attribs, $parser ) {
1135 $anchor = Html::rawElement( 'a',
1136 array( 'href' => $this->getURL( array( 'localsettings' => 1 ) ) ),
1137 wfMessage( 'config-download-localsettings' )->parse()
1138 );
1139
1140 return Html::rawElement( 'div', array( 'class' => 'config-download-link' ), $anchor );
1141 }
1142
1143 /**
1144 * @return bool
1145 */
1146 public function envCheckPath() {
1147 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
1148 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
1149 // to get the path to the current script... hopefully it's reliable. SIGH
1150 $path = false;
1151 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
1152 $path = $_SERVER['PHP_SELF'];
1153 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
1154 $path = $_SERVER['SCRIPT_NAME'];
1155 }
1156 if ( $path !== false ) {
1157 $scriptPath = preg_replace( '{^(.*)/(mw-)?config.*$}', '$1', $path );
1158 $scriptExtension = $this->getVar( 'wgScriptExtension' );
1159
1160 $this->setVar( 'wgScriptPath', "$scriptPath" );
1161 // Update variables set from Setup.php that are derived from wgScriptPath
1162 $this->setVar( 'wgScript', "$scriptPath/index$scriptExtension" );
1163 $this->setVar( 'wgLoadScript', "$scriptPath/load$scriptExtension" );
1164 $this->setVar( 'wgStylePath', "$scriptPath/skins" );
1165 $this->setVar( 'wgLocalStylePath', "$scriptPath/skins" );
1166 $this->setVar( 'wgExtensionAssetsPath', "$scriptPath/extensions" );
1167 $this->setVar( 'wgUploadPath', "$scriptPath/images" );
1168
1169 } else {
1170 $this->showError( 'config-no-uri' );
1171
1172 return false;
1173 }
1174
1175 return parent::envCheckPath();
1176 }
1177
1178 /**
1179 * @return string
1180 */
1181 protected function envGetDefaultServer() {
1182 return WebRequest::detectServer();
1183 }
1184
1185 /**
1186 * Output stylesheet for web installer pages
1187 */
1188 public function outputCss() {
1189 $this->request->response()->header( 'Content-type: text/css' );
1190 echo $this->output->getCSS();
1191 }
1192
1193 /**
1194 * @return string[]
1195 */
1196 public function getPhpErrors() {
1197 return $this->phpErrors;
1198 }
1199
1200 }