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