Admin/config

* added onUpdate handler to rebuild affected files
This commit is contained in:
Sarjuuk
2017-05-07 18:40:45 +02:00
parent 33a1a45a63
commit 9c6a789d2b
5 changed files with 153 additions and 23 deletions

View File

@@ -319,25 +319,29 @@ class AjaxAdmin extends AjaxHandler
{
$key = trim($this->_get['key']);
$val = trim(urldecode($this->_get['val']));
$msg = '';
if (!strlen($key))
return 'empty option name given';
$flags = DB::Aowow()->selectCell('SELECT `flags` FROM ?_config WHERE `key` = ?', $key);
if (!$flags)
$cfg = DB::Aowow()->selectRow('SELECT `flags`, `value` FROM ?_config WHERE `key` = ?', $key);
if (!$cfg)
return 'configuration option not found';
if (!($flags & CON_FLAG_TYPE_STRING) && !strlen($val))
if (!($cfg['flags'] & CON_FLAG_TYPE_STRING) && !strlen($val))
return 'empty value given';
else if ($flags & CON_FLAG_TYPE_INT && !preg_match('/^-?\d+$/i', $val))
else if ($cfg['flags'] & CON_FLAG_TYPE_INT && !preg_match('/^-?\d+$/i', $val))
return "value must be integer";
else if ($flags & CON_FLAG_TYPE_FLOAT && !preg_match('/^-?\d*(,|.)?\d+$/i', $val))
else if ($cfg['flags'] & CON_FLAG_TYPE_FLOAT && !preg_match('/^-?\d*(,|.)?\d+$/i', $val))
return "value must be float";
else if ($flags & CON_FLAG_TYPE_BOOL)
else if ($cfg['flags'] & CON_FLAG_TYPE_BOOL)
$val = (int)!!$val; // *snort* bwahahaa
DB::Aowow()->query('UPDATE ?_config SET `value` = ? WHERE `key` = ?', $val, $key);
return '';
if (!$this->confOnChange($key, $val, $msg))
DB::Aowow()->query('UPDATE ?_config SET `value` = ? WHERE `key` = ?', $cfg['value'], $key);
return $msg;
}
protected function wtSave()
@@ -443,4 +447,46 @@ class AjaxAdmin extends AjaxHandler
return null;
}
private function confOnChange($key, $val, &$msg)
{
$fn = $buildList = null;
switch ($key)
{
case 'battlegroup':
$buildList = 'realms,realmMenu';
break;
case 'name_short':
$buildList = 'searchboxBody,demo,searchplugin';
break;
case 'site_host':
$buildList = 'searchplugin,demo,power,searchboxBody';
break;
case 'static_host':
$buildList = 'searchplugin,power,searchboxBody,searchboxScript';
break;
case 'profiler_queue':
$fn = function($x) use (&$msg) {
if (!$x)
return true;
return Profiler::queueStart($msg);
};
break;
default: // nothing to do, everything is fine
return true;
}
if ($buildList)
{
// we need to use exec as build() can only be run from CLI
exec('php aowow --build='.$buildList, $out);
foreach ($out as $o)
if (strstr($o, 'ERR'))
$msg .= explode('0m]', $o)[1]."<br />\n";
}
return $fn ? $fn($val) : true;
}
}

View File

@@ -16,8 +16,8 @@ function firstrun()
require_once 'setup/tools/sqlGen.class.php';
require_once 'setup/tools/fileGen.class.php';
SqlGen::init(true);
FileGen::init(true);
SqlGen::init(SqlGen::MODE_FIRSTRUN);
FileGen::init(FileGen::MODE_FIRSTRUN);
/****************/
/* define steps */

View File

