Merge "Document the wikipage.content hook"
[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 * @throws Exception
330 * @return bool
331 */
332 public function startSession() {
333 if ( wfIniGetBool( 'session.auto_start' ) || session_id() ) {
334 // Done already
335 return true;
336 }
337
338 $this->phpErrors = array();
339 set_error_handler( array( $this, 'errorHandler' ) );
340 try {
341 session_start();
342 } catch ( Exception $e ) {
343 restore_error_handler();
344 throw $e;
345 }
346 restore_error_handler();
347
348 if ( $this->phpErrors ) {
349 $this->showError( 'config-session-error', $this->phpErrors[0] );
350
351 return false;
352 }
353
354 return true;
355 }
356
357 /**
358 * Get a hash of data identifying this MW installation.
359 *
360 * This is used by mw-config/index.php to prevent multiple installations of MW
361 * on the same cookie domain from interfering with each other.
362 *
363 * @return string
364 */
365 public function getFingerprint() {
366 // Get the base URL of the installation
367 $url = $this->request->getFullRequestURL();
368 if ( preg_match( '!^(.*\?)!', $url, $m ) ) {
369 // Trim query string
370 $url = $m[1];
371 }
372 if ( preg_match( '!^(.*)/[^/]*/[^/]*$!', $url, $m ) ) {
373 // This... seems to try to get the base path from
374 // the /mw-config/index.php. Kinda scary though?
375 $url = $m[1];
376 }
377
378 return md5( serialize( array(
379 'local path' => dirname( __DIR__ ),
380 'url' => $url,
381 'version' => $GLOBALS['wgVersion']
382 ) ) );
383 }
384
385 /**
386 * Show an error message in a box. Parameters are like wfMessage().
387 * @param $msg
388 */
389 public function showError( $msg /*...*/ ) {
390 $args = func_get_args();
391 array_shift( $args );
392 $args = array_map( 'htmlspecialchars', $args );
393 $msg = wfMessage( $msg, $args )->useDatabase( false )->plain();
394 $this->output->addHTML( $this->getErrorBox( $msg ) );
395 }
396
397 /**
398 * Temporary error handler for session start debugging.
399 * @param $errno
400 * @param string $errstr
401 */
402 public function errorHandler( $errno, $errstr ) {
403 $this->phpErrors[] = $errstr;
404 }
405
406 /**
407 * Clean up from execute()
408 *
409 * @return array
410 */
411 public function finish() {
412 $this->output->output();
413
414 $this->session['happyPages'] = $this->happyPages;
415 $this->session['skippedPages'] = $this->skippedPages;
416 $this->session['settings'] = $this->settings;
417
418 return $this->session;
419 }
420
421 /**
422 * We're restarting the installation, reset the session, happyPages, etc
423 */
424 public function reset() {
425 $this->session = array();
426 $this->happyPages = array();
427 $this->settings = array();
428 }
429
430 /**
431 * Get a URL for submission back to the same script.
432 *
433 * @param array $query
434 * @return string
435 */
436 public function getUrl( $query = array() ) {
437 $url = $this->request->getRequestURL();
438 # Remove existing query
439 $url = preg_replace( '/\?.*$/', '', $url );
440
441 if ( $query ) {
442 $url .= '?' . wfArrayToCgi( $query );
443 }
444
445 return $url;
446 }
447
448 /**
449 * Get a WebInstallerPage by name.
450 *
451 * @param string $pageName
452 * @return WebInstallerPage
453 */
454 public function getPageByName( $pageName ) {
455 $pageClass = 'WebInstaller_' . $pageName;
456
457 return new $pageClass( $this );
458 }
459
460 /**
461 * Get a session variable.
462 *
463 * @param string $name
464 * @param $default
465 * @return null
466 */
467 public function getSession( $name, $default = null ) {
468 if ( !isset( $this->session[$name] ) ) {
469 return $default;
470 } else {
471 return $this->session[$name];
472 }
473 }
474
475 /**
476 * Set a session variable.
477 * @param string $name Key for the variable
478 * @param mixed $value
479 */
480 public function setSession( $name, $value ) {
481 $this->session[$name] = $value;
482 }
483
484 /**
485 * Get the next tabindex attribute value.
486 * @return int
487 */
488 public function nextTabIndex() {
489 return $this->tabIndex++;
490 }
491
492 /**
493 * Initializes language-related variables.
494 */
495 public function setupLanguage() {
496 global $wgLang, $wgContLang, $wgLanguageCode;
497
498 if ( $this->getSession( 'test' ) === null && !$this->request->wasPosted() ) {
499 $wgLanguageCode = $this->getAcceptLanguage();
500 $wgLang = $wgContLang = Language::factory( $wgLanguageCode );
501 $this->setVar( 'wgLanguageCode', $wgLanguageCode );
502 $this->setVar( '_UserLang', $wgLanguageCode );
503 } else {
504 $wgLanguageCode = $this->getVar( 'wgLanguageCode' );
505 $wgContLang = Language::factory( $wgLanguageCode );
506 }
507 }
508
509 /**
510 * Retrieves MediaWiki language from Accept-Language HTTP header.
511 *
512 * @return string
513 */
514 public function getAcceptLanguage() {
515 global $wgLanguageCode, $wgRequest;
516
517 $mwLanguages = Language::fetchLanguageNames();
518 $headerLanguages = array_keys( $wgRequest->getAcceptLang() );
519
520 foreach ( $headerLanguages as $lang ) {
521 if ( isset( $mwLanguages[$lang] ) ) {
522 return $lang;
523 }
524 }
525
526 return $wgLanguageCode;
527 }
528
529 /**
530 * Called by execute() before page output starts, to show a page list.
531 *
532 * @param string $currentPageName
533 */
534 private function startPageWrapper( $currentPageName ) {
535 $s = "<div class=\"config-page-wrapper\">\n";
536 $s .= "<div class=\"config-page\">\n";
537 $s .= "<div class=\"config-page-list\"><ul>\n";
538 $lastHappy = -1;
539
540 foreach ( $this->pageSequence as $id => $pageName ) {
541 $happy = !empty( $this->happyPages[$id] );
542 $s .= $this->getPageListItem(
543 $pageName,
544 $happy || $lastHappy == $id - 1,
545 $currentPageName
546 );
547
548 if ( $happy ) {
549 $lastHappy = $id;
550 }
551 }
552
553 $s .= "</ul><br/><ul>\n";
554 $s .= $this->getPageListItem( 'Restart', true, $currentPageName );
555 // End list pane
556 $s .= "</ul></div>\n";
557
558 // Messages:
559 // config-page-language, config-page-welcome, config-page-dbconnect, config-page-upgrade,
560 // config-page-dbsettings, config-page-name, config-page-options, config-page-install,
561 // config-page-complete, config-page-restart, config-page-readme, config-page-releasenotes,
562 // config-page-copying, config-page-upgradedoc, config-page-existingwiki
563 $s .= Html::element( 'h2', array(),
564 wfMessage( 'config-page-' . strtolower( $currentPageName ) )->text() );
565
566 $this->output->addHTMLNoFlush( $s );
567 }
568
569 /**
570 * Get a list item for the page list.
571 *
572 * @param string $pageName
573 * @param bool $enabled
574 * @param string $currentPageName
575 *
576 * @return string
577 */
578 private function getPageListItem( $pageName, $enabled, $currentPageName ) {
579 $s = "<li class=\"config-page-list-item\">";
580
581 // Messages:
582 // config-page-language, config-page-welcome, config-page-dbconnect, config-page-upgrade,
583 // config-page-dbsettings, config-page-name, config-page-options, config-page-install,
584 // config-page-complete, config-page-restart, config-page-readme, config-page-releasenotes,
585 // config-page-copying, config-page-upgradedoc, config-page-existingwiki
586 $name = wfMessage( 'config-page-' . strtolower( $pageName ) )->text();
587
588 if ( $enabled ) {
589 $query = array( 'page' => $pageName );
590
591 if ( !in_array( $pageName, $this->pageSequence ) ) {
592 if ( in_array( $currentPageName, $this->pageSequence ) ) {
593 $query['lastPage'] = $currentPageName;
594 }
595
596 $link = Html::element( 'a',
597 array(
598 'href' => $this->getUrl( $query )
599 ),
600 $name
601 );
602 } else {
603 $link = htmlspecialchars( $name );
604 }
605
606 if ( $pageName == $currentPageName ) {
607 $s .= "<span class=\"config-page-current\">$link</span>";
608 } else {
609 $s .= $link;
610 }
611 } else {
612 $s .= Html::element( 'span',
613 array(
614 'class' => 'config-page-disabled'
615 ),
616 $name
617 );
618 }
619
620 $s .= "</li>\n";
621
622 return $s;
623 }
624
625 /**
626 * Output some stuff after a page is finished.
627 */
628 private function endPageWrapper() {
629 $this->output->addHTMLNoFlush(
630 "<div class=\"visualClear\"></div>\n" .
631 "</div>\n" .
632 "<div class=\"visualClear\"></div>\n" .
633 "</div>" );
634 }
635
636 /**
637 * Get HTML for an error box with an icon.
638 *
639 * @param string $text Wikitext, get this with wfMessage()->plain()
640 *
641 * @return string
642 */
643 public function getErrorBox( $text ) {
644 return $this->getInfoBox( $text, 'critical-32.png', 'config-error-box' );
645 }
646
647 /**
648 * Get HTML for a warning box with an icon.
649 *
650 * @param string $text Wikitext, get this with wfMessage()->plain()
651 *
652 * @return string
653 */
654 public function getWarningBox( $text ) {
655 return $this->getInfoBox( $text, 'warning-32.png', 'config-warning-box' );
656 }
657
658 /**
659 * Get HTML for an info box with an icon.
660 *
661 * @param string $text Wikitext, get this with wfMessage()->plain()
662 * @param string|bool $icon Icon name, file in skins/common/images. Default: false
663 * @param string|bool $class Additional class name to add to the wrapper div. Default: false.
664 *
665 * @return string
666 */
667 public function getInfoBox( $text, $icon = false, $class = false ) {
668 $text = $this->parse( $text, true );
669 $icon = ( $icon == false ) ?
670 '../skins/common/images/info-32.png' :
671 '../skins/common/images/' . $icon;
672 $alt = wfMessage( 'config-information' )->text();
673
674 return Html::infoBox( $text, $icon, $alt, $class, false );
675 }
676
677 /**
678 * Get small text indented help for a preceding form field.
679 * Parameters like wfMessage().
680 *
681 * @param $msg
682 * @return string
683 */
684 public function getHelpBox( $msg /*, ... */ ) {
685 $args = func_get_args();
686 array_shift( $args );
687 $args = array_map( 'htmlspecialchars', $args );
688 $text = wfMessage( $msg, $args )->useDatabase( false )->plain();
689 $html = $this->parse( $text, true );
690
691 return "<div class=\"mw-help-field-container\">\n" .
692 "<span class=\"mw-help-field-hint\">" . wfMessage( 'config-help' )->escaped() .
693 "</span>\n" .
694 "<span class=\"mw-help-field-data\">" . $html . "</span>\n" .
695 "</div>\n";
696 }
697
698 /**
699 * Output a help box.
700 * @param string $msg Key for wfMessage()
701 */
702 public function showHelpBox( $msg /*, ... */ ) {
703 $args = func_get_args();
704 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
705 $this->output->addHTML( $html );
706 }
707
708 /**
709 * Show a short informational message.
710 * Output looks like a list.
711 *
712 * @param string $msg
713 */
714 public function showMessage( $msg /*, ... */ ) {
715 $args = func_get_args();
716 array_shift( $args );
717 $html = '<div class="config-message">' .
718 $this->parse( wfMessage( $msg, $args )->useDatabase( false )->plain() ) .
719 "</div>\n";
720 $this->output->addHTML( $html );
721 }
722
723 /**
724 * @param Status $status
725 */
726 public function showStatusMessage( Status $status ) {
727 $errors = array_merge( $status->getErrorsArray(), $status->getWarningsArray() );
728 foreach ( $errors as $error ) {
729 call_user_func_array( array( $this, 'showMessage' ), $error );
730 }
731 }
732
733 /**
734 * Label a control by wrapping a config-input div around it and putting a
735 * label before it.
736 *
737 * @param $msg
738 * @param $forId
739 * @param $contents
740 * @param string $helpData
741 * @return string
742 */
743 public function label( $msg, $forId, $contents, $helpData = "" ) {
744 if ( strval( $msg ) == '' ) {
745 $labelText = '&#160;';
746 } else {
747 $labelText = wfMessage( $msg )->escaped();
748 }
749
750 $attributes = array( 'class' => 'config-label' );
751
752 if ( $forId ) {
753 $attributes['for'] = $forId;
754 }
755
756 return "<div class=\"config-block\">\n" .
757 " <div class=\"config-block-label\">\n" .
758 Xml::tags( 'label',
759 $attributes,
760 $labelText
761 ) . "\n" .
762 $helpData .
763 " </div>\n" .
764 " <div class=\"config-block-elements\">\n" .
765 $contents .
766 " </div>\n" .
767 "</div>\n";
768 }
769
770 /**
771 * Get a labelled text box to configure a variable.
772 *
773 * @param array $params
774 * Parameters are:
775 * var: The variable to be configured (required)
776 * label: The message name for the label (required)
777 * attribs: Additional attributes for the input element (optional)
778 * controlName: The name for the input element (optional)
779 * value: The current value of the variable (optional)
780 * help: The html for the help text (optional)
781 *
782 * @return string
783 */
784 public function getTextBox( $params ) {
785 if ( !isset( $params['controlName'] ) ) {
786 $params['controlName'] = 'config_' . $params['var'];
787 }
788
789 if ( !isset( $params['value'] ) ) {
790 $params['value'] = $this->getVar( $params['var'] );
791 }
792
793 if ( !isset( $params['attribs'] ) ) {
794 $params['attribs'] = array();
795 }
796 if ( !isset( $params['help'] ) ) {
797 $params['help'] = "";
798 }
799
800 return $this->label(
801 $params['label'],
802 $params['controlName'],
803 Xml::input(
804 $params['controlName'],
805 30, // intended to be overridden by CSS
806 $params['value'],
807 $params['attribs'] + array(
808 'id' => $params['controlName'],
809 'class' => 'config-input-text',
810 'tabindex' => $this->nextTabIndex()
811 )
812 ),
813 $params['help']
814 );
815 }
816
817 /**
818 * Get a labelled textarea to configure a variable
819 *
820 * @param array $params
821 * Parameters are:
822 * var: The variable to be configured (required)
823 * label: The message name for the label (required)
824 * attribs: Additional attributes for the input element (optional)
825 * controlName: The name for the input element (optional)
826 * value: The current value of the variable (optional)
827 * help: The html for the help text (optional)
828 *
829 * @return string
830 */
831 public function getTextArea( $params ) {
832 if ( !isset( $params['controlName'] ) ) {
833 $params['controlName'] = 'config_' . $params['var'];
834 }
835
836 if ( !isset( $params['value'] ) ) {
837 $params['value'] = $this->getVar( $params['var'] );
838 }
839
840 if ( !isset( $params['attribs'] ) ) {
841 $params['attribs'] = array();
842 }
843 if ( !isset( $params['help'] ) ) {
844 $params['help'] = "";
845 }
846
847 return $this->label(
848 $params['label'],
849 $params['controlName'],
850 Xml::textarea(
851 $params['controlName'],
852 $params['value'],
853 30,
854 5,
855 $params['attribs'] + array(
856 'id' => $params['controlName'],
857 'class' => 'config-input-text',
858 'tabindex' => $this->nextTabIndex()
859 )
860 ),
861 $params['help']
862 );
863 }
864
865 /**
866 * Get a labelled password box to configure a variable.
867 *
868 * Implements password hiding
869 * @param array $params
870 * Parameters are:
871 * var: The variable to be configured (required)
872 * label: The message name for the label (required)
873 * attribs: Additional attributes for the input element (optional)
874 * controlName: The name for the input element (optional)
875 * value: The current value of the variable (optional)
876 * help: The html for the help text (optional)
877 *
878 * @return string
879 */
880 public function getPasswordBox( $params ) {
881 if ( !isset( $params['value'] ) ) {
882 $params['value'] = $this->getVar( $params['var'] );
883 }
884
885 if ( !isset( $params['attribs'] ) ) {
886 $params['attribs'] = array();
887 }
888
889 $params['value'] = $this->getFakePassword( $params['value'] );
890 $params['attribs']['type'] = 'password';
891
892 return $this->getTextBox( $params );
893 }
894
895 /**
896 * Get a labelled checkbox to configure a boolean variable.
897 *
898 * @param array $params
899 * Parameters are:
900 * var: The variable to be configured (required)
901 * label: The message name for the label (required)
902 * attribs: Additional attributes for the input element (optional)
903 * controlName: The name for the input element (optional)
904 * value: The current value of the variable (optional)
905 * help: The html for the help text (optional)
906 *
907 * @return string
908 */
909 public function getCheckBox( $params ) {
910 if ( !isset( $params['controlName'] ) ) {
911 $params['controlName'] = 'config_' . $params['var'];
912 }
913
914 if ( !isset( $params['value'] ) ) {
915 $params['value'] = $this->getVar( $params['var'] );
916 }
917
918 if ( !isset( $params['attribs'] ) ) {
919 $params['attribs'] = array();
920 }
921 if ( !isset( $params['help'] ) ) {
922 $params['help'] = "";
923 }
924 if ( isset( $params['rawtext'] ) ) {
925 $labelText = $params['rawtext'];
926 } else {
927 $labelText = $this->parse( wfMessage( $params['label'] )->text() );
928 }
929
930 return "<div class=\"config-input-check\">\n" .
931 $params['help'] .
932 "<label>\n" .
933 Xml::check(
934 $params['controlName'],
935 $params['value'],
936 $params['attribs'] + array(
937 'id' => $params['controlName'],
938 'tabindex' => $this->nextTabIndex(),
939 )
940 ) .
941 $labelText . "\n" .
942 "</label>\n" .
943 "</div>\n";
944 }
945
946 /**
947 * Get a set of labelled radio buttons.
948 *
949 * @param array $params
950 * Parameters are:
951 * var: The variable to be configured (required)
952 * label: The message name for the label (required)
953 * itemLabelPrefix: The message name prefix for the item labels (required)
954 * values: List of allowed values (required)
955 * itemAttribs: Array of attribute arrays, outer key is the value name (optional)
956 * commonAttribs: Attribute array applied to all items
957 * controlName: The name for the input element (optional)
958 * value: The current value of the variable (optional)
959 * help: The html for the help text (optional)
960 *
961 * @return string
962 */
963 public function getRadioSet( $params ) {
964 if ( !isset( $params['controlName'] ) ) {
965 $params['controlName'] = 'config_' . $params['var'];
966 }
967
968 if ( !isset( $params['value'] ) ) {
969 $params['value'] = $this->getVar( $params['var'] );
970 }
971
972 if ( !isset( $params['label'] ) ) {
973 $label = '';
974 } else {
975 $label = $params['label'];
976 }
977 if ( !isset( $params['help'] ) ) {
978 $params['help'] = "";
979 }
980 $s = "<ul>\n";
981 foreach ( $params['values'] as $value ) {
982 $itemAttribs = array();
983
984 if ( isset( $params['commonAttribs'] ) ) {
985 $itemAttribs = $params['commonAttribs'];
986 }
987
988 if ( isset( $params['itemAttribs'][$value] ) ) {
989 $itemAttribs = $params['itemAttribs'][$value] + $itemAttribs;
990 }
991
992 $checked = $value == $params['value'];
993 $id = $params['controlName'] . '_' . $value;
994 $itemAttribs['id'] = $id;
995 $itemAttribs['tabindex'] = $this->nextTabIndex();
996
997 $s .=
998 '<li>' .
999 Xml::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
1000 '&#160;' .
1001 Xml::tags( 'label', array( 'for' => $id ), $this->parse(
1002 wfMessage( $params['itemLabelPrefix'] . strtolower( $value ) )->plain()
1003 ) ) .
1004 "</li>\n";
1005 }
1006
1007 $s .= "</ul>\n";
1008
1009 return $this->label( $label, $params['controlName'], $s, $params['help'] );
1010 }
1011
1012 /**
1013 * Output an error or warning box using a Status object.
1014 *
1015 * @param Status $status
1016 */
1017 public function showStatusBox( $status ) {
1018 if ( !$status->isGood() ) {
1019 $text = $status->getWikiText();
1020
1021 if ( $status->isOk() ) {
1022 $box = $this->getWarningBox( $text );
1023 } else {
1024 $box = $this->getErrorBox( $text );
1025 }
1026
1027 $this->output->addHTML( $box );
1028 }
1029 }
1030
1031 /**
1032 * Convenience function to set variables based on form data.
1033 * Assumes that variables containing "password" in the name are (potentially
1034 * fake) passwords.
1035 *
1036 * @param array $varNames
1037 * @param string $prefix The prefix added to variables to obtain form names
1038 *
1039 * @return array
1040 */
1041 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
1042 $newValues = array();
1043
1044 foreach ( $varNames as $name ) {
1045 $value = trim( $this->request->getVal( $prefix . $name ) );
1046 $newValues[$name] = $value;
1047
1048 if ( $value === null ) {
1049 // Checkbox?
1050 $this->setVar( $name, false );
1051 } else {
1052 if ( stripos( $name, 'password' ) !== false ) {
1053 $this->setPassword( $name, $value );
1054 } else {
1055 $this->setVar( $name, $value );
1056 }
1057 }
1058 }
1059
1060 return $newValues;
1061 }
1062
1063 /**
1064 * Helper for Installer::docLink()
1065 *
1066 * @param $page
1067 * @return string
1068 */
1069 protected function getDocUrl( $page ) {
1070 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1071
1072 if ( in_array( $this->currentPageName, $this->pageSequence ) ) {
1073 $url .= '&lastPage=' . urlencode( $this->currentPageName );
1074 }
1075
1076 return $url;
1077 }
1078
1079 /**
1080 * Extension tag hook for a documentation link.
1081 *
1082 * @param $linkText
1083 * @param $attribs
1084 * @param $parser
1085 * @return string
1086 */
1087 public function docLink( $linkText, $attribs, $parser ) {
1088 $url = $this->getDocUrl( $attribs['href'] );
1089
1090 return '<a href="' . htmlspecialchars( $url ) . '">' .
1091 htmlspecialchars( $linkText ) .
1092 '</a>';
1093 }
1094
1095 /**
1096 * Helper for "Download LocalSettings" link on WebInstall_Complete
1097 *
1098 * @param $text
1099 * @param $attribs
1100 * @param $parser
1101 * @return string Html for download link
1102 */
1103 public function downloadLinkHook( $text, $attribs, $parser ) {
1104 $img = Html::element( 'img', array(
1105 'src' => '../skins/common/images/download-32.png',
1106 'width' => '32',
1107 'height' => '32',
1108 ) );
1109 $anchor = Html::rawElement( 'a',
1110 array( 'href' => $this->getURL( array( 'localsettings' => 1 ) ) ),
1111 $img . ' ' . wfMessage( 'config-download-localsettings' )->parse() );
1112
1113 return Html::rawElement( 'div', array( 'class' => 'config-download-link' ), $anchor );
1114 }
1115
1116 /**
1117 * @return bool
1118 */
1119 public function envCheckPath() {
1120 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
1121 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
1122 // to get the path to the current script... hopefully it's reliable. SIGH
1123 $path = false;
1124 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
1125 $path = $_SERVER['PHP_SELF'];
1126 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
1127 $path = $_SERVER['SCRIPT_NAME'];
1128 }
1129 if ( $path !== false ) {
1130 $uri = preg_replace( '{^(.*)/(mw-)?config.*$}', '$1', $path );
1131 $this->setVar( 'wgScriptPath', $uri );
1132 } else {
1133 $this->showError( 'config-no-uri' );
1134
1135 return false;
1136 }
1137
1138 return parent::envCheckPath();
1139 }
1140
1141 protected function envGetDefaultServer() {
1142 return WebRequest::detectServer();
1143 }
1144 }