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