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