reworked bookmarking

This commit is contained in:
Sebastian Mendel
2008-05-09 12:37:30 +00:00
parent 58b3661876
commit 32c61a2ff6
4 changed files with 99 additions and 96 deletions

View File

@@ -3,6 +3,7 @@
/**
* Core script for import, this is just the glue around all other stuff
*
* @uses PMA_Bookmark_getList()
* @version $Id$
*/
@@ -137,7 +138,7 @@ if (!empty($id_bookmark)) {
require_once './libraries/bookmark.lib.php';
switch ($action_bookmark) {
case 0: // bookmarked query that have to be run
$import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark, 'id', isset($action_bookmark_all));
$import_text = PMA_Bookmark_get($db, $id_bookmark, 'id', isset($action_bookmark_all));
if (isset($bookmark_variable) && !empty($bookmark_variable)) {
$import_text = preg_replace('|/\*(.*)\[VARIABLE\](.*)\*/|imsU', '${1}' . PMA_sqlAddslashes($bookmark_variable) . '${2}', $import_text);
}
@@ -149,12 +150,12 @@ if (!empty($id_bookmark)) {
break;
case 1: // bookmarked query that have to be displayed
$import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark);
$import_text = PMA_Bookmark_get($db, $id_bookmark);
$run_query = FALSE;
break;
case 2: // bookmarked query that have to be deleted
$import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark);
PMA_deleteBookmarks($db, $cfg['Bookmark'], $id_bookmark);
$import_text = PMA_Bookmark_get($db, $id_bookmark);
PMA_Bookmark_delete($db, $id_bookmark);
$run_query = FALSE;
$error = TRUE; // this is kind of hack to skip processing the query
break;
@@ -179,15 +180,15 @@ if (!empty($bkm_label) && !empty($import_text)) {
// Should we replace bookmark?
if (isset($bkm_replace)) {
$bookmarks = PMA_listBookmarks($db, $cfg['Bookmark']);
$bookmarks = PMA_Bookmark_getList($db);
foreach ($bookmarks as $key => $val) {
if ($val == $bkm_label) {
PMA_deleteBookmarks($db, $cfg['Bookmark'], $key);
PMA_Bookmark_delete($db, $key);
}
}
}
PMA_addBookmarks($bfields, $cfg['Bookmark'], isset($bkm_all_users));
PMA_Bookmark_save($bfields, isset($bkm_all_users));
$bookmark_created = TRUE;
} // end store bookmarks

View File

