Few bits of documentation
[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
16 WebInstaller extends Installer {
17
18 /**
19 * @var WebInstallerOutput
20 */
21 public $output;
22
23 /**
24 * WebRequest object.
25 *
26 * @var WebRequest
27 */
28 public $request;
29
30 /**
31 * Cached session array.
32 *
33 * @var array
34 */
35 protected $session;
36
37 /**
38 * Captured PHP error text. Temporary.
39 * @var array
40 */
41 protected $phpErrors;
42
43 /**
44 * The main sequence of page names. These will be displayed in turn.
45 * To add one:
46 * * Add it here
47 * * Add a config-page-<name> message
48 * * Add a WebInstaller_<name> class
49 * @var array
50 */
51 public $pageSequence = array(
52 'Language',
53 'ExistingWiki',
54 'Welcome',
55 'DBConnect',
56 'Upgrade',
57 'DBSettings',
58 'Name',
59 'Options',
60 'Install',
61 'Complete',
62 );
63
64 /**
65 * Out of sequence pages, selectable by the user at any time.
66 * @var array
67 */
68 protected $otherPages = array(
69 'Restart',
70 'Readme',
71 'ReleaseNotes',
72 'Copying',
73 'UpgradeDoc', // Can't use Upgrade due to Upgrade step
74 );
75
76 /**
77 * Array of pages which have declared that they have been submitted, have validated
78 * their input, and need no further processing.
79 * @var array
80 */
81 protected $happyPages;
82
83 /**
84 * List of "skipped" pages. These are pages that will automatically continue
85 * to the next page on any GET request. To avoid breaking the "back" button,
86 * they need to be skipped during a back operation.
87 * @var array
88 */
89 protected $skippedPages;
90
91 /**
92 * Flag indicating that session data may have been lost.
93 * @var bool
94 */
95 public $showSessionWarning = false;
96
97 /**
98 * Numeric index of the page we're on
99 * @var int
100 */
101 protected $tabIndex = 1;
102
103 /**
104 * Name of the page we're on
105 * @var string
106 */
107 protected $currentPageName;
108
109 /**
110 * Constructor.
111 *
112 * @param $request WebRequest
113 */
114 public function __construct( WebRequest $request ) {
115 parent::__construct();
116 $this->output = new WebInstallerOutput( $this );
117 $this->request = $request;
118
119 // Add parser hooks
120 global $wgParser;
121 $wgParser->setHook( 'downloadlink', array( $this, 'downloadLinkHook' ) );
122 $wgParser->setHook( 'doclink', array( $this, 'docLink' ) );
123 }
124
125 /**
126 * Main entry point.
127 *
128 * @param $session Array: initial session array
129 *
130 * @return Array: new session array
131 */
132 public function execute( array $session ) {
133 $this->session = $session;
134
135 if ( isset( $session['settings'] ) ) {
136 $this->settings = $session['settings'] + $this->settings;
137 }
138
139 $this->exportVars();
140 $this->setupLanguage();
141
142 if( ( $this->getVar( '_InstallDone' ) || $this->getVar( '_UpgradeDone' ) )
143 && $this->request->getVal( 'localsettings' ) )
144 {
145 $this->request->response()->header( 'Content-type: application/x-httpd-php' );
146 $this->request->response()->header(
147 'Content-Disposition: attachment; filename="LocalSettings.php"'
148 );
149
150 $ls = new LocalSettingsGenerator( $this );
151 $rightsProfile = $this->rightsProfiles[$this->getVar( '_RightsProfile' )];
152 foreach( $rightsProfile as $group => $rightsArr ) {
153 $ls->setGroupRights( $group, $rightsArr );
154 }
155 echo $ls->getText();
156 return $this->session;
157 }
158
159 $cssDir = $this->request->getVal( 'css' );
160 if( $cssDir ) {
161 $cssDir = ( $cssDir == 'rtl' ? 'rtl' : 'ltr' );
162 $this->request->response()->header( 'Content-type: text/css' );
163 echo $this->output->getCSS( $cssDir );
164 return $this->session;
165 }
166
167 if ( isset( $session['happyPages'] ) ) {
168 $this->happyPages = $session['happyPages'];
169 } else {
170 $this->happyPages = array();
171 }
172
173 if ( isset( $session['skippedPages'] ) ) {
174 $this->skippedPages = $session['skippedPages'];
175 } else {
176 $this->skippedPages = array();
177 }
178
179 $lowestUnhappy = $this->getLowestUnhappy();
180
181 # Special case for Creative Commons partner chooser box.
182 if ( $this->request->getVal( 'SubmitCC' ) ) {
183 $page = $this->getPageByName( 'Options' );
184 $this->output->useShortHeader();
185 $this->output->allowFrames();
186 $page->submitCC();
187 return $this->finish();
188 }
189
190 if ( $this->request->getVal( 'ShowCC' ) ) {
191 $page = $this->getPageByName( 'Options' );
192 $this->output->useShortHeader();
193 $this->output->allowFrames();
194 $this->output->addHTML( $page->getCCDoneBox() );
195 return $this->finish();
196 }
197
198 # Get the page name.
199 $pageName = $this->request->getVal( 'page' );
200
201 if ( in_array( $pageName, $this->otherPages ) ) {
202 # Out of sequence
203 $pageId = false;
204 $page = $this->getPageByName( $pageName );
205 } else {
206 # Main sequence
207 if ( !$pageName || !in_array( $pageName, $this->pageSequence ) ) {
208 $pageId = $lowestUnhappy;
209 } else {
210 $pageId = array_search( $pageName, $this->pageSequence );
211 }
212
213 # If necessary, move back to the lowest-numbered unhappy page
214 if ( $pageId > $lowestUnhappy ) {
215 $pageId = $lowestUnhappy;
216 if ( $lowestUnhappy == 0 ) {
217 # Knocked back to start, possible loss of session data.
218 $this->showSessionWarning = true;
219 }
220 }
221
222 $pageName = $this->pageSequence[$pageId];
223 $page = $this->getPageByName( $pageName );
224 }
225
226 # If a back button was submitted, go back without submitting the form data.
227 if ( $this->request->wasPosted() && $this->request->getBool( 'submit-back' ) ) {
228 if ( $this->request->getVal( 'lastPage' ) ) {
229 $nextPage = $this->request->getVal( 'lastPage' );
230 } elseif ( $pageId !== false ) {
231 # Main sequence page
232 # Skip the skipped pages
233 $nextPageId = $pageId;
234
235 do {
236 $nextPageId--;
237 $nextPage = $this->pageSequence[$nextPageId];
238 } while( isset( $this->skippedPages[$nextPage] ) );
239 } else {
240 $nextPage = $this->pageSequence[$lowestUnhappy];
241 }
242
243 $this->output->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
244 return $this->finish();
245 }
246
247 # Execute the page.
248 $this->currentPageName = $page->getName();
249 $this->startPageWrapper( $pageName );
250
251 if( $page->isSlow() ) {
252 $this->disableTimeLimit();
253 }
254
255 $result = $page->execute();
256
257 $this->endPageWrapper();
258
259 if ( $result == 'skip' ) {
260 # Page skipped without explicit submission.
261 # Skip it when we click "back" so that we don't just go forward again.
262 $this->skippedPages[$pageName] = true;
263 $result = 'continue';
264 } else {
265 unset( $this->skippedPages[$pageName] );
266 }
267
268 # If it was posted, the page can request a continue to the next page.
269 if ( $result === 'continue' && !$this->output->headerDone() ) {
270 if ( $pageId !== false ) {
271 $this->happyPages[$pageId] = true;
272 }
273
274 $lowestUnhappy = $this->getLowestUnhappy();
275
276 if ( $this->request->getVal( 'lastPage' ) ) {
277 $nextPage = $this->request->getVal( 'lastPage' );
278 } elseif ( $pageId !== false ) {
279 $nextPage = $this->pageSequence[$pageId + 1];
280 } else {
281 $nextPage = $this->pageSequence[$lowestUnhappy];
282 }
283
284 if ( array_search( $nextPage, $this->pageSequence ) > $lowestUnhappy ) {
285 $nextPage = $this->pageSequence[$lowestUnhappy];
286 }
287
288 $this->output->redirect( $this->getUrl( array( 'page' => $nextPage ) ) );
289 }
290
291 return $this->finish();
292 }
293
294 /**
295 * Find the next page in sequence that hasn't been completed
296 * @return int
297 */
298 public function getLowestUnhappy() {
299 if ( count( $this->happyPages ) == 0 ) {
300 return 0;
301 } else {
302 return max( array_keys( $this->happyPages ) ) + 1;
303 }
304 }
305
306 /**
307 * Start the PHP session. This may be called before execute() to start the PHP session.
308 *
309 * @return bool
310 */
311 public function startSession() {
312 if( wfIniGetBool( 'session.auto_start' ) || session_id() ) {
313 // Done already
314 return true;
315 }
316
317 $this->phpErrors = array();
318 set_error_handler( array( $this, 'errorHandler' ) );
319 session_start();
320 restore_error_handler();
321
322 if ( $this->phpErrors ) {
323 $this->showError( 'config-session-error', $this->phpErrors[0] );
324 return false;
325 }
326
327 return true;
328 }
329
330 /**
331 * Get a hash of data identifying this MW installation.
332 *
333 * This is used by mw-config/index.php to prevent multiple installations of MW
334 * on the same cookie domain from interfering with each other.
335 *
336 * @return string
337 */
338 public function getFingerprint() {
339 // Get the base URL of the installation
340 $url = $this->request->getFullRequestURL();
341 if ( preg_match( '!^(.*\?)!', $url, $m) ) {
342 // Trim query string
343 $url = $m[1];
344 }
345 if ( preg_match( '!^(.*)/[^/]*/[^/]*$!', $url, $m ) ) {
346 // This... seems to try to get the base path from
347 // the /mw-config/index.php. Kinda scary though?
348 $url = $m[1];
349 }
350 return md5( serialize( array(
351 'local path' => dirname( dirname( __FILE__ ) ),
352 'url' => $url,
353 'version' => $GLOBALS['wgVersion']
354 ) ) );
355 }
356
357 /**
358 * Show an error message in a box. Parameters are like wfMsg().
359 * @param $msg
360 */
361 public function showError( $msg /*...*/ ) {
362 $args = func_get_args();
363 array_shift( $args );
364 $args = array_map( 'htmlspecialchars', $args );
365 $msg = wfMsgReal( $msg, $args, false, false, false );
366 $this->output->addHTML( $this->getErrorBox( $msg ) );
367 }
368
369 /**
370 * Temporary error handler for session start debugging.
371 * @param $errno
372 * @param $errstr string
373 */
374 public function errorHandler( $errno, $errstr ) {
375 $this->phpErrors[] = $errstr;
376 }
377
378 /**
379 * Clean up from execute()
380 *
381 * @return array
382 */
383 public function finish() {
384 $this->output->output();
385
386 $this->session['happyPages'] = $this->happyPages;
387 $this->session['skippedPages'] = $this->skippedPages;
388 $this->session['settings'] = $this->settings;
389
390 return $this->session;
391 }
392
393 /**
394 * We're restarting the installation, reset the session, happyPages, etc
395 */
396 public function reset() {
397 $this->session = array();
398 $this->happyPages = array();
399 $this->settings = array();
400 }
401
402 /**
403 * Get a URL for submission back to the same script.
404 *
405 * @param $query array
406 * @return string
407 */
408 public function getUrl( $query = array() ) {
409 $url = $this->request->getRequestURL();
410 # Remove existing query
411 $url = preg_replace( '/\?.*$/', '', $url );
412
413 if ( $query ) {
414 $url .= '?' . wfArrayToCGI( $query );
415 }
416
417 return $url;
418 }
419
420 /**
421 * Get a WebInstallerPage by name.
422 *
423 * @param $pageName String
424 * @return WebInstallerPage
425 */
426 public function getPageByName( $pageName ) {
427 $pageClass = 'WebInstaller_' . $pageName;
428
429 return new $pageClass( $this );
430 }
431
432 /**
433 * Get a session variable.
434 *
435 * @param $name String
436 * @param $default
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::getLanguageNames();
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 $text = $status->getWikiText();
682 $this->output->addWikiText(
683 "<div class=\"config-message\">\n" .
684 $text .
685 "</div>"
686 );
687 }
688
689 /**
690 * Label a control by wrapping a config-input div around it and putting a
691 * label before it.
692 *
693 * @param $msg
694 * @param $forId
695 * @param $contents
696 * @param $helpData string
697 * @return string
698 */
699 public function label( $msg, $forId, $contents, $helpData = "" ) {
700 if ( strval( $msg ) == '' ) {
701 $labelText = '&#160;';
702 } else {
703 $labelText = wfMsgHtml( $msg );
704 }
705
706 $attributes = array( 'class' => 'config-label' );
707
708 if ( $forId ) {
709 $attributes['for'] = $forId;
710 }
711
712 return
713 "<div class=\"config-block\">\n" .
714 " <div class=\"config-block-label\">\n" .
715 Xml::tags( 'label',
716 $attributes,
717 $labelText ) . "\n" .
718 $helpData .
719 " </div>\n" .
720 " <div class=\"config-block-elements\">\n" .
721 $contents .
722 " </div>\n" .
723 "</div>\n";
724 }
725
726 /**
727 * Get a labelled text box to configure a variable.
728 *
729 * @param $params Array
730 * Parameters are:
731 * var: The variable to be configured (required)
732 * label: The message name for the label (required)
733 * attribs: Additional attributes for the input element (optional)
734 * controlName: The name for the input element (optional)
735 * value: The current value of the variable (optional)
736 * help: The html for the help text (optional)
737 *
738 * @return string
739 */
740 public function getTextBox( $params ) {
741 if ( !isset( $params['controlName'] ) ) {
742 $params['controlName'] = 'config_' . $params['var'];
743 }
744
745 if ( !isset( $params['value'] ) ) {
746 $params['value'] = $this->getVar( $params['var'] );
747 }
748
749 if ( !isset( $params['attribs'] ) ) {
750 $params['attribs'] = array();
751 }
752 if ( !isset( $params['help'] ) ) {
753 $params['help'] = "";
754 }
755 return
756 $this->label(
757 $params['label'],
758 $params['controlName'],
759 Xml::input(
760 $params['controlName'],
761 30, // intended to be overridden by CSS
762 $params['value'],
763 $params['attribs'] + array(
764 'id' => $params['controlName'],
765 'class' => 'config-input-text',
766 'tabindex' => $this->nextTabIndex()
767 )
768 ),
769 $params['help']
770 );
771 }
772
773 /**
774 * Get a labelled textarea to configure a variable
775 *
776 * @param $params Array
777 * Parameters are:
778 * var: The variable to be configured (required)
779 * label: The message name for the label (required)
780 * attribs: Additional attributes for the input element (optional)
781 * controlName: The name for the input element (optional)
782 * value: The current value of the variable (optional)
783 * help: The html for the help text (optional)
784 *
785 * @return string
786 */
787 public function getTextArea( $params ) {
788 if ( !isset( $params['controlName'] ) ) {
789 $params['controlName'] = 'config_' . $params['var'];
790 }
791
792 if ( !isset( $params['value'] ) ) {
793 $params['value'] = $this->getVar( $params['var'] );
794 }
795
796 if ( !isset( $params['attribs'] ) ) {
797 $params['attribs'] = array();
798 }
799 if ( !isset( $params['help'] ) ) {
800 $params['help'] = "";
801 }
802 return
803 $this->label(
804 $params['label'],
805 $params['controlName'],
806 Xml::textarea(
807 $params['controlName'],
808 $params['value'],
809 30,
810 5,
811 $params['attribs'] + array(
812 'id' => $params['controlName'],
813 'class' => 'config-input-text',
814 'tabindex' => $this->nextTabIndex()
815 )
816 ),
817 $params['help']
818 );
819 }
820
821 /**
822 * Get a labelled password box to configure a variable.
823 *
824 * Implements password hiding
825 * @param $params Array
826 * Parameters are:
827 * var: The variable to be configured (required)
828 * label: The message name for the label (required)
829 * attribs: Additional attributes for the input element (optional)
830 * controlName: The name for the input element (optional)
831 * value: The current value of the variable (optional)
832 * help: The html for the help text (optional)
833 *
834 * @return string
835 */
836 public function getPasswordBox( $params ) {
837 if ( !isset( $params['value'] ) ) {
838 $params['value'] = $this->getVar( $params['var'] );
839 }
840
841 if ( !isset( $params['attribs'] ) ) {
842 $params['attribs'] = array();
843 }
844
845 $params['value'] = $this->getFakePassword( $params['value'] );
846 $params['attribs']['type'] = 'password';
847
848 return $this->getTextBox( $params );
849 }
850
851 /**
852 * Get a labelled checkbox to configure a boolean variable.
853 *
854 * @param $params Array
855 * Parameters are:
856 * var: The variable to be configured (required)
857 * label: The message name for the label (required)
858 * attribs: Additional attributes for the input element (optional)
859 * controlName: The name for the input element (optional)
860 * value: The current value of the variable (optional)
861 * help: The html for the help text (optional)
862 *
863 * @return string
864 */
865 public function getCheckBox( $params ) {
866 if ( !isset( $params['controlName'] ) ) {
867 $params['controlName'] = 'config_' . $params['var'];
868 }
869
870 if ( !isset( $params['value'] ) ) {
871 $params['value'] = $this->getVar( $params['var'] );
872 }
873
874 if ( !isset( $params['attribs'] ) ) {
875 $params['attribs'] = array();
876 }
877 if ( !isset( $params['help'] ) ) {
878 $params['help'] = "";
879 }
880 if( isset( $params['rawtext'] ) ) {
881 $labelText = $params['rawtext'];
882 } else {
883 $labelText = $this->parse( wfMsg( $params['label'] ) );
884 }
885
886 return
887 "<div class=\"config-input-check\">\n" .
888 $params['help'] .
889 "<label>\n" .
890 Xml::check(
891 $params['controlName'],
892 $params['value'],
893 $params['attribs'] + array(
894 'id' => $params['controlName'],
895 'tabindex' => $this->nextTabIndex(),
896 )
897 ) .
898 $labelText . "\n" .
899 "</label>\n" .
900 "</div>\n";
901 }
902
903 /**
904 * Get a set of labelled radio buttons.
905 *
906 * @param $params Array
907 * Parameters are:
908 * var: The variable to be configured (required)
909 * label: The message name for the label (required)
910 * itemLabelPrefix: The message name prefix for the item labels (required)
911 * values: List of allowed values (required)
912 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
913 * commonAttribs Attribute array applied to all items
914 * controlName: The name for the input element (optional)
915 * value: The current value of the variable (optional)
916 * help: The html for the help text (optional)
917 *
918 * @return string
919 */
920 public function getRadioSet( $params ) {
921 if ( !isset( $params['controlName'] ) ) {
922 $params['controlName'] = 'config_' . $params['var'];
923 }
924
925 if ( !isset( $params['value'] ) ) {
926 $params['value'] = $this->getVar( $params['var'] );
927 }
928
929 if ( !isset( $params['label'] ) ) {
930 $label = '';
931 } else {
932 $label = $params['label'];
933 }
934 if ( !isset( $params['help'] ) ) {
935 $params['help'] = "";
936 }
937 $s = "<ul>\n";
938 foreach ( $params['values'] as $value ) {
939 $itemAttribs = array();
940
941 if ( isset( $params['commonAttribs'] ) ) {
942 $itemAttribs = $params['commonAttribs'];
943 }
944
945 if ( isset( $params['itemAttribs'][$value] ) ) {
946 $itemAttribs = $params['itemAttribs'][$value] + $itemAttribs;
947 }
948
949 $checked = $value == $params['value'];
950 $id = $params['controlName'] . '_' . $value;
951 $itemAttribs['id'] = $id;
952 $itemAttribs['tabindex'] = $this->nextTabIndex();
953
954 $s .=
955 '<li>' .
956 Xml::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
957 '&#160;' .
958 Xml::tags( 'label', array( 'for' => $id ), $this->parse(
959 wfMsgNoTrans( $params['itemLabelPrefix'] . strtolower( $value ) )
960 ) ) .
961 "</li>\n";
962 }
963
964 $s .= "</ul>\n";
965
966 return $this->label( $label, $params['controlName'], $s, $params['help'] );
967 }
968
969 /**
970 * Output an error or warning box using a Status object.
971 *
972 * @param $status Status
973 */
974 public function showStatusBox( $status ) {
975 if( !$status->isGood() ) {
976 $text = $status->getWikiText();
977
978 if( $status->isOk() ) {
979 $box = $this->getWarningBox( $text );
980 } else {
981 $box = $this->getErrorBox( $text );
982 }
983
984 $this->output->addHTML( $box );
985 }
986 }
987
988 /**
989 * Convenience function to set variables based on form data.
990 * Assumes that variables containing "password" in the name are (potentially
991 * fake) passwords.
992 *
993 * @param $varNames Array
994 * @param $prefix String: the prefix added to variables to obtain form names
995 *
996 * @return array
997 */
998 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
999 $newValues = array();
1000
1001 foreach ( $varNames as $name ) {
1002 $value = trim( $this->request->getVal( $prefix . $name ) );
1003 $newValues[$name] = $value;
1004
1005 if ( $value === null ) {
1006 // Checkbox?
1007 $this->setVar( $name, false );
1008 } else {
1009 if ( stripos( $name, 'password' ) !== false ) {
1010 $this->setPassword( $name, $value );
1011 } else {
1012 $this->setVar( $name, $value );
1013 }
1014 }
1015 }
1016
1017 return $newValues;
1018 }
1019
1020 /**
1021 * Helper for Installer::docLink()
1022 *
1023 * @param $page
1024 * @return string
1025 */
1026 protected function getDocUrl( $page ) {
1027 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1028
1029 if ( in_array( $this->currentPageName, $this->pageSequence ) ) {
1030 $url .= '&lastPage=' . urlencode( $this->currentPageName );
1031 }
1032
1033 return $url;
1034 }
1035
1036 /**
1037 * Extension tag hook for a documentation link.
1038 *
1039 * @param $linkText
1040 * @param $attribs
1041 * @param $parser
1042 * @return string
1043 */
1044 public function docLink( $linkText, $attribs, $parser ) {
1045 $url = $this->getDocUrl( $attribs['href'] );
1046 return '<a href="' . htmlspecialchars( $url ) . '">' .
1047 htmlspecialchars( $linkText ) .
1048 '</a>';
1049 }
1050
1051 /**
1052 * Helper for "Download LocalSettings" link on WebInstall_Complete
1053 *
1054 * @param $text
1055 * @param $attribs
1056 * @param $parser
1057 * @return String Html for download link
1058 */
1059 public function downloadLinkHook( $text, $attribs, $parser ) {
1060 $img = Html::element( 'img', array(
1061 'src' => '../skins/common/images/download-32.png',
1062 'width' => '32',
1063 'height' => '32',
1064 ) );
1065 $anchor = Html::rawElement( 'a',
1066 array( 'href' => $this->getURL( array( 'localsettings' => 1 ) ) ),
1067 $img . ' ' . wfMsgHtml( 'config-download-localsettings' ) );
1068 return Html::rawElement( 'div', array( 'class' => 'config-download-link' ), $anchor );
1069 }
1070
1071 /**
1072 * @return bool
1073 */
1074 public function envCheckPath( ) {
1075 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
1076 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
1077 // to get the path to the current script... hopefully it's reliable. SIGH
1078 $path = false;
1079 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
1080 $path = $_SERVER['PHP_SELF'];
1081 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
1082 $path = $_SERVER['SCRIPT_NAME'];
1083 }
1084 if ($path !== false) {
1085 $uri = preg_replace( '{^(.*)/(mw-)?config.*$}', '$1', $path );
1086 $this->setVar( 'wgScriptPath', $uri );
1087 } else {
1088 $this->showError( 'config-no-uri' );
1089 return false;
1090 }
1091 return parent::envCheckPath();
1092 }
1093 }