Merge "Clean up zh-tw.json"
[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_name( 'mw_installer_session' );
347 session_start();
348 } catch ( Exception $e ) {
349 restore_error_handler();
350 throw $e;
351 }
352 restore_error_handler();
353
354 if ( $this->phpErrors ) {
355 return false;
356 }
357
358 return true;
359 }
360
361 /**
362 * Get a hash of data identifying this MW installation.
363 *
364 * This is used by mw-config/index.php to prevent multiple installations of MW
365 * on the same cookie domain from interfering with each other.
366 *
367 * @return string
368 */
369 public function getFingerprint() {
370 // Get the base URL of the installation
371 $url = $this->request->getFullRequestURL();
372 if ( preg_match( '!^(.*\?)!', $url, $m ) ) {
373 // Trim query string
374 $url = $m[1];
375 }
376 if ( preg_match( '!^(.*)/[^/]*/[^/]*$!', $url, $m ) ) {
377 // This... seems to try to get the base path from
378 // the /mw-config/index.php. Kinda scary though?
379 $url = $m[1];
380 }
381
382 return md5( serialize( array(
383 'local path' => dirname( __DIR__ ),
384 'url' => $url,
385 'version' => $GLOBALS['wgVersion']
386 ) ) );
387 }
388
389 /**
390 * Show an error message in a box. Parameters are like wfMessage().
391 * @param string $msg
392 */
393 public function showError( $msg /*...*/ ) {
394 $args = func_get_args();
395 array_shift( $args );
396 $args = array_map( 'htmlspecialchars', $args );
397 $msg = wfMessage( $msg, $args )->useDatabase( false )->plain();
398 $this->output->addHTML( $this->getErrorBox( $msg ) );
399 }
400
401 /**
402 * Temporary error handler for session start debugging.
403 *
404 * @param int $errno Unused
405 * @param string $errstr
406 */
407 public function errorHandler( $errno, $errstr ) {
408 $this->phpErrors[] = $errstr;
409 }
410
411 /**
412 * Clean up from execute()
413 *
414 * @return array[]
415 */
416 public function finish() {
417 $this->output->output();
418
419 $this->session['happyPages'] = $this->happyPages;
420 $this->session['skippedPages'] = $this->skippedPages;
421 $this->session['settings'] = $this->settings;
422
423 return $this->session;
424 }
425
426 /**
427 * We're restarting the installation, reset the session, happyPages, etc
428 */
429 public function reset() {
430 $this->session = array();
431 $this->happyPages = array();
432 $this->settings = array();
433 }
434
435 /**
436 * Get a URL for submission back to the same script.
437 *
438 * @param string[] $query
439 *
440 * @return string
441 */
442 public function getUrl( $query = array() ) {
443 $url = $this->request->getRequestURL();
444 # Remove existing query
445 $url = preg_replace( '/\?.*$/', '', $url );
446
447 if ( $query ) {
448 $url .= '?' . wfArrayToCgi( $query );
449 }
450
451 return $url;
452 }
453
454 /**
455 * Get a WebInstallerPage by name.
456 *
457 * @param string $pageName
458 * @return WebInstallerPage
459 */
460 public function getPageByName( $pageName ) {
461 $pageClass = 'WebInstaller' . $pageName;
462
463 return new $pageClass( $this );
464 }
465
466 /**
467 * Get a session variable.
468 *
469 * @param string $name
470 * @param array $default
471 *
472 * @return array
473 */
474 public function getSession( $name, $default = null ) {
475 if ( !isset( $this->session[$name] ) ) {
476 return $default;
477 } else {
478 return $this->session[$name];
479 }
480 }
481
482 /**
483 * Set a session variable.
484 *
485 * @param string $name Key for the variable
486 * @param mixed $value
487 */
488 public function setSession( $name, $value ) {
489 $this->session[$name] = $value;
490 }
491
492 /**
493 * Get the next tabindex attribute value.
494 *
495 * @return int
496 */
497 public function nextTabIndex() {
498 return $this->tabIndex++;
499 }
500
501 /**
502 * Initializes language-related variables.
503 */
504 public function setupLanguage() {
505 global $wgLang, $wgContLang, $wgLanguageCode;
506
507 if ( $this->getSession( 'test' ) === null && !$this->request->wasPosted() ) {
508 $wgLanguageCode = $this->getAcceptLanguage();
509 $wgLang = $wgContLang = Language::factory( $wgLanguageCode );
510 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
511 $this->setVar( '_UserLang', $wgLanguageCode );
512 } else {
513 $wgLanguageCode = $this->getVar( 'wgLanguageCode' );
514 $wgContLang = Language::factory( $wgLanguageCode );
515 }
516 }
517
518 /**
519 * Retrieves MediaWiki language from Accept-Language HTTP header.
520 *
521 * @return string
522 */
523 public function getAcceptLanguage() {
524 global $wgLanguageCode, $wgRequest;
525
526 $mwLanguages = Language::fetchLanguageNames();
527 $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
528
529 foreach ( $headerLanguages as $lang ) {
530 if ( isset( $mwLanguages[$lang] ) ) {
531 return $lang;
532 }
533 }
534
535 return $wgLanguageCode;
536 }
537
538 /**
539 * Called by execute() before page output starts, to show a page list.
540 *
541 * @param string $currentPageName
542 */
543 private function startPageWrapper( $currentPageName ) {
544 $s = "<div class=\"config-page-wrapper\">\n";
545 $s .= "<div class=\"config-page\">\n";
546 $s .= "<div class=\"config-page-list\"><ul>\n";
547 $lastHappy = -1;
548
549 foreach ( $this->pageSequence as $id => $pageName ) {
550 $happy = !empty( $this->happyPages[$id] );
551 $s .= $this->getPageListItem(
552 $pageName,
553 $happy || $lastHappy == $id - 1,
554 $currentPageName
555 );
556
557 if ( $happy ) {
558 $lastHappy = $id;
559 }
560 }
561
562 $s .= "</ul><br/><ul>\n";
563 $s .= $this->getPageListItem( 'Restart', true, $currentPageName );
564 // End list pane
565 $s .= "</ul></div>\n";
566
567 // Messages:
568 // config-page-language, config-page-welcome, config-page-dbconnect, config-page-upgrade,
569 // config-page-dbsettings, config-page-name, config-page-options, config-page-install,
570 // config-page-complete, config-page-restart, config-page-readme, config-page-releasenotes,
571 // config-page-copying, config-page-upgradedoc, config-page-existingwiki
572 $s .= Html::element( 'h2', array(),
573 wfMessage( 'config-page-' . strtolower( $currentPageName ) )->text() );
574
575 $this->output->addHTMLNoFlush( $s );
576 }
577
578 /**
579 * Get a list item for the page list.
580 *
581 * @param string $pageName
582 * @param bool $enabled
583 * @param string $currentPageName
584 *
585 * @return string
586 */
587 private function getPageListItem( $pageName, $enabled, $currentPageName ) {
588 $s = "<li class=\"config-page-list-item\">";
589
590 // Messages:
591 // config-page-language, config-page-welcome, config-page-dbconnect, config-page-upgrade,
592 // config-page-dbsettings, config-page-name, config-page-options, config-page-install,
593 // config-page-complete, config-page-restart, config-page-readme, config-page-releasenotes,
594 // config-page-copying, config-page-upgradedoc, config-page-existingwiki
595 $name = wfMessage( 'config-page-' . strtolower( $pageName ) )->text();
596
597 if ( $enabled ) {
598 $query = array( 'page' => $pageName );
599
600 if ( !in_array( $pageName, $this->pageSequence ) ) {
601 if ( in_array( $currentPageName, $this->pageSequence ) ) {
602 $query['lastPage'] = $currentPageName;
603 }
604
605 $link = Html::element( 'a',
606 array(
607 'href' => $this->getUrl( $query )
608 ),
609 $name
610 );
611 } else {
612 $link = htmlspecialchars( $name );
613 }
614
615 if ( $pageName == $currentPageName ) {
616 $s .= "<span class=\"config-page-current\">$link</span>";
617 } else {
618 $s .= $link;
619 }
620 } else {
621 $s .= Html::element( 'span',
622 array(
623 'class' => 'config-page-disabled'
624 ),
625 $name
626 );
627 }
628
629 $s .= "</li>\n";
630
631 return $s;
632 }
633
634 /**
635 * Output some stuff after a page is finished.
636 */
637 private function endPageWrapper() {
638 $this->output->addHTMLNoFlush(
639 "<div class=\"visualClear\"></div>\n" .
640 "</div>\n" .
641 "<div class=\"visualClear\"></div>\n" .
642 "</div>" );
643 }
644
645 /**
646 * Get HTML for an error box with an icon.
647 *
648 * @param string $text Wikitext, get this with wfMessage()->plain()
649 *
650 * @return string
651 */
652 public function getErrorBox( $text ) {
653 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
654 }
655
656 /**
657 * Get HTML for a warning box with an icon.
658 *
659 * @param string $text Wikitext, get this with wfMessage()->plain()
660 *
661 * @return string
662 */
663 public function getWarningBox( $text ) {
664 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
665 }
666
667 /**
668 * Get HTML for an info box with an icon.
669 *
670 * @param string $text Wikitext, get this with wfMessage()->plain()
671 * @param string|bool $icon Icon name, file in mw-config/images. Default: false
672 * @param string|bool $class Additional class name to add to the wrapper div. Default: false.
673 *
674 * @return string
675 */
676 public function getInfoBox( $text, $icon = false, $class = false ) {
677 $text = $this->parse( $text, true );
678 $icon = ( $icon == false ) ?
679 'images/info-32.png' :
680 'images/' . $icon;
681 $alt = wfMessage( 'config-information' )->text();
682
683 return Html::infoBox( $text, $icon, $alt, $class );
684 }
685
686 /**
687 * Get small text indented help for a preceding form field.
688 * Parameters like wfMessage().
689 *
690 * @param string $msg
691 * @return string
692 */
693 public function getHelpBox( $msg /*, ... */ ) {
694 $args = func_get_args();
695 array_shift( $args );
696 $args = array_map( 'htmlspecialchars', $args );
697 $text = wfMessage( $msg, $args )->useDatabase( false )->plain();
698 $html = $this->parse( $text, true );
699
700 return "<div class=\"config-help-field-container\">\n" .
701 "<span class=\"config-help-field-hint\" title=\"" .
702 wfMessage( 'config-help-tooltip' )->escaped() . "\">" .
703 wfMessage( 'config-help' )->escaped() . "</span>\n" .
704 "<span class=\"config-help-field-data\">" . $html . "</span>\n" .
705 "</div>\n";
706 }
707
708 /**
709 * Output a help box.
710 * @param string $msg Key for wfMessage()
711 */
712 public function showHelpBox( $msg /*, ... */ ) {
713 $args = func_get_args();
714 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
715 $this->output->addHTML( $html );
716 }
717
718 /**
719 * Show a short informational message.
720 * Output looks like a list.
721 *
722 * @param string $msg
723 */
724 public function showMessage( $msg /*, ... */ ) {
725 $args = func_get_args();
726 array_shift( $args );
727 $html = '<div class="config-message">' .
728 $this->parse( wfMessage( $msg, $args )->useDatabase( false )->plain() ) .
729 "</div>\n";
730 $this->output->addHTML( $html );
731 }
732
733 /**
734 * @param Status $status
735 */
736 public function showStatusMessage( Status $status ) {
737 $errors = array_merge( $status->getErrorsArray(), $status->getWarningsArray() );
738 foreach ( $errors as $error ) {
739 call_user_func_array( array( $this, 'showMessage' ), $error );
740 }
741 }
742
743 /**
744 * Label a control by wrapping a config-input div around it and putting a
745 * label before it.
746 *
747 * @param string $msg
748 * @param string $forId
749 * @param string $contents
750 * @param string $helpData
751 * @return string
752 */
753 public function label( $msg, $forId, $contents, $helpData = "" ) {
754 if ( strval( $msg ) == '' ) {
755 $labelText = '&#160;';
756 } else {
757 $labelText = wfMessage( $msg )->escaped();
758 }
759
760 $attributes = array( 'class' => 'config-label' );
761
762 if ( $forId ) {
763 $attributes['for'] = $forId;
764 }
765
766 return "<div class=\"config-block\">\n" .
767 " <div class=\"config-block-label\">\n" .
768 Xml::tags( 'label',
769 $attributes,
770 $labelText
771 ) . "\n" .
772 $helpData .
773 " </div>\n" .
774 " <div class=\"config-block-elements\">\n" .
775 $contents .
776 " </div>\n" .
777 "</div>\n";
778 }
779
780 /**
781 * Get a labelled text box to configure a variable.
782 *
783 * @param mixed[] $params
784 * Parameters are:
785 * var: The variable to be configured (required)
786 * label: The message name for the label (required)
787 * attribs: Additional attributes for the input element (optional)
788 * controlName: The name for the input element (optional)
789 * value: The current value of the variable (optional)
790 * help: The html for the help text (optional)
791 *
792 * @return string
793 */
794 public function getTextBox( $params ) {
795 if ( !isset( $params['controlName'] ) ) {
796 $params['controlName'] = 'config_' . $params['var'];
797 }
798
799 if ( !isset( $params['value'] ) ) {
800 $params['value'] = $this->getVar( $params['var'] );
801 }
802
803 if ( !isset( $params['attribs'] ) ) {
804 $params['attribs'] = array();
805 }
806 if ( !isset( $params['help'] ) ) {
807 $params['help'] = "";
808 }
809
810 return $this->label(
811 $params['label'],
812 $params['controlName'],
813 Xml::input(
814 $params['controlName'],
815 30, // intended to be overridden by CSS
816 $params['value'],
817 $params['attribs'] + array(
818 'id' => $params['controlName'],
819 'class' => 'config-input-text',
820 'tabindex' => $this->nextTabIndex()
821 )
822 ),
823 $params['help']
824 );
825 }
826
827 /**
828 * Get a labelled textarea to configure a variable
829 *
830 * @param mixed[] $params
831 * Parameters are:
832 * var: The variable to be configured (required)
833 * label: The message name for the label (required)
834 * attribs: Additional attributes for the input element (optional)
835 * controlName: The name for the input element (optional)
836 * value: The current value of the variable (optional)
837 * help: The html for the help text (optional)
838 *
839 * @return string
840 */
841 public function getTextArea( $params ) {
842 if ( !isset( $params['controlName'] ) ) {
843 $params['controlName'] = 'config_' . $params['var'];
844 }
845
846 if ( !isset( $params['value'] ) ) {
847 $params['value'] = $this->getVar( $params['var'] );
848 }
849
850 if ( !isset( $params['attribs'] ) ) {
851 $params['attribs'] = array();
852 }
853 if ( !isset( $params['help'] ) ) {
854 $params['help'] = "";
855 }
856
857 return $this->label(
858 $params['label'],
859 $params['controlName'],
860 Xml::textarea(
861 $params['controlName'],
862 $params['value'],
863 30,
864 5,
865 $params['attribs'] + array(
866 'id' => $params['controlName'],
867 'class' => 'config-input-text',
868 'tabindex' => $this->nextTabIndex()
869 )
870 ),
871 $params['help']
872 );
873 }
874
875 /**
876 * Get a labelled password box to configure a variable.
877 *
878 * Implements password hiding
879 * @param mixed[] $params
880 * Parameters are:
881 * var: The variable to be configured (required)
882 * label: The message name for the label (required)
883 * attribs: Additional attributes for the input element (optional)
884 * controlName: The name for the input element (optional)
885 * value: The current value of the variable (optional)
886 * help: The html for the help text (optional)
887 *
888 * @return string
889 */
890 public function getPasswordBox( $params ) {
891 if ( !isset( $params['value'] ) ) {
892 $params['value'] = $this->getVar( $params['var'] );
893 }
894
895 if ( !isset( $params['attribs'] ) ) {
896 $params['attribs'] = array();
897 }
898
899 $params['value'] = $this->getFakePassword( $params['value'] );
900 $params['attribs']['type'] = 'password';
901
902 return $this->getTextBox( $params );
903 }
904
905 /**
906 * Get a labelled checkbox to configure a boolean variable.
907 *
908 * @param mixed[] $params
909 * Parameters are:
910 * var: The variable to be configured (required)
911 * label: The message name for the label (required)
912 * attribs: Additional attributes for the input element (optional)
913 * controlName: The name for the input element (optional)
914 * value: The current value of the variable (optional)
915 * help: The html for the help text (optional)
916 *
917 * @return string
918 */
919 public function getCheckBox( $params ) {
920 if ( !isset( $params['controlName'] ) ) {
921 $params['controlName'] = 'config_' . $params['var'];
922 }
923
924 if ( !isset( $params['value'] ) ) {
925 $params['value'] = $this->getVar( $params['var'] );
926 }
927
928 if ( !isset( $params['attribs'] ) ) {
929 $params['attribs'] = array();
930 }
931 if ( !isset( $params['help'] ) ) {
932 $params['help'] = "";
933 }
934 if ( isset( $params['rawtext'] ) ) {
935 $labelText = $params['rawtext'];
936 } else {
937 $labelText = $this->parse( wfMessage( $params['label'] )->text() );
938 }
939
940 return "<div class=\"config-input-check\">\n" .
941 $params['help'] .
942 "<label>\n" .
943 Xml::check(
944 $params['controlName'],
945 $params['value'],
946 $params['attribs'] + array(
947 'id' => $params['controlName'],
948 'tabindex' => $this->nextTabIndex(),
949 )
950 ) .
951 $labelText . "\n" .
952 "</label>\n" .
953 "</div>\n";
954 }
955
956 /**
957 * Get a set of labelled radio buttons.
958 *
959 * @param mixed[] $params
960 * Parameters are:
961 * var: The variable to be configured (required)
962 * label: The message name for the label (required)
963 * itemLabelPrefix: The message name prefix for the item labels (required)
964 * itemLabels: List of message names to use for the item labels instead
965 * of itemLabelPrefix, keyed by values
966 * values: List of allowed values (required)
967 * itemAttribs: Array of attribute arrays, outer key is the value name (optional)
968 * commonAttribs: Attribute array applied to all items
969 * controlName: The name for the input element (optional)
970 * value: The current value of the variable (optional)
971 * help: The html for the help text (optional)
972 *
973 * @return string
974 */
975 public function getRadioSet( $params ) {
976 $items = $this->getRadioElements( $params );
977
978 if ( !isset( $params['label'] ) ) {
979 $label = '';
980 } else {
981 $label = $params['label'];
982 }
983
984 if ( !isset( $params['controlName'] ) ) {
985 $params['controlName'] = 'config_' . $params['var'];
986 }
987
988 if ( !isset( $params['help'] ) ) {
989 $params['help'] = "";
990 }
991
992 $s = "<ul>\n";
993 foreach ( $items as $value => $item ) {
994 $s .= "<li>$item</li>\n";
995 }
996 $s .= "</ul>\n";
997
998 return $this->label( $label, $params['controlName'], $s, $params['help'] );
999 }
1000
1001 /**
1002 * Get a set of labelled radio buttons. You probably want to use getRadioSet(), not this.
1003 *
1004 * @see getRadioSet
1005 *
1006 * @return array
1007 */
1008 public function getRadioElements( $params ) {
1009 if ( !isset( $params['controlName'] ) ) {
1010 $params['controlName'] = 'config_' . $params['var'];
1011 }
1012
1013 if ( !isset( $params['value'] ) ) {
1014 $params['value'] = $this->getVar( $params['var'] );
1015 }
1016
1017 $items = array();
1018
1019 foreach ( $params['values'] as $value ) {
1020 $itemAttribs = array();
1021
1022 if ( isset( $params['commonAttribs'] ) ) {
1023 $itemAttribs = $params['commonAttribs'];
1024 }
1025
1026 if ( isset( $params['itemAttribs'][$value] ) ) {
1027 $itemAttribs = $params['itemAttribs'][$value] + $itemAttribs;
1028 }
1029
1030 $checked = $value == $params['value'];
1031 $id = $params['controlName'] . '_' . $value;
1032 $itemAttribs['id'] = $id;
1033 $itemAttribs['tabindex'] = $this->nextTabIndex();
1034
1035 $items[$value] =
1036 Xml::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
1037 '&#160;' .
1038 Xml::tags( 'label', array( 'for' => $id ), $this->parse(
1039 isset( $params['itemLabels'] ) ?
1040 wfMessage( $params['itemLabels'][$value] )->plain() :
1041 wfMessage( $params['itemLabelPrefix'] . strtolower( $value ) )->plain()
1042 ) );
1043 }
1044
1045 return $items;
1046 }
1047
1048 /**
1049 * Output an error or warning box using a Status object.
1050 *
1051 * @param Status $status
1052 */
1053 public function showStatusBox( $status ) {
1054 if ( !$status->isGood() ) {
1055 $text = $status->getWikiText();
1056
1057 if ( $status->isOk() ) {
1058 $box = $this->getWarningBox( $text );
1059 } else {
1060 $box = $this->getErrorBox( $text );
1061 }
1062
1063 $this->output->addHTML( $box );
1064 }
1065 }
1066
1067 /**
1068 * Convenience function to set variables based on form data.
1069 * Assumes that variables containing "password" in the name are (potentially
1070 * fake) passwords.
1071 *
1072 * @param string[] $varNames
1073 * @param string $prefix The prefix added to variables to obtain form names
1074 *
1075 * @return string[]
1076 */
1077 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
1078 $newValues = array();
1079
1080 foreach ( $varNames as $name ) {
1081 $value = $this->request->getVal( $prefix . $name );
1082 // bug 30524, do not trim passwords
1083 if ( stripos( $name, 'password' ) === false ) {
1084 $value = trim( $value );
1085 }
1086 $newValues[$name] = $value;
1087
1088 if ( $value === null ) {
1089 // Checkbox?
1090 $this->setVar( $name, false );
1091 } else {
1092 if ( stripos( $name, 'password' ) !== false ) {
1093 $this->setPassword( $name, $value );
1094 } else {
1095 $this->setVar( $name, $value );
1096 }
1097 }
1098 }
1099
1100 return $newValues;
1101 }
1102
1103 /**
1104 * Helper for Installer::docLink()
1105 *
1106 * @param string $page
1107 *
1108 * @return string
1109 */
1110 protected function getDocUrl( $page ) {
1111 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1112
1113 if ( in_array( $this->currentPageName, $this->pageSequence ) ) {
1114 $url .= '&lastPage=' . urlencode( $this->currentPageName );
1115 }
1116
1117 return $url;
1118 }
1119
1120 /**
1121 * Extension tag hook for a documentation link.
1122 *
1123 * @param string $linkText
1124 * @param string[] $attribs
1125 * @param Parser $parser Unused
1126 *
1127 * @return string
1128 */
1129 public function docLink( $linkText, $attribs, $parser ) {
1130 $url = $this->getDocUrl( $attribs['href'] );
1131
1132 return '<a href="' . htmlspecialchars( $url ) . '">' .
1133 htmlspecialchars( $linkText ) .
1134 '</a>';
1135 }
1136
1137 /**
1138 * Helper for "Download LocalSettings" link on WebInstall_Complete
1139 *
1140 * @param string $text Unused
1141 * @param string[] $attribs Unused
1142 * @param Parser $parser Unused
1143 *
1144 * @return string Html for download link
1145 */
1146 public function downloadLinkHook( $text, $attribs, $parser ) {
1147 $anchor = Html::rawElement( 'a',
1148 array( 'href' => $this->getURL( array( 'localsettings' => 1 ) ) ),
1149 wfMessage( 'config-download-localsettings' )->parse()
1150 );
1151
1152 return Html::rawElement( 'div', array( 'class' => 'config-download-link' ), $anchor );
1153 }
1154
1155 /**
1156 * @return bool
1157 */
1158 public function envCheckPath() {
1159 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
1160 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
1161 // to get the path to the current script... hopefully it's reliable. SIGH
1162 $path = false;
1163 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
1164 $path = $_SERVER['PHP_SELF'];
1165 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
1166 $path = $_SERVER['SCRIPT_NAME'];
1167 }
1168 if ( $path === false ) {
1169 $this->showError( 'config-no-uri' );
1170 return false;
1171 }
1172
1173 return parent::envCheckPath();
1174 }
1175
1176 public function envPrepPath() {
1177 parent::envPrepPath();
1178 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
1179 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
1180 // to get the path to the current script... hopefully it's reliable. SIGH
1181 $path = false;
1182 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
1183 $path = $_SERVER['PHP_SELF'];
1184 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
1185 $path = $_SERVER['SCRIPT_NAME'];
1186 }
1187 if ( $path !== false ) {
1188 $scriptPath = preg_replace( '{^(.*)/(mw-)?config.*$}', '$1', $path );
1189
1190 $this->setVar( 'wgScriptPath', "$scriptPath" );
1191 // Update variables set from Setup.php that are derived from wgScriptPath
1192 $this->setVar( 'wgScript', "$scriptPath/index.php" );
1193 $this->setVar( 'wgLoadScript', "$scriptPath/load.php" );
1194 $this->setVar( 'wgStylePath', "$scriptPath/skins" );
1195 $this->setVar( 'wgLocalStylePath', "$scriptPath/skins" );
1196 $this->setVar( 'wgExtensionAssetsPath', "$scriptPath/extensions" );
1197 $this->setVar( 'wgUploadPath', "$scriptPath/images" );
1198 $this->setVar( 'wgResourceBasePath', "$scriptPath" );
1199 }
1200 }
1201
1202 /**
1203 * @return string
1204 */
1205 protected function envGetDefaultServer() {
1206 return WebRequest::detectServer();
1207 }
1208
1209 /**
1210 * Output stylesheet for web installer pages
1211 */
1212 public function outputCss() {
1213 $this->request->response()->header( 'Content-type: text/css' );
1214 echo $this->output->getCSS();
1215 }
1216
1217 /**
1218 * @return string[]
1219 */
1220 public function getPhpErrors() {
1221 return $this->phpErrors;
1222 }
1223
1224 }