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