2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used with the bookmark feature
9 if (! defined('PHPMYADMIN')) {
14 * Defines the bookmark parameters for the current user
16 * @return array the bookmark parameters for the current user
19 function PMA_Bookmark_getParams()
21 static $cfgBookmark = null;
23 if (null !== $cfgBookmark) {
27 $cfgRelation = PMA_getRelationsParam();
29 if ($cfgRelation['bookmarkwork']) {
31 'user' => $GLOBALS['cfg']['Server']['user'],
32 'db' => $GLOBALS['cfg']['Server']['pmadb'],
33 'table' => $GLOBALS['cfg']['Server']['bookmarktable'],
40 } // end of the 'PMA_Bookmark_getParams()' function
44 * Gets the list of bookmarks defined for the current database
46 * @param string $db the current database name
48 * @return array the bookmarks list (key as index, label as value)
52 * @global resource the controluser db connection handle
54 function PMA_Bookmark_getList($db)
58 $cfgBookmark = PMA_Bookmark_getParams();
60 if (empty($cfgBookmark)) {
64 $query = 'SELECT label, id FROM '. PMA_Util
::backquote($cfgBookmark['db'])
65 . '.' . PMA_Util
::backquote($cfgBookmark['table'])
66 . ' WHERE dbase = \'' . PMA_Util
::sqlAddSlashes($db) . '\''
67 . ' AND user = \'' . PMA_Util
::sqlAddSlashes($cfgBookmark['user']) . '\''
69 $per_user = $GLOBALS['dbi']->fetchResult(
70 $query, 'id', 'label', $controllink, PMA_DatabaseInterface
::QUERY_STORE
73 $query = 'SELECT label, id FROM '. PMA_Util
::backquote($cfgBookmark['db'])
74 . '.' . PMA_Util
::backquote($cfgBookmark['table'])
75 . ' WHERE dbase = \'' . PMA_Util
::sqlAddSlashes($db) . '\''
78 $global = $GLOBALS['dbi']->fetchResult(
79 $query, 'id', 'label', $controllink, PMA_DatabaseInterface
::QUERY_STORE
82 foreach ($global as $key => $val) {
83 $global[$key] = $val . ' (' . __('shared') . ')';
86 $ret = $global +
$per_user;
91 } // end of the 'PMA_Bookmark_getList()' function
95 * Gets the sql command from a bookmark
97 * @param string $db the current database name
98 * @param mixed $id the id of the bookmark to get
99 * @param string $id_field which field to look up the $id
100 * @param boolean $action_bookmark_all true: get all bookmarks regardless
102 * @param boolean $exact_user_match whether to ignore bookmarks with no user
104 * @return string the sql query
108 * @global resource the controluser db connection handle
111 function PMA_Bookmark_get($db, $id, $id_field = 'id', $action_bookmark_all = false,
112 $exact_user_match = false
116 $cfgBookmark = PMA_Bookmark_getParams();
118 if (empty($cfgBookmark)) {
122 $query = 'SELECT query FROM ' . PMA_Util
::backquote($cfgBookmark['db'])
123 . '.' . PMA_Util
::backquote($cfgBookmark['table'])
124 . ' WHERE dbase = \'' . PMA_Util
::sqlAddSlashes($db) . '\'';
126 if (! $action_bookmark_all) {
127 $query .= ' AND (user = \''
128 . PMA_Util
::sqlAddSlashes($cfgBookmark['user']) . '\'';
129 if (! $exact_user_match) {
130 $query .= ' OR user = \'\'';
135 $query .= ' AND ' . PMA_Util
::backquote($id_field) . ' = ' . $id;
137 return $GLOBALS['dbi']->fetchValue($query, 0, 0, $controllink);
138 } // end of the 'PMA_Bookmark_get()' function
143 * @param array $bkm_fields the properties of the bookmark to add; here,
144 * $bkm_fields['bkm_sql_query'] is urlencoded
145 * @param boolean $all_users whether to make the bookmark available for all users
147 * @return boolean whether the INSERT succeeds or not
151 * @global resource the controluser db connection handle
153 function PMA_Bookmark_save($bkm_fields, $all_users = false)
157 $cfgBookmark = PMA_Bookmark_getParams();
159 if (empty($cfgBookmark)) {
163 $query = 'INSERT INTO ' . PMA_Util
::backquote($cfgBookmark['db'])
164 . '.' . PMA_Util
::backquote($cfgBookmark['table'])
165 . ' (id, dbase, user, query, label)'
166 . ' VALUES (NULL, \''
167 . PMA_Util
::sqlAddSlashes($bkm_fields['bkm_database']) . '\', '
169 . ($all_users ?
'' : PMA_Util
::sqlAddSlashes($bkm_fields['bkm_user']))
172 . PMA_Util
::sqlAddSlashes(urldecode($bkm_fields['bkm_sql_query']))
174 . '\'' . PMA_Util
::sqlAddSlashes($bkm_fields['bkm_label']) . '\')';
175 return $GLOBALS['dbi']->query($query, $controllink);
176 } // end of the 'PMA_Bookmark_save()' function
182 * @param string $db the current database name
183 * @param integer $id the id of the bookmark to get
185 * @return bool true if successful
189 * @global resource the controluser db connection handle
191 function PMA_Bookmark_delete($db, $id)
195 $cfgBookmark = PMA_Bookmark_getParams();
197 if (empty($cfgBookmark)) {
201 $query = 'DELETE FROM ' . PMA_Util
::backquote($cfgBookmark['db'])
202 . '.' . PMA_Util
::backquote($cfgBookmark['table'])
203 . ' WHERE (user = \'' . PMA_Util
::sqlAddSlashes($cfgBookmark['user']) . '\''
205 . ' AND id = ' . $id;
206 return $GLOBALS['dbi']->tryQuery($query, $controllink);
207 } // end of the 'PMA_Bookmark_delete()' function
213 if (!defined('TESTSUITE')) {
214 $GLOBALS['cfg']['Bookmark'] = PMA_Bookmark_getParams();