From a6ee8eda1b76b9edd487d00825fb4ad03eae1974 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Thu, 15 Aug 2019 10:31:43 +0200 Subject: [PATCH] allow apps to specify to be automatic deinstalled this can either happen uncoditional or with an SQL statement checking they are empty first: $setup_info[wiki][autodeinstall] = "SELECT COUNT(*)<=3 FROM egw_wiki_pages"; which would deinstall wiki if it has only the 3 default pages in it --- setup/applications.php | 31 +------------- setup/inc/class.setup_cmd.inc.php | 36 +++++++++++++++++ setup/inc/class.setup_process.inc.php | 58 ++++++++++++++++++++++++--- 3 files changed, 90 insertions(+), 35 deletions(-) diff --git a/setup/applications.php b/setup/applications.php index ee6fdd5815..0eb23f1b64 100644 --- a/setup/applications.php +++ b/setup/applications.php @@ -127,36 +127,7 @@ if(@$_POST['submit']) if(!empty($remove) && is_array($remove)) { - $historylog = new Api\Storage\History(); - $historylog->db = $GLOBALS['egw_setup']->db; - - foreach($remove as $appname => $key) - { - $app_title = $setup_info[$appname]['title'] ? $setup_info[$appname]['title'] : $setup_info[$appname]['name']; - $terror = array(); - $terror[$appname] = $setup_info[$appname]; - - if ($setup_info[$appname]['tables']) - { - $GLOBALS['egw_setup']->process->droptables($terror,$DEBUG); - echo '
' . $app_title . ' ' . lang('tables dropped') . '.'; - } - - $GLOBALS['egw_setup']->deregister_app($setup_info[$appname]['name']); - echo '
' . $app_title . ' ' . lang('deregistered') . '.'; - - $register_hooks = true; - - $historylog->appname = $appname; - if ($historylog->delete(null)) - { - echo '
' . $app_title . ' ' . lang('Historylog removed') . '.'; - } - - // delete all application categories and ACL - $GLOBALS['egw_setup']->db->delete($GLOBALS['egw_setup']->cats_table,array('cat_appname' => $appname),__LINE__,__FILE__); - $GLOBALS['egw_setup']->db->delete($GLOBALS['egw_setup']->acl_table,array('acl_appname' => $appname),__LINE__,__FILE__); - } + $register_hooks = $GLOBALS['egw_setup']->process->remove(array_keys($remove), $setup_info, $DEBUG) > 0; } if(!empty($install) && is_array($install)) diff --git a/setup/inc/class.setup_cmd.inc.php b/setup/inc/class.setup_cmd.inc.php index ac5c2c4e95..9e87699c95 100644 --- a/setup/inc/class.setup_cmd.inc.php +++ b/setup/inc/class.setup_cmd.inc.php @@ -329,6 +329,9 @@ abstract class setup_cmd extends admin_cmd self::$apps_to_install[] = $app; } } + // add autodeinstall apps + self::$apps_to_upgrade = array_unique(array_merge(self::$apps_to_upgrade, self::check_autodeinstall())); + if (self::$apps_to_install) { self::_echo_message($verbose); @@ -370,6 +373,39 @@ abstract class setup_cmd extends admin_cmd return $ret; } + /** + * Check if app should be automatically deinstalled + * + * @return array with app-names to automatic deinstall + */ + static function check_autodeinstall() + { + global $setup_info; + + $ret = array_values(array_filter(array_keys($setup_info), function($app) + { + global $setup_info; + if (empty($setup_info[$app]['autodeinstall'])) + { + return false; + } + $autodeinstall = $setup_info[$app]['autodeinstall']; + if (!is_bool($autodeinstall)) + { + try { + $autodeinstall = (bool)$GLOBALS['egw_setup']->db->query($autodeinstall, __LINE__, __FILE__)->fetchColumn(); + } + catch (\Exception $e) { + _egw_log_exception($e); + $autodeinstall = false; + } + } + return $autodeinstall; + })); + //error_log(__METHOD__."() apps=".json_encode(array_keys($setup_info)).' returning '.json_encode($ret)); + return $ret; + } + /** * Echo the given message, if $verbose * diff --git a/setup/inc/class.setup_process.inc.php b/setup/inc/class.setup_process.inc.php index bc01d9e505..2f1d6a5707 100755 --- a/setup/inc/class.setup_process.inc.php +++ b/setup/inc/class.setup_process.inc.php @@ -105,8 +105,8 @@ class setup_process $i = 1; $passed = array(); $passing = array(); - $pass_string = implode (':', $pass); - $passing_string = implode (':', $passing); + $pass_string = implode (':', array_keys($pass)); + $passing_string = implode (':', array_keys($passing)); while($pass_string != $passing_string) { $passing = array(); @@ -193,9 +193,16 @@ class setup_process _debug_array($passed); exit; } - $pass_string = implode (':', $pass); - $passing_string = implode (':', $passing); + $pass_string = implode (':', array_keys($pass)); + $passing_string = implode (':', array_keys($passing)); } + + // remove all apps which should be automatic deinstalled + if (($deinstall = setup_cmd::check_autodeinstall())) + { + $this->remove($deinstall, $setup_info, $DEBUG); + } + try { // flush instance cache: also registers hooks and flushes image cache Api\Cache::flush(Api\Cache::INSTANCE); @@ -771,7 +778,7 @@ class setup_process $sColumns = null; $GLOBALS['egw_setup']->oProc->m_oTranslator->_GetColumns($GLOBALS['egw_setup']->oProc, $tablename, $sColumns); - foreach($GLOBALS['egw_setup']->oProc->m_oTranslator->sCol as $key => $tbldata) + foreach($GLOBALS['egw_setup']->oProc->m_oTranslator->sCol as $tbldata) { $arr .= $tbldata; } @@ -782,4 +789,45 @@ class setup_process return array($arr,$pk,$fk,$ix,$uc); } + + /** + * Deinstall given apps + * + * @param array $apps name of apps to deinstall + * @param array $setup_info + * @param bool $DEBUG =false + * @return int + */ + function remove(array $apps, array $setup_info, $DEBUG=false) + { + $historylog = new Api\Storage\History(); + $historylog->db = $GLOBALS['egw_setup']->db; + + foreach($apps as $appname) + { + $app_title = $setup_info[$appname]['title'] ? $setup_info[$appname]['title'] : $setup_info[$appname]['name']; + $terror = array(); + $terror[$appname] = $setup_info[$appname]; + + if ($setup_info[$appname]['tables']) + { + $this->droptables($terror,$DEBUG); + echo '
' . $app_title . ' ' . lang('tables dropped') . '.'; + } + + $GLOBALS['egw_setup']->deregister_app($setup_info[$appname]['name']); + echo '
' . $app_title . ' ' . lang('deregistered') . '.'; + + $historylog->appname = $appname; + if ($historylog->delete(null)) + { + echo '
' . $app_title . ' ' . lang('Historylog removed') . '.'; + } + + // delete all application categories and ACL + $GLOBALS['egw_setup']->db->delete($GLOBALS['egw_setup']->cats_table,array('cat_appname' => $appname),__LINE__,__FILE__); + $GLOBALS['egw_setup']->db->delete($GLOBALS['egw_setup']->acl_table,array('acl_appname' => $appname),__LINE__,__FILE__); + } + return count($apps); + } }