Revert "Add support for mysqli extension"
[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 ) ? '../skins/common/images/info-32.png' : '../skins/common/images/' . $icon;
664 $alt = wfMessage( 'config-information' )->text();
665
666 return Html::infoBox( $text, $icon, $alt, $class, false );
667 }
668
669 /**
670 * Get small text indented help for a preceding form field.
671 * Parameters like wfMessage().
672 *
673 * @param $msg
674 * @return string
675 */
676 public function getHelpBox( $msg /*, ... */ ) {
677 $args = func_get_args();
678 array_shift( $args );
679 $args = array_map( 'htmlspecialchars', $args );
680 $text = wfMessage( $msg, $args )->useDatabase( false )->plain();
681 $html = $this->parse( $text, true );
682
683 return "<div class=\"mw-help-field-container\">\n" .
684 "<span class=\"mw-help-field-hint\">" . wfMessage( 'config-help' )->escaped() .
685 "</span>\n" .
686 "<span class=\"mw-help-field-data\">" . $html . "</span>\n" .
687 "</div>\n";
688 }
689
690 /**
691 * Output a help box.
692 * @param string $msg key for wfMessage()
693 */
694 public function showHelpBox( $msg /*, ... */ ) {
695 $args = func_get_args();
696 $html = call_user_func_array( array( $this, 'getHelpBox' ), $args );
697 $this->output->addHTML( $html );
698 }
699
700 /**
701 * Show a short informational message.
702 * Output looks like a list.
703 *
704 * @param $msg string
705 */
706 public function showMessage( $msg /*, ... */ ) {
707 $args = func_get_args();
708 array_shift( $args );
709 $html = '<div class="config-message">' .
710 $this->parse( wfMessage( $msg, $args )->useDatabase( false )->plain() ) .
711 "</div>\n";
712 $this->output->addHTML( $html );
713 }
714
715 /**
716 * @param $status Status
717 */
718 public function showStatusMessage( Status $status ) {
719 $errors = array_merge( $status->getErrorsArray(), $status->getWarningsArray() );
720 foreach ( $errors as $error ) {
721 call_user_func_array( array( $this, 'showMessage' ), $error );
722 }
723 }
724
725 /**
726 * Label a control by wrapping a config-input div around it and putting a
727 * label before it.
728 *
729 * @param $msg
730 * @param $forId
731 * @param $contents
732 * @param $helpData string
733 * @return string
734 */
735 public function label( $msg, $forId, $contents, $helpData = "" ) {
736 if ( strval( $msg ) == '' ) {
737 $labelText = '&#160;';
738 } else {
739 $labelText = wfMessage( $msg )->escaped();
740 }
741
742 $attributes = array( 'class' => 'config-label' );
743
744 if ( $forId ) {
745 $attributes['for'] = $forId;
746 }
747
748 return "<div class=\"config-block\">\n" .
749 " <div class=\"config-block-label\">\n" .
750 Xml::tags( 'label',
751 $attributes,
752 $labelText
753 ) . "\n" .
754 $helpData .
755 " </div>\n" .
756 " <div class=\"config-block-elements\">\n" .
757 $contents .
758 " </div>\n" .
759 "</div>\n";
760 }
761
762 /**
763 * Get a labelled text box to configure a variable.
764 *
765 * @param $params Array
766 * Parameters are:
767 * var: The variable to be configured (required)
768 * label: The message name for the label (required)
769 * attribs: Additional attributes for the input element (optional)
770 * controlName: The name for the input element (optional)
771 * value: The current value of the variable (optional)
772 * help: The html for the help text (optional)
773 *
774 * @return string
775 */
776 public function getTextBox( $params ) {
777 if ( !isset( $params['controlName'] ) ) {
778 $params['controlName'] = 'config_' . $params['var'];
779 }
780
781 if ( !isset( $params['value'] ) ) {
782 $params['value'] = $this->getVar( $params['var'] );
783 }
784
785 if ( !isset( $params['attribs'] ) ) {
786 $params['attribs'] = array();
787 }
788 if ( !isset( $params['help'] ) ) {
789 $params['help'] = "";
790 }
791
792 return $this->label(
793 $params['label'],
794 $params['controlName'],
795 Xml::input(
796 $params['controlName'],
797 30, // intended to be overridden by CSS
798 $params['value'],
799 $params['attribs'] + array(
800 'id' => $params['controlName'],
801 'class' => 'config-input-text',
802 'tabindex' => $this->nextTabIndex()
803 )
804 ),
805 $params['help']
806 );
807 }
808
809 /**
810 * Get a labelled textarea to configure a variable
811 *
812 * @param $params Array
813 * Parameters are:
814 * var: The variable to be configured (required)
815 * label: The message name for the label (required)
816 * attribs: Additional attributes for the input element (optional)
817 * controlName: The name for the input element (optional)
818 * value: The current value of the variable (optional)
819 * help: The html for the help text (optional)
820 *
821 * @return string
822 */
823 public function getTextArea( $params ) {
824 if ( !isset( $params['controlName'] ) ) {
825 $params['controlName'] = 'config_' . $params['var'];
826 }
827
828 if ( !isset( $params['value'] ) ) {
829 $params['value'] = $this->getVar( $params['var'] );
830 }
831
832 if ( !isset( $params['attribs'] ) ) {
833 $params['attribs'] = array();
834 }
835 if ( !isset( $params['help'] ) ) {
836 $params['help'] = "";
837 }
838
839 return $this->label(
840 $params['label'],
841 $params['controlName'],
842 Xml::textarea(
843 $params['controlName'],
844 $params['value'],
845 30,
846 5,
847 $params['attribs'] + array(
848 'id' => $params['controlName'],
849 'class' => 'config-input-text',
850 'tabindex' => $this->nextTabIndex()
851 )
852 ),
853 $params['help']
854 );
855 }
856
857 /**
858 * Get a labelled password box to configure a variable.
859 *
860 * Implements password hiding
861 * @param $params Array
862 * Parameters are:
863 * var: The variable to be configured (required)
864 * label: The message name for the label (required)
865 * attribs: Additional attributes for the input element (optional)
866 * controlName: The name for the input element (optional)
867 * value: The current value of the variable (optional)
868 * help: The html for the help text (optional)
869 *
870 * @return string
871 */
872 public function getPasswordBox( $params ) {
873 if ( !isset( $params['value'] ) ) {
874 $params['value'] = $this->getVar( $params['var'] );
875 }
876
877 if ( !isset( $params['attribs'] ) ) {
878 $params['attribs'] = array();
879 }
880
881 $params['value'] = $this->getFakePassword( $params['value'] );
882 $params['attribs']['type'] = 'password';
883
884 return $this->getTextBox( $params );
885 }
886
887 /**
888 * Get a labelled checkbox to configure a boolean variable.
889 *
890 * @param $params Array
891 * Parameters are:
892 * var: The variable to be configured (required)
893 * label: The message name for the label (required)
894 * attribs: Additional attributes for the input element (optional)
895 * controlName: The name for the input element (optional)
896 * value: The current value of the variable (optional)
897 * help: The html for the help text (optional)
898 *
899 * @return string
900 */
901 public function getCheckBox( $params ) {
902 if ( !isset( $params['controlName'] ) ) {
903 $params['controlName'] = 'config_' . $params['var'];
904 }
905
906 if ( !isset( $params['value'] ) ) {
907 $params['value'] = $this->getVar( $params['var'] );
908 }
909
910 if ( !isset( $params['attribs'] ) ) {
911 $params['attribs'] = array();
912 }
913 if ( !isset( $params['help'] ) ) {
914 $params['help'] = "";
915 }
916 if ( isset( $params['rawtext'] ) ) {
917 $labelText = $params['rawtext'];
918 } else {
919 $labelText = $this->parse( wfMessage( $params['label'] )->text() );
920 }
921
922 return "<div class=\"config-input-check\">\n" .
923 $params['help'] .
924 "<label>\n" .
925 Xml::check(
926 $params['controlName'],
927 $params['value'],
928 $params['attribs'] + array(
929 'id' => $params['controlName'],
930 'tabindex' => $this->nextTabIndex(),
931 )
932 ) .
933 $labelText . "\n" .
934 "</label>\n" .
935 "</div>\n";
936 }
937
938 /**
939 * Get a set of labelled radio buttons.
940 *
941 * @param $params Array
942 * Parameters are:
943 * var: The variable to be configured (required)
944 * label: The message name for the label (required)
945 * itemLabelPrefix: The message name prefix for the item labels (required)
946 * values: List of allowed values (required)
947 * itemAttribs: Array of attribute arrays, outer key is the value name (optional)
948 * commonAttribs: Attribute array applied to all items
949 * controlName: The name for the input element (optional)
950 * value: The current value of the variable (optional)
951 * help: The html for the help text (optional)
952 *
953 * @return string
954 */
955 public function getRadioSet( $params ) {
956 if ( !isset( $params['controlName'] ) ) {
957 $params['controlName'] = 'config_' . $params['var'];
958 }
959
960 if ( !isset( $params['value'] ) ) {
961 $params['value'] = $this->getVar( $params['var'] );
962 }
963
964 if ( !isset( $params['label'] ) ) {
965 $label = '';
966 } else {
967 $label = $params['label'];
968 }
969 if ( !isset( $params['help'] ) ) {
970 $params['help'] = "";
971 }
972 $s = "<ul>\n";
973 foreach ( $params['values'] as $value ) {
974 $itemAttribs = array();
975
976 if ( isset( $params['commonAttribs'] ) ) {
977 $itemAttribs = $params['commonAttribs'];
978 }
979
980 if ( isset( $params['itemAttribs'][$value] ) ) {
981 $itemAttribs = $params['itemAttribs'][$value] + $itemAttribs;
982 }
983
984 $checked = $value == $params['value'];
985 $id = $params['controlName'] . '_' . $value;
986 $itemAttribs['id'] = $id;
987 $itemAttribs['tabindex'] = $this->nextTabIndex();
988
989 $s .=
990 '<li>' .
991 Xml::radio( $params['controlName'], $value, $checked, $itemAttribs ) .
992 '&#160;' .
993 Xml::tags( 'label', array( 'for' => $id ), $this->parse(
994 wfMessage( $params['itemLabelPrefix'] . strtolower( $value ) )->plain()
995 ) ) .
996 "</li>\n";
997 }
998
999 $s .= "</ul>\n";
1000
1001 return $this->label( $label, $params['controlName'], $s, $params['help'] );
1002 }
1003
1004 /**
1005 * Output an error or warning box using a Status object.
1006 *
1007 * @param $status Status
1008 */
1009 public function showStatusBox( $status ) {
1010 if ( !$status->isGood() ) {
1011 $text = $status->getWikiText();
1012
1013 if ( $status->isOk() ) {
1014 $box = $this->getWarningBox( $text );
1015 } else {
1016 $box = $this->getErrorBox( $text );
1017 }
1018
1019 $this->output->addHTML( $box );
1020 }
1021 }
1022
1023 /**
1024 * Convenience function to set variables based on form data.
1025 * Assumes that variables containing "password" in the name are (potentially
1026 * fake) passwords.
1027 *
1028 * @param $varNames Array
1029 * @param string $prefix the prefix added to variables to obtain form names
1030 *
1031 * @return array
1032 */
1033 public function setVarsFromRequest( $varNames, $prefix = 'config_' ) {
1034 $newValues = array();
1035
1036 foreach ( $varNames as $name ) {
1037 $value = trim( $this->request->getVal( $prefix . $name ) );
1038 $newValues[$name] = $value;
1039
1040 if ( $value === null ) {
1041 // Checkbox?
1042 $this->setVar( $name, false );
1043 } else {
1044 if ( stripos( $name, 'password' ) !== false ) {
1045 $this->setPassword( $name, $value );
1046 } else {
1047 $this->setVar( $name, $value );
1048 }
1049 }
1050 }
1051
1052 return $newValues;
1053 }
1054
1055 /**
1056 * Helper for Installer::docLink()
1057 *
1058 * @param $page
1059 * @return string
1060 */
1061 protected function getDocUrl( $page ) {
1062 $url = "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1063
1064 if ( in_array( $this->currentPageName, $this->pageSequence ) ) {
1065 $url .= '&lastPage=' . urlencode( $this->currentPageName );
1066 }
1067
1068 return $url;
1069 }
1070
1071 /**
1072 * Extension tag hook for a documentation link.
1073 *
1074 * @param $linkText
1075 * @param $attribs
1076 * @param $parser
1077 * @return string
1078 */
1079 public function docLink( $linkText, $attribs, $parser ) {
1080 $url = $this->getDocUrl( $attribs['href'] );
1081
1082 return '<a href="' . htmlspecialchars( $url ) . '">' .
1083 htmlspecialchars( $linkText ) .
1084 '</a>';
1085 }
1086
1087 /**
1088 * Helper for "Download LocalSettings" link on WebInstall_Complete
1089 *
1090 * @param $text
1091 * @param $attribs
1092 * @param $parser
1093 * @return String Html for download link
1094 */
1095 public function downloadLinkHook( $text, $attribs, $parser ) {
1096 $img = Html::element( 'img', array(
1097 'src' => '../skins/common/images/download-32.png',
1098 'width' => '32',
1099 'height' => '32',
1100 ) );
1101 $anchor = Html::rawElement( 'a',
1102 array( 'href' => $this->getURL( array( 'localsettings' => 1 ) ) ),
1103 $img . ' ' . wfMessage( 'config-download-localsettings' )->parse() );
1104
1105 return Html::rawElement( 'div', array( 'class' => 'config-download-link' ), $anchor );
1106 }
1107
1108 /**
1109 * @return bool
1110 */
1111 public function envCheckPath() {
1112 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
1113 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
1114 // to get the path to the current script... hopefully it's reliable. SIGH
1115 $path = false;
1116 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
1117 $path = $_SERVER['PHP_SELF'];
1118 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
1119 $path = $_SERVER['SCRIPT_NAME'];
1120 }
1121 if ( $path !== false ) {
1122 $uri = preg_replace( '{^(.*)/(mw-)?config.*$}', '$1', $path );
1123 $this->setVar( 'wgScriptPath', $uri );
1124 } else {
1125 $this->showError( 'config-no-uri' );
1126
1127 return false;
1128 }
1129
1130 return parent::envCheckPath();
1131 }
1132
1133 protected function envGetDefaultServer() {
1134 return WebRequest::detectServer();
1135 }
1136 }