]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/installer/CliInstaller.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / includes / installer / CliInstaller.php
1 <?php
2 /**
3  * Core installer command line 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 command line interface.
26  *
27  * @ingroup Deployment
28  * @since 1.17
29  */
30 class CliInstaller extends Installer {
31         private $specifiedScriptPath = false;
32
33         private $optionMap = [
34                 'dbtype' => 'wgDBtype',
35                 'dbserver' => 'wgDBserver',
36                 'dbname' => 'wgDBname',
37                 'dbuser' => 'wgDBuser',
38                 'dbpass' => 'wgDBpassword',
39                 'dbprefix' => 'wgDBprefix',
40                 'dbtableoptions' => 'wgDBTableOptions',
41                 'dbmysql5' => 'wgDBmysql5',
42                 'dbport' => 'wgDBport',
43                 'dbschema' => 'wgDBmwschema',
44                 'dbpath' => 'wgSQLiteDataDir',
45                 'server' => 'wgServer',
46                 'scriptpath' => 'wgScriptPath',
47         ];
48
49         /**
50          * @param string $siteName
51          * @param string $admin
52          * @param array $option
53          */
54         function __construct( $siteName, $admin = null, array $option = [] ) {
55                 global $wgContLang;
56
57                 parent::__construct();
58
59                 if ( isset( $option['scriptpath'] ) ) {
60                         $this->specifiedScriptPath = true;
61                 }
62
63                 foreach ( $this->optionMap as $opt => $global ) {
64                         if ( isset( $option[$opt] ) ) {
65                                 $GLOBALS[$global] = $option[$opt];
66                                 $this->setVar( $global, $option[$opt] );
67                         }
68                 }
69
70                 if ( isset( $option['lang'] ) ) {
71                         global $wgLang, $wgLanguageCode;
72                         $this->setVar( '_UserLang', $option['lang'] );
73                         $wgContLang = Language::factory( $option['lang'] );
74                         $wgLang = Language::factory( $option['lang'] );
75                         $wgLanguageCode = $option['lang'];
76                         RequestContext::getMain()->setLanguage( $wgLang );
77                 }
78
79                 $this->setVar( 'wgSitename', $siteName );
80
81                 $metaNS = $wgContLang->ucfirst( str_replace( ' ', '_', $siteName ) );
82                 if ( $metaNS == 'MediaWiki' ) {
83                         $metaNS = 'Project';
84                 }
85                 $this->setVar( 'wgMetaNamespace', $metaNS );
86
87                 if ( $admin ) {
88                         $this->setVar( '_AdminName', $admin );
89                 }
90
91                 if ( !isset( $option['installdbuser'] ) ) {
92                         $this->setVar( '_InstallUser',
93                                 $this->getVar( 'wgDBuser' ) );
94                         $this->setVar( '_InstallPassword',
95                                 $this->getVar( 'wgDBpassword' ) );
96                 } else {
97                         $this->setVar( '_InstallUser',
98                                 $option['installdbuser'] );
99                         $this->setVar( '_InstallPassword',
100                                 isset( $option['installdbpass'] ) ? $option['installdbpass'] : "" );
101
102                         // Assume that if we're given the installer user, we'll create the account.
103                         $this->setVar( '_CreateDBAccount', true );
104                 }
105
106                 if ( isset( $option['pass'] ) ) {
107                         $this->setVar( '_AdminPassword', $option['pass'] );
108                 }
109
110                 // Detect and inject any extension found
111                 if ( isset( $option['with-extensions'] ) ) {
112                         $this->setVar( '_Extensions', array_keys( $this->findExtensions() ) );
113                 }
114
115                 // Set up the default skins
116                 $skins = array_keys( $this->findExtensions( 'skins' ) );
117                 $this->setVar( '_Skins', $skins );
118
119                 if ( $skins ) {
120                         $skinNames = array_map( 'strtolower', $skins );
121                         $this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) );
122                 }
123         }
124
125         /**
126          * Main entry point.
127          */
128         public function execute() {
129                 $vars = Installer::getExistingLocalSettings();
130                 if ( $vars ) {
131                         $this->showStatusMessage(
132                                 Status::newFatal( "config-localsettings-cli-upgrade" )
133                         );
134                 }
135
136                 $this->performInstallation(
137                         [ $this, 'startStage' ],
138                         [ $this, 'endStage' ]
139                 );
140         }
141
142         /**
143          * Write LocalSettings.php to a given path
144          *
145          * @param string $path Full path to write LocalSettings.php to
146          */
147         public function writeConfigurationFile( $path ) {
148                 $ls = InstallerOverrides::getLocalSettingsGenerator( $this );
149                 $ls->writeFile( "$path/LocalSettings.php" );
150         }
151
152         public function startStage( $step ) {
153                 // Messages: config-install-database, config-install-tables, config-install-interwiki,
154                 // config-install-stats, config-install-keys, config-install-sysop, config-install-mainpage,
155                 // config-install-extensions
156                 $this->showMessage( "config-install-$step" );
157         }
158
159         public function endStage( $step, $status ) {
160                 $this->showStatusMessage( $status );
161                 $this->showMessage( 'config-install-step-done' );
162         }
163
164         public function showMessage( $msg /*, ... */ ) {
165                 echo $this->getMessageText( func_get_args() ) . "\n";
166                 flush();
167         }
168
169         public function showError( $msg /*, ... */ ) {
170                 echo "***{$this->getMessageText( func_get_args() )}***\n";
171                 flush();
172         }
173
174         /**
175          * @param array $params
176          *
177          * @return string
178          */
179         protected function getMessageText( $params ) {
180                 $msg = array_shift( $params );
181
182                 $text = wfMessage( $msg, $params )->parse();
183
184                 $text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 &lt;$1&gt;', $text );
185
186                 return Sanitizer::stripAllTags( $text );
187         }
188
189         /**
190          * Dummy
191          */
192         public function showHelpBox( $msg /*, ... */ ) {
193         }
194
195         public function showStatusMessage( Status $status ) {
196                 $warnings = array_merge( $status->getWarningsArray(),
197                         $status->getErrorsArray() );
198
199                 if ( count( $warnings ) !== 0 ) {
200                         foreach ( $warnings as $w ) {
201                                 call_user_func_array( [ $this, 'showMessage' ], $w );
202                         }
203                 }
204
205                 if ( !$status->isOK() ) {
206                         echo "\n";
207                         exit( 1 );
208                 }
209         }
210
211         public function envCheckPath() {
212                 if ( !$this->specifiedScriptPath ) {
213                         $this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) );
214                 }
215
216                 return parent::envCheckPath();
217         }
218
219         protected function envGetDefaultServer() {
220                 return null; // Do not guess if installing from CLI
221         }
222
223         public function dirIsExecutable( $dir, $url ) {
224                 $this->showMessage( 'config-no-cli-uploads-check', $dir );
225
226                 return false;
227         }
228 }