3.3.6-rc1
[phpmyadmin/dkf.git] / libraries / db_info.inc.php
blob1e5b401ad4c970b638695dce3f68c56813223bf5
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Gets the list of the table in the current db and informations about these
5 * tables if possible
7 * fills tooltip arrays and provides $tables, $num_tables, $is_show_stats
8 * and $db_is_information_schema
10 * staybyte: speedup view on locked tables - 11 June 2001
12 * @uses $cfg['ShowStats']
13 * @uses $cfg['ShowTooltip']
14 * @uses $cfg['ShowTooltipAliasTB']
15 * @uses $cfg['SkipLockedTables']
16 * @uses $GLOBALS['db']
17 * @uses PMA_fillTooltip()
18 * @uses PMA_checkParameters()
19 * @uses PMA_escape_mysql_wildcards()
20 * @uses PMA_DBI_query()
21 * @uses PMA_backquote()
22 * @uses PMA_DBI_num_rows()
23 * @uses PMA_DBI_fetch_row()
24 * @uses PMA_DBI_fetch_assoc()
25 * @uses PMA_DBI_free_result()
26 * @uses PMA_DBI_get_tables_full()
27 * @uses PMA_isValid()
28 * @uses preg_match()
29 * @uses preg_quote()
30 * @uses uksort()
31 * @uses strnatcasecmp()
32 * @uses count()
33 * @uses addslashes()
34 * @version $Id$
35 * @package phpMyAdmin
37 if (! defined('PHPMYADMIN')) {
38 exit;
41 /**
42 * requirements
44 require_once './libraries/common.inc.php';
46 /**
47 * limits for table list
49 if (! isset($_SESSION['tmp_user_values']['table_limit_offset']) || $_SESSION['tmp_user_values']['table_limit_offset_db'] != $db) {
50 $_SESSION['tmp_user_values']['table_limit_offset'] = 0;
51 $_SESSION['tmp_user_values']['table_limit_offset_db'] = $db;
53 if (isset($_REQUEST['pos'])) {
54 $_SESSION['tmp_user_values']['table_limit_offset'] = (int) $_REQUEST['pos'];
56 $pos = $_SESSION['tmp_user_values']['table_limit_offset'];
58 /**
59 * fills given tooltip arrays
61 * @uses $cfg['ShowTooltipAliasTB']
62 * @uses $GLOBALS['strStatCreateTime']
63 * @uses PMA_localisedDate()
64 * @uses strtotime()
65 * @param array $tooltip_truename tooltip data
66 * @param array $tooltip_aliasname tooltip data
67 * @param array $table tabledata
69 function PMA_fillTooltip(&$tooltip_truename, &$tooltip_aliasname, $table)
71 if (empty($table['Comment'])) {
72 $table['Comment'] = $table['Name'];
73 } else {
74 // why?
75 $table['Comment'] .= ' ';
78 if ($GLOBALS['cfg']['ShowTooltipAliasTB']
79 && $GLOBALS['cfg']['ShowTooltipAliasTB'] != 'nested') {
80 $tooltip_truename[$table['Name']] = $table['Comment'];
81 $tooltip_aliasname[$table['Name']] = $table['Name'];
82 } else {
83 $tooltip_truename[$table['Name']] = $table['Name'];
84 $tooltip_aliasname[$table['Name']] = $table['Comment'];
87 if (isset($table['Create_time']) && !empty($table['Create_time'])) {
88 $tooltip_aliasname[$table['Name']] .= ', ' . $GLOBALS['strStatCreateTime']
89 . ': ' . PMA_localisedDate(strtotime($table['Create_time']));
92 if (! empty($table['Update_time'])) {
93 $tooltip_aliasname[$table['Name']] .= ', ' . $GLOBALS['strStatUpdateTime']
94 . ': ' . PMA_localisedDate(strtotime($table['Update_time']));
97 if (! empty($table['Check_time'])) {
98 $tooltip_aliasname[$table['Name']] .= ', ' . $GLOBALS['strStatCheckTime']
99 . ': ' . PMA_localisedDate(strtotime($table['Check_time']));
103 PMA_checkParameters(array('db'));
106 * @global bool whether to display extended stats
108 $is_show_stats = $cfg['ShowStats'];
111 * @global bool whether selected db is information_schema
113 $db_is_information_schema = false;
115 if ($db == 'information_schema') {
116 $is_show_stats = false;
117 $db_is_information_schema = true;
121 * @global array information about tables in db
123 $tables = array();
125 // When used in Nested table group mode, only show tables matching the given groupname
126 if (PMA_isValid($tbl_group) && !$cfg['ShowTooltipAliasTB']) {
127 $tbl_group_sql = ' LIKE "' . PMA_escape_mysql_wildcards($tbl_group) . '%"';
128 } else {
129 $tbl_group_sql = '';
132 if ($cfg['ShowTooltip']) {
133 $tooltip_truename = array();
134 $tooltip_aliasname = array();
137 // Special speedup for newer MySQL Versions (in 4.0 format changed)
138 if (true === $cfg['SkipLockedTables']) {
139 $db_info_result = PMA_DBI_query('SHOW OPEN TABLES FROM ' . PMA_backquote($db) . ';');
141 // Blending out tables in use
142 if ($db_info_result && PMA_DBI_num_rows($db_info_result) > 0) {
143 while ($tmp = PMA_DBI_fetch_row($db_info_result)) {
144 // if in use memorize tablename
145 if (preg_match('@in_use=[1-9]+@i', $tmp[1])) {
146 $sot_cache[$tmp[0]] = true;
149 PMA_DBI_free_result($db_info_result);
151 if (isset($sot_cache)) {
152 $db_info_result = PMA_DBI_query(
153 'SHOW TABLES FROM ' . PMA_backquote($db) . $tbl_group_sql . ';',
154 null, PMA_DBI_QUERY_STORE);
155 if ($db_info_result && PMA_DBI_num_rows($db_info_result) > 0) {
156 while ($tmp = PMA_DBI_fetch_row($db_info_result)) {
157 if (!isset($sot_cache[$tmp[0]])) {
158 $sts_result = PMA_DBI_query(
159 'SHOW TABLE STATUS FROM ' . PMA_backquote($db)
160 . ' LIKE \'' . addslashes($tmp[0]) . '\';');
161 $sts_tmp = PMA_DBI_fetch_assoc($sts_result);
162 PMA_DBI_free_result($sts_result);
163 unset($sts_result);
165 if (!isset($sts_tmp['Type']) && isset($sts_tmp['Engine'])) {
166 $sts_tmp['Type'] =& $sts_tmp['Engine'];
169 if (!empty($tbl_group) && $cfg['ShowTooltipAliasTB']
170 && !preg_match('@' . preg_quote($tbl_group, '@') . '@i', $sts_tmp['Comment'])) {
171 continue;
174 if ($cfg['ShowTooltip']) {
175 PMA_fillTooltip($tooltip_truename, $tooltip_aliasname, $sts_tmp);
178 $tables[$sts_tmp['Name']] = $sts_tmp;
179 } else { // table in use
180 $tables[$tmp[0]] = array('Name' => $tmp[0]);
183 if ($GLOBALS['cfg']['NaturalOrder']) {
184 uksort($tables, 'strnatcasecmp');
187 $sot_ready = true;
188 } elseif ($db_info_result) {
189 PMA_DBI_free_result($db_info_result);
191 unset($sot_cache);
193 unset($tmp);
194 } elseif ($db_info_result) {
195 PMA_DBI_free_result($db_info_result);
199 if (! isset($sot_ready)) {
201 // Set some sorting defaults
202 $sort = 'Name';
203 $sort_order = 'ASC';
205 if (isset($_REQUEST['sort'])) {
206 $sortable_name_mappings = array(
207 'table' => 'Name',
208 'records' => 'Rows',
209 'type' => 'Engine',
210 'collation' => 'Collation',
211 'size' => 'Data_length',
212 'overhead' => 'Data_free'
215 // Make sure the sort type is implemented
216 if (isset($sortable_name_mappings[$_REQUEST['sort']])) {
217 $sort = $sortable_name_mappings[$_REQUEST['sort']];
218 if ($_REQUEST['sort_order'] == 'DESC') {
219 $sort_order = 'DESC';
224 if (! empty($tbl_group) && ! $cfg['ShowTooltipAliasTB']) {
225 // only tables for selected group
226 $tables = PMA_DBI_get_tables_full($db, $tbl_group, true, null, 0, false, $sort, $sort_order);
227 } elseif (! empty($tbl_group) && $cfg['ShowTooltipAliasTB']) {
228 // only tables for selected group,
229 // but grouping is done on comment ...
230 $tables = PMA_DBI_get_tables_full($db, $tbl_group, 'comment', null, 0, false, $sort, $sort_order);
231 } else {
232 // all tables in db
233 // - get the total number of tables
234 // (needed for proper working of the MaxTableList feature)
235 $tables = PMA_DBI_get_tables($db);
236 $total_num_tables = count($tables);
237 if (isset($sub_part) && $sub_part == '_export') {
238 // (don't fetch only a subset if we are coming from db_export.php,
239 // because I think it's too risky to display only a subset of the
240 // table names when exporting a db)
243 * @todo Page selector for table names?
245 $tables = PMA_DBI_get_tables_full($db, false, false, null, 0, false, $sort, $sort_order);
246 } else {
247 // fetch the details for a possible limited subset
248 $tables = PMA_DBI_get_tables_full($db, false, false, null, $pos, true, $sort, $sort_order);
252 if ($cfg['ShowTooltip']) {
253 foreach ($tables as $each_table) {
254 PMA_fillTooltip($tooltip_truename, $tooltip_aliasname, $each_table);
260 * @global int count of tables in db
262 $num_tables = count($tables);
263 // (needed for proper working of the MaxTableList feature)
264 if (! isset($total_num_tables)) {
265 $total_num_tables = $num_tables;
269 * cleanup
271 unset($each_table, $tbl_group_sql, $db_info_result);
274 * Displays top menu links
276 require './libraries/db_links.inc.php';