1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Recent and Favorite table list handling
*
* @package PhpMyAdmin
*/
if (! defined('PHPMYADMIN')) {
exit;
}
require_once './libraries/Message.class.php';
/**
* Handles the recently used and favorite tables.
*
* @TODO Change the release version in table pma_recent
* (#recent in documentation)
*
* @package PhpMyAdmin
*/
class PMA_RecentFavoriteTable
{
/**
* Defines the internal PMA table which contains recent/favorite tables.
*
* @access private
* @var string
*/
private $_pmaTable;
/**
* Reference to session variable containing recently used or favorite tables.
*
* @access private
* @var array
*/
private $_tables;
/**
* Defines type of action, Favorite or Recent table.
*
* @access private
* @var string
*/
private $_tableType;
/**
* PMA_RecentFavoriteTable instances.
*
* @access private
* @var array
*/
private static $_instances = array();
/**
* Creates a new instance of PMA_RecentFavoriteTable
*
* @access private
* @param string $type the table type
*/
private function __construct($type)
{
$this->_tableType = $type;
if (strlen($GLOBALS['cfg']['Server']['pmadb'])
&& strlen($GLOBALS['cfg']['Server'][$this->_tableType])
) {
$this->_pmaTable
= PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb']) . "."
. PMA_Util::backquote($GLOBALS['cfg']['Server'][$this->_tableType]);
}
$server_id = $GLOBALS['server'];
if (! isset($_SESSION['tmpval'][$this->_tableType . '_tables'][$server_id])) {
$_SESSION['tmpval'][$this->_tableType . '_tables'][$server_id]
= isset($this->_pmaTable) ? $this->getFromDb() : array();
}
$this->_tables =& $_SESSION['tmpval'][$this->_tableType . '_tables'][$server_id];
}
/**
* Returns class instance.
*
* @param string $type the table type
*
* @return PMA_RecentFavoriteTable
*/
public static function getInstance($type)
{
if (! array_key_exists($type, self::$_instances)) {
self::$_instances[$type] = new PMA_RecentFavoriteTable($type);
}
return self::$_instances[$type];
}
/**
* Returns the recent/favorite tables array
*
* @return array
*/
public function getTables()
{
return $this->_tables;
}
/**
* Returns recently used tables or favorite from phpMyAdmin database.
*
* @return array
*/
public function getFromDb()
{
// Read from phpMyAdmin database, if recent tables is not in session
$sql_query
= " SELECT `tables` FROM " . $this->_pmaTable .
" WHERE `username` = '" . $GLOBALS['cfg']['Server']['user'] . "'";
$return = array();
$result = PMA_queryAsControlUser($sql_query, false);
if ($result) {
$row = $GLOBALS['dbi']->fetchArray($result);
if (isset($row[0])) {
$return = json_decode($row[0], true);
}
}
return $return;
}
/**
* Save recent/favorite tables into phpMyAdmin database.
*
* @return true|PMA_Message
*/
public function saveToDb()
{
$username = $GLOBALS['cfg']['Server']['user'];
$sql_query
= " REPLACE INTO " . $this->_pmaTable . " (`username`, `tables`)" .
" VALUES ('" . $username . "', '"
. PMA_Util::sqlAddSlashes(
json_encode($this->_tables)
) . "')";
$success = $GLOBALS['dbi']->tryQuery($sql_query, $GLOBALS['controllink']);
if (! $success) {
$error_msg = '';
switch ($this->_tableType) {
case 'recent':
$error_msg = __('Could not save recent table!');
break;
case 'favorite':
$error_msg = __('Could not save favorite table!');
break;
}
$message = PMA_Message::error($error_msg);
$message->addMessage('<br /><br />');
$message->addMessage(
PMA_Message::rawError(
$GLOBALS['dbi']->getError($GLOBALS['controllink'])
)
);
return $message;
}
return true;
}
/**
* Trim recent.favorite table according to the
* NumRecentTables/NumFavoriteTables configuration.
*
* @return boolean True if trimming occurred
*/
public function trim()
{
$max = max(
$GLOBALS['cfg']['Num' . ucfirst($this->_tableType) . 'Tables'], 0
);
$trimming_occurred = count($this->_tables) > $max;
while (count($this->_tables) > $max) {
array_pop($this->_tables);
}
return $trimming_occurred;
}
/**
* Return HTML ul.
*
* @return string
*/
public function getHtmlList()
{
// Remove Recent/Favorite tables that don't exist.
foreach ($this->_tables as $tbl) {
if (! $GLOBALS['dbi']->getColumns($tbl['db'], $tbl['table'])) {
$this->remove($tbl['db'], $tbl['table']);
}
}
$html = '';
if (count($this->_tables)) {
if ($this->_tableType == 'recent') {
foreach ($this->_tables as $table) {
$html .= '<li class="warp_link">';
$recent_params = array(
'db' => $table['db'],
'table' => $table['table']
);
$recent_url = 'sql.php'
. PMA_URL_getCommon($recent_params);
$html .= '<a href="' . $recent_url . '">`'
. htmlspecialchars($table['db']) . '`.`'
. htmlspecialchars($table['table']) . '`</a>';
$html .= '</li>';
}
} else {
foreach ($this->_tables as $table) {
$html .= '<li class="warp_link">';
$html .= '<a class="ajax favorite_table_anchor" ';
$fav_params = array(
'db' => $table['db'],
'ajax_request' => true,
'favorite_table' => $table['table'],
'remove_favorite' => true
);
$fav_rm_url = 'db_structure.php'
. PMA_URL_getCommon($fav_params);
$html .= 'href="' . $fav_rm_url
. '" title="' . __("Remove from Favorites")
. '" data-favtargetn="'
. md5($table['db'] . "." . $table['table'])
. '" >'
. PMA_Util::getIcon('b_favorite.png')
. '</a>';
$fav_params = array(
'db' => $table['db'],
'table' => $table['table']
);
$table_url = 'sql.php'
. PMA_URL_getCommon($fav_params);
$html .= '<a href="' . $table_url . '">`'
. htmlspecialchars($table['db']) . '`.`'
. htmlspecialchars($table['table']) . '`</a>';
$html .= '</li>';
}
}
} else {
$html .= '<li class="warp_link">'
. ($this->_tableType == 'recent'
?__('There are no recent tables.')
:__('There are no favorite tables.'))
. '</li>';
}
return $html;
}
/**
* Return HTML.
*
* @return string
*/
public function getHtml()
{
$html = '<div class="drop_list">';
if ($this->_tableType == 'recent') {
$html .= '<span title="' . __('Recent tables')
. '" class="drop_button">'
. __('Recent') . '</span><ul id="pma_recent_list">';
} else {
$html .= '<span title="' . __('Favorite tables')
. '" class="drop_button">'
. __('Favorites') . '</span><ul id="pma_favorite_list">';
}
$html .= $this->getHtmlList();
$html .= '</ul></div>';
return $html;
}
/**
* Add recently used or favorite 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)
{
// If table doesnot exist, do not add.
if (! $GLOBALS['dbi']->getColumns($db, $table)) {
return true;
}
$table_arr = array();
$table_arr['db'] = $db;
$table_arr['table'] = $table;
// add only if this is new table
if (! isset($this->_tables[0]) || $this->_tables[0] != $table_arr) {
array_unshift($this->_tables, $table_arr);
$this->_tables = array_merge(array_unique($this->_tables, SORT_REGULAR));
$this->trim();
if (isset($this->_pmaTable)) {
return $this->saveToDb();
}
}
return true;
}
/**
* Remove favorite 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 remove($db, $table)
{
$table_arr = array();
$table_arr['db'] = $db;
$table_arr['table'] = $table;
foreach ($this->_tables as $key => $value) {
if ($value['db'] == $db && $value['table'] == $table) {
unset($this->_tables[$key]);
}
}
if (isset($this->_pmaTable)) {
return $this->saveToDb();
}
return true;
}
/**
* Generate Html for sync Favorite tables anchor. (from localStorage to pmadb)
*
* @return string
*/
public function getHtmlSyncFavoriteTables()
{
$retval = '';
$server_id = $GLOBALS['server'];
// Not to show this once list is synchronized.
$is_synced = isset($_SESSION['tmpval']['favorites_synced'][$server_id]) ?
true : false;
if (!$is_synced) {
$params = array('ajax_request' => true, 'favorite_table' => true,
'sync_favorite_tables' => true);
$url = 'db_structure.php' . PMA_URL_getCommon($params);
$retval = '<a class="hide" id="sync_favorite_tables"';
$retval .= ' href="' . $url . '"></a>';
}
return $retval;
}
}
?>
|