friendica-github/src/Core/Console/AutomaticInstallation.php

167 lines
3.8 KiB
PHP
Raw Normal View History

<?php
namespace Friendica\Core\Console;
use Asika\SimpleConsole\Console;
use dba;
use Friendica\App;
use Friendica\Core\Install;
require_once 'mod/install.php';
class AutomaticInstallation extends Console
{
protected function getHelp()
{
return <<<HELP
Installation - Install Friendica automatically
Synopsis
2018-04-18 12:30:42 +00:00
bin/console autoinstall [-h|--help|-?] [-v] [-a]
2018-04-19 08:21:41 +00:00
Description
Installs Friendica with data based on the htconfig.php file
Notes:
Not checking .htaccess/URL-Rewrite during CLI installation.
Options
-h|--help|-? Show help information
-v Show more debug information.
-a All setup checks are required (except .htaccess)
HELP;
}
protected function doExecute()
{
// Initialise the app
2018-04-18 12:30:42 +00:00
$this->out("Initializing setup...\n");
$a = get_app();
$db_host = '';
$db_user = '';
$db_pass = '';
$db_data = '';
2018-04-18 12:30:42 +00:00
require_once 'htconfig.php';
Install::setInstallMode();
2018-04-18 12:30:42 +00:00
$this->out(" Complete!\n\n");
// Check basic setup
2018-04-18 12:30:42 +00:00
$this->out("Checking basic setup...\n");
$checkResults = [];
$checkResults['basic'] = $this->runBasicChecks($a);
$errorMessage = $this->extractErrors($checkResults['basic']);
if ($errorMessage !== '') {
2018-04-18 12:30:42 +00:00
throw new \RuntimeException($errorMessage);
}
2018-04-18 12:30:42 +00:00
$this->out(" Complete!\n\n");
// Check database connection
2018-04-18 12:30:42 +00:00
$this->out("Checking database...\n");
$checkResults['db'] = array();
$checkResults['db'][] = $this->runDatabaseCheck($db_host, $db_user, $db_pass, $db_data);
$errorMessage = $this->extractErrors($checkResults['db']);
if ($errorMessage !== '') {
2018-04-18 12:30:42 +00:00
throw new \RuntimeException($errorMessage);
}
2018-04-18 12:30:42 +00:00
$this->out(" Complete!\n\n");
// Install database
2018-04-18 12:30:42 +00:00
$this->out("Inserting data into database...\n");
$checkResults['data'] = Install::loadDatabase();
if ($checkResults['data'] !== '') {
2018-04-18 12:30:42 +00:00
throw new \RuntimeException("ERROR: DB Database creation error. Is the DB empty?\n");
}
2018-04-18 12:30:42 +00:00
$this->out(" Complete!\n\n");
// Copy config file
2018-04-18 12:30:42 +00:00
$this->out("Saving config file...\n");
if (!copy('htconfig.php', '.htconfig.php')) {
throw new \RuntimeException("ERROR: Saving config file failed. Please copy .htautoinstall.php to .htconfig.php manually.\n");
}
2018-04-18 12:30:42 +00:00
$this->out(" Complete!\n\n");
$this->out("\nInstallation is finished\n");
return 0;
}
/**
* @param App $app
* @return array
*/
2018-04-18 12:30:42 +00:00
private function runBasicChecks($app)
{
$checks = [];
Install::checkFunctions($checks);
Install::checkImagik($checks);
Install::checkHtConfig($checks);
Install::checkSmarty3($checks);
Install::checkKeys($checks);
if (!empty($app->config['php_path'])) {
Install::checkPHP($app->config['php_path'], $checks);
} else {
2018-04-18 12:30:42 +00:00
throw new \RuntimeException(" ERROR: The php_path is not set in the config. Please check the file .htconfig.php.\n");
}
2018-04-18 12:30:42 +00:00
$this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
return $checks;
}
/**
* @param $db_host
* @param $db_user
* @param $db_pass
* @param $db_data
* @return array
*/
2018-04-18 12:30:42 +00:00
private function runDatabaseCheck($db_host, $db_user, $db_pass, $db_data)
{
$result = array(
'title' => 'MySQL Connection',
'required' => true,
'status' => true,
'help' => '',
);
if (!dba::connect($db_host, $db_user, $db_pass, $db_data, true)) {
$result['status'] = false;
$result['help'] = 'Failed, please check your MySQL settings and credentials.';
}
return $result;
}
/**
* @param array $results
* @return string
*/
2018-04-18 12:30:42 +00:00
private function extractErrors($results)
{
$errorMessage = '';
$allChecksRequired = $this->getOption('a') !== null;
foreach ($results as $result) {
if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
$errorMessage .= "--------\n";
$errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
}
}
return $errorMessage;
}
}