dolibarr 21.0.0-alpha
dbtable.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2005 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
29// Load Dolibarr environment
30require '../../main.inc.php';
31
32$langs->load("admin");
33
34if (!$user->admin) {
36}
37
38$table = GETPOST('table', 'aZ09');
39$field = GETPOST('field', 'aZ09');
40$action = GETPOST('action', 'aZ09');
41
42
43/*
44 * Actions
45 */
46
47if ($action == 'convertutf8') {
48 $sql = "SHOW FULL COLUMNS IN ".$db->sanitize($table);
49
50 $resql = $db->query($sql);
51 if ($resql) {
52 $num = $db->num_rows($resql);
53 $i = 0;
54 while ($i < $num) {
55 $row = $db->fetch_row($resql);
56 if ($row[0] == $field) {
57 $sql = "ALTER TABLE ".$db->sanitize($table)." MODIFY ".$db->sanitize($row[0])." ".$row[1]." CHARACTER SET utf8"; // We must not sanitize the $row[1]
58 $db->query($sql);
59
60 $collation = 'utf8_unicode_ci';
61 $defaultcollation = $db->getDefaultCollationDatabase();
62 if (preg_match('/general/', $defaultcollation)) {
63 $collation = 'utf8_general_ci';
64 }
65
66 $sql = "ALTER TABLE ".$db->sanitize($table)." MODIFY ".$db->sanitize($row[0])." ".$row[1]." COLLATE ".$db->sanitize($collation); // We must not sanitize the $row[1]
67 $resql2 = $db->query($sql);
68 if (!$resql2) {
69 setEventMessages($db->lasterror(), null, 'warnings');
70 }
71
72 break;
73 }
74 }
75 }
76}
77if ($action == 'convertutf8mb4') {
78 $sql = "SHOW FULL COLUMNS IN ".$db->sanitize($table);
79
80 $resql = $db->query($sql);
81 if ($resql) {
82 $num = $db->num_rows($resql);
83 $i = 0;
84 while ($i < $num) {
85 $row = $db->fetch_row($resql);
86 if ($row[0] == $field) {
87 $sql = "ALTER TABLE ".$db->sanitize($table)." MODIFY ".$db->sanitize($row[0])." ".$row[1]." CHARACTER SET utf8mb4"; // We must not sanitize the $row[1]
88 $db->query($sql);
89
90 $collation = 'utf8mb4_unicode_ci';
91 $defaultcollation = $db->getDefaultCollationDatabase();
92 if (preg_match('/general/', $defaultcollation)) {
93 $collation = 'utf8mb4_general_ci';
94 }
95
96 $sql = "ALTER TABLE ".$db->sanitize($table)." MODIFY ".$db->sanitize($row[0])." ".$row[1]." COLLATE ".$db->sanitize($collation); // We must not sanitize the $row[1]
97 $resql2 = $db->query($sql);
98 if (!$resql2) {
99 setEventMessages($db->lasterror(), null, 'warnings');
100 }
101
102 break;
103 }
104 }
105 }
106}
107
108
109/*
110 * View
111 */
112
113llxHeader('', '', '', '', 0, 0, '', '', '', 'mod-admin page-system_dbtable');
114
115
116print load_fiche_titre($langs->trans("Table")." ".$table, '', 'title_setup');
117
118// Define request to get table description
119$base = 0;
120$sql = null;
121if (preg_match('/mysql/i', $conf->db->type)) {
122 $sql = "SHOW TABLE STATUS LIKE '".$db->escape($db->escapeforlike($table))."'";
123 $base = 1;
124} elseif ($conf->db->type == 'pgsql') {
125 $sql = "SELECT conname,contype FROM pg_constraint";
126 $base = 2;
127}
128
129if (!$base || $sql === null) {
130 print $langs->trans("FeatureNotAvailableWithThisDatabaseDriver");
131} else {
132 $resql = $db->query($sql);
133 if ($resql) {
134 $num = $db->num_rows($resql);
135 $i = 0;
136 while ($i < $num) {
137 $row = $db->fetch_row($resql);
138 $i++;
139 }
140 }
141
142 if ($base == 1) { // mysql
143 $link = array();
144 $cons = explode(";", $row[14]);
145 if (!empty($cons)) {
146 foreach ($cons as $cc) {
147 $cx = preg_replace("/\‍)\sREFER/", "", $cc);
148 $cx = preg_replace("/\‍(`/", "", $cx);
149 $cx = preg_replace("/`\‍)/", "", $cx);
150 $cx = preg_replace("/`\s/", "", $cx);
151
152 $val = explode("`", $cx);
153
154 $link[trim($val[0])][0] = (isset($val[1]) ? $val[1] : '');
155 $link[trim($val[0])][1] = (isset($val[2]) ? $val[2] : '');
156 }
157 }
158
159 print '<div class="div-table-responsive-no-min">';
160 print '<table class="noborder">';
161 print '<tr class="liste_titre">';
162 print '<td>'.$langs->trans("Fields").'</td>';
163 print '<td>'.$langs->trans("Type").'</td>';
164 print '<td>'.$langs->trans("Collation").'</td>';
165 print '<td>'.$langs->trans("Null").'</td>';
166 print '<td>'.$langs->trans("Index").'</td>';
167 print '<td>'.$langs->trans("Default").'</td>';
168 print '<td>'.$langs->trans("Extra").'</td>';
169 print '<td>'.$langs->trans("Privileges").'</td>';
170 print '<td>'.$langs->trans("FieldsLinked").'</td>';
171 print '</tr>';
172
173 // $sql = "DESCRIBE ".$table;
174 $sql = "SHOW FULL COLUMNS IN ".$db->sanitize($table);
175
176 $resql = $db->query($sql);
177 if ($resql) {
178 $num = $db->num_rows($resql);
179 $i = 0;
180 while ($i < $num) {
181 $row = $db->fetch_row($resql);
182
183 print '<tr class="oddeven">';
184
185 // field
186 print "<td>".$row[0]."</td>";
187
188 // type
189 print "<td>";
190 $proptype = $row[1];
191 $pictoType = '';
192 $matches = array();
193 if (preg_match('/^varchar/', $proptype, $matches)) {
194 $pictoType = 'varchar';
195 } elseif (strpos($proptype, 'int') === 0 || strpos($proptype, 'tinyint') === 0 || strpos($proptype, 'bigint') === 0) {
196 $pictoType = 'int';
197 } elseif (strpos($proptype, 'timestamp') === 0) {
198 $pictoType = 'datetime';
199 } elseif (strpos($proptype, 'real') === 0) {
200 $pictoType = 'double';
201 }
202 print(!empty($pictoType) ? getPictoForType($pictoType) : getPictoForType($proptype)).'<span title="'.dol_escape_htmltag($proptype).'">'.dol_escape_htmltag($proptype).'</span>';
203 print "</td>";
204
205 // collation
206 print "<td>".(empty($row[2]) ? '&nbsp;' : $row[2]);
207
208 // Link to convert collation
209 if (isset($row[2])) {
210 print '<br><span class="opacitymedium small">'.$langs->trans("ConvertInto");
211 if (!in_array($row[2], array("utf8_unicode_ci"))) {
212 print ' <a class="reposition" href="dbtable.php?action=convertutf8&table='.urlencode($table).'&field='.urlencode($row[0]).'&token='.newToken().'">utf8</a>';
213 }
214 if (!in_array($row[2], array("utf8mb4_unicode_ci"))) {
215 print ' <a class="reposition" href="dbtable.php?action=convertutf8mb4&table='.urlencode($table).'&field='.urlencode($row[0]).'&token='.newToken().'">utf8mb4</a>';
216 }
217 print '</span>';
218 } else {
219 print '<br>&nbsp;';
220 }
221
222 print "</td>";
223
224 // null
225 print "<td>".$row[3]."</td>";
226 // key
227 print "<td>".(empty($row[4]) ? '' : $row[4])."</td>";
228 // default
229 print "<td>".(empty($row[5]) ? '' : $row[5])."</td>";
230 // extra
231 print "<td>".(empty($row[6]) ? '' : $row[6])."</td>";
232 // privileges
233 print "<td>".(empty($row[7]) ? '' : $row[7])."</td>";
234
235 print "<td>".(isset($link[$row[0]][0]) ? $link[$row[0]][0] : '').".";
236 print(isset($link[$row[0]][1]) ? $link[$row[0]][1] : '')."</td>";
237
238 print '<!-- ALTER TABLE '.$table.' MODIFY '.$row[0].' '.$row[1].' COLLATE utf8mb4_unicode_ci; -->';
239 print '<!-- ALTER TABLE '.$table.' MODIFY '.$row[0].' '.$row[1].' CHARACTER SET utf8mb4; -->';
240 print '</tr>';
241 $i++;
242 }
243 }
244 print '</table>';
245 print '</div>';
246 }
247}
248
249// End of page
250llxFooter();
251$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
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.
getPictoForType($key, $morecss='')
Return the picto for a data type.
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.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.