Rework App modes

- Replace App mode constants with capability-based flags
- Add App->isInstallMode()
- Add file config fallback in (P)Config abstraction
- Removed logger disabling code
This commit is contained in:
Hypolite Petovan 2018-06-30 14:40:09 -04:00
parent d487c399dd
commit cd9b864045
12 changed files with 74 additions and 79 deletions

View File

@ -28,7 +28,7 @@ require_once "include/dba.php";
$a = new App(dirname(__DIR__)); $a = new App(dirname(__DIR__));
if ($a->mode === App::MODE_INSTALL) { if ($a->isInstallMode()) {
die("Friendica isn't properly installed yet.\n"); die("Friendica isn't properly installed yet.\n");
} }

View File

@ -608,17 +608,9 @@ function logger($msg, $level = 0) {
$a = get_app(); $a = get_app();
global $LOGGER_LEVELS; global $LOGGER_LEVELS;
// turn off logger in install mode $debugging = Config::get('system', 'debugging');
if ( $logfile = Config::get('system', 'logfile');
$a->mode == App::MODE_INSTALL $loglevel = intval(Config::get('system', 'loglevel'));
|| !dba::$connected
) {
return;
}
$debugging = Config::get('system','debugging');
$logfile = Config::get('system','logfile');
$loglevel = intval(Config::get('system','loglevel'));
if ( if (
!$debugging !$debugging
@ -687,14 +679,6 @@ function logger($msg, $level = 0) {
function dlogger($msg, $level = 0) { function dlogger($msg, $level = 0) {
$a = get_app(); $a = get_app();
// turn off logger in install mode
if (
$a->mode == App::MODE_INSTALL
|| !dba::$connected
) {
return;
}
$logfile = Config::get('system', 'dlogfile'); $logfile = Config::get('system', 'dlogfile');
if (!$logfile) { if (!$logfile) {
return; return;
@ -716,7 +700,7 @@ function dlogger($msg, $level = 0) {
$process_id = session_id(); $process_id = session_id();
if ($process_id == '') { if ($process_id == '') {
$process_id = get_app()->process_id; $process_id = $a->process_id;
} }
$callers = debug_backtrace(); $callers = debug_backtrace();

View File

@ -35,20 +35,20 @@ $a->backend = false;
require_once "include/dba.php"; require_once "include/dba.php";
if (!$a->mode == App::MODE_INSTALL) { // Missing DB connection: ERROR
/** if ($a->mode & App::MODE_LOCALCONFIGPRESENT && !($a->mode & App::MODE_DBAVAILABLE)) {
* Load configs from db. Overwrite configs from config/local.ini.php System::httpExit(500, ['title' => 'Error 500 - Internal Server Error', 'description' => 'Apologies but the website is unavailable at the moment.']);
*/ }
Config::load();
// Max Load Average reached: ERROR
if ($a->isMaxProcessesReached() || $a->isMaxLoadReached()) { if ($a->isMaxProcessesReached() || $a->isMaxLoadReached()) {
header($_SERVER["SERVER_PROTOCOL"] . ' 503 Service Temporarily Unavailable');
header('Retry-After: 120'); header('Retry-After: 120');
header('Refresh: 120; url=' . System::baseUrl() . "/" . $a->query_string); header('Refresh: 120; url=' . System::baseUrl() . "/" . $a->query_string);
die("System is currently unavailable. Please try again later");
}
System::httpExit(503, ['title' => 'Error 503 - Service Temporarily Unavailable', 'description' => 'System is currently overloaded. Please try again later.']);
}
if ($a->isInstallMode()) {
if (Config::get('system', 'force_ssl') && ($a->get_scheme() == "http") if (Config::get('system', 'force_ssl') && ($a->get_scheme() == "http")
&& (intval(Config::get('system', 'ssl_policy')) == SSL_POLICY_FULL) && (intval(Config::get('system', 'ssl_policy')) == SSL_POLICY_FULL)
&& (substr(System::baseUrl(), 0, 8) == "https://") && (substr(System::baseUrl(), 0, 8) == "https://")
@ -167,9 +167,9 @@ $_SESSION['last_updated'] = defaults($_SESSION, 'last_updated', []);
// in install mode, any url loads install module // in install mode, any url loads install module
// but we need "view" module for stylesheet // but we need "view" module for stylesheet
if ($a->mode == App::MODE_INSTALL && $a->module!="view") { if ($a->isInstallMode() && $a->module!="view") {
$a->module = 'install'; $a->module = 'install';
} elseif ($a->mode == App::MODE_MAINTENANCE && $a->module!="view") { } elseif (!($a->mode & App::MODE_MAINTENANCEDISABLED) && $a->module != "view") {
$a->module = 'maintenance'; $a->module = 'maintenance';
} else { } else {
check_url($a); check_url($a);

View File

@ -34,9 +34,20 @@ require_once 'include/text.php';
*/ */
class App class App
{ {
const MODE_NORMAL = 0; const MODE_LOCALCONFIGPRESENT = 1;
const MODE_INSTALL = 1; const MODE_DBAVAILABLE = 2;
const MODE_MAINTENANCE = 2; const MODE_DBCONFIGAVAILABLE = 4;
const MODE_MAINTENANCEDISABLED = 8;
/**
* @deprecated since version 2008.08 Use App->isInstallMode() instead to check for install mode.
*/
const MODE_INSTALL = 0;
/**
* @deprecated since version 2008.08 Use the precise mode constant to check for a specific capability instead.
*/
const MODE_NORMAL = App::MODE_LOCALCONFIGPRESENT | App::MODE_DBAVAILABLE | App::MODE_DBCONFIGAVAILABLE | App::MODE_MAINTENANCEDISABLED;
public $module_loaded = false; public $module_loaded = false;
public $module_class = null; public $module_class = null;
@ -59,7 +70,7 @@ class App
public $argv; public $argv;
public $argc; public $argc;
public $module; public $module;
public $mode = App::MODE_NORMAL; public $mode = App::MODE_INSTALL;
public $strings; public $strings;
public $basepath; public $basepath;
public $urlpath; public $urlpath;
@ -152,7 +163,9 @@ class App
$this->determineUrlPath(); $this->determineUrlPath();
if ($this->mode === self::MODE_NORMAL) { Config::load();
if ($this->mode & self::MODE_DBAVAILABLE) {
Core\Addon::loadHooks(); Core\Addon::loadHooks();
$this->loadAddonConfig(); $this->loadAddonConfig();
@ -449,30 +462,32 @@ class App
*/ */
private function determineMode() private function determineMode()
{ {
$this->mode = App::MODE_INSTALL; $this->mode = 0;
// Missing local config files: MODE_INSTALL
if (!file_exists($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php') if (!file_exists($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')
&& !file_exists($this->basepath . DIRECTORY_SEPARATOR . '.htconfig.php')) { && !file_exists($this->basepath . DIRECTORY_SEPARATOR . '.htconfig.php')) {
return; return;
} }
// Missing DB connection: ERROR $this->mode |= App::MODE_LOCALCONFIGPRESENT;
if (!\dba::connected()) { if (!\dba::connected()) {
System::unavailable(); return;
} }
// Working DB connection, missing tables: MODE_INSTALL $this->mode |= App::MODE_DBAVAILABLE;
if (\dba::fetch_first("SHOW TABLES LIKE 'config'") === false) { if (\dba::fetch_first("SHOW TABLES LIKE 'config'") === false) {
return; return;
} }
// Maintenance mode check $this->mode |= App::MODE_DBCONFIGAVAILABLE;
if (Config::get('system', 'maintenance')) { if (Config::get('system', 'maintenance')) {
$this->mode = App::MODE_MAINTENANCE; return;
} else {
$this->mode = App::MODE_NORMAL;
} }
$this->mode |= App::MODE_MAINTENANCEDISABLED;
} }
public function loadDatabase() public function loadDatabase()
@ -520,6 +535,16 @@ class App
$this->save_timestamp($stamp1, "network"); $this->save_timestamp($stamp1, "network");
} }
/**
* Install mode is when the local config file is missing or the DB schema hasn't been installed yet.
*
* @return bool
*/
public function isInstallMode()
{
return !($this->mode & App::MODE_LOCALCONFIGPRESENT) || !($this->mode & App::MODE_DBCONFIGAVAILABLE);
}
/** /**
* @brief Returns the base filesystem path of the App * @brief Returns the base filesystem path of the App
* *
@ -1311,7 +1336,7 @@ class App
*/ */
public function getCurrentTheme() public function getCurrentTheme()
{ {
if ($this->mode == App::MODE_INSTALL) { if ($this->isInstallMode()) {
return ''; return '';
} }

View File

@ -30,7 +30,7 @@ class Config extends BaseObject
public static function init() public static function init()
{ {
// Database isn't ready or populated yet // Database isn't ready or populated yet
if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) { if (!(self::getApp()->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
return; return;
} }
@ -54,7 +54,7 @@ class Config extends BaseObject
public static function load($family = "config") public static function load($family = "config")
{ {
// Database isn't ready or populated yet // Database isn't ready or populated yet
if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) { if (!(self::getApp()->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
return; return;
} }
@ -87,7 +87,7 @@ class Config extends BaseObject
public static function get($family, $key, $default_value = null, $refresh = false) public static function get($family, $key, $default_value = null, $refresh = false)
{ {
// Database isn't ready or populated yet, fallback to file config // Database isn't ready or populated yet, fallback to file config
if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) { if (!(self::getApp()->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
return self::getApp()->getConfigValue($family, $key, $default_value); return self::getApp()->getConfigValue($family, $key, $default_value);
} }
@ -115,7 +115,7 @@ class Config extends BaseObject
public static function set($family, $key, $value) public static function set($family, $key, $value)
{ {
// Database isn't ready or populated yet // Database isn't ready or populated yet
if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) { if (!(self::getApp()->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
return false; return false;
} }
@ -140,7 +140,7 @@ class Config extends BaseObject
public static function delete($family, $key) public static function delete($family, $key)
{ {
// Database isn't ready or populated yet // Database isn't ready or populated yet
if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) { if (!(self::getApp()->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
return false; return false;
} }

View File

@ -92,7 +92,7 @@ HELP;
throw new CommandArgsException('Too many arguments'); throw new CommandArgsException('Too many arguments');
} }
if ($a->mode === \Friendica\App::MODE_INSTALL) { if (!($a->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
$this->out('Database isn\'t ready or populated yet, showing file config only'); $this->out('Database isn\'t ready or populated yet, showing file config only');
} }
@ -126,7 +126,7 @@ HELP;
if (count($this->args) == 0) { if (count($this->args) == 0) {
Core\Config::load(); Core\Config::load();
if (Core\Config::get('system', 'config_adapter') != 'preload' && $a->mode !== \Friendica\App::MODE_INSTALL) { if (Core\Config::get('system', 'config_adapter') != 'preload' && $a->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE) {
$this->out('Warning: The JIT (Just In Time) Config adapter doesn\'t support loading the entire configuration, showing file config only'); $this->out('Warning: The JIT (Just In Time) Config adapter doesn\'t support loading the entire configuration, showing file config only');
} }

View File

@ -56,7 +56,7 @@ HELP;
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments'); throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
} }
if ($a->mode == \Friendica\App::MODE_INSTALL) { if ($a->isInstallMode()) {
throw new \RuntimeException('Database isn\'t ready or populated yet'); throw new \RuntimeException('Database isn\'t ready or populated yet');
} }

View File

@ -64,7 +64,7 @@ HELP;
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments'); throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
} }
if ($a->mode == \Friendica\App::MODE_INSTALL) { if ($a->isInstallMode()) {
throw new \RuntimeException('Database isn\'t ready or populated yet'); throw new \RuntimeException('Database isn\'t ready or populated yet');
} }

View File

@ -64,7 +64,7 @@ HELP;
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments'); throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
} }
if ($a->mode == \Friendica\App::MODE_INSTALL) { if ($a->isInstallMode()) {
throw new \RuntimeException('Database isn\'t ready or populated yet'); throw new \RuntimeException('Database isn\'t ready or populated yet');
} }

View File

@ -58,7 +58,7 @@ HELP;
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments'); throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
} }
if ($a->mode == \Friendica\App::MODE_INSTALL) { if ($a->isInstallMode()) {
throw new \RuntimeException('Database isn\'t ready or populated yet'); throw new \RuntimeException('Database isn\'t ready or populated yet');
} }

View File

@ -29,7 +29,7 @@ class PConfig extends BaseObject
public static function init($uid) public static function init($uid)
{ {
// Database isn't ready or populated yet // Database isn't ready or populated yet
if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) { if (!(self::getApp()->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
return; return;
} }
@ -54,7 +54,7 @@ class PConfig extends BaseObject
public static function load($uid, $family) public static function load($uid, $family)
{ {
// Database isn't ready or populated yet // Database isn't ready or populated yet
if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) { if (!(self::getApp()->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
return; return;
} }
@ -83,7 +83,7 @@ class PConfig extends BaseObject
public static function get($uid, $family, $key, $default_value = null, $refresh = false) public static function get($uid, $family, $key, $default_value = null, $refresh = false)
{ {
// Database isn't ready or populated yet // Database isn't ready or populated yet
if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) { if (!(self::getApp()->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
return; return;
} }
@ -112,7 +112,7 @@ class PConfig extends BaseObject
public static function set($uid, $family, $key, $value) public static function set($uid, $family, $key, $value)
{ {
// Database isn't ready or populated yet // Database isn't ready or populated yet
if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) { if (!(self::getApp()->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
return false; return false;
} }
@ -138,7 +138,7 @@ class PConfig extends BaseObject
public static function delete($uid, $family, $key) public static function delete($uid, $family, $key)
{ {
// Database isn't ready or populated yet // Database isn't ready or populated yet
if (self::getApp()->mode === \Friendica\App::MODE_INSTALL) { if (!(self::getApp()->mode & \Friendica\App::MODE_DBCONFIGAVAILABLE)) {
return false; return false;
} }

View File

@ -85,20 +85,6 @@ class System extends BaseObject
return implode(', ', $callstack2); return implode(', ', $callstack2);
} }
/**
* @brief Called from db initialisation when db is dead.
*/
static public function unavailable() {
echo <<< EOT
<html>
<head><title>System Unavailable</title></head>
<body>Apologies but this site is unavailable at the moment. Please try again later.</body>
</html>
EOT;
killme();
}
/** /**
* Generic XML return * Generic XML return
* Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable * Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable