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