dolibarr 24.0.0-beta
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
40$langs->load("admin");
41
42if (!$user->admin) {
44}
45
46$table = GETPOST('table', 'aZ09');
47$field = GETPOST('field', 'aZ09');
48$action = GETPOST('action', 'aZ09');
49
50
51/*
52 * Actions
53 */
54
55
56$logsql = '';
57$resultsql = true;
58
59if ($action == 'convertutf8') {
60 $sql = "SHOW FULL COLUMNS IN ".$db->sanitize($table);
61 $logsql .= $sql.'<br>';
62
63 $resql = $db->query($sql);
64 if ($resql) {
65 $num = $db->num_rows($resql);
66 $i = 0;
67 while ($i < $num) {
68 $row = $db->fetch_row($resql);
69 if ($row[0] == $field) {
70 $sql = "ALTER TABLE ".$db->sanitize($table)." MODIFY ".$db->sanitize($row[0])." ".$row[1]." CHARACTER SET utf8"; // We must not sanitize the $row[1]
71 $logsql .= $sql.'<br>';
72
73 $db->query($sql);
74
75 $collation = 'utf8_unicode_ci';
76 $defaultcollation = $db->getDefaultCollationDatabase();
77 if (preg_match('/general/', $defaultcollation)) {
78 $collation = 'utf8_general_ci';
79 }
80
81 $sql = "ALTER TABLE ".$db->sanitize($table)." MODIFY ".$db->sanitize($row[0])." ".$row[1]." COLLATE ".$db->sanitize($collation); // We must not sanitize the $row[1]
82 $logsql .= $sql.'<br>';
83
84 $resql2 = $db->query($sql);
85 if (!$resql2) {
86 setEventMessages($db->lasterror(), null, 'warnings');
87 $resultsql = false;
88 }
89
90 break;
91 }
92 }
93 }
94}
95if ($action == 'convertutf8mb4') {
96 $sql = "SHOW FULL COLUMNS IN ".$db->sanitize($table);
97 $logsql .= $sql.'<br>';
98
99 $resql = $db->query($sql);
100 if ($resql) {
101 $num = $db->num_rows($resql);
102 $i = 0;
103 while ($i < $num) {
104 $row = $db->fetch_row($resql);
105 if ($row[0] == $field) {
106 $sql = "ALTER TABLE ".$db->sanitize($table)." MODIFY ".$db->sanitize($row[0])." ".$row[1]." CHARACTER SET utf8mb4"; // We must not sanitize the $row[1]
107 $logsql .= $sql.'<br>';
108
109 $db->query($sql);
110
111 $collation = 'utf8mb4_unicode_ci';
112 $defaultcollation = $db->getDefaultCollationDatabase();
113 if (preg_match('/general/', $defaultcollation)) {
114 $collation = 'utf8mb4_general_ci';
115 }
116
117 $sql = "ALTER TABLE ".$db->sanitize($table)." MODIFY ".$db->sanitize($row[0])." ".$row[1]." COLLATE ".$db->sanitize($collation); // We must not sanitize the $row[1]
118 $logsql .= $sql.'<br>';
119
120 $resql2 = $db->query($sql);
121 if (!$resql2) {
122 setEventMessages($db->lasterror(), null, 'warnings');
123 $resultsql = false;
124 }
125
126 break;
127 }
128 }
129 }
130}
131
132
133/*
134 * View
135 */
136
137llxHeader('', '', '', '', 0, 0, '', '', '', 'mod-admin page-system_dbtable');
138
139$linkback = '<a href="'.DOL_URL_ROOT.'/admin/system/database-tables.php?restore_lastsearch_values=1">'.img_picto($langs->trans("GoBack"), 'back', 'class="pictofixedwidth"').'<span class="hideonsmartphone">'.$langs->trans("GoBack").'</span></a>';
140
141print load_fiche_titre($langs->trans("Table")." ".$table, $linkback, 'title_setup');
142
143if ($logsql) {
144 print info_admin($logsql.' '.($resultsql ? ' => OK' : ' => KO '.$db->lasterror()));
145}
146
147// Define request to get table description
148$base = 0;
149$sql = null;
150$row = array();
151if (preg_match('/mysql/i', $conf->db->type)) {
152 $sql = "SHOW TABLE STATUS LIKE '".$db->escape($db->escapeforlike($table))."'";
153 $base = 1;
154} elseif ($conf->db->type == 'pgsql') {
155 $sql = "SELECT conname,contype FROM pg_constraint";
156 $base = 2;
157}
158
159if (!$base || $sql === null) {
160 print $langs->trans("FeatureNotAvailableWithThisDatabaseDriver");
161} else {
162 $resql = $db->query($sql);
163 if ($resql) {
164 $num = $db->num_rows($resql);
165 $i = 0;
166 while ($i < $num) {
167 $row = $db->fetch_row($resql);
168 $i++;
169 }
170 }
171
172 if ($base == 1) { // mysql
173 $link = array();
174 $cons = explode(";", $row[14]);
175 if (!empty($cons)) {
176 foreach ($cons as $cc) {
177 $cx = preg_replace("/\‍)\sREFER/", "", $cc);
178 $cx = preg_replace("/\‍(`/", "", $cx);
179 $cx = preg_replace("/`\‍)/", "", $cx);
180 $cx = preg_replace("/`\s/", "", $cx);
181
182 $val = explode("`", $cx);
183
184 $link[trim($val[0])][0] = (isset($val[1]) ? $val[1] : '');
185 $link[trim($val[0])][1] = (isset($val[2]) ? $val[2] : '');
186 }
187 }
188
189 print '<div class="div-table-responsive-no-min">';
190 print '<table class="noborder">';
191 print '<tr class="liste_titre">';
192 print '<td>'.$langs->trans("Fields").'</td>';
193 print '<td>'.$langs->trans("Type").'</td>';
194 print '<td>'.$langs->trans("Collation").'</td>';
195 print '<td>'.$langs->trans("Null").'</td>';
196 print '<td>'.$langs->trans("Index").'</td>';
197 print '<td>'.$langs->trans("Default").'</td>';
198 print '<td>'.$langs->trans("Extra").'</td>';
199 print '<td>'.$langs->trans("Privileges").'</td>';
200 print '<td>'.$langs->trans("FieldsLinked").'</td>';
201 print '</tr>';
202
203 // $sql = "DESCRIBE ".$table;
204 $sql = "SHOW FULL COLUMNS IN ".$db->sanitize($table);
205
206 $resql = $db->query($sql);
207 if ($resql) {
208 $num = $db->num_rows($resql);
209 $i = 0;
210 while ($i < $num) {
211 $row = $db->fetch_row($resql);
212
213 print '<tr class="oddeven">';
214
215 // field
216 print "<td>".$row[0]."</td>";
217
218 // type
219 print "<td>";
220 $proptype = $row[1];
221 $pictoType = '';
222 $matches = array();
223 if (preg_match('/^varchar/', $proptype, $matches)) {
224 $pictoType = 'varchar';
225 } elseif (strpos($proptype, 'int') === 0 || strpos($proptype, 'tinyint') === 0 || strpos($proptype, 'bigint') === 0) {
226 $pictoType = 'int';
227 } elseif (strpos($proptype, 'timestamp') === 0) {
228 $pictoType = 'datetime';
229 } elseif (strpos($proptype, 'real') === 0) {
230 $pictoType = 'double';
231 }
232 print(!empty($pictoType) ? getPictoForType($pictoType) : getPictoForType($proptype)).'<span title="'.dol_escape_htmltag($proptype).'">'.dol_escape_htmltag($proptype).'</span>';
233 print "</td>";
234
235 // collation
236 print "<td>".(empty($row[2]) ? '&nbsp;' : $row[2]);
237
238 // Link to convert collation
239 if (isset($row[2])) {
240 print '<br><span class="opacitymedium small">'.$langs->trans("ConvertInto");
241 if (!in_array($row[2], array("utf8_unicode_ci"))) {
242 print ' <a class="reposition" href="dbtable.php?action=convertutf8&table='.urlencode($table).'&field='.urlencode($row[0]).'&token='.newToken().'">utf8</a>';
243 }
244 if (!in_array($row[2], array("utf8mb4_unicode_ci"))) {
245 print ' <a class="reposition" href="dbtable.php?action=convertutf8mb4&table='.urlencode($table).'&field='.urlencode($row[0]).'&token='.newToken().'">utf8mb4</a>';
246 }
247 print '</span>';
248 } else {
249 print '<br>&nbsp;';
250 }
251
252 print "</td>";
253
254 // null
255 print "<td>".$row[3]."</td>";
256 // key
257 print "<td>".(empty($row[4]) ? '' : $row[4])."</td>";
258 // default
259 print "<td>".(empty($row[5]) ? '' : $row[5])."</td>";
260 // extra
261 print "<td>".(empty($row[6]) ? '' : $row[6])."</td>";
262 // privileges
263 print "<td>".(empty($row[7]) ? '' : $row[7])."</td>";
264
265 print "<td>".(isset($link[$row[0]][0]) ? $link[$row[0]][0] : '').".";
266 print(isset($link[$row[0]][1]) ? $link[$row[0]][1] : '')."</td>";
267
268 print '<!-- ALTER TABLE '.$table.' MODIFY '.$row[0].' '.$row[1].' COLLATE utf8mb4_unicode_ci; -->';
269 print '<!-- ALTER TABLE '.$table.' MODIFY '.$row[0].' '.$row[1].' CHARACTER SET utf8mb4; -->';
270 print '</tr>';
271 $i++;
272 }
273 }
274 print '</table>';
275 print '</div>';
276 }
277}
278
279// End of page
280llxFooter();
281$db->close();
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
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:73
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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, $allowothertags=array())
Show picto whatever it's its name (generic function)
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='', $picto='', $textonpictotooltip='')
Show information in HTML for admin users or standard users.
getPictoForType($key, $morecss='')
Return the picto for a data type.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
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.