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