Debian Codegraph @

maintenance/install.php

raw symbols SCIP
1 <?php
2 /**
3 * CLI-based MediaWiki installation and configuration.
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 Maintenance
22 */
23
24 use MediaWiki\Installer\Installer;
25 use MediaWiki\Installer\InstallerOverrides;
26 use MediaWiki\Installer\InstallException;
27 use MediaWiki\Settings\SettingsBuilder;
28 use Wikimedia\AtEase\AtEase;
29
30 // @codeCoverageIgnoreStart
31 require_once __DIR__ . '/Maintenance.php';
32
33 define( 'MW_CONFIG_CALLBACK', [ Installer::class, 'overrideConfig' ] );
34 define( 'MEDIAWIKI_INSTALL', true );
35 // @codeCoverageIgnoreEnd
36
37 /**
38 * Maintenance script to install and configure MediaWiki
39 *
40 * Default values for the options are defined in MainConfigSchema.php
41 * (see the mapping in includes/installer/CliInstaller.php)
42 * Default for --dbpath (SQLite-specific) is defined in SqliteInstaller::getGlobalDefaults
43 *
44 * @ingroup Maintenance
45 */
46 class CommandLineInstaller extends Maintenance {
47 public function __construct() {
48 parent::__construct();
49 global $IP;
50
51 $this->addDescription( "CLI-based MediaWiki installation and configuration.\n" .
52 "Default options are indicated in parentheses.\n" .
53 "If no options are provided, the script will run in interactive mode." );
54
55 $this->addArg( 'name', 'The name of the wiki', false );
56 $this->addArg( 'admin', 'The username of the wiki administrator.', false );
57
58 $this->addOption( 'pass', 'The password for the wiki administrator.', false, true );
59 $this->addOption(
60 'passfile',
61 'An alternative way to provide pass option, as the contents of this file',
62 false,
63 true
64 );
65 $this->addOption(
66 'scriptpath',
67 'The relative path of the wiki in the web server (/' . basename( dirname( __DIR__ ) ) . ')',
68 false,
69 true
70 );
71 $this->addOption(
72 'server',
73 'The base URL of the web server the wiki will be on (http://localhost)',
74 false,
75 true
76 );
77
78 $this->addOption( 'lang', 'The language to use (en)', false, true );
79
80 $this->addOption( 'dbtype', 'The type of database (mysql)', false, true );
81 $this->addOption( 'dbserver', 'The database host (localhost)', false, true );
82 $this->addOption( 'dbssl', 'Connect to the database over SSL' );
83 $this->addOption( 'dbport', 'The database port; only for PostgreSQL (5432)', false, true );
84 $this->addOption( 'dbname', 'The database name (my_wiki)', false, true );
85 $this->addOption( 'dbpath', 'The path for the SQLite DB ($IP/data)', false, true );
86 $this->addOption( 'dbprefix', 'Optional database table name prefix', false, true );
87 $this->addOption( 'installdbuser', 'The user to use for installing (root)', false, true );
88 $this->addOption( 'installdbpass', 'The password for the DB user to install as.', false, true );
89 $this->addOption( 'dbuser', 'The user to use for normal operations (wikiuser)', false, true );
90 $this->addOption( 'dbpass', 'The password for the DB user for normal operations', false, true );
91 $this->addOption(
92 'dbpassfile',
93 'An alternative way to provide dbpass option, as the contents of this file',
94 false,
95 true
96 );
97 $this->addOption( 'confpath', "Path to write LocalSettings.php to ($IP)", false, true );
98 $this->addOption( 'dbschema', 'The schema for the MediaWiki DB in '
99 . 'PostgreSQL (mediawiki)', false, true );
100
101 $this->addOption( 'env-checks', "Run environment checks only, don't change anything" );
102
103 $this->addOption( 'with-extensions', "Detect and include extensions" );
104 $this->addOption( 'extensions', 'Comma-separated list of extensions to install',
105 false, true, false, true );
106 $this->addOption( 'skins', 'Comma-separated list of skins to install (default: all)',
107 false, true, false, true );
108 $this->addOption( 'with-developmentsettings', 'Load DevelopmentSettings.php in LocalSettings.php' );
109 }
110
111 public function canExecuteWithoutLocalSettings(): bool {
112 return true;
113 }
114
115 public function finalSetup( SettingsBuilder $settingsBuilder ) {
116 parent::finalSetup( $settingsBuilder );
117 Installer::overrideConfig( $settingsBuilder );
118 }
119
120 public function getDbType() {
121 if ( $this->hasOption( 'env-checks' ) ) {
122 return Maintenance::DB_NONE;
123 }
124 return parent::getDbType();
125 }
126
127 public function execute() {
128 global $IP;
129
130 if ( $this->hasOption( 'help' ) ) {
131 $this->maybeHelp();
132 return;
133 }
134
135 // Manually check for required arguments, as 0 arguments allows interactive mode to be used
136 if ( $this->getArg( 0 ) && !$this->getArg( 1 ) ) {
137 $this->fatalError( 'Argument <' . $this->getArgName( 1 ) . '> is required!' );
138 }
139
140 // No arguments, means interactive mode
141 if ( !$this->getArg( 0 ) || !$this->getArg( 1 ) ) {
142 $this->output( "Hello, I'm the MediaWiki installer!\n\n" );
143 $this->output( "This script will guide you through the process of installing MediaWiki.\n" );
144 $this->output( "If you actually want to see the help for this script, use --help.\n\n" );
145
146 $siteName = $this->prompt( 'Enter the name of the wiki:', "Wiki" );
147 $language = $this->prompt( 'Enter the language code of the wiki:', 'en' );
148 $server = $this->prompt(
149 'Enter the base URL of the web server the wiki will be on (without a path):',
150 'http://localhost'
151 );
152 $adminName = $this->prompt( 'Enter the username of the MediaWiki account that will be created ' .
153 'at the end of the installation and given administrator rights:', "Admin" );
154 $adminPass = $this->prompt(
155 'Enter the password for the wiki administrator:',
156 $this->generateStrongPassword()
157 );
158 $dbType = $this->prompt( 'Enter the type of database (for example mysql or sqlite):', 'mysql' );
159 // Assume that sqlite is the only db type that needs a path
160 $dbPath = $dbType == 'sqlite' ?
161 $this->prompt(
162 'Enter the path for the SQLite DB (advised to be outside the web root):',
163 "$IP/data"
164 ) :
165 null;
166 $dbName = $this->prompt( 'Enter the name of the database:', 'my_wiki' );
167 // Assume that everything other than sqlite needs a server and credentials
168 $dbUser = $dbType == 'sqlite' ? null : $this->prompt( 'Enter the database user:', 'wikiuser' );
169 $dbPass = $dbType == 'sqlite' ? null : $this->prompt( 'Enter the database password:', '' );
170 $dbServer = $dbType == 'sqlite' ? null : $this->prompt( 'Enter the database server:', 'localhost' );
171
172 $this->output( "\n" );
173 $this->setArg( 0, $siteName );
174 $this->setArg( 1, $adminName );
175 $this->setOption( 'lang', $language );
176 $this->setOption( 'server', $server );
177 $this->setOption( 'pass', $adminPass );
178 $this->setOption( 'dbtype', $dbType );
179 $this->setOption( 'dbpath', $dbPath );
180 $this->setOption( 'dbname', $dbName );
181 $this->setOption( 'dbuser', $dbUser );
182 $this->setOption( 'dbpass', $dbPass );
183 $this->setOption( 'dbserver', $dbServer );
184 }
185
186 $siteName = $this->getArg( 0, 'MediaWiki' ); // Will not be set if used with --env-checks
187 $adminName = $this->getArg( 1 );
188 $envChecksOnly = $this->hasOption( 'env-checks' );
189
190 $scriptpath = $this->getOption( 'scriptpath', false );
191 if ( $scriptpath === false ) {
192 $this->setOption( 'scriptpath', '/' . basename( dirname( __DIR__ ) ) );
193 }
194
195 $this->setDbPassOption();
196 if ( !$envChecksOnly ) {
197 $this->setPassOption();
198 }
199
200 try {
201 $installer = InstallerOverrides::getCliInstaller( $siteName, $adminName, $this->parameters->getOptions() );
202 } catch ( InstallException $e ) {
203 $this->error( $e->getStatus() );
204 return false;
205 }
206
207 $status = $installer->doEnvironmentChecks();
208 if ( $status->isGood() ) {
209 $installer->showMessage( 'config-env-good' );
210 } else {
211 return false;
212 }
213 if ( !$envChecksOnly ) {
214 $status = $installer->execute();
215 if ( !$status->isGood() ) {
216 return false;
217 }
218 $installer->writeConfigurationFile( $this->getOption( 'confpath', $IP ) );
219 $installer->showMessage(
220 'config-install-success',
221 $installer->getVar( 'wgServer' ),
222 $installer->getVar( 'wgScriptPath' )
223 );
224 }
225 return true;
226 }
227
228 private function generateStrongPassword() {
229 $strongPassword = '';
230 $strongPasswordLength = 20;
231 $strongPasswordChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-={}|;:,.<>?';
232 $strongPasswordCharsLength = strlen( $strongPasswordChars );
233 for ( $i = 0; $i < $strongPasswordLength; $i++ ) {
234 $strongPassword .= $strongPasswordChars[ rand( 0, $strongPasswordCharsLength - 1 ) ];
235 }
236 return $strongPassword;
237 }
238
239 private function setDbPassOption() {
240 $dbpassfile = $this->getOption( 'dbpassfile' );
241 if ( $dbpassfile !== null ) {
242 if ( $this->getOption( 'dbpass' ) !== null ) {
243 $this->error( 'WARNING: You have provided the options "dbpass" and "dbpassfile". '
244 . 'The content of "dbpassfile" overrides "dbpass".' );
245 }
246 AtEase::suppressWarnings();
247 $dbpass = file_get_contents( $dbpassfile ); // returns false on failure
248 AtEase::restoreWarnings();
249 if ( $dbpass === false ) {
250 $this->fatalError( "Couldn't open $dbpassfile" );
251 }
252 $this->setOption( 'dbpass', trim( $dbpass, "\r\n" ) );
253 }
254 }
255
256 private function setPassOption() {
257 $passfile = $this->getOption( 'passfile' );
258 if ( $passfile !== null ) {
259 if ( $this->getOption( 'pass' ) !== null ) {
260 $this->error( 'WARNING: You have provided the option --pass or --passfile. '
261 . 'The content of "passfile" overrides "pass".' );
262 }
263 AtEase::suppressWarnings();
264 $pass = file_get_contents( $passfile ); // returns false on failure
265 AtEase::restoreWarnings();
266 if ( $pass === false ) {
267 $this->fatalError( "Couldn't open $passfile" );
268 }
269 $this->setOption( 'pass', trim( $pass, "\r\n" ) );
270 } elseif ( $this->getOption( 'pass' ) === null ) {
271 $this->fatalError( 'You need to provide the option "pass" or "passfile"' );
272 }
273 }
274
275 public function validateParamsAndArgs() {
276 if ( !$this->hasOption( 'env-checks' ) ) {
277 $this->parameters->validate();
278 }
279 }
280 }
281
282 // @codeCoverageIgnoreStart
283 $maintClass = CommandLineInstaller::class;
284
285 require_once RUN_MAINTENANCE_IF_MAIN;
286 // @codeCoverageIgnoreEnd