/dbadmin.com/public/libraries/db_info.inc.php

https://github.com/slikk66/DbAdmin · PHP · 210 lines · 133 code · 23 blank · 54 comment · 37 complexity · da2d95fa6e2c1865c2b3abc84dbc6985 MD5 · raw file

  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
  6. *
  7. * fills tooltip arrays and provides $tables, $num_tables, $is_show_stats
  8. * and $db_is_information_schema
  9. *
  10. * speedup view on locked tables
  11. *
  12. * @package PhpMyAdmin
  13. */
  14. if (! defined('PHPMYADMIN')) {
  15. exit;
  16. }
  17. /**
  18. * limits for table list
  19. */
  20. if (! isset($_SESSION['tmp_user_values']['table_limit_offset'])
  21. || $_SESSION['tmp_user_values']['table_limit_offset_db'] != $db
  22. ) {
  23. $_SESSION['tmp_user_values']['table_limit_offset'] = 0;
  24. $_SESSION['tmp_user_values']['table_limit_offset_db'] = $db;
  25. }
  26. if (isset($_REQUEST['pos'])) {
  27. $_SESSION['tmp_user_values']['table_limit_offset'] = (int) $_REQUEST['pos'];
  28. }
  29. $pos = $_SESSION['tmp_user_values']['table_limit_offset'];
  30. PMA_Util::checkParameters(array('db'));
  31. /**
  32. * @global bool whether to display extended stats
  33. */
  34. $is_show_stats = $cfg['ShowStats'];
  35. /**
  36. * @global bool whether selected db is information_schema
  37. */
  38. $db_is_information_schema = false;
  39. if (PMA_is_system_schema($db)) {
  40. $is_show_stats = false;
  41. $db_is_information_schema = true;
  42. }
  43. /**
  44. * @global array information about tables in db
  45. */
  46. $tables = array();
  47. // When used in Nested table group mode,
  48. // only show tables matching the given groupname
  49. if (PMA_isValid($_REQUEST['tbl_group'])) {
  50. $tbl_group_sql = ' LIKE "'
  51. . PMA_Util::escapeMysqlWildcards($_REQUEST['tbl_group'])
  52. . '%"';
  53. } else {
  54. $tbl_group_sql = '';
  55. }
  56. $tooltip_truename = array();
  57. $tooltip_aliasname = array();
  58. // Special speedup for newer MySQL Versions (in 4.0 format changed)
  59. if (true === $cfg['SkipLockedTables']) {
  60. $db_info_result = PMA_DBI_query(
  61. 'SHOW OPEN TABLES FROM ' . PMA_Util::backquote($db) . ';'
  62. );
  63. // Blending out tables in use
  64. if ($db_info_result && PMA_DBI_num_rows($db_info_result) > 0) {
  65. while ($tmp = PMA_DBI_fetch_row($db_info_result)) {
  66. // if in use memorize tablename
  67. if (preg_match('@in_use=[1-9]+@i', $tmp[1])) {
  68. $sot_cache[$tmp[0]] = true;
  69. }
  70. }
  71. PMA_DBI_free_result($db_info_result);
  72. if (isset($sot_cache)) {
  73. $db_info_result = PMA_DBI_query(
  74. 'SHOW TABLES FROM ' . PMA_Util::backquote($db) . $tbl_group_sql . ';',
  75. null, PMA_DBI_QUERY_STORE
  76. );
  77. if ($db_info_result && PMA_DBI_num_rows($db_info_result) > 0) {
  78. while ($tmp = PMA_DBI_fetch_row($db_info_result)) {
  79. if (! isset($sot_cache[$tmp[0]])) {
  80. $sts_result = PMA_DBI_query(
  81. 'SHOW TABLE STATUS FROM ' . PMA_Util::backquote($db)
  82. . ' LIKE \'' . PMA_Util::sqlAddSlashes($tmp[0], true) . '\';'
  83. );
  84. $sts_tmp = PMA_DBI_fetch_assoc($sts_result);
  85. PMA_DBI_free_result($sts_result);
  86. unset($sts_result);
  87. if (! isset($sts_tmp['Type']) && isset($sts_tmp['Engine'])) {
  88. $sts_tmp['Type'] =& $sts_tmp['Engine'];
  89. }
  90. if (! empty($_REQUEST['tbl_group'])
  91. && ! preg_match('@' . preg_quote($_REQUEST['tbl_group'], '@') . '@i', $sts_tmp['Comment'])
  92. ) {
  93. continue;
  94. }
  95. $tables[$sts_tmp['Name']] = $sts_tmp;
  96. } else { // table in use
  97. $tables[$tmp[0]] = array('Name' => $tmp[0]);
  98. }
  99. }
  100. if ($GLOBALS['cfg']['NaturalOrder']) {
  101. uksort($tables, 'strnatcasecmp');
  102. }
  103. $sot_ready = true;
  104. } elseif ($db_info_result) {
  105. PMA_DBI_free_result($db_info_result);
  106. }
  107. unset($sot_cache);
  108. }
  109. unset($tmp);
  110. } elseif ($db_info_result) {
  111. PMA_DBI_free_result($db_info_result);
  112. }
  113. }
  114. if (! isset($sot_ready)) {
  115. // Set some sorting defaults
  116. $sort = 'Name';
  117. $sort_order = 'ASC';
  118. if (isset($_REQUEST['sort'])) {
  119. $sortable_name_mappings = array(
  120. 'table' => 'Name',
  121. 'records' => 'Rows',
  122. 'type' => 'Engine',
  123. 'collation' => 'Collation',
  124. 'size' => 'Data_length',
  125. 'overhead' => 'Data_free',
  126. 'creation' => 'Create_time',
  127. 'last_update' => 'Update_time',
  128. 'last_check' => 'Check_time'
  129. );
  130. // Make sure the sort type is implemented
  131. if (isset($sortable_name_mappings[$_REQUEST['sort']])) {
  132. $sort = $sortable_name_mappings[$_REQUEST['sort']];
  133. if ($_REQUEST['sort_order'] == 'DESC') {
  134. $sort_order = 'DESC';
  135. }
  136. }
  137. }
  138. if (! empty($_REQUEST['tbl_group'])) {
  139. // only tables for selected group
  140. $tables = PMA_DBI_get_tables_full(
  141. $db, $_REQUEST['tbl_group'], true, null, 0, false, $sort, $sort_order
  142. );
  143. } else {
  144. // all tables in db
  145. // - get the total number of tables
  146. // (needed for proper working of the MaxTableList feature)
  147. $tables = PMA_DBI_get_tables($db);
  148. $total_num_tables = count($tables);
  149. if (isset($sub_part) && $sub_part == '_export') {
  150. // (don't fetch only a subset if we are coming from db_export.php,
  151. // because I think it's too risky to display only a subset of the
  152. // table names when exporting a db)
  153. /**
  154. *
  155. * @todo Page selector for table names?
  156. */
  157. $tables = PMA_DBI_get_tables_full(
  158. $db, false, false, null, 0, false, $sort, $sort_order
  159. );
  160. } else {
  161. // fetch the details for a possible limited subset
  162. $tables = PMA_DBI_get_tables_full(
  163. $db, false, false, null, $pos, true, $sort, $sort_order
  164. );
  165. }
  166. }
  167. }
  168. /**
  169. * @global int count of tables in db
  170. */
  171. $num_tables = count($tables);
  172. // (needed for proper working of the MaxTableList feature)
  173. if (! isset($total_num_tables)) {
  174. $total_num_tables = $num_tables;
  175. }
  176. /**
  177. * cleanup
  178. */
  179. unset($each_table, $tbl_group_sql, $db_info_result);
  180. /**
  181. * If coming from a Show MySQL link on the home page,
  182. * put something in $sub_part
  183. */
  184. if (empty($sub_part)) {
  185. $sub_part = '_structure';
  186. }
  187. ?>