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