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