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