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