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