db_info.inc.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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_system_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['tmpval']['table_limit_offset'])
  21. || $_SESSION['tmpval']['table_limit_offset_db'] != $db
  22. ) {
  23. $_SESSION['tmpval']['table_limit_offset'] = 0;
  24. $_SESSION['tmpval']['table_limit_offset_db'] = $db;
  25. }
  26. if (isset($_REQUEST['pos'])) {
  27. $_SESSION['tmpval']['table_limit_offset'] = (int) $_REQUEST['pos'];
  28. }
  29. $pos = $_SESSION['tmpval']['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_system_schema = false;
  39. if ($GLOBALS['dbi']->isSystemSchema($db)) {
  40. $is_show_stats = false;
  41. $db_is_system_schema = true;
  42. }
  43. /**
  44. * @global array information about tables in db
  45. */
  46. $tables = array();
  47. $tooltip_truename = array();
  48. $tooltip_aliasname = array();
  49. // Special speedup for newer MySQL Versions (in 4.0 format changed)
  50. if (true === $cfg['SkipLockedTables'] && ! PMA_DRIZZLE) {
  51. $db_info_result = $GLOBALS['dbi']->query(
  52. 'SHOW OPEN TABLES FROM ' . PMA_Util::backquote($db) . ';'
  53. );
  54. // Blending out tables in use
  55. if ($db_info_result && $GLOBALS['dbi']->numRows($db_info_result) > 0) {
  56. while ($tmp = $GLOBALS['dbi']->fetchAssoc($db_info_result)) {
  57. // if in use, memorize table name
  58. if ($tmp['In_use'] > 0) {
  59. $sot_cache[$tmp['Table']] = true;
  60. }
  61. }
  62. $GLOBALS['dbi']->freeResult($db_info_result);
  63. if (isset($sot_cache)) {
  64. $db_info_result = false;
  65. $tblGroupSql = "";
  66. $whereAdded = false;
  67. if (PMA_isValid($_REQUEST['tbl_group'])) {
  68. $group = PMA_Util::escapeMysqlWildcards($_REQUEST['tbl_group']);
  69. $groupWithSeperator = PMA_Util::escapeMysqlWildcards(
  70. $_REQUEST['tbl_group']
  71. . $GLOBALS['cfg']['NavigationTreeTableSeparator']
  72. );
  73. $tblGroupSql .= " WHERE ("
  74. . PMA_Util::backquote('Tables_in_' . $db)
  75. . " LIKE '" . $groupWithSeperator . "%'"
  76. . " OR "
  77. . PMA_Util::backquote('Tables_in_' . $db)
  78. . " LIKE '" . $group . "')";
  79. $whereAdded = true;
  80. }
  81. if (PMA_isValid($_REQUEST['tbl_type'], array('table', 'view'))) {
  82. $tblGroupSql .= $whereAdded ? " AND" : " WHERE";
  83. if ($_REQUEST['tbl_type'] == 'view') {
  84. $tblGroupSql .= " `Table_type` != 'BASE TABLE'";
  85. } else {
  86. $tblGroupSql .= " `Table_type` = 'BASE TABLE'";
  87. }
  88. }
  89. $db_info_result = $GLOBALS['dbi']->query(
  90. 'SHOW FULL TABLES FROM ' . PMA_Util::backquote($db) . $tblGroupSql,
  91. null, PMA_DatabaseInterface::QUERY_STORE
  92. );
  93. unset($tblGroupSql, $whereAdded);
  94. if ($db_info_result && $GLOBALS['dbi']->numRows($db_info_result) > 0) {
  95. while ($tmp = $GLOBALS['dbi']->fetchRow($db_info_result)) {
  96. if (! isset($sot_cache[$tmp[0]])) {
  97. $sts_result = $GLOBALS['dbi']->query(
  98. "SHOW TABLE STATUS FROM " . PMA_Util::backquote($db)
  99. . " LIKE '" . PMA_Util::sqlAddSlashes($tmp[0], true)
  100. . "';"
  101. );
  102. $sts_tmp = $GLOBALS['dbi']->fetchAssoc($sts_result);
  103. $GLOBALS['dbi']->freeResult($sts_result);
  104. unset($sts_result);
  105. $tableArray = $GLOBALS['dbi']->copyTableProperties(
  106. array($sts_tmp), $db
  107. );
  108. $tables[$sts_tmp['Name']] = $tableArray[0];
  109. } else { // table in use
  110. $tables[$tmp[0]] = array(
  111. 'TABLE_NAME' => $tmp[0],
  112. 'ENGINE' => '',
  113. 'TABLE_TYPE' => '',
  114. 'TABLE_ROWS' => 0,
  115. );
  116. }
  117. }
  118. if ($GLOBALS['cfg']['NaturalOrder']) {
  119. uksort($tables, 'strnatcasecmp');
  120. }
  121. $sot_ready = true;
  122. } elseif ($db_info_result) {
  123. $GLOBALS['dbi']->freeResult($db_info_result);
  124. }
  125. unset($sot_cache);
  126. }
  127. unset($tmp);
  128. } elseif ($db_info_result) {
  129. $GLOBALS['dbi']->freeResult($db_info_result);
  130. }
  131. }
  132. if (! isset($sot_ready)) {
  133. // Set some sorting defaults
  134. $sort = 'Name';
  135. $sort_order = 'ASC';
  136. if (isset($_REQUEST['sort'])) {
  137. $sortable_name_mappings = array(
  138. 'table' => 'Name',
  139. 'records' => 'Rows',
  140. 'type' => 'Engine',
  141. 'collation' => 'Collation',
  142. 'size' => 'Data_length',
  143. 'overhead' => 'Data_free',
  144. 'creation' => 'Create_time',
  145. 'last_update' => 'Update_time',
  146. 'last_check' => 'Check_time'
  147. );
  148. // Make sure the sort type is implemented
  149. if (isset($sortable_name_mappings[$_REQUEST['sort']])) {
  150. $sort = $sortable_name_mappings[$_REQUEST['sort']];
  151. if ($_REQUEST['sort_order'] == 'DESC') {
  152. $sort_order = 'DESC';
  153. }
  154. }
  155. }
  156. $tbl_group = false;
  157. $groupWithSeperator = false;
  158. $tbl_type = null;
  159. $limit_offset = 0;
  160. $limit_count = false;
  161. $groupTable = array();
  162. if (! empty($_REQUEST['tbl_group']) || ! empty($_REQUEST['tbl_type'])) {
  163. if (! empty($_REQUEST['tbl_type'])) {
  164. // only tables for selected type
  165. $tbl_type = $_REQUEST['tbl_type'];
  166. }
  167. if (! empty($_REQUEST['tbl_group'])) {
  168. // only tables for selected group
  169. $tbl_group = $_REQUEST['tbl_group'];
  170. // include the table with the exact name of the group if such exists
  171. $groupTable = $GLOBALS['dbi']->getTablesFull(
  172. $db, $tbl_group, false, null, $limit_offset,
  173. $limit_count, $sort, $sort_order, $tbl_type
  174. );
  175. $groupWithSeperator = $tbl_group
  176. . $GLOBALS['cfg']['NavigationTreeTableSeparator'];
  177. }
  178. } else {
  179. // all tables in db
  180. // - get the total number of tables
  181. // (needed for proper working of the MaxTableList feature)
  182. $tables = $GLOBALS['dbi']->getTables($db);
  183. $total_num_tables = count($tables);
  184. if (isset($sub_part) && $sub_part == '_export') {
  185. // (don't fetch only a subset if we are coming from db_export.php,
  186. // because I think it's too risky to display only a subset of the
  187. // table names when exporting a db)
  188. /**
  189. *
  190. * @todo Page selector for table names?
  191. */
  192. } else {
  193. // fetch the details for a possible limited subset
  194. $limit_offset = $pos;
  195. $limit_count = true;
  196. }
  197. }
  198. $tables = array_merge(
  199. $groupTable,
  200. $GLOBALS['dbi']->getTablesFull(
  201. $db, $groupWithSeperator, ($groupWithSeperator != false), null,
  202. $limit_offset, $limit_count, $sort, $sort_order, $tbl_type
  203. )
  204. );
  205. }
  206. /**
  207. * @global int count of tables in db
  208. */
  209. $num_tables = count($tables);
  210. // (needed for proper working of the MaxTableList feature)
  211. if (! isset($total_num_tables)) {
  212. $total_num_tables = $num_tables;
  213. }
  214. /**
  215. * cleanup
  216. */
  217. unset($each_table, $db_info_result);
  218. /**
  219. * If coming from a Show MySQL link on the home page,
  220. * put something in $sub_part
  221. */
  222. if (empty($sub_part)) {
  223. $sub_part = '_structure';
  224. }
  225. ?>