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