@@ -14,6 +14,7 @@ if (!CLI)
function siteconfig()
{
$reqKeys = ['site_host', 'static_host'];
$updScripts = [];
if (!DB::isConnected(DB_AOWOW))
{
@@ -22,6 +23,43 @@ function siteconfig()
return;
}
$onChange = function($key, $val) use (&$updScripts)
{
$fn = null;
switch ($key)
{
case 'battlegroup':
array_push($updScripts, 'realms', 'realmMenu');
break;
case 'name_short':
array_push($updScripts, 'searchboxBody', 'demo', 'searchplugin');
break;
case 'site_host':
array_push($updScripts, 'searchplugin', 'demo', 'power', 'searchboxBody');
break;
case 'static_host':
array_push($updScripts, 'searchplugin', 'power', 'searchboxBody', 'searchboxScript');
break;
case 'profiler_queue':
$fn = function($x) {
if (!$x)
return true;
$ok = Profiler::queueStart($msg);
if ($msg)
CLISetup::log($msg, CLISetup::LOG_ERROR);
return $ok;
};
break;
default: // nothing to do, everything is fine
return true;
}
return $fn ? $fn($val) : true;
};
while (true)
{
CLISetup::log();
@@ -163,11 +201,12 @@ function siteconfig()
{
$conf = $cfgList[$inp['idx']];
$info = explode(' - ', $conf['comment']);
$key = strtolower($conf['key']);
$buff = '';
CLISetup::log();
$buff .= $conf['flags'] & CON_FLAG_PHP ? " PHP: " : "AOWOW: ";
$buff .= $conf['flags'] & CON_FLAG_PHP ? strtolower($conf['key']) : strtoupper('cfg_'.$conf['key']);
$buff .= $conf['flags'] & CON_FLAG_PHP ? $key : strtoupper('cfg_'.$conf['key']);
if (!empty($info[1]))
$buff .= " - ".$info[1];
@@ -281,7 +320,17 @@ function siteconfig()
}
else
{
DB::Aowow()->query('UPDATE ?_config SET `value` = ? WHERE `key` = ?', $use['idx'], strtolower($conf['key']));
$oldVal = DB::Aowow()->selectCell('SELECT `value` FROM ?_config WHERE `key` = ?', $key);
DB::Aowow()->query('UPDATE ?_config SET `value` = ? WHERE `key` = ?', $use['idx'], $key);
// postChange returned false => reset value
if (!$onChange($key, $use['idx']))
{
DB::Aowow()->query('UPDATE ?_config SET `value` = ? WHERE `key` = ?', $oldVal, $key);
sleep(1);
break;
}
CLISetup::log("setting updated", CLISetup::LOG_OK);
sleep(1);
break 3;
@@ -304,8 +353,9 @@ function siteconfig()
$val = trim(explode('default:', $info[0])[1]);
if (!($conf['flags'] & CON_FLAG_TYPE_STRING))
$val = @eval('return ('.$val.');');
if (DB::Aowow()->query('UPDATE ?_config SET `value` = ? WHERE `key` = ?', $val, strtolower($conf['key'])))
if (DB::Aowow()->query('UPDATE ?_config SET `value` = ? WHERE `key` = ?', $val, $key))
{
$onChange($key, $val);
CLISetup::log("default value restored", CLISetup::LOG_OK);
sleep(1);
}
@@ -314,7 +364,7 @@ function siteconfig()
if ($conf['flags'] & CON_FLAG_PERSISTENT)
continue 2;
if (DB::Aowow()->query('DELETE FROM ?_config WHERE `key` = ? AND (`flags` & ?d) = 0', strtolower($conf['key']), CON_FLAG_PERSISTENT))
if (DB::Aowow()->query('DELETE FROM ?_config WHERE `key` = ? AND (`flags` & ?d) = 0', $key, CON_FLAG_PERSISTENT))
{
CLISetup::log("php setting deleted ['".$conf['key']."': '".$conf['value']."']", CLISetup::LOG_OK);
sleep(1);
@@ -325,7 +375,7 @@ function siteconfig()
else
{
CLISetup::log();
CLISetup::log("edit canceled! returning to list...", CLISetup::LOG_INFO);
CLISetup::log('edit canceled! returning to list...', CLISetup::LOG_INFO);
sleep(1);
break;
}
@@ -334,16 +384,35 @@ function siteconfig()
else
{
CLISetup::log();
CLISetup::log("invalid selection", CLISetup::LOG_ERROR);
CLISetup::log('invalid selection', CLISetup::LOG_ERROR);
sleep(1);
}
}
else
{
CLISetup::log();
CLISetup::log("site configuration aborted", CLISetup::LOG_INFO);
CLISetup::log('site configuration aborted', CLISetup::LOG_INFO);
break;
}
// propagate changes to static files
if ($updScripts && (!class_exists('FileGen') || FileGen::getMode() != FileGen::MODE_FIRSTRUN))
{
require_once 'setup/tools/clisetup/build.func.php';
CLISetup::log();
CLISetup::log('regenerating affected static content', CLISetup::LOG_INFO);
CLISetup::log();
sleep(1);
if ($_ = array_diff($updScripts, build($updScripts)))
{
CLISetup::log(' - the following updates returned with errors, please recheck those - '.implode(', ', $_), CLISetup::LOG_ERROR);
sleep(1);
}
sleep(1);
$updScripts = [];
}
}
}

View File

@@ -10,9 +10,9 @@ if (!CLI)
class FileGen
{
const MODE_NORMAL = 0;
const MODE_FIRSTRUN = 1;
const MODE_UPDATE = 2;
const MODE_NORMAL = 1;
const MODE_FIRSTRUN = 2;
const MODE_UPDATE = 3;
public static $tplPath = 'setup/tools/filegen/templates/';
@@ -23,6 +23,7 @@ class FileGen
'icons', 'glyphs', 'pagetexts', 'loadingscreens', // whole images
'artwork', 'talentbgs', 'maps', 'spawn-maps', 'area-maps' // images from image parts
);
private static $mode = 0;
public static $subScripts = [];
public static $tplFiles = array(
@@ -107,6 +108,8 @@ class FileGen
CLISetup::log('created '.$pathOk.' extra paths'.($pathOk == count(self::$reqDirs) ? '' : ' with errors'));
CLISetup::log();
self::$mode = $mode;
}
private static function handleCLIOpts(&$doScripts)
@@ -271,6 +274,10 @@ class FileGen
return $success;
}
public static function getMode()
{
return self::$mode;
}
}
?>

View File

@@ -21,9 +21,9 @@ if (!CLI)
class SqlGen
{
const MODE_NORMAL = 0;
const MODE_FIRSTRUN = 1;
const MODE_UPDATE = 2;
const MODE_NORMAL = 1;
const MODE_FIRSTRUN = 2;
const MODE_UPDATE = 3;
private static $tables = array( // [dbcName, saveDbc, AowowDeps, TCDeps]
'achievementcategory' => ['achievement_category', false, null, null],
@@ -78,6 +78,7 @@ class SqlGen
public static $cliOpts = [];
private static $shortOpts = 'h';
private static $longOpts = ['sql::', 'help', 'sync:']; // general
private static $mode = 0;
public static $subScripts = [];
@@ -109,6 +110,8 @@ class SqlGen
CLISetup::log('No valid locale specified. Check your config or --locales parameter, if used', CLISetup::LOG_ERROR);
exit;
}
self::$mode = $mode;
}
private static function handleCLIOpts(&$doTbls)
@@ -201,6 +204,11 @@ class SqlGen
CLISetup::log(sprintf(ERR_MISSING_INCL, $tableName, 'setup/tools/sqlgen/'.$tableName.'.func.php'), CLISetup::LOG_ERROR);
}
public static function getMode()
{
return self::$mode;
}
}
?>