2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Recent table list handling
9 if (! defined('PHPMYADMIN')) {
13 require_once './libraries/Message.class.php';
16 * Handles the recently used tables.
18 * @TODO Change the release version in table pma_recent
19 * (#recent in documentation)
26 * Defines the internal PMA table which contains recent tables.
34 * Reference to session variable containing recently used tables.
42 * PMA_RecentTable instance.
44 * @var PMA_RecentTable
46 private static $_instance;
49 * Creates a new instance of PMA_RecentTable
51 * @return New PMA_RecentTable
53 public function __construct()
55 if (strlen($GLOBALS['cfg']['Server']['pmadb'])
56 && strlen($GLOBALS['cfg']['Server']['recent'])
59 = PMA_Util
::backquote($GLOBALS['cfg']['Server']['pmadb']) . "."
60 . PMA_Util
::backquote($GLOBALS['cfg']['Server']['recent']);
62 $server_id = $GLOBALS['server'];
63 if (! isset($_SESSION['tmpval']['recent_tables'][$server_id])) {
64 $_SESSION['tmpval']['recent_tables'][$server_id]
65 = isset($this->_pmaTable
) ?
$this->getFromDb() : array();
67 $this->tables
=& $_SESSION['tmpval']['recent_tables'][$server_id];
71 * Returns class instance.
73 * @return PMA_RecentTable
75 public static function getInstance()
77 if (is_null(self
::$_instance)) {
78 self
::$_instance = new PMA_RecentTable();
80 return self
::$_instance;
84 * Returns recently used tables from phpMyAdmin database.
88 public function getFromDb()
90 // Read from phpMyAdmin database, if recent tables is not in session
92 = " SELECT `tables` FROM " . $this->_pmaTable
.
93 " WHERE `username` = '" . $GLOBALS['cfg']['Server']['user'] . "'";
96 $result = PMA_queryAsControlUser($sql_query, false);
98 $row = $GLOBALS['dbi']->fetchArray($result);
100 $return = json_decode($row[0], true);
107 * Save recent tables into phpMyAdmin database.
109 * @return true|PMA_Message
111 public function saveToDb()
113 $username = $GLOBALS['cfg']['Server']['user'];
115 = " REPLACE INTO " . $this->_pmaTable
. " (`username`, `tables`)" .
116 " VALUES ('" . $username . "', '"
117 . PMA_Util
::sqlAddSlashes(
118 json_encode($this->tables
)
121 $success = $GLOBALS['dbi']->tryQuery($sql_query, $GLOBALS['controllink']);
124 $message = PMA_Message
::error(__('Could not save recent table'));
125 $message->addMessage('<br /><br />');
126 $message->addMessage(
127 PMA_Message
::rawError(
128 $GLOBALS['dbi']->getError($GLOBALS['controllink'])
137 * Trim recent table according to the NumRecentTables configuration.
139 * @return boolean True if trimming occurred
141 public function trim()
143 $max = max($GLOBALS['cfg']['NumRecentTables'], 0);
144 $trimming_occurred = count($this->tables
) > $max;
145 while (count($this->tables
) > $max) {
146 array_pop($this->tables
);
148 return $trimming_occurred;
152 * Return options for HTML select.
156 public function getHtmlSelectOption()
158 // trim and save, in case where the configuration is changed
159 if ($this->trim() && isset($this->_pmaTable
)) {
163 $html = '<option value="">(' . __('Recent tables') . ') ...</option>';
164 if (count($this->tables
)) {
165 foreach ($this->tables
as $table) {
166 $html .= '<option value="'
167 . htmlspecialchars(json_encode($table)) . '">'
169 '`' . $table['db'] . '`.`' . $table['table'] . '`'
174 $html .= '<option value="">'
175 . __('There are no recent tables')
182 * Return HTML select.
186 public function getHtmlSelect()
188 $html = '<select name="selected_recent_table" id="recentTable">';
189 $html .= $this->getHtmlSelectOption();
190 $html .= '</select>';
196 * Add recently used tables.
198 * @param string $db database name where the table is located
199 * @param string $table table name
201 * @return true|PMA_Message True if success, PMA_Message if not
203 public function add($db, $table)
205 $table_arr = array();
206 $table_arr['db'] = $db;
207 $table_arr['table'] = $table;
209 // add only if this is new table
210 if (! isset($this->tables
[0]) ||
$this->tables
[0] != $table_arr) {
211 array_unshift($this->tables
, $table_arr);
212 $this->tables
= array_merge(array_unique($this->tables
, SORT_REGULAR
));
214 if (isset($this->_pmaTable
)) {
215 return $this->saveToDb();