@@ -6,53 +6,70 @@
* @version $Id$
*/
/**
*
*/
require_once './libraries/relation.lib.php';
/**
* Defines the bookmark parameters for the current user
*
* @uses $GLOBALS['server']
* @uses PMA_getRelationsParam()
* @uses $GLOBALS['cfg']['Server']['user']
* @uses $GLOBALS['cfg']['Server']['pmadb']
* @uses $GLOBALS['cfg']['Server']['bookmarktable']
* @return array the bookmark parameters for the current user
*
* @global integer the id of the current server
*
* @access public
*/
function PMA_getBookmarksParam()
function PMA_Bookmark_getParams()
{
global $server;
$cfgBookmark = '';
// No server selected -> no bookmark table
if ($server == 0) {
return '';
static $cfgBookmark = null;
if (null !== $cfgBookmark) {
return $cfgBookmark;
}
$cfgRelation = PMA_getRelationsParam();
$cfgBookmark['user'] = $GLOBALS['cfg']['Server']['user'];
$cfgBookmark['db'] = $GLOBALS['cfg']['Server']['pmadb'];
$cfgBookmark['table'] = $GLOBALS['cfg']['Server']['bookmarktable'];
if ($cfgRelation['bookmarkwork']) {
$cfgBookmark = array(
'user' => $GLOBALS['cfg']['Server']['user'],
'db' => $GLOBALS['cfg']['Server']['pmadb'],
'table' => $GLOBALS['cfg']['Server']['bookmarktable'],
);
} else {
$cfgBookmark = false;
}
return $cfgBookmark;
} // end of the 'PMA_getBookmarksParam()' function
} // end of the 'PMA_Bookmark_getParams()' function
/**
* Gets the list of bookmarks defined for the current database
*
* @uses PMA_backquote()
* @uses PMA_sqlAddslashes()
* @uses PMA_DBI_fetch_result()
* @uses PMA_DBI_QUERY_STORE
* @uses PMA_Bookmark_getParams()
* @global resource the controluser db connection handle
*
* @param string the current database name
* @param array the bookmark parameters for the current user
*
* @return mixed the bookmarks list if defined, false else
* @return array the bookmarks list
*
* @access public
*/
function PMA_listBookmarks($db, $cfgBookmark)
function PMA_Bookmark_getList($db)
{
global $controllink;
$cfgBookmark = PMA_Bookmark_getParams();
if (empty($cfgBookmark['db']) || empty($cfgBookmark['table'])) {
return '';
if (empty($cfgBookmark)) {
return array();
}
$query = 'SELECT label, id FROM '. PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
@@ -60,30 +77,20 @@ function PMA_listBookmarks($db, $cfgBookmark)
. ' AND (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
. ' OR user = \'\')'
. ' ORDER BY label';
$result = PMA_DBI_query($query, $controllink, PMA_DBI_QUERY_STORE);
// There are some bookmarks -> store them
// use the unique id as the key
if ($result && PMA_DBI_num_rows($result) > 0) {
while ($row = PMA_DBI_fetch_row($result)) {
$bookmark_list[$row[1]] = $row[0];
} // end while
return $bookmark_list;
}
// No bookmarks for the current database
else {
return FALSE;
}
} // end of the 'PMA_listBookmarks()' function
return PMA_DBI_fetch_result($query, 'label', 'id', $controllink, PMA_DBI_QUERY_STORE);
} // end of the 'PMA_Bookmark_getList()' function
/**
* Gets the sql command from a bookmark
*
* @uses PMA_backquote()
* @uses PMA_sqlAddslashes()
* @uses PMA_DBI_fetch_value()
* @uses PMA_Bookmark_getParams()
* @global resource the controluser db connection handle
*
* @param string the current database name
* @param array the bookmark parameters for the current user
* @param mixed the id of the bookmark to get
* @param string which field to look up the $id
* @param boolean TRUE: get all bookmarks regardless of the owning user
@@ -92,89 +99,91 @@ function PMA_listBookmarks($db, $cfgBookmark)
*
* @access public
*/
function PMA_queryBookmarks($db, $cfgBookmark, $id, $id_field = 'id', $action_bookmark_all = FALSE)
function PMA_Bookmark_get($db, $id, $id_field = 'id', $action_bookmark_all = FALSE)
{
global $controllink;
$cfgBookmark = PMA_Bookmark_getParams();
if (empty($cfgBookmark['db']) || empty($cfgBookmark['table'])) {
if (empty($cfgBookmark)) {
return '';
}
$query = 'SELECT query FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
. ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\''
. ($action_bookmark_all? '' : ' AND (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
. ' OR user = \'\')')
. ' AND ' . PMA_backquote($id_field) . ' = ' . $id;
$result = PMA_DBI_try_query($query, $controllink);
if (!$result) {
return FALSE;
}
list($bookmark_query) = PMA_DBI_fetch_row($result) or array(FALSE);
return $bookmark_query;
} // end of the 'PMA_queryBookmarks()' function
$query = 'SELECT query FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
. ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\''
. ($action_bookmark_all? '' : ' AND (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
. ' OR user = \'\')')
. ' AND ' . PMA_backquote($id_field) . ' = ' . $id;
return PMA_DBI_fetch_value($query, 0, 0, $controllink);
} // end of the 'PMA_Bookmark_get()' function
/**
* Adds a bookmark
*
* @uses PMA_backquote()
* @uses PMA_sqlAddslashes()
* @uses PMA_DBI_query()
* @uses PMA_Bookmark_getParams()
* @global resource the controluser db connection handle
*
* @param array the properties of the bookmark to add
* @param array the bookmark parameters for the current user
* @param boolean whether to make the bookmark available for all users
*
* @return boolean whether the INSERT succeeds or not
*
* @access public
*/
function PMA_addBookmarks($fields, $cfgBookmark, $all_users = false)
function PMA_Bookmark_save($fields, $all_users = false)
{
global $controllink;
$cfgBookmark = PMA_Bookmark_getParams();
if (empty($cfgBookmark)) {
return false;
}
$query = 'INSERT INTO ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
. ' (id, dbase, user, query, label) VALUES (NULL, \'' . PMA_sqlAddslashes($fields['dbase']) . '\', \'' . ($all_users ? '' : PMA_sqlAddslashes($fields['user'])) . '\', \'' . PMA_sqlAddslashes($fields['query']) . '\', \'' . PMA_sqlAddslashes($fields['label']) . '\')';
$result = PMA_DBI_query($query, $controllink);
return TRUE;
} // end of the 'PMA_addBookmarks()' function
return PMA_DBI_query($query, $controllink);
} // end of the 'PMA_Bookmark_save()' function
/**
* Deletes a bookmark
*
* @uses PMA_backquote()
* @uses PMA_sqlAddslashes()
* @uses PMA_DBI_try_query()
* @uses PMA_Bookmark_getParams()
* @global resource the controluser db connection handle
*
* @param string the current database name
* @param array the bookmark parameters for the current user
* @param integer the id of the bookmark to get
*
* @access public
*/
function PMA_deleteBookmarks($db, $cfgBookmark, $id)
function PMA_Bookmark_delete($db, $id)
{
global $controllink;
$cfgBookmark = PMA_Bookmark_getParams();
if (empty($cfgBookmark)) {
return false;
}
$query = 'DELETE FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
. ' WHERE (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
. ' OR user = \'\')'
. ' AND id = ' . $id;
$result = PMA_DBI_try_query($query, $controllink);
} // end of the 'PMA_deleteBookmarks()' function
return PMA_DBI_try_query($query, $controllink);
} // end of the 'PMA_Bookmark_delete()' function
/**
* Bookmark Support
*/
if (! isset($GLOBALS['cfgRelation'])) {
require_once './libraries/relation.lib.php';
$GLOBALS['cfgRelation'] = PMA_getRelationsParam();
}
if ($GLOBALS['cfgRelation']['bookmarkwork']) {
$cfg['Bookmark'] = PMA_getBookmarksParam();
} else {
$cfg['Bookmark'] = array();
}
$GLOBALS['cfg']['Bookmark'] = PMA_Bookmark_getParams();
?>

View File

@@ -37,8 +37,7 @@ require_once './libraries/bookmark.lib.php'; // used for file listing
* @uses $GLOBALS['cfg']['DefaultTabDatabase']
* @uses $GLOBALS['cfg']['DefaultQueryDatabase']
* @uses $GLOBALS['cfg']['DefaultQueryTable']
* @uses $GLOBALS['cfg']['Bookmark']['db']
* @uses $GLOBALS['cfg']['Bookmark']['table']
* @uses $GLOBALS['cfg']['Bookmark']
* @uses $GLOBALS['strSuccess']
* @uses PMA_generate_common_url()
* @uses PMA_backquote()
@@ -151,9 +150,7 @@ function PMA_sqlQueryForm($query = true, $display_tab = false, $delimiter = ';')
// Bookmark Support
if ($display_tab === 'full' || $display_tab === 'history') {
if (! empty($GLOBALS['cfg']['Bookmark'])
&& $GLOBALS['cfg']['Bookmark']['db']
&& $GLOBALS['cfg']['Bookmark']['table']) {
if (! empty($GLOBALS['cfg']['Bookmark'])) {
PMA_sqlQueryFormBookmark();
}
}
@@ -330,9 +327,7 @@ function PMA_sqlQueryFormInsert($query = '', $is_querywindow = false, $delimiter
echo '<div class="clearfloat"></div>' . "\n";
echo '</div>' . "\n";
if (! empty($GLOBALS['cfg']['Bookmark'])
&& $GLOBALS['cfg']['Bookmark']['db']
&& $GLOBALS['cfg']['Bookmark']['table']) {
if (! empty($GLOBALS['cfg']['Bookmark'])) {
?>
<div id="bookmarkoptions">
<div class="formelement">
@@ -395,10 +390,9 @@ function PMA_sqlQueryFormInsert($query = '', $is_querywindow = false, $delimiter
* prints bookmark fieldset
*
* @usedby PMA_sqlQueryForm()
* @uses PMA_listBookmarks()
* @uses PMA_Bookmark_getList()
* @uses $GLOBALS['db']
* @uses $GLOBALS['pmaThemeImage']
* @uses $GLOBALS['cfg']['Bookmark']
* @uses $GLOBALS['cfg']['ReplaceHelpImg']
* @uses $GLOBALS['strBookmarkQuery']
* @uses $GLOBALS['strBookmarkView']
@@ -412,7 +406,7 @@ function PMA_sqlQueryFormInsert($query = '', $is_querywindow = false, $delimiter
*/
function PMA_sqlQueryFormBookmark()
{
$bookmark_list = PMA_listBookmarks($GLOBALS['db'], $GLOBALS['cfg']['Bookmark']);
$bookmark_list = PMA_Bookmark_getList($GLOBALS['db']);
if (! $bookmark_list || count($bookmark_list) < 1) {
return;
}

View File

@@ -51,8 +51,7 @@ if (isset($fields['dbase'])) {
// (needed for browsing from DefaultTabTable)
if (empty($sql_query) && strlen($table) && strlen($db)) {
require_once './libraries/bookmark.lib.php';
$book_sql_query = PMA_queryBookmarks($db,
$GLOBALS['cfg']['Bookmark'], '\'' . PMA_sqlAddslashes($table) . '\'',
$book_sql_query = PMA_Bookmark_get($db, '\'' . PMA_sqlAddslashes($table) . '\'',
'label');
if (! empty($book_sql_query)) {
@@ -104,7 +103,7 @@ if (isset($find_real_end) && $find_real_end) {
* Bookmark add
*/
if (isset($store_bkm)) {
PMA_addBookmarks($fields, $cfg['Bookmark'], (isset($bkm_all_users) && $bkm_all_users == 'true' ? true : false));
PMA_Bookmark_save($fields, (isset($bkm_all_users) && $bkm_all_users == 'true' ? true : false));
PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . $goto);
} // end if
@@ -634,7 +633,7 @@ else {
// Bookmark support if required
if ($disp_mode[7] == '1'
&& (isset($cfg['Bookmark']) && ! empty($cfg['Bookmark']['db']) && ! empty($cfg['Bookmark']['table']) && empty($id_bookmark))
&& (! empty($cfg['Bookmark']) && empty($id_bookmark))
&& !empty($sql_query)) {
echo "\n";