From 954f63b81abcdcc063bab4d89284949c68b8d26b Mon Sep 17 00:00:00 2001 From: Laurent Opprecht Date: Thu, 10 May 2012 11:22:17 +0200 Subject: [PATCH] 4728 Remove databases after upgrade --- main/admin/admin_page.class.php | 72 +++++++++++++++++++ main/admin/index.php | 1 + main/admin/system_management.php | 114 +++++++++++++++++++++++++++++++ main/inc/lib/chamilo.class.php | 7 +- 4 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 main/admin/admin_page.class.php create mode 100644 main/admin/system_management.php diff --git a/main/admin/admin_page.class.php b/main/admin/admin_page.class.php new file mode 100644 index 0000000000..1ffaebcd75 --- /dev/null +++ b/main/admin/admin_page.class.php @@ -0,0 +1,72 @@ + + */ +class AdminPage +{ + + protected $title = ''; + protected $breadcrumbs = ''; + + function __construct($title = '', $breadcrumbs = array()) + { + global $this_section; + $this_section = SECTION_PLATFORM_ADMIN; + + api_protect_admin_script(); + + if (empty($title)) { + $title = get_lang(get_class($this)); + } + + $this->title = $title; + + if (empty($breadcrumbs)) { + $breadcrumbs[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin')); + } + + $this->breadcrumbs = $breadcrumbs; + } + + public function get_title() + { + return $this->title; + } + + public function get_breadcrumbs() + { + return $this->breadcrumbs; + } + + function display() + { + $this->display_header(); + $this->display_content(); + $this->display_footer(); + } + + public function display_header() + { + $breadcrumbs = $this->get_breadcrumbs(); + $title = $this->get_title(); + + global $interbreadcrumb; + $interbreadcrumb = $breadcrumbs; + Display :: display_header($title); + } + + public function display_content() + { + ; + } + + public function display_footer() + { + Display :: display_footer(); + } + +} \ No newline at end of file diff --git a/main/admin/index.php b/main/admin/index.php index b60803c64d..b517dd99c9 100644 --- a/main/admin/index.php +++ b/main/admin/index.php @@ -242,6 +242,7 @@ if (api_is_platform_admin()) { $items[] = array('url'=>'archive_cleanup.php', 'label' => get_lang('ArchiveDirCleanup')); } + $items[] = array('url'=>'system_management.php', 'label' => get_lang('SystemManagement')); //$items[] = array('url'=>'statistics/index.php?action=activities', 'label' => get_lang('ImportantActivities')); $blocks['settings']['items'] = $items; diff --git a/main/admin/system_management.php b/main/admin/system_management.php new file mode 100644 index 0000000000..5526e8255e --- /dev/null +++ b/main/admin/system_management.php @@ -0,0 +1,114 @@ + + * @license see /license.txt + */ +$language_file = array('admin'); +$cidReset = true; +require_once '../inc/global.inc.php'; +require_once __DIR__ . '/admin_page.class.php'; + +class SystemManagementPage extends AdminPage +{ + + const PARAM_ACTION = 'action'; + const PARAM_SECURITY_TOKEN = 'security_token'; + const ACTION_DEFAULT = 'list'; + const ACTION_SECURITY_FAILED = 'security_failed'; + + function get_action() + { + $result = Request::get(self::PARAM_ACTION, self::ACTION_DEFAULT); + if ($result != self::ACTION_DEFAULT) { + $passed = Security::check_token('get'); + $result = $passed ? $result : self::ACTION_SECURITY_FAILED; + } + return $result; + } + + function url($params) + { + $token = Security::get_token(); + $params[self::PARAM_SECURITY_TOKEN] = $token; + return Uri::here($params); + } + + function display_default() + { + $message = get_lang('RemoveOldDatabaseMessage'); + $url = $this->url(array(PARAM_ACTION => 'drop_old_databases')); + $go = get_lang('go'); + + echo << +
  • +
    $message
    + $go +
  • + +EOT; + } + + function display_security_failed() + { + Display::display_error_message(get_lang('NotAuthorized')); + } + + function display_content() + { + $action = $this->get_action(); + switch ($action) { + case self::ACTION_DEFAULT: + $this->display_default(); + return; + + case self::ACTION_SECURITY_FAILED: + $this->display_security_failed(); + return; + + default: + $f == array($this, $action); + if (is_callable($f)) { + call_user_func($f); + return; + } else { + Display::display_error_message(get_lang('UnknownAction')); + } + return; + } + } + + /** + * + * @return ResultSet + */ + function get_old_databases() + { + $course_db = Database::get_main_table(TABLE_MAIN_COURSE); + $sql = "SELECT id, code, db_name, directory, course_language FROM $course_db WHERE target_course_code IS NULL AND db_name IS NOT NULL ORDER BY code"; + return new ResultSet($sql); + } + + function drop_old_databases() + { + $result = array(); + $courses = $this->get_old_databases(); + foreach ($courses as $course) { + $drop_statement = 'DROP DATABASE ' . $course['db_name']; + $success = Database::query($drop_statement); + if ($sucess) { + Database::update($course_db, array('course_db' => null), array('id' => $course['id'])); + $result[] = $course['db_name']; + } + } + return $result; + } + +} + +$page = new SystemManagementPage(); +$page->display(); diff --git a/main/inc/lib/chamilo.class.php b/main/inc/lib/chamilo.class.php index fac36aec69..7e9962995c 100644 --- a/main/inc/lib/chamilo.class.php +++ b/main/inc/lib/chamilo.class.php @@ -39,7 +39,12 @@ class Chamilo { return Uri::url($path, $params, $html); } - + + public static function here($params = array(), $html = true) + { + return Uri::here($params, $html); + } + /** * Application web root */