database-tables.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2021 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
  5. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  6. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/admin/system/database-tables.php
  23. * \brief Page with information on database tables. Add also some maintenance action to convert tables.
  24. */
  25. if (! defined('CSRFCHECK_WITH_TOKEN')) {
  26. define('CSRFCHECK_WITH_TOKEN', '1'); // Force use of CSRF protection with tokens even for GET
  27. }
  28. // Load Dolibarr environment
  29. require '../../main.inc.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  32. $langs->load("admin");
  33. if (!$user->admin) {
  34. accessforbidden();
  35. }
  36. $action = GETPOST('action', 'aZ09');
  37. /*
  38. * Actions
  39. */
  40. if ($action == 'convert') {
  41. $sql = "ALTER TABLE ".$db->escape(GETPOST("table", "aZ09"))." ENGINE=INNODB";
  42. $db->query($sql);
  43. }
  44. if ($action == 'convertutf8') {
  45. $sql = "ALTER TABLE ".$db->escape(GETPOST("table", "aZ09"))." CHARACTER SET utf8 COLLATE utf8_unicode_ci";
  46. $db->query($sql);
  47. }
  48. if ($action == 'convertdynamic') {
  49. $sql = "ALTER TABLE ".$db->escape(GETPOST("table", "aZ09"))." ROW_FORMAT=DYNAMIC;";
  50. $db->query($sql);
  51. }
  52. /*
  53. * View
  54. */
  55. llxHeader();
  56. print load_fiche_titre($langs->trans("Tables")." ".ucfirst($conf->db->type), '', 'title_setup');
  57. // Define request to get table description
  58. $base = 0;
  59. if (preg_match('/mysql/i', $conf->db->type)) {
  60. $sql = "SHOW TABLE STATUS";
  61. $base = 1;
  62. } elseif ($conf->db->type == 'pgsql') {
  63. $sql = "SELECT conname, contype FROM pg_constraint;";
  64. $base = 2;
  65. } elseif ($conf->db->type == 'mssql') {
  66. //$sqls[0] = "";
  67. //$base=3;
  68. } elseif ($conf->db->type == 'sqlite' || $conf->db->type == 'sqlite3') {
  69. //$sql = "SELECT name, type FROM sqlite_master";
  70. $base = 4;
  71. }
  72. if (!$base) {
  73. print $langs->trans("FeatureNotAvailableWithThisDatabaseDriver");
  74. } else {
  75. if ($base == 1) {
  76. print '<div class="div-table-responsive-no-min">';
  77. print '<table class="noborder">';
  78. print '<tr class="liste_titre">';
  79. print '<td>#</td>';
  80. print '<td>'.$langs->trans("TableName").'</td>';
  81. print '<td colspan="2">'.$langs->trans("Type").'</td>';
  82. print '<td>'.$langs->trans("Format").'</td>';
  83. print '<td class="right">'.$langs->trans("NbOfRecord").'</td>';
  84. print '<td class="right">Avg_row_length</td>';
  85. print '<td class="right">Data_length</td>';
  86. print '<td class="right">Max_Data_length</td>';
  87. print '<td class="right">Index_length</td>';
  88. print '<td class="right">Increment</td>';
  89. print '<td class="right">Last check</td>';
  90. print '<td class="right">Collation</td>';
  91. print "</tr>\n";
  92. $arrayoffilesrich = dol_dir_list(DOL_DOCUMENT_ROOT.'/install/mysql/tables/', 'files', 0, '\.sql$');
  93. $arrayoffiles = array();
  94. $arrayoftablesautocreated = array();
  95. foreach ($arrayoffilesrich as $value) {
  96. //print $shortsqlfilename.' ';
  97. $shortsqlfilename = preg_replace('/\-[a-z]+\./', '.', $value['name']);
  98. $arrayoffiles[$value['name']] = $shortsqlfilename;
  99. if ($value['name'] == $shortsqlfilename && ! preg_match('/\.key\.sql$/', $value['name'])) {
  100. // This is a sql file automatically created
  101. $arrayoftablesautocreated[$value['name']] = $shortsqlfilename;
  102. }
  103. }
  104. // Now loop on tables really found into database
  105. $sql = "SHOW TABLE STATUS";
  106. $resql = $db->query($sql);
  107. if ($resql) {
  108. $num = $db->num_rows($resql);
  109. $i = 0;
  110. while ($i < $num) {
  111. $obj = $db->fetch_object($resql);
  112. print '<tr class="oddeven">';
  113. print '<td>'.($i+1).'</td>';
  114. print '<td><a href="dbtable.php?table='.$obj->Name.'">'.$obj->Name.'</a>';
  115. $tablename = preg_replace('/^'.MAIN_DB_PREFIX.'/', 'llx_', $obj->Name);
  116. if (in_array($tablename.'.sql', $arrayoffiles)) {
  117. if (in_array($tablename.'.sql', $arrayoftablesautocreated)) {
  118. $img = "info";
  119. } else {
  120. $img = "info_black";
  121. print img_picto($langs->trans("NotAvailableByDefaultEnabledOnModuleActivation"), $img, 'class="small opacitymedium"');
  122. }
  123. } else {
  124. $img = "info_black";
  125. print img_picto($langs->trans("ExternalModule"), $img, 'class="small"');
  126. }
  127. print '</td>';
  128. print '<td>'.$obj->Engine.'</td>';
  129. if (isset($obj->Engine) && $obj->Engine == "MyISAM") {
  130. print '<td><a class="reposition" href="database-tables.php?action=convert&table='.urlencode($obj->Name).'&token='.newToken().'">'.$langs->trans("Convert").' InnoDb</a></td>';
  131. } else {
  132. print '<td>&nbsp;</td>';
  133. }
  134. print '<td>';
  135. print $obj->Row_format;
  136. if (isset($obj->Row_format) && (in_array($obj->Row_format, array("Compact")))) {
  137. print '<br><a class="reposition" href="database-tables.php?action=convertdynamic&table='.urlencode($obj->Name).'&token='.newToken().'">'.$langs->trans("Convert").' Dynamic</a>';
  138. }
  139. print '</td>';
  140. print '<td align="right">'.$obj->Rows.'</td>';
  141. print '<td align="right">'.$obj->Avg_row_length.'</td>';
  142. print '<td align="right">'.$obj->Data_length.'</td>';
  143. print '<td align="right">'.$obj->Max_data_length.'</td>';
  144. print '<td align="right">'.$obj->Index_length.'</td>';
  145. print '<td align="right">'.$obj->Auto_increment.'</td>';
  146. print '<td align="right">'.$obj->Check_time.'</td>';
  147. print '<td align="right">'.$obj->Collation;
  148. if (isset($obj->Collation) && (in_array($obj->Collation, array("utf8mb4_general_ci", "utf8mb4_unicode_ci", "latin1_swedish_ci")))) {
  149. print '<br><a class="reposition" href="database-tables.php?action=convertutf8&table='.urlencode($obj->Name).'&token='.newToken().'">'.$langs->trans("Convert").' UTF8</a>';
  150. }
  151. print '</td>';
  152. print '</tr>';
  153. $i++;
  154. }
  155. }
  156. print '</table>';
  157. print '</div>';
  158. }
  159. if ($base == 2) {
  160. print '<div class="div-table-responsive-no-min">';
  161. print '<table class="noborder">';
  162. print '<tr class="liste_titre">';
  163. print '<td>#</td>';
  164. print '<td>'.$langs->trans("TableName").'</td>';
  165. print '<td>Nb of tuples</td>';
  166. print '<td>Nb index fetcher.</td>';
  167. print '<td>Nb tuples insert</td>';
  168. print '<td>Nb tuples modify</td>';
  169. print '<td>Nb tuples delete</td>';
  170. print "</tr>\n";
  171. $sql = "SELECT relname, seq_tup_read, idx_tup_fetch, n_tup_ins, n_tup_upd, n_tup_del";
  172. $sql .= " FROM pg_stat_user_tables";
  173. $resql = $db->query($sql);
  174. if ($resql) {
  175. $num = $db->num_rows($resql);
  176. $i = 0;
  177. while ($i < $num) {
  178. $row = $db->fetch_row($resql);
  179. print '<tr class="oddeven">';
  180. print '<td>'.($i+1).'</td>';
  181. print '<td>'.$row[0].'</td>';
  182. print '<td class="right">'.$row[1].'</td>';
  183. print '<td class="right">'.$row[2].'</td>';
  184. print '<td class="right">'.$row[3].'</td>';
  185. print '<td class="right">'.$row[4].'</td>';
  186. print '<td class="right">'.$row[5].'</td>';
  187. print '</tr>';
  188. $i++;
  189. }
  190. }
  191. print '</table>';
  192. print '</div>';
  193. }
  194. if ($base == 4) {
  195. // Sqlite by PDO or by Sqlite3
  196. print '<div class="div-table-responsive-no-min">';
  197. print '<table class="noborder">';
  198. print '<tr class="liste_titre">';
  199. print '<td>#</td>';
  200. print '<td>'.$langs->trans("TableName").'</td>';
  201. print '<td>'.$langs->trans("NbOfRecord").'</td>';
  202. print "</tr>\n";
  203. $sql = "SELECT name, type FROM sqlite_master where type='table' and name not like 'sqlite%' ORDER BY name";
  204. $resql = $db->query($sql);
  205. if ($resql) {
  206. while ($row = $db->fetch_row($resql)) {
  207. $rescount = $db->query("SELECT COUNT(*) FROM ".$row[0]);
  208. if ($rescount) {
  209. $row_count = $db->fetch_row($rescount);
  210. $count = $row_count[0];
  211. } else {
  212. $count = '?';
  213. }
  214. print '<tr class="oddeven">';
  215. print '<td>'.($i+1).'</td>';
  216. print '<td>'.$row[0].'</td>';
  217. print '<td>'.$count.'</td>';
  218. print '</tr>';
  219. }
  220. }
  221. print '</table>';
  222. print '</div>';
  223. }
  224. }
  225. // End of page
  226. llxFooter();
  227. $db->close();