dolibarr 21.0.0-beta
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
29if (! defined('CSRFCHECK_WITH_TOKEN')) {
30 define('CSRFCHECK_WITH_TOKEN', '1'); // Force use of CSRF protection with tokens even for GET
31}
32
33// Load Dolibarr environment
34require '../../main.inc.php';
35require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
36require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
37
46$langs->load("admin");
47
48if (!$user->admin) {
50}
51
52$table = GETPOST('table', 'aZ09');
53$action = GETPOST('action', 'aZ09');
54
55
56/*
57 * Actions
58 */
59
60if ($action == 'convert') { // Convert engine into innodb
61 $sql = "ALTER TABLE ".$db->sanitize($table)." ENGINE=INNODB";
62 $db->query($sql);
63}
64if ($action == 'convertutf8') {
65 $collation = 'utf8_unicode_ci';
66 $defaultcollation = $db->getDefaultCollationDatabase();
67 if (preg_match('/general/', $defaultcollation)) {
68 $collation = 'utf8_general_ci';
69 }
70 $sql = "ALTER TABLE ".$db->sanitize($table)." CHARACTER SET utf8 COLLATE ".$db->sanitize($collation); // Set the default value on table
71 $resql1 = $db->query($sql);
72 if (!$resql1) {
73 setEventMessages($db->lasterror(), null, 'warnings');
74 } else {
75 $sql = "ALTER TABLE ".$db->sanitize($table)." CONVERT TO CHARACTER SET utf8 COLLATE ".$db->sanitize($collation); // Switch fields (may fails due to foreign key)
76 $resql2 = $db->query($sql);
77 if (!$resql2) {
78 setEventMessages($db->lasterror(), null, 'warnings');
79 }
80 }
81}
82if ($action == 'convertutf8mb4') {
83 $collation = 'utf8mb4_unicode_ci';
84 $defaultcollation = $db->getDefaultCollationDatabase();
85 if (preg_match('/general/', $defaultcollation)) {
86 $collation = 'utf8mb4_general_ci';
87 }
88 $sql = "ALTER TABLE ".$db->sanitize($table)." CHARACTER SET utf8mb4 COLLATE ".$db->sanitize($collation); // Set the default value on table
89 $resql1 = $db->query($sql);
90 if (!$resql1) {
91 setEventMessages($db->lasterror(), null, 'warnings');
92 } else {
93 $sql = "ALTER TABLE ".$db->sanitize($table)." CONVERT TO CHARACTER SET utf8mb4 COLLATE ".$db->sanitize($collation); // Switch fields (may fails due to foreign key)
94 $resql2 = $db->query($sql);
95 if (!$resql2) {
96 setEventMessages($db->lasterror(), null, 'warnings');
97 }
98 }
99}
100if ($action == 'convertdynamic') {
101 $sql = "ALTER TABLE ".$db->sanitize($table)." ROW_FORMAT=DYNAMIC;";
102 $db->query($sql);
103}
104
105
106/*
107 * View
108 */
109
110llxHeader('', '', '', '', 0, 0, '', '', '', 'mod-admin page-database_tables');
111
112print load_fiche_titre($langs->trans("Tables")." ".ucfirst($conf->db->type), '', 'title_setup');
113
114
115// Define request to get table description
116$base = 0;
117if (preg_match('/mysql/i', $conf->db->type)) {
118 $sql = "SHOW TABLE STATUS";
119 $base = 1;
120} elseif ($conf->db->type == 'pgsql') {
121 $sql = "SELECT conname, contype FROM pg_constraint;";
122 $base = 2;
123} elseif ($conf->db->type == 'mssql') {
124 //$sqls[0] = "";
125 //$base=3;
126} elseif ($conf->db->type == 'sqlite' || $conf->db->type == 'sqlite3') {
127 //$sql = "SELECT name, type FROM sqlite_master";
128 $base = 4;
129}
130
131
132if (!$base) {
133 print $langs->trans("FeatureNotAvailableWithThisDatabaseDriver");
134} else {
135 $i = 0;
136 if ($base == 1) {
137 print '<div class="div-table-responsive-no-min">';
138 print '<table class="noborder">';
139 print '<tr class="liste_titre">';
140 print '<td>#</td>';
141 print '<td>'.$langs->trans("TableName").'</td>';
142 print '<td colspan="2">'.$langs->trans("Type").'</td>';
143 print '<td>'.$langs->trans("Format").'</td>';
144 print '<td class="right">'.$langs->trans("NbOfRecord").'</td>';
145 print '<td class="right">Avg_row_length</td>';
146 print '<td class="right">Data_length</td>';
147 print '<td class="right">Max_Data_length</td>';
148 print '<td class="right">Index_length</td>';
149 print '<td class="right">Increment</td>';
150 print '<td class="right">Last check</td>';
151 print '<td class="right">Collation</td>';
152 print "</tr>\n";
153
154 $arrayoffilesrich = dol_dir_list(DOL_DOCUMENT_ROOT.'/install/mysql/tables/', 'files', 0, '\.sql$');
155 $arrayoffiles = array();
156 $arrayoftablesautocreated = array();
157 foreach ($arrayoffilesrich as $value) {
158 //print $shortsqlfilename.' ';
159 $shortsqlfilename = preg_replace('/\-[a-z]+\./', '.', $value['name']);
160 $arrayoffiles[$value['name']] = $shortsqlfilename;
161 if ($value['name'] == $shortsqlfilename && ! preg_match('/\.key\.sql$/', $value['name'])) {
162 // This is a sql file automatically created
163 $arrayoftablesautocreated[$value['name']] = $shortsqlfilename;
164 }
165 }
166
167 // Now loop on tables really found into database
168 $sql = "SHOW TABLE STATUS";
169
170 $resql = $db->query($sql);
171 if ($resql) {
172 $num = $db->num_rows($resql);
173 $i = 0;
174 while ($i < $num) {
175 $obj = $db->fetch_object($resql);
176
177 print '<tr class="oddeven">';
178
179 print '<td>'.($i + 1).'</td>';
180 print '<td class="tdoverflowmax300" title="'.dol_escape_htmltag($obj->Name).'"><a href="dbtable.php?table='.urlencode($obj->Name).'">'.$obj->Name.'</a>';
181 $tablename = preg_replace('/^'.MAIN_DB_PREFIX.'/', 'llx_', $obj->Name);
182
183 if (in_array($tablename.'.sql', $arrayoffiles)) {
184 if (in_array($tablename.'.sql', $arrayoftablesautocreated)) {
185 $img = "info";
186 } else {
187 $img = "info_black";
188 print img_picto($langs->trans("NotAvailableByDefaultEnabledOnModuleActivation"), $img, 'class="small opacitymedium"');
189 }
190 } else {
191 $img = "info_black";
192 print img_picto($langs->trans("ExternalModule"), $img, 'class="small"');
193 }
194 print '</td>';
195 print '<td>'.$obj->Engine.'</td>';
196 if (isset($obj->Engine) && $obj->Engine == "MyISAM") {
197 print '<td><a class="reposition" href="database-tables.php?action=convert&table='.urlencode($obj->Name).'&token='.newToken().'">'.$langs->trans("Convert").' InnoDb</a></td>';
198 } else {
199 print '<td>&nbsp;</td>';
200 }
201 print '<td>';
202 print $obj->Row_format;
203 if (isset($obj->Row_format) && (in_array($obj->Row_format, array("Compact")))) {
204 print '<br><a class="reposition" href="database-tables.php?action=convertdynamic&table='.urlencode($obj->Name).'&token='.newToken().'">'.$langs->trans("Convert").' Dynamic</a>';
205 }
206 print '</td>';
207 print '<td class="right">'.$obj->Rows.'</td>';
208 print '<td class="right">'.$obj->Avg_row_length.'</td>';
209 print '<td class="right">'.$obj->Data_length.'</td>';
210 print '<td class="right">'.$obj->Max_data_length.'</td>';
211 print '<td class="right">'.$obj->Index_length.'</td>';
212 print '<td class="right">'.$obj->Auto_increment.'</td>';
213 print '<td class="right">'.$obj->Check_time.'</td>';
214 print '<td class="right nowraponall">'.$obj->Collation;
215 // Link to convert collation
216 if (isset($obj->Collation)) {
217 print '<br><span class="opacitymedium small">'.$langs->trans("ConvertInto");
218 if (!in_array($obj->Collation, array("utf8_unicode_ci"))) {
219 print ' <a class="reposition" href="database-tables.php?action=convertutf8&table='.urlencode($obj->Name).'&token='.newToken().'">utf8</a>';
220 }
221 if (!in_array($obj->Collation, array("utf8mb4_unicode_ci"))) {
222 print ' <a class="reposition" href="database-tables.php?action=convertutf8mb4&table='.urlencode($obj->Name).'&token='.newToken().'">utf8mb4</a>';
223 }
224 print '</span>';
225 }
226 print '</td>';
227 print '</tr>';
228 $i++;
229 }
230 }
231 print '</table>';
232 print '</div>';
233 }
234
235 if ($base == 2) {
236 print '<div class="div-table-responsive-no-min">';
237 print '<table class="noborder">';
238 print '<tr class="liste_titre">';
239
240 print '<td>#</td>';
241 print '<td>'.$langs->trans("TableName").'</td>';
242 print '<td>Nb of tuples</td>';
243 print '<td>Nb index fetcher.</td>';
244 print '<td>Nb tuples insert</td>';
245 print '<td>Nb tuples modify</td>';
246 print '<td>Nb tuples delete</td>';
247 print "</tr>\n";
248
249 $sql = "SELECT relname, seq_tup_read, idx_tup_fetch, n_tup_ins, n_tup_upd, n_tup_del";
250 $sql .= " FROM pg_stat_user_tables ORDER BY relname";
251
252 $resql = $db->query($sql);
253 if ($resql) {
254 $num = $db->num_rows($resql);
255 $i = 0;
256 while ($i < $num) {
257 $row = $db->fetch_row($resql);
258 print '<tr class="oddeven">';
259 print '<td>'.($i + 1).'</td>';
260 print '<td>'.$row[0].'</td>';
261 print '<td class="right">'.$row[1].'</td>';
262 print '<td class="right">'.$row[2].'</td>';
263 print '<td class="right">'.$row[3].'</td>';
264 print '<td class="right">'.$row[4].'</td>';
265 print '<td class="right">'.$row[5].'</td>';
266 print '</tr>';
267 $i++;
268 }
269 }
270 print '</table>';
271 print '</div>';
272 }
273
274 if ($base == 4) {
275 // Sqlite by PDO or by Sqlite3
276 print '<div class="div-table-responsive-no-min">';
277 print '<table class="noborder">';
278 print '<tr class="liste_titre">';
279 print '<td>#</td>';
280 print '<td>'.$langs->trans("TableName").'</td>';
281 print '<td>'.$langs->trans("NbOfRecord").'</td>';
282 print "</tr>\n";
283
284 $sql = "SELECT name, type FROM sqlite_master where type='table' and name not like 'sqlite%' ORDER BY name";
285 $resql = $db->query($sql);
286
287 if ($resql) {
288 while ($row = $db->fetch_row($resql)) {
289 $rescount = $db->query("SELECT COUNT(*) FROM ".$row[0]);
290 if ($rescount) {
291 $row_count = $db->fetch_row($rescount);
292 $count = $row_count[0];
293 } else {
294 $count = '?';
295 }
296
297 print '<tr class="oddeven">';
298 print '<td>'.($i + 1).'</td>';
299 print '<td>'.$row[0].'</td>';
300 print '<td>'.$count.'</td>';
301 print '</tr>';
302 }
303 }
304
305 print '</table>';
306 print '</div>';
307 }
308}
309
310// End of page
311llxFooter();
312$db->close();
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:71
llxFooter()
Footer empty.
Definition document.php:107
dol_dir_list($utf8_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:63
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.