2 /* vim: set expandtab sw=4 ts=4 sts=4: */
7 if (! defined('PHPMYADMIN')) {
11 require_once './libraries/Message.class.php';
14 * Handles the recently used tables.
16 * @TODO Change the release version in table pma_recent
17 * (#recent in documentation)
24 * Defines the internal PMA table which contains recent tables.
32 * Reference to session variable containing recently used tables.
40 * PMA_RecentTable instance.
42 * @var PMA_RecentTable
44 private static $_instance;
46 public function __construct()
48 if (strlen($GLOBALS['cfg']['Server']['pmadb'])
49 && strlen($GLOBALS['cfg']['Server']['recent'])
52 = PMA_Util
::backquote($GLOBALS['cfg']['Server']['pmadb']) . "."
53 . PMA_Util
::backquote($GLOBALS['cfg']['Server']['recent']);
55 $server_id = $GLOBALS['server'];
56 if (! isset($_SESSION['tmp_user_values']['recent_tables'][$server_id])) {
57 $_SESSION['tmp_user_values']['recent_tables'][$server_id]
58 = isset($this->_pmaTable
) ?
$this->getFromDb() : array();
60 $this->tables
=& $_SESSION['tmp_user_values']['recent_tables'][$server_id];
64 * Returns class instance.
66 * @return PMA_RecentTable
68 public static function getInstance()
70 if (is_null(self
::$_instance)) {
71 self
::$_instance = new PMA_RecentTable();
73 return self
::$_instance;
77 * Returns recently used tables from phpMyAdmin database.
81 public function getFromDb()
83 // Read from phpMyAdmin database, if recent tables is not in session
85 = " SELECT `tables` FROM " . $this->_pmaTable
.
86 " WHERE `username` = '" . $GLOBALS['cfg']['Server']['user'] . "'";
88 $row = $GLOBALS['dbi']->fetchArray(PMA_queryAsControlUser($sql_query));
90 return json_decode($row[0], true);
97 * Save recent tables into phpMyAdmin database.
99 * @return true|PMA_Message
101 public function saveToDb()
103 $username = $GLOBALS['cfg']['Server']['user'];
105 = " REPLACE INTO " . $this->_pmaTable
. " (`username`, `tables`)" .
106 " VALUES ('" . $username . "', '"
107 . PMA_Util
::sqlAddSlashes(
108 json_encode($this->tables
)
111 $success = $GLOBALS['dbi']->tryQuery($sql_query, $GLOBALS['controllink']);
114 $message = PMA_Message
::error(__('Could not save recent table'));
115 $message->addMessage('<br /><br />');
116 $message->addMessage(
117 PMA_Message
::rawError($GLOBALS['dbi']->getError($GLOBALS['controllink']))
125 * Trim recent table according to the NumRecentTables configuration.
127 * @return boolean True if trimming occurred
129 public function trim()
131 $max = max($GLOBALS['cfg']['NumRecentTables'], 0);
132 $trimming_occured = count($this->tables
) > $max;
133 while (count($this->tables
) > $max) {
134 array_pop($this->tables
);
136 return $trimming_occured;
140 * Return options for HTML select.
144 public function getHtmlSelectOption()
146 // trim and save, in case where the configuration is changed
147 if ($this->trim() && isset($this->_pmaTable
)) {
151 $html = '<option value="">(' . __('Recent tables') . ') ...</option>';
152 if (count($this->tables
)) {
153 foreach ($this->tables
as $table) {
154 $html .= '<option value="'
155 . htmlspecialchars(json_encode($table)) . '">'
157 '`' . $table['db'] . '`.`' . $table['table'] . '`'
162 $html .= '<option value="">'
163 . __('There are no recent tables')
170 * Return HTML select.
174 public function getHtmlSelect()
176 $html = '<select name="selected_recent_table" id="recentTable">';
177 $html .= $this->getHtmlSelectOption();
178 $html .= '</select>';
184 * Add recently used tables.
186 * @param string $db database name where the table is located
187 * @param string $table table name
189 * @return true|PMA_Message True if success, PMA_Message if not
191 public function add($db, $table)
193 $table_arr = array();
194 $table_arr['db'] = $db;
195 $table_arr['table'] = $table;
197 // add only if this is new table
198 if (! isset($this->tables
[0]) ||
$this->tables
[0] != $table_arr) {
199 array_unshift($this->tables
, $table_arr);
200 $this->tables
= array_merge(array_unique($this->tables
, SORT_REGULAR
));
202 if (isset($this->_pmaTable
)) {
203 return $this->saveToDb();