Merge "Fix checks to pass or display "return to" links in Special:UserLogin."
[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( dirname( __FILE__ ) ),
366 'url' => $url,
367 'version' => $GLOBALS['wgVersion']
368 ) ) );
369 }
370
371 /**
372 * Show an error message in a box. Parameters are like wfMsg().
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 wfMsg( 'config-page-' . strtolower( $currentPageName ) ) );
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 = wfMsg( 'config-page-' . strtolower( $pageName ) );
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 wfMsgNoTrans()
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 wfMsgNoTrans()
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 wfMsgNoTrans()
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 = wfMsg( 'config-information' );
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 wfMsg().
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\">" . wfMsgHtml( 'config-help' ) . "</span>\n" .
663 "<span class=\"mw-help-field-data\">" . $html . "</span>\n" .
664 "</div>\n";
665 }
666
667 /**
668 * Output a help box.
669 * @param $msg String key for wfMsg()
670 */
671 public function showHelpBox( $msg /*, ... */ ) {
672 $args = func_get_args();
673 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
674 $this->output->addHTML( $html );
675 }
676
677 /**
678 * Show a short informational message.
679 * Output looks like a list.
680 *
681 * @param $msg string
682 */
683 public function showMessage( $msg /*, ... */ ) {
684 $args = func_get_args();
685 array_shift( $args );
686 $html = '<div class="config-message">' .
687 $this->parse( wfMsgReal( $msg, $args, false, false, false ) ) .
688 "</div>\n";
689 $this->output->addHTML( $html );
690 }
691
692 /**
693 * @param $status Status
694 */
695 public function showStatusMessage( Status $status ) {
696 $errors = array_merge( $status->getErrorsArray(), $status->getWarningsArray() );
697 foreach ( $errors as $error ) {
698 call_user_func_array( array( $this, 'showMessage' ), $error );
699 }
700 }
701
702 /**
703 * Label a control by wrapping a config-input div around it and putting a
704 * label before it.
705 *
706 * @param $msg
707 * @param $forId
708 * @param $contents
709 * @param $helpData string
710 * @return string
711 */
712 public function label( $msg, $forId, $contents, $helpData = "" ) {
713 if ( strval( $msg ) == '' ) {
714 $labelText = '&#160;';
715 } else {
716 $labelText = wfMsgHtml( $msg );
717 }
718
719 $attributes = array( 'class' => 'config-label' );
720
721 if ( $forId ) {
722 $attributes['for'] = $forId;
723 }
724
725 return
726 "<div class=\"config-block\">\n" .
727 " <div class=\"config-block-label\">\n" .
728 Xml::tags( 'label',
729 $attributes,
730 $labelText ) . "\n" .
731 $helpData .
732 " </div>\n" .
733 " <div class=\"config-block-elements\">\n" .
734 $contents .
735 " </div>\n" .
736 "</div>\n";
737 }
738
739 /**
740 * Get a labelled text box to configure a variable.
741 *
742 * @param $params Array
743 * Parameters are:
744 * var: The variable to be configured (required)
745 * label: The message name for the label (required)
746 * attribs: Additional attributes for the input element (optional)
747 * controlName: The name for the input element (optional)
748 * value: The current value of the variable (optional)
749 * help: The html for the help text (optional)
750 *
751 * @return string
752 */
753 public function getTextBox( $params ) {
754 if ( !isset( $params['controlName'] ) ) {
755 $params['controlName'] = 'config_' . $params['var'];
756 }
757
758 if ( !isset( $params['value'] ) ) {
759 $params['value'] = $this->getVar( $params['var'] );
760 }
761
762 if ( !isset( $params['attribs'] ) ) {
763 $params['attribs'] = array();
764 }
765 if ( !isset( $params['help'] ) ) {
766 $params['help'] = "";
767 }
768 return
769 $this->label(
770 $params['label'],
771 $params['controlName'],
772 Xml::input(
773 $params['controlName'],
774 30, // intended to be overridden by CSS
775 $params['value'],
776 $params['attribs'] + array(
777 'id' => $params['controlName'],
778 'class' => 'config-input-text',
779 'tabindex' => $this->nextTabIndex()
780 )
781 ),
782 $params['help']
783 );
784 }
785
786 /**
787 * Get a labelled textarea to configure a variable
788 *
789 * @param $params Array
790 * Parameters are:
791 * var: The variable to be configured (required)
792 * label: The message name for the label (required)
793 * attribs: Additional attributes for the input element (optional)
794 * controlName: The name for the input element (optional)
795 * value: The current value of the variable (optional)
796 * help: The html for the help text (optional)
797 *
798 * @return string
799 */
800 public function getTextArea( $params ) {
801 if ( !isset( $params['controlName'] ) ) {
802 $params['controlName'] = 'config_' . $params['var'];
803 }
804
805 if ( !isset( $params['value'] ) ) {
806 $params['value'] = $this->getVar( $params['var'] );
807 }
808
809 if ( !isset( $params['attribs'] ) ) {
810 $params['attribs'] = array();
811 }
812 if ( !isset( $params['help'] ) ) {
813 $params['help'] = "";
814 }
815 return
816 $this->label(
817 $params['label'],
818 $params['controlName'],
819 Xml::textarea(
820 $params['controlName'],
821 $params['value'],
822 30,
823 5,
824 $params['attribs'] + array(
825 'id' => $params['controlName'],
826 'class' => 'config-input-text',
827 'tabindex' => $this->nextTabIndex()
828 )
829 ),
830 $params['help']
831 );
832 }
833
834 /**
835 * Get a labelled password box to configure a variable.
836 *
837 * Implements password hiding
838 * @param $params Array
839 * Parameters are:
840 * var: The variable to be configured (required)
841 * label: The message name for the label (required)
842 * attribs: Additional attributes for the input element (optional)
843 * controlName: The name for the input element (optional)
844 * value: The current value of the variable (optional)
845 * help: The html for the help text (optional)
846 *
847 * @return string
848 */
849 public function getPasswordBox( $params ) {
850 if ( !isset( $params['value'] ) ) {
851 $params['value'] = $this->getVar( $params['var'] );
852 }
853
854 if ( !isset( $params['attribs'] ) ) {
855 $params['attribs'] = array();
856 }
857
858 $params['value'] = $this->getFakePassword( $params['value'] );
859 $params['attribs']['type'] = 'password';
860
861 return $this->getTextBox( $params );
862 }
863
864 /**
865 * Get a labelled checkbox to configure a boolean variable.
866 *
867 * @param $params Array
868 * Parameters are:
869 * var: The variable to be configured (required)
870 * label: The message name for the label (required)
871 * attribs: Additional attributes for the input element (optional)
872 * controlName: The name for the input element (optional)
873 * value: The current value of the variable (optional)
874 * help: The html for the help text (optional)
875 *
876 * @return string
877 */
878 public function getCheckBox( $params ) {
879 if ( !isset( $params['controlName'] ) ) {
880 $params['controlName'] = 'config_' . $params['var'];
881 }
882
883 if ( !isset( $params['value'] ) ) {
884 $params['value'] = $this->getVar( $params['var'] );
885 }
886
887 if ( !isset( $params['attribs'] ) ) {
888 $params['attribs'] = array();
889 }
890 if ( !isset( $params['help'] ) ) {
891 $params['help'] = "";
892 }
893 if( isset( $params['rawtext'] ) ) {
894 $labelText = $params['rawtext'];
895 } else {
896 $labelText = $this->parse( wfMsg( $params['label'] ) );
897 }
898
899 return
900 "<div class=\"config-input-check\">\n" .
901 $params['help'] .
902 "<label>\n" .
903 Xml::check(
904 $params['controlName'],
905 $params['value'],
906 $params['attribs'] + array(
907 'id' => $params['controlName'],
908 'tabindex' => $this->nextTabIndex(),
909 )
910 ) .
911 $labelText . "\n" .
912 "</label>\n" .
913 "</div>\n";
914 }
915
916 /**
917 * Get a set of labelled radio buttons.
918 *
919 * @param $params Array
920 * Parameters are:
921 * var: The variable to be configured (required)
922 * label: The message name for the label (required)
923 * itemLabelPrefix: The message name prefix for the item labels (required)
924 * values: List of allowed values (required)
925 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
926 * commonAttribs Attribute array applied to all items
927 * controlName: The name for the input element (optional)
928 * value: The current value of the variable (optional)
929 * help: The html for the help text (optional)
930 *
931 * @return string
932 */
933 public function getRadioSet( $params ) {
934 if ( !isset( $params['controlName'] ) ) {
935 $params['controlName'] = 'config_' . $params['var'];
936 }
937
938 if ( !isset( $params['value'] ) ) {
939 $params['value'] = $this->getVar( $params['var'] );
940 }
941
942 if ( !isset( $params['label'] ) ) {
943 $label = '';
944 } else {
945 $label = $params['label'];
946 }
947 if ( !isset( $params['help'] ) ) {
948 $params['help'] = "";
949 }
950 $s = "<ul>\n";
951 foreach ( $params['values'] as $value ) {
952 $itemAttribs = array();
953
954 if ( isset( $params['commonAttribs'] ) ) {
955 $itemAttribs = $params['commonAttribs'];
956 }
957
958 if ( isset( $params['itemAttribs'][$value] ) ) {
959 $itemAttribs = $params['itemAttribs'][$value] + $itemAttribs;
960 }
961
962 $checked = $value == $params['value'];
963 $id = $params['controlName'] . '_' . $value;
964 $itemAttribs['id'] = $id;
965 $itemAttribs['tabindex'] = $this->nextTabIndex();
966
967 $s .=
968 '<li>' .
969 Xml::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
970 '&#160;' .
971 Xml::tags( 'label', array( 'for' => $id ), $this->parse(
972 wfMsgNoTrans( $params['itemLabelPrefix'] . strtolower( $value ) )
973 ) ) .
974 "</li>\n";
975 }
976
977 $s .= "</ul>\n";
978
979 return $this->label( $label, $params['controlName'], $s, $params['help'] );
980 }
981
982 /**
983 * Output an error or warning box using a Status object.
984 *
985 * @param $status Status
986 */
987 public function showStatusBox( $status ) {
988 if( !$status->isGood() ) {
989 $text = $status->getWikiText();
990
991 if( $status->isOk() ) {
992 $box = $this->getWarningBox( $text );
993 } else {
994 $box = $this->getErrorBox( $text );
995 }
996
997 $this->output->addHTML( $box );
998 }
999 }
1000
1001 /**
1002 * Convenience function to set variables based on form data.
1003 * Assumes that variables containing "password" in the name are (potentially
1004 * fake) passwords.
1005 *
1006 * @param $varNames Array
1007 * @param $prefix String: the prefix added to variables to obtain form names
1008 *
1009 * @return array
1010 */
1011 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
1012 $newValues = array();
1013
1014 foreach ( $varNames as $name ) {
1015 $value = trim( $this->request->getVal( $prefix . $name ) );
1016 $newValues[$name] = $value;
1017
1018 if ( $value === null ) {
1019 // Checkbox?
1020 $this->setVar( $name, false );
1021 } else {
1022 if ( stripos( $name, 'password' ) !== false ) {
1023 $this->setPassword( $name, $value );
1024 } else {
1025 $this->setVar( $name, $value );
1026 }
1027 }
1028 }
1029
1030 return $newValues;
1031 }
1032
1033 /**
1034 * Helper for Installer::docLink()
1035 *
1036 * @param $page
1037 * @return string
1038 */
1039 protected function getDocUrl( $page ) {
1040 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1041
1042 if ( in_array( $this->currentPageName, $this->pageSequence ) ) {
1043 $url .= '&lastPage=' . urlencode( $this->currentPageName );
1044 }
1045
1046 return $url;
1047 }
1048
1049 /**
1050 * Extension tag hook for a documentation link.
1051 *
1052 * @param $linkText
1053 * @param $attribs
1054 * @param $parser
1055 * @return string
1056 */
1057 public function docLink( $linkText, $attribs, $parser ) {
1058 $url = $this->getDocUrl( $attribs['href'] );
1059 return '<a href="' . htmlspecialchars( $url ) . '">' .
1060 htmlspecialchars( $linkText ) .
1061 '</a>';
1062 }
1063
1064 /**
1065 * Helper for "Download LocalSettings" link on WebInstall_Complete
1066 *
1067 * @param $text
1068 * @param $attribs
1069 * @param $parser
1070 * @return String Html for download link
1071 */
1072 public function downloadLinkHook( $text, $attribs, $parser ) {
1073 $img = Html::element( 'img', array(
1074 'src' => '../skins/common/images/download-32.png',
1075 'width' => '32',
1076 'height' => '32',
1077 ) );
1078 $anchor = Html::rawElement( 'a',
1079 array( 'href' => $this->getURL( array( 'localsettings' => 1 ) ) ),
1080 $img . ' ' . wfMsgHtml( 'config-download-localsettings' ) );
1081 return Html::rawElement( 'div', array( 'class' => 'config-download-link' ), $anchor );
1082 }
1083
1084 /**
1085 * @return bool
1086 */
1087 public function envCheckPath( ) {
1088 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
1089 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
1090 // to get the path to the current script... hopefully it's reliable. SIGH
1091 $path = false;
1092 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
1093 $path = $_SERVER['PHP_SELF'];
1094 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
1095 $path = $_SERVER['SCRIPT_NAME'];
1096 }
1097 if ($path !== false) {
1098 $uri = preg_replace( '{^(.*)/(mw-)?config.*$}', '$1', $path );
1099 $this->setVar( 'wgScriptPath', $uri );
1100 } else {
1101 $this->showError( 'config-no-uri' );
1102 return false;
1103 }
1104 return parent::envCheckPath();
1105 }
1106
1107 protected function envGetDefaultServer() {
1108 return WebRequest::detectServer();
1109 }
1110 }