Update git submodules
[mediawiki.git] / maintenance / install.php
blob889048de7556b989016a3d6c8354932a12c68dca
1 <?php
2 /**
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
20 * @file
21 * @ingroup Maintenance
24 use MediaWiki\Settings\SettingsBuilder;
25 use Wikimedia\AtEase\AtEase;
27 require_once __DIR__ . '/Maintenance.php';
29 define( 'MW_CONFIG_CALLBACK', 'Installer::overrideConfig' );
30 define( 'MEDIAWIKI_INSTALL', true );
32 /**
33 * Maintenance script to install and configure MediaWiki
35 * Default values for the options are defined in MainConfigSchema.php
36 * (see the mapping in includes/installer/CliInstaller.php)
37 * Default for --dbpath (SQLite-specific) is defined in SqliteInstaller::getGlobalDefaults
39 * @ingroup Maintenance
41 class CommandLineInstaller extends Maintenance {
42 public function __construct() {
43 parent::__construct();
44 global $IP;
46 $this->addDescription( "CLI-based MediaWiki installation and configuration.\n" .
47 "Default options are indicated in parentheses." );
49 $this->addArg( 'name', 'The name of the wiki' );
50 $this->addArg( 'admin', 'The username of the wiki administrator.' );
52 $this->addOption( 'pass', 'The password for the wiki administrator.', false, true );
53 $this->addOption(
54 'passfile',
55 'An alternative way to provide pass option, as the contents of this file',
56 false,
57 true
59 /* $this->addOption( 'email', 'The email for the wiki administrator', false, true ); */
60 $this->addOption(
61 'scriptpath',
62 'The relative path of the wiki in the web server (/' . basename( dirname( __DIR__ ) ) . ')',
63 false,
64 true
66 $this->addOption(
67 'server',
68 'The base URL of the web server the wiki will be on (http://localhost)',
69 false,
70 true
73 $this->addOption( 'lang', 'The language to use (en)', false, true );
74 /* $this->addOption( 'cont-lang', 'The content language (en)', false, true ); */
76 $this->addOption( 'dbtype', 'The type of database (mysql)', false, true );
77 $this->addOption( 'dbserver', 'The database host (localhost)', false, true );
78 $this->addOption( 'dbssl', 'Connect to the database over SSL' );
79 $this->addOption( 'dbport', 'The database port; only for PostgreSQL (5432)', false, true );
80 $this->addOption( 'dbname', 'The database name (my_wiki)', false, true );
81 $this->addOption( 'dbpath', 'The path for the SQLite DB ($IP/data)', false, true );
82 $this->addOption( 'dbprefix', 'Optional database table name prefix', false, true );
83 $this->addOption( 'installdbuser', 'The user to use for installing (root)', false, true );
84 $this->addOption( 'installdbpass', 'The password for the DB user to install as.', false, true );
85 $this->addOption( 'dbuser', 'The user to use for normal operations (wikiuser)', false, true );
86 $this->addOption( 'dbpass', 'The password for the DB user for normal operations', false, true );
87 $this->addOption(
88 'dbpassfile',
89 'An alternative way to provide dbpass option, as the contents of this file',
90 false,
91 true
93 $this->addOption( 'confpath', "Path to write LocalSettings.php to ($IP)", false, true );
94 $this->addOption( 'dbschema', 'The schema for the MediaWiki DB in '
95 . 'PostgreSQL (mediawiki)', false, true );
97 $this->addOption( 'namespace', 'The project namespace (same as the "name" argument)',
98 false, true );
100 $this->addOption( 'env-checks', "Run environment checks only, don't change anything" );
102 $this->addOption( 'with-extensions', "Detect and include extensions" );
103 $this->addOption( 'extensions', 'Comma-separated list of extensions to install',
104 false, true, false, true );
105 $this->addOption( 'skins', 'Comma-separated list of skins to install (default: all)',
106 false, true, false, true );
109 public function canExecuteWithoutLocalSettings(): bool {
110 return true;
113 public function finalSetup( SettingsBuilder $settingsBuilder = null ) {
114 if ( !$settingsBuilder ) {
115 $settingsBuilder = SettingsBuilder::getInstance();
118 parent::finalSetup( $settingsBuilder );
119 Installer::overrideConfig( $settingsBuilder );
122 public function getDbType() {
123 if ( $this->hasOption( 'env-checks' ) ) {
124 return Maintenance::DB_NONE;
126 return parent::getDbType();
129 public function execute() {
130 global $IP;
132 $siteName = $this->getArg( 0, 'MediaWiki' ); // Will not be set if used with --env-checks
133 $adminName = $this->getArg( 1 );
134 $envChecksOnly = $this->hasOption( 'env-checks' );
136 $scriptpath = $this->getOption( 'scriptpath', false );
137 if ( $scriptpath === false ) {
138 $this->mOptions['scriptpath'] = '/' . basename( dirname( __DIR__ ) );
141 $this->setDbPassOption();
142 if ( !$envChecksOnly ) {
143 $this->setPassOption();
146 try {
147 $installer = InstallerOverrides::getCliInstaller( $siteName, $adminName, $this->parameters->getOptions() );
148 } catch ( \MediaWiki\Installer\InstallException $e ) {
149 $this->error( $e->getStatus()->getMessage( false, false, 'en' )->text() . "\n" );
150 return false;
153 $status = $installer->doEnvironmentChecks();
154 if ( $status->isGood() ) {
155 $installer->showMessage( 'config-env-good' );
156 } else {
157 return false;
159 if ( !$envChecksOnly ) {
160 $status = $installer->execute();
161 if ( !$status->isGood() ) {
162 return false;
164 $installer->writeConfigurationFile( $this->getOption( 'confpath', $IP ) );
165 $installer->showMessage(
166 'config-install-success',
167 $installer->getVar( 'wgServer' ),
168 $installer->getVar( 'wgScriptPath' )
171 return true;
174 private function setDbPassOption() {
175 $dbpassfile = $this->getOption( 'dbpassfile' );
176 if ( $dbpassfile !== null ) {
177 if ( $this->getOption( 'dbpass' ) !== null ) {
178 $this->error( 'WARNING: You have provided the options "dbpass" and "dbpassfile". '
179 . 'The content of "dbpassfile" overrides "dbpass".' );
181 AtEase::suppressWarnings();
182 $dbpass = file_get_contents( $dbpassfile ); // returns false on failure
183 AtEase::restoreWarnings();
184 if ( $dbpass === false ) {
185 $this->fatalError( "Couldn't open $dbpassfile" );
187 $this->setOption( 'dbpass', trim( $dbpass, "\r\n" ) );
191 private function setPassOption() {
192 $passfile = $this->getOption( 'passfile' );
193 if ( $passfile !== null ) {
194 if ( $this->getOption( 'pass' ) !== null ) {
195 $this->error( 'WARNING: You have provided the option --pass or --passfile. '
196 . 'The content of "passfile" overrides "pass".' );
198 AtEase::suppressWarnings();
199 $pass = file_get_contents( $passfile ); // returns false on failure
200 AtEase::restoreWarnings();
201 if ( $pass === false ) {
202 $this->fatalError( "Couldn't open $passfile" );
204 $this->setOption( 'pass', trim( $pass, "\r\n" ) );
205 } elseif ( $this->getOption( 'pass' ) === null ) {
206 $this->fatalError( 'You need to provide the option "pass" or "passfile"' );
210 public function validateParamsAndArgs() {
211 if ( !$this->hasOption( 'env-checks' ) ) {
212 parent::validateParamsAndArgs();
217 $maintClass = CommandLineInstaller::class;
219 require_once RUN_MAINTENANCE_IF_MAIN;