From 23021911b14cf6834fa4e04f5cbe79c736e2c425 Mon Sep 17 00:00:00 2001 From: Aris Feryanto Date: Sun, 8 May 2011 02:42:38 +0700 Subject: [PATCH] Recently used tables (class, configuration, remember in pma database) --- config.sample.inc.php | 1 + libraries/RecentTable.class.php | 198 ++++++++++++++++++++++++++++++ libraries/common.lib.php | 31 ----- libraries/config.default.php | 7 ++ libraries/config/messages.inc.php | 2 + libraries/header.inc.php | 15 ++- libraries/relation.lib.php | 16 ++- navigation.php | 24 +--- 8 files changed, 237 insertions(+), 57 deletions(-) create mode 100644 libraries/RecentTable.class.php diff --git a/config.sample.inc.php b/config.sample.inc.php index 0ea16d5f76..95d3a1a911 100644 --- a/config.sample.inc.php +++ b/config.sample.inc.php @@ -52,6 +52,7 @@ $cfg['Servers'][$i]['AllowNoPassword'] = false; // $cfg['Servers'][$i]['pdf_pages'] = 'pma_pdf_pages'; // $cfg['Servers'][$i]['column_info'] = 'pma_column_info'; // $cfg['Servers'][$i]['history'] = 'pma_history'; +// $cfg['Servers'][$i]['recent'] = 'pma_recent'; // $cfg['Servers'][$i]['tracking'] = 'pma_tracking'; // $cfg['Servers'][$i]['designer_coords'] = 'pma_designer_coords'; // $cfg['Servers'][$i]['userconfig'] = 'pma_userconfig'; diff --git a/libraries/RecentTable.class.php b/libraries/RecentTable.class.php new file mode 100644 index 0000000000..12eb7f5112 --- /dev/null +++ b/libraries/RecentTable.class.php @@ -0,0 +1,198 @@ +pma_table = PMA_backquote($GLOBALS['cfg']['Server']['pmadb']) .".". + PMA_backquote($GLOBALS['cfg']['Server']['recent']); + } + if (!isset($_SESSION['tmp_user_values']['recent_tables'])) { + $_SESSION['tmp_user_values']['recent_tables'] = + isset($this->pma_table) ? $this->getFromDb() : array(); + } + $this->tables =& $_SESSION['tmp_user_values']['recent_tables']; + } + + /** + * Returns class instance. + * + * @return RecentTable + */ + public static function getInstance() + { + if (is_null(self::$_instance)) { + self::$_instance = new RecentTable(); + } + return self::$_instance; + } + + /** + * Returns recently used tables from phpMyAdmin database. + * + * @uses $pma_table + * @uses PMA_query_as_controluser() + * @uses PMA_DBI_fetch_array() + * @uses json_decode() + * + * @return array + */ + public function getFromDb() + { + // Read from phpMyAdmin database, if recent tables is not in session + $sql_query = + " SELECT `tables` FROM " . $this->pma_table . + " WHERE `username` = '" . $GLOBALS['cfg']['Server']['user'] . "'"; + + $row = PMA_DBI_fetch_array(PMA_query_as_controluser($sql_query)); + if (isset($row[0])) { + return json_decode($row[0]); + } else { + return array(); + } + } + + /** + * Save recent tables into phpMyAdmin database. + * + * @uses PMA_query_as_controluser() + * @uses PMA_DBI_fetch_value() + * @uses PMA_DBI_try_query() + * @uses json_decode() + * @uses PMA_Message + * + * @return true|PMA_Message + */ + public function saveToDb() + { + $username = $GLOBALS['cfg']['Server']['user']; + $sql_query = + " SELECT COUNT(*) FROM " . $this->pma_table . + " WHERE `username` = '" . $username . "'"; + + $exist = PMA_DBI_fetch_value(PMA_query_as_controluser($sql_query)); + if ($exist) { + $sql_query = + " UPDATE " . $this->pma_table . + " SET `tables` = '" . PMA_sqlAddslashes(json_encode($this->tables)) . "'" . + " WHERE `username` = '" . $username . "'"; + + $success = PMA_DBI_try_query($sql_query, $GLOBALS['controllink']); + } else { + $sql_query = + " INSERT INTO " . $this->pma_table . " (`username`, `tables`)" . + " VALUES ('" . $username . "', '" . PMA_sqlAddslashes(json_encode($this->tables)) . "')"; + + $success = PMA_DBI_try_query($sql_query, $GLOBALS['controllink']); + } + + if (!$success) { + $message = PMA_Message::error(__('Could not save configuration')); + $message->addMessage('

'); + $message->addMessage(PMA_Message::rawError(PMA_DBI_getError($GLOBALS['controllink']))); + return $message; + } + return true; + } + + /** + * Trim recent table according to the LeftRecentTable configuration. + */ + public function trim() + { + while (count($this->tables) > $GLOBALS['cfg']['LeftRecentTable']) { + array_pop($this->tables); + } + } + + /** + * Return HTML code "select" for recently used tables. + * + * @return string + */ + public function getHtmlSelect() + { + $this->trim(); + + $html = ''; + + return $html; + } + + /** + * Add recently used tables. + * + * @param string $db Database name where the table is located + * @param string $table Table name + * + * @return true|PMA_Message True if success, PMA_Message if not + */ + public function add($db, $table) + { + $table_str = $db . '.' . $table; + + // add only if this is new table + if (!isset($this->tables[0]) || $this->tables[0] != $table_str) { + array_unshift($this->tables, $table_str); + $this->tables = array_unique($this->tables); + $this->trim(); + if (isset($this->pma_table)) { + return $this->saveToDb(); + } + } + return true; + } + +} +?> diff --git a/libraries/common.lib.php b/libraries/common.lib.php index 4ff850b242..56618a180a 100644 --- a/libraries/common.lib.php +++ b/libraries/common.lib.php @@ -3023,35 +3023,4 @@ function PMA_buildActionTitles() { $titles['Edit'] = PMA_getIcon('b_edit.png', __('Edit'), true); return $titles; } - -/** - * Trim recent table according to the LeftRecentTable configuration - */ -function PMA_trimRecentTable() { - while (count($_SESSION['tmp_user_values']['recent_tables']) > $GLOBALS['cfg']['LeftRecentTable']) { - array_pop($_SESSION['tmp_user_values']['recent_tables']); - } -} - -/** - * Add recently used tables - * - * @param string $db Database name where the table is located - * @param string $table Table name - * - * @uses PMA_trimRecentTable() - */ -function PMA_addRecentTable($db, $table) { - $recent_tables =& $_SESSION['tmp_user_values']['recent_tables']; - if (isset($recent_tables)) { - array_unshift($recent_tables, $db . '.' . $table); - $recent_tables = array_unique($recent_tables); - PMA_trimRecentTable(); - } else { - $recent_tables = array(); - array_unshift($recent_tables, $db . '.' . $table); - } - $GLOBALS['reload'] = true; - PMA_reloadNavigation(); -} ?> diff --git a/libraries/config.default.php b/libraries/config.default.php index 9111aec89d..ae4018c5e2 100644 --- a/libraries/config.default.php +++ b/libraries/config.default.php @@ -338,6 +338,13 @@ $cfg['Servers'][$i]['history'] = ''; */ $cfg['Servers'][$i]['designer_coords'] = ''; +/** + * table to store recently used tables + * - leave blank for no "persistent" recently used tables + * SUGGESTED: 'pma_recent' + */ +$cfg['Servers'][$i]['recent'] = ''; + /** * table to store SQL tracking * - leave blank for no SQL tracking diff --git a/libraries/config/messages.inc.php b/libraries/config/messages.inc.php index 9ebbfea8f6..47ccf7febf 100644 --- a/libraries/config/messages.inc.php +++ b/libraries/config/messages.inc.php @@ -282,6 +282,8 @@ $strConfigLeftLogoLinkWindow_desc = __('Open the linked page in the main window $strConfigLeftLogoLinkWindow_name = __('Logo link target'); $strConfigLeftPointerEnable_desc = __('Highlight server under the mouse cursor'); $strConfigLeftPointerEnable_name = __('Enable highlighting'); +$strConfigLeftRecentTable_desc = __('Maximum number of recently used tables; set 0 to disable'); +$strConfigLeftRecentTable_name = __('Recently used tables'); $strConfigLightTabs_desc = __('Use less graphically intense tabs'); $strConfigLightTabs_name = __('Light tabs'); $strConfigLimitChars_desc = __('Maximum number of characters shown in any non-numeric column on browse view'); diff --git a/libraries/header.inc.php b/libraries/header.inc.php index af9d4c6356..b581070e6c 100644 --- a/libraries/header.inc.php +++ b/libraries/header.inc.php @@ -8,10 +8,8 @@ if (! defined('PHPMYADMIN')) { exit; } -/** - * - */ require_once './libraries/common.inc.php'; +require_once './libraries/RecentTable.class.php'; /** @@ -152,8 +150,15 @@ if (isset($GLOBALS['is_ajax_request']) && !$GLOBALS['is_ajax_request']) { .'"' . "\n"; } // end if - // remember accessed table - PMA_addRecentTable($GLOBALS['db'], $GLOBALS['table']); + // add recently used table and reload the navigation + $result = RecentTable::getInstance()->add($GLOBALS['db'], $GLOBALS['table']); + if ($result === true) { + $GLOBALS['reload'] = true; + PMA_reloadNavigation(); + } else { + $error = $result; + $error->display(); + } } else { // no table selected, display database comment if present /** diff --git a/libraries/relation.lib.php b/libraries/relation.lib.php index 9588853f94..1236275dd9 100644 --- a/libraries/relation.lib.php +++ b/libraries/relation.lib.php @@ -138,6 +138,10 @@ function PMA_printRelationsParamDiagnostic($cfgRelation) PMA_printDiagMessageForFeature(__('Designer'), 'designerwork', $messages); + PMA_printDiagMessageForParameter('recent', isset($cfgRelation['recent']), $messages, 'recent'); + + PMA_printDiagMessageForFeature(__('Recently used tables'), 'recentwork', $messages); + PMA_printDiagMessageForParameter('tracking', isset($cfgRelation['tracking']), $messages, 'tracking'); PMA_printDiagMessageForFeature(__('Tracking'), 'trackingwork', $messages); @@ -220,6 +224,7 @@ function PMA__getRelationsParam() $cfgRelation['commwork'] = false; $cfgRelation['mimework'] = false; $cfgRelation['historywork'] = false; + $cfgRelation['recentwork'] = false; $cfgRelation['trackingwork'] = false; $cfgRelation['designerwork'] = false; $cfgRelation['userconfigwork'] = false; @@ -271,6 +276,8 @@ function PMA__getRelationsParam() $cfgRelation['pdf_pages'] = $curr_table[0]; } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['history']) { $cfgRelation['history'] = $curr_table[0]; + } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['recent']) { + $cfgRelation['recent'] = $curr_table[0]; } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['tracking']) { $cfgRelation['tracking'] = $curr_table[0]; } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['userconfig']) { @@ -325,6 +332,10 @@ function PMA__getRelationsParam() $cfgRelation['historywork'] = true; } + if (isset($cfgRelation['recent'])) { + $cfgRelation['recentwork'] = true; + } + if (isset($cfgRelation['tracking'])) { $cfgRelation['trackingwork'] = true; } @@ -346,8 +357,9 @@ function PMA__getRelationsParam() if ($cfgRelation['relwork'] && $cfgRelation['displaywork'] && $cfgRelation['pdfwork'] && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfgRelation['historywork'] - && $cfgRelation['trackingwork'] && $cfgRelation['userconfigwork'] - && $cfgRelation['bookmarkwork'] && $cfgRelation['designerwork']) { + && $cfgRelation['recentwork'] && $cfgRelation['trackingwork'] + && $cfgRelation['userconfigwork'] && $cfgRelation['bookmarkwork'] + && $cfgRelation['designerwork']) { $cfgRelation['allworks'] = true; } diff --git a/navigation.php b/navigation.php index 86f360a5af..2bff387795 100644 --- a/navigation.php +++ b/navigation.php @@ -180,27 +180,13 @@ require_once './libraries/header_http.inc.php'; -
- -
-tables)) { + echo '
'; + echo RecentTable::getInstance()->getHtmlSelect(); + echo '
'; } if (! $GLOBALS['server']) {