dolibarr 18.0.6
database-tables.php
Go to the documentation of this file.
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
27if (! defined('CSRFCHECK_WITH_TOKEN')) {
28 define('CSRFCHECK_WITH_TOKEN', '1'); // Force use of CSRF protection with tokens even for GET
29}
30
31// Load Dolibarr environment
32require '../../main.inc.php';
33require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
34require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
35
36$langs->load("admin");
37
38if (!$user->admin) {
40}
41
42$action = GETPOST('action', 'aZ09');
43
44
45/*
46 * Actions
47 */
48
49if ($action == 'convert') {
50 $sql = "ALTER TABLE ".$db->escape(GETPOST("table", "aZ09"))." ENGINE=INNODB";
51 $db->query($sql);
52}
53if ($action == 'convertutf8') {
54 $sql = "ALTER TABLE ".$db->escape(GETPOST("table", "aZ09"))." CHARACTER SET utf8 COLLATE utf8_unicode_ci";
55 $db->query($sql);
56}
57if ($action == 'convertdynamic') {
58 $sql = "ALTER TABLE ".$db->escape(GETPOST("table", "aZ09"))." ROW_FORMAT=DYNAMIC;";
59 $db->query($sql);
60}
61
62
63/*
64 * View
65 */
66
67llxHeader();
68
69print load_fiche_titre($langs->trans("Tables")." ".ucfirst($conf->db->type), '', 'title_setup');
70
71
72// Define request to get table description
73$base = 0;
74if (preg_match('/mysql/i', $conf->db->type)) {
75 $sql = "SHOW TABLE STATUS";
76 $base = 1;
77} elseif ($conf->db->type == 'pgsql') {
78 $sql = "SELECT conname, contype FROM pg_constraint;";
79 $base = 2;
80} elseif ($conf->db->type == 'mssql') {
81 //$sqls[0] = "";
82 //$base=3;
83} elseif ($conf->db->type == 'sqlite' || $conf->db->type == 'sqlite3') {
84 //$sql = "SELECT name, type FROM sqlite_master";
85 $base = 4;
86}
87
88
89if (!$base) {
90 print $langs->trans("FeatureNotAvailableWithThisDatabaseDriver");
91} else {
92 if ($base == 1) {
93 print '<div class="div-table-responsive-no-min">';
94 print '<table class="noborder">';
95 print '<tr class="liste_titre">';
96 print '<td>#</td>';
97 print '<td>'.$langs->trans("TableName").'</td>';
98 print '<td colspan="2">'.$langs->trans("Type").'</td>';
99 print '<td>'.$langs->trans("Format").'</td>';
100 print '<td class="right">'.$langs->trans("NbOfRecord").'</td>';
101 print '<td class="right">Avg_row_length</td>';
102 print '<td class="right">Data_length</td>';
103 print '<td class="right">Max_Data_length</td>';
104 print '<td class="right">Index_length</td>';
105 print '<td class="right">Increment</td>';
106 print '<td class="right">Last check</td>';
107 print '<td class="right">Collation</td>';
108 print "</tr>\n";
109
110 $arrayoffilesrich = dol_dir_list(DOL_DOCUMENT_ROOT.'/install/mysql/tables/', 'files', 0, '\.sql$');
111 $arrayoffiles = array();
112 $arrayoftablesautocreated = array();
113 foreach ($arrayoffilesrich as $value) {
114 //print $shortsqlfilename.' ';
115 $shortsqlfilename = preg_replace('/\-[a-z]+\./', '.', $value['name']);
116 $arrayoffiles[$value['name']] = $shortsqlfilename;
117 if ($value['name'] == $shortsqlfilename && ! preg_match('/\.key\.sql$/', $value['name'])) {
118 // This is a sql file automatically created
119 $arrayoftablesautocreated[$value['name']] = $shortsqlfilename;
120 }
121 }
122
123 // Now loop on tables really found into database
124 $sql = "SHOW TABLE STATUS";
125
126 $resql = $db->query($sql);
127 if ($resql) {
128 $num = $db->num_rows($resql);
129 $i = 0;
130 while ($i < $num) {
131 $obj = $db->fetch_object($resql);
132
133 print '<tr class="oddeven">';
134
135 print '<td>'.($i+1).'</td>';
136 print '<td><a href="dbtable.php?table='.$obj->Name.'">'.$obj->Name.'</a>';
137 $tablename = preg_replace('/^'.MAIN_DB_PREFIX.'/', 'llx_', $obj->Name);
138
139 if (in_array($tablename.'.sql', $arrayoffiles)) {
140 if (in_array($tablename.'.sql', $arrayoftablesautocreated)) {
141 $img = "info";
142 } else {
143 $img = "info_black";
144 print img_picto($langs->trans("NotAvailableByDefaultEnabledOnModuleActivation"), $img, 'class="small opacitymedium"');
145 }
146 } else {
147 $img = "info_black";
148 print img_picto($langs->trans("ExternalModule"), $img, 'class="small"');
149 }
150 print '</td>';
151 print '<td>'.$obj->Engine.'</td>';
152 if (isset($obj->Engine) && $obj->Engine == "MyISAM") {
153 print '<td><a class="reposition" href="database-tables.php?action=convert&table='.urlencode($obj->Name).'&token='.newToken().'">'.$langs->trans("Convert").' InnoDb</a></td>';
154 } else {
155 print '<td>&nbsp;</td>';
156 }
157 print '<td>';
158 print $obj->Row_format;
159 if (isset($obj->Row_format) && (in_array($obj->Row_format, array("Compact")))) {
160 print '<br><a class="reposition" href="database-tables.php?action=convertdynamic&table='.urlencode($obj->Name).'&token='.newToken().'">'.$langs->trans("Convert").' Dynamic</a>';
161 }
162 print '</td>';
163 print '<td align="right">'.$obj->Rows.'</td>';
164 print '<td align="right">'.$obj->Avg_row_length.'</td>';
165 print '<td align="right">'.$obj->Data_length.'</td>';
166 print '<td align="right">'.$obj->Max_data_length.'</td>';
167 print '<td align="right">'.$obj->Index_length.'</td>';
168 print '<td align="right">'.$obj->Auto_increment.'</td>';
169 print '<td align="right">'.$obj->Check_time.'</td>';
170 print '<td align="right">'.$obj->Collation;
171 if (isset($obj->Collation) && (in_array($obj->Collation, array("utf8mb4_general_ci", "utf8mb4_unicode_ci", "latin1_swedish_ci")))) {
172 print '<br><a class="reposition" href="database-tables.php?action=convertutf8&table='.urlencode($obj->Name).'&token='.newToken().'">'.$langs->trans("Convert").' UTF8</a>';
173 }
174 print '</td>';
175 print '</tr>';
176 $i++;
177 }
178 }
179 print '</table>';
180 print '</div>';
181 }
182
183 if ($base == 2) {
184 print '<div class="div-table-responsive-no-min">';
185 print '<table class="noborder">';
186 print '<tr class="liste_titre">';
187
188 print '<td>#</td>';
189 print '<td>'.$langs->trans("TableName").'</td>';
190 print '<td>Nb of tuples</td>';
191 print '<td>Nb index fetcher.</td>';
192 print '<td>Nb tuples insert</td>';
193 print '<td>Nb tuples modify</td>';
194 print '<td>Nb tuples delete</td>';
195 print "</tr>\n";
196
197 $sql = "SELECT relname, seq_tup_read, idx_tup_fetch, n_tup_ins, n_tup_upd, n_tup_del";
198 $sql .= " FROM pg_stat_user_tables";
199
200 $resql = $db->query($sql);
201 if ($resql) {
202 $num = $db->num_rows($resql);
203 $i = 0;
204 while ($i < $num) {
205 $row = $db->fetch_row($resql);
206 print '<tr class="oddeven">';
207 print '<td>'.($i+1).'</td>';
208 print '<td>'.$row[0].'</td>';
209 print '<td class="right">'.$row[1].'</td>';
210 print '<td class="right">'.$row[2].'</td>';
211 print '<td class="right">'.$row[3].'</td>';
212 print '<td class="right">'.$row[4].'</td>';
213 print '<td class="right">'.$row[5].'</td>';
214 print '</tr>';
215 $i++;
216 }
217 }
218 print '</table>';
219 print '</div>';
220 }
221
222 if ($base == 4) {
223 // Sqlite by PDO or by Sqlite3
224 print '<div class="div-table-responsive-no-min">';
225 print '<table class="noborder">';
226 print '<tr class="liste_titre">';
227 print '<td>#</td>';
228 print '<td>'.$langs->trans("TableName").'</td>';
229 print '<td>'.$langs->trans("NbOfRecord").'</td>';
230 print "</tr>\n";
231
232 $sql = "SELECT name, type FROM sqlite_master where type='table' and name not like 'sqlite%' ORDER BY name";
233 $resql = $db->query($sql);
234
235 if ($resql) {
236 while ($row = $db->fetch_row($resql)) {
237 $rescount = $db->query("SELECT COUNT(*) FROM ".$row[0]);
238 if ($rescount) {
239 $row_count = $db->fetch_row($rescount);
240 $count = $row_count[0];
241 } else {
242 $count = '?';
243 }
244
245 print '<tr class="oddeven">';
246 print '<td>'.($i+1).'</td>';
247 print '<td>'.$row[0].'</td>';
248 print '<td>'.$count.'</td>';
249 print '</tr>';
250 }
251 }
252
253 print '</table>';
254 print '</div>';
255 }
256}
257
258// End of page
259llxFooter();
260$db->close();
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:56
llxFooter()
Empty footer.
Definition wrapper.php:70
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:62
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.