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