| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Installer; |
| 24 |
|
| 25 |
use MediaWiki\Context\RequestContext; |
| 26 |
use MediaWiki\MediaWikiServices; |
| 27 |
use MediaWiki\Parser\Sanitizer; |
| 28 |
use MediaWiki\Password\UserPasswordPolicy; |
| 29 |
use MediaWiki\Status\Status; |
| 30 |
use MediaWiki\User\User; |
| 31 |
use Wikimedia\Message\MessageSpecifier; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class CliInstaller extends Installer { |
| 40 |
|
| 41 |
private $specifiedScriptPath = false; |
| 42 |
|
| 43 |
private const OPTION_MAP = [ |
| 44 |
'dbtype' => 'wgDBtype', |
| 45 |
'dbserver' => 'wgDBserver', |
| 46 |
'dbname' => 'wgDBname', |
| 47 |
'dbuser' => 'wgDBuser', |
| 48 |
'dbpass' => 'wgDBpassword', |
| 49 |
'dbprefix' => 'wgDBprefix', |
| 50 |
'dbtableoptions' => 'wgDBTableOptions', |
| 51 |
'dbport' => 'wgDBport', |
| 52 |
'dbssl' => 'wgDBssl', |
| 53 |
'dbschema' => 'wgDBmwschema', |
| 54 |
'dbpath' => 'wgSQLiteDataDir', |
| 55 |
'server' => 'wgServer', |
| 56 |
'scriptpath' => 'wgScriptPath', |
| 57 |
]; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function __construct( $siteName, $admin = null, array $options = [] ) { |
| 66 |
global $wgPasswordPolicy; |
| 67 |
|
| 68 |
parent::__construct(); |
| 69 |
|
| 70 |
if ( isset( $options['scriptpath'] ) ) { |
| 71 |
$this->specifiedScriptPath = true; |
| 72 |
} |
| 73 |
|
| 74 |
foreach ( self::OPTION_MAP as $opt => $global ) { |
| 75 |
if ( isset( $options[$opt] ) ) { |
| 76 |
$GLOBALS[$global] = $options[$opt]; |
| 77 |
$this->setVar( $global, $options[$opt] ); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if ( isset( $options['lang'] ) ) { |
| 82 |
global $wgLang, $wgLanguageCode; |
| 83 |
$this->setVar( '_UserLang', $options['lang'] ); |
| 84 |
$wgLanguageCode = $options['lang']; |
| 85 |
$this->setVar( 'wgLanguageCode', $wgLanguageCode ); |
| 86 |
$wgLang = MediaWikiServices::getInstance()->getLanguageFactory() |
| 87 |
->getLanguage( $options['lang'] ); |
| 88 |
RequestContext::getMain()->setLanguage( $wgLang ); |
| 89 |
} |
| 90 |
|
| 91 |
$this->setVar( 'wgSitename', $siteName ); |
| 92 |
|
| 93 |
$contLang = MediaWikiServices::getInstance()->getContentLanguage(); |
| 94 |
$metaNS = $contLang->ucfirst( str_replace( ' ', '_', $siteName ) ); |
| 95 |
if ( $metaNS == 'MediaWiki' ) { |
| 96 |
$metaNS = 'Project'; |
| 97 |
} |
| 98 |
$this->setVar( 'wgMetaNamespace', $metaNS ); |
| 99 |
|
| 100 |
if ( !isset( $options['installdbuser'] ) ) { |
| 101 |
$this->setVar( '_InstallUser', |
| 102 |
$this->getVar( 'wgDBuser' ) ); |
| 103 |
$this->setVar( '_InstallPassword', |
| 104 |
$this->getVar( 'wgDBpassword' ) ); |
| 105 |
} else { |
| 106 |
$this->setVar( '_InstallUser', |
| 107 |
$options['installdbuser'] ); |
| 108 |
$this->setVar( '_InstallPassword', |
| 109 |
$options['installdbpass'] ?? "" ); |
| 110 |
|
| 111 |
|
| 112 |
$this->setVar( '_CreateDBAccount', true ); |
| 113 |
} |
| 114 |
|
| 115 |
if ( $admin ) { |
| 116 |
$this->setVar( '_AdminName', $admin ); |
| 117 |
if ( isset( $options['pass'] ) ) { |
| 118 |
$adminUser = User::newFromName( $admin ); |
| 119 |
if ( !$adminUser ) { |
| 120 |
throw new InstallException( Status::newFatal( 'config-admin-name-invalid' ) ); |
| 121 |
} |
| 122 |
$upp = new UserPasswordPolicy( |
| 123 |
$wgPasswordPolicy['policies'], |
| 124 |
$wgPasswordPolicy['checks'] |
| 125 |
); |
| 126 |
$status = $upp->checkUserPasswordForGroups( $adminUser, $options['pass'], |
| 127 |
[ 'bureaucrat', 'sysop', 'interface-admin' ] ); |
| 128 |
if ( !$status->isGood() ) { |
| 129 |
throw new InstallException( Status::newFatal( |
| 130 |
$status->getMessage( 'config-admin-error-password-invalid' ) ) ); |
| 131 |
} |
| 132 |
$this->setVar( '_AdminPassword', $options['pass'] ); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
if ( isset( $options['extensions'] ) ) { |
| 138 |
$status = $this->validateExtensions( |
| 139 |
'extension', 'extensions', $options['extensions'] ); |
| 140 |
if ( !$status->isOK() ) { |
| 141 |
throw new InstallException( $status ); |
| 142 |
} |
| 143 |
$this->setVar( '_Extensions', $status->value ); |
| 144 |
} elseif ( isset( $options['with-extensions'] ) ) { |
| 145 |
$status = $this->findExtensions(); |
| 146 |
if ( !$status->isOK() ) { |
| 147 |
throw new InstallException( $status ); |
| 148 |
} |
| 149 |
$this->setVar( '_Extensions', array_keys( $status->value ) ); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
if ( isset( $options['skins'] ) ) { |
| 154 |
$status = $this->validateExtensions( 'skin', 'skins', $options['skins'] ); |
| 155 |
if ( !$status->isOK() ) { |
| 156 |
throw new InstallException( $status ); |
| 157 |
} |
| 158 |
$skins = $status->value; |
| 159 |
} else { |
| 160 |
$status = $this->findExtensions( 'skins' ); |
| 161 |
if ( !$status->isOK() ) { |
| 162 |
throw new InstallException( $status ); |
| 163 |
} |
| 164 |
$skins = array_keys( $status->value ); |
| 165 |
} |
| 166 |
$this->setVar( '_Skins', $skins ); |
| 167 |
|
| 168 |
if ( $skins ) { |
| 169 |
$skinNames = array_map( 'strtolower', $skins ); |
| 170 |
$this->setVar( 'wgDefaultSkin', $this->getDefaultSkin( $skinNames ) ); |
| 171 |
} |
| 172 |
|
| 173 |
$this->setVar( '_WithDevelopmentSettings', isset( $options['with-developmentsettings'] ) ); |
| 174 |
} |
| 175 |
|
| 176 |
private function validateExtensions( $type, $directory, $nameLists ) { |
| 177 |
$extensions = []; |
| 178 |
$status = new Status; |
| 179 |
foreach ( (array)$nameLists as $nameList ) { |
| 180 |
foreach ( explode( ',', $nameList ) as $name ) { |
| 181 |
$name = trim( $name ); |
| 182 |
if ( $name === '' ) { |
| 183 |
continue; |
| 184 |
} |
| 185 |
$extStatus = $this->getExtensionInfo( $type, $directory, $name ); |
| 186 |
if ( $extStatus->isOK() ) { |
| 187 |
$extensions[] = $name; |
| 188 |
} else { |
| 189 |
$status->merge( $extStatus ); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
$extensions = array_unique( $extensions ); |
| 194 |
$status->value = $extensions; |
| 195 |
return $status; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public function execute() { |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
if ( count( $this->getVar( '_Caches' ) ) ) { |
| 208 |
|
| 209 |
$this->setVar( '_MainCacheType', 'accel' ); |
| 210 |
} |
| 211 |
|
| 212 |
$vars = Installer::getExistingLocalSettings(); |
| 213 |
if ( $vars ) { |
| 214 |
$status = Status::newFatal( "config-localsettings-cli-upgrade" ); |
| 215 |
$this->showStatusMessage( $status ); |
| 216 |
return $status; |
| 217 |
} |
| 218 |
|
| 219 |
$result = $this->performInstallation( |
| 220 |
[ $this, 'startStage' ], |
| 221 |
[ $this, 'endStage' ] |
| 222 |
); |
| 223 |
|
| 224 |
|
| 225 |
$lastStepStatus = end( $result ); |
| 226 |
if ( $lastStepStatus->isOK() ) { |
| 227 |
return Status::newGood(); |
| 228 |
} else { |
| 229 |
return $lastStepStatus; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public function writeConfigurationFile( $path ) { |
| 239 |
$ls = InstallerOverrides::getLocalSettingsGenerator( $this ); |
| 240 |
$ls->writeFile( "$path/LocalSettings.php" ); |
| 241 |
} |
| 242 |
|
| 243 |
public function startStage( $step ) { |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
$this->showMessage( "config-install-$step" ); |
| 248 |
} |
| 249 |
|
| 250 |
public function endStage( $step, $status ) { |
| 251 |
$this->showStatusMessage( $status ); |
| 252 |
if ( $status->isOK() ) { |
| 253 |
$this->showMessage( 'config-install-step-done' ); |
| 254 |
} else { |
| 255 |
$this->showError( 'config-install-step-failed' ); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
public function showMessage( $msg, ...$params ) { |
| 260 |
|
| 261 |
echo $this->getMessageText( $msg, $params ) . "\n"; |
| 262 |
flush(); |
| 263 |
} |
| 264 |
|
| 265 |
public function showSuccess( $msg, ...$params ) { |
| 266 |
|
| 267 |
echo $this->getMessageText( $msg, $params ) . "\n"; |
| 268 |
flush(); |
| 269 |
} |
| 270 |
|
| 271 |
public function showWarning( $msg, ...$params ) { |
| 272 |
|
| 273 |
echo $this->getMessageText( $msg, $params ) . "\n"; |
| 274 |
flush(); |
| 275 |
} |
| 276 |
|
| 277 |
public function showError( $msg, ...$params ) { |
| 278 |
|
| 279 |
echo "***{$this->getMessageText( $msg, $params )}***\n"; |
| 280 |
flush(); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
protected function getMessageText( $msg, $params ) { |
| 289 |
$text = wfMessage( $msg, $params )->parse(); |
| 290 |
|
| 291 |
$text = preg_replace( '/<a href="(.*?)".*?>(.*?)<\/a>/', '$2 <$1>', $text ); |
| 292 |
|
| 293 |
return Sanitizer::stripAllTags( $text ); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
public function showHelpBox( $msg, ...$params ) { |
| 302 |
} |
| 303 |
|
| 304 |
public function showStatusMessage( Status $status ) { |
| 305 |
|
| 306 |
foreach ( $status->getMessages( 'warning' ) as $msg ) { |
| 307 |
$this->showMessage( $msg ); |
| 308 |
} |
| 309 |
foreach ( $status->getMessages( 'error' ) as $msg ) { |
| 310 |
$this->showMessage( $msg ); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
public function envCheckPath() { |
| 315 |
if ( !$this->specifiedScriptPath ) { |
| 316 |
$this->showMessage( 'config-no-cli-uri', $this->getVar( "wgScriptPath" ) ); |
| 317 |
} |
| 318 |
|
| 319 |
return parent::envCheckPath(); |
| 320 |
} |
| 321 |
|
| 322 |
protected function envGetDefaultServer() { |
| 323 |
|
| 324 |
return 'http://localhost'; |
| 325 |
} |
| 326 |
|
| 327 |
public function dirIsExecutable( $dir, $url ) { |
| 328 |
$this->showMessage( 'config-no-cli-uploads-check', $dir ); |
| 329 |
|
| 330 |
return false; |
| 331 |
} |
| 332 |
} |