MediaWiki  master
install.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
26 define( 'MW_CONFIG_CALLBACK', 'Installer::overrideConfig' );
27 define( 'MEDIAWIKI_INSTALL', true );
28 
39  function __construct() {
40  parent::__construct();
41  global $IP;
42 
43  $this->addDescription( "CLI-based MediaWiki installation and configuration.\n" .
44  "Default options are indicated in parentheses." );
45 
46  $this->addArg( 'name', 'The name of the wiki (MediaWiki)', false );
47 
48  $this->addArg( 'admin', 'The username of the wiki administrator.' );
49  $this->addOption( 'pass', 'The password for the wiki administrator.', false, true );
50  $this->addOption(
51  'passfile',
52  'An alternative way to provide pass option, as the contents of this file',
53  false,
54  true
55  );
56  /* $this->addOption( 'email', 'The email for the wiki administrator', false, true ); */
57  $this->addOption(
58  'scriptpath',
59  'The relative path of the wiki in the web server (/wiki)',
60  false,
61  true
62  );
63 
64  $this->addOption( 'lang', 'The language to use (en)', false, true );
65  /* $this->addOption( 'cont-lang', 'The content language (en)', false, true ); */
66 
67  $this->addOption( 'dbtype', 'The type of database (mysql)', false, true );
68  $this->addOption( 'dbserver', 'The database host (localhost)', false, true );
69  $this->addOption( 'dbport', 'The database port; only for PostgreSQL (5432)', false, true );
70  $this->addOption( 'dbname', 'The database name (my_wiki)', false, true );
71  $this->addOption( 'dbpath', 'The path for the SQLite DB ($IP/data)', false, true );
72  $this->addOption( 'dbprefix', 'Optional database table name prefix', false, true );
73  $this->addOption( 'installdbuser', 'The user to use for installing (root)', false, true );
74  $this->addOption( 'installdbpass', 'The password for the DB user to install as.', false, true );
75  $this->addOption( 'dbuser', 'The user to use for normal operations (wikiuser)', false, true );
76  $this->addOption( 'dbpass', 'The password for the DB user for normal operations', false, true );
77  $this->addOption(
78  'dbpassfile',
79  'An alternative way to provide dbpass option, as the contents of this file',
80  false,
81  true
82  );
83  $this->addOption( 'confpath', "Path to write LocalSettings.php to ($IP)", false, true );
84  $this->addOption( 'dbschema', 'The schema for the MediaWiki DB in '
85  . 'PostgreSQL/Microsoft SQL Server (mediawiki)', false, true );
86  /*
87  $this->addOption( 'namespace', 'The project namespace (same as the "name" argument)',
88  false, true );
89  */
90  $this->addOption( 'env-checks', "Run environment checks only, don't change anything" );
91 
92  $this->addOption( 'with-extensions', "Detect and include extensions" );
93  $this->addOption( 'extensions', 'Comma-separated list of extensions to install',
94  false, true, false, true );
95  $this->addOption( 'skins', 'Comma-separated list of skins to install (default: all)',
96  false, true, false, true );
97  }
98 
99  public function getDbType() {
100  if ( $this->hasOption( 'env-checks' ) ) {
101  return Maintenance::DB_NONE;
102  }
103  return parent::getDbType();
104  }
105 
106  function execute() {
107  global $IP;
108 
109  $siteName = $this->getArg( 0, 'MediaWiki' ); // Will not be set if used with --env-checks
110  $adminName = $this->getArg( 1 );
111  $envChecksOnly = $this->hasOption( 'env-checks' );
112 
113  $this->setDbPassOption();
114  if ( !$envChecksOnly ) {
115  $this->setPassOption();
116  }
117 
118  try {
119  $installer = InstallerOverrides::getCliInstaller( $siteName, $adminName, $this->mOptions );
120  } catch ( \MediaWiki\Installer\InstallException $e ) {
121  $this->output( $e->getStatus()->getMessage()->text() . "\n" );
122  return false;
123  }
124 
125  $status = $installer->doEnvironmentChecks();
126  if ( $status->isGood() ) {
127  $installer->showMessage( 'config-env-good' );
128  } else {
129  $installer->showStatusMessage( $status );
130 
131  return false;
132  }
133  if ( !$envChecksOnly ) {
134  $status = $installer->execute();
135  if ( !$status->isGood() ) {
136  $installer->showStatusMessage( $status );
137 
138  return false;
139  }
140  $installer->writeConfigurationFile( $this->getOption( 'confpath', $IP ) );
141  $installer->showMessage(
142  'config-install-success',
143  $installer->getVar( 'wgServer' ),
144  $installer->getVar( 'wgScriptPath' )
145  );
146  }
147  return true;
148  }
149 
150  private function setDbPassOption() {
151  $dbpassfile = $this->getOption( 'dbpassfile' );
152  if ( $dbpassfile !== null ) {
153  if ( $this->getOption( 'dbpass' ) !== null ) {
154  $this->error( 'WARNING: You have provided the options "dbpass" and "dbpassfile". '
155  . 'The content of "dbpassfile" overrides "dbpass".' );
156  }
157  Wikimedia\suppressWarnings();
158  $dbpass = file_get_contents( $dbpassfile ); // returns false on failure
159  Wikimedia\restoreWarnings();
160  if ( $dbpass === false ) {
161  $this->fatalError( "Couldn't open $dbpassfile" );
162  }
163  $this->mOptions['dbpass'] = trim( $dbpass, "\r\n" );
164  }
165  }
166 
167  private function setPassOption() {
168  $passfile = $this->getOption( 'passfile' );
169  if ( $passfile !== null ) {
170  if ( $this->getOption( 'pass' ) !== null ) {
171  $this->error( 'WARNING: You have provided the options "pass" and "passfile". '
172  . 'The content of "passfile" overrides "pass".' );
173  }
174  Wikimedia\suppressWarnings();
175  $pass = file_get_contents( $passfile ); // returns false on failure
176  Wikimedia\restoreWarnings();
177  if ( $pass === false ) {
178  $this->fatalError( "Couldn't open $passfile" );
179  }
180  $this->mOptions['pass'] = trim( $pass, "\r\n" );
181  } elseif ( $this->getOption( 'pass' ) === null ) {
182  $this->fatalError( 'You need to provide the option "pass" or "passfile"' );
183  }
184  }
185 
187  if ( !$this->hasOption( 'env-checks' ) ) {
188  parent::validateParamsAndArgs();
189  }
190  }
191 }
192 
193 $maintClass = CommandLineInstaller::class;
194 
195 require_once RUN_MAINTENANCE_IF_MAIN;
getArg( $argId=0, $default=null)
Get an argument.
const DB_NONE
Constants for DB access type.
Definition: Maintenance.php:87
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
error( $err, $die=0)
Throw an error to the user.
$IP
Definition: WebStart.php:41
getOption( $name, $default=null)
Get an option, or return the default.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
hasOption( $name)
Checks to see if a particular option exists.
A helper class for throttling authentication attempts.
addDescription( $text)
Set the description text.
Maintenance script to install and configure MediaWiki.
Definition: install.php:38
addArg( $arg, $description, $required=true)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
Base installer class.
Definition: Installer.php:46
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
static getCliInstaller( $siteName, $admin=null, array $options=[])
Instantiates and returns an instance of CliInstaller or its descendant classes.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
$maintClass
Definition: install.php:193