Skip to content

Commit

Permalink
Merge pull request #3 from startersclan/fix/web-admintasks-fix-tools-…
Browse files Browse the repository at this point in the history
…resetdbcollations.php-to-convert-db-character-set-to-utf8mb4-and-collation-to-utf8mb4-unicode-ci

Fix (web/admintasks): Fix `Admin Panel > Reset All DB Collations` tool to convert DB character set to `utf8mb4` and collation to `utf8mb4_unicode_ci`
  • Loading branch information
leojonathanoh authored Mar 4, 2023
2 parents b811c86 + 273a65e commit 1670f52
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 30 deletions.
2 changes: 1 addition & 1 deletion web/pages/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ function message($icon, $msg)
$admintasks['tools_ipstats'] = new AdminTask('Host Statistics', 80, 'tool', 'See which ISPs your players are using.');
$admintasks['tools_optimize'] = new AdminTask('Optimize Database', 100, 'tool', 'This operation tells the MySQL server to clean up the database tables, optimizing them for better performance. It is recommended that you run this at least once a month.');
//$admintasks['tools_synchronize'] = new AdminTask('Synchronize Statistics', 80, 'tool', 'Sychronize all players with the offical global ELstatsNEO banlist with catched VAC cheaters.');
$admintasks['tools_resetdbcollations'] = new AdminTask('Reset All DB Collations to UTF8', 100, 'tool', 'Reset DB Collations to UTF-8 if you receive collation errors after an upgrade from another HLstats(X)-based system.');
$admintasks['tools_resetdbcollations'] = new AdminTask('Reset All DB Collations to utf8mb4', 100, 'tool', 'Reset DB Collations to UTF-8 if you receive collation errors after an upgrade from another HLstats(X)-based system.');

// Sub-Tools
$admintasks['tools_editdetails_player'] = new AdminTask('Edit Player Details', 80, 'subtool', 'Edit a player\'s profile information.');
Expand Down
76 changes: 47 additions & 29 deletions web/pages/admintasks/tools_resetdbcollations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Copyleft (L) 2008-20XX Nicholas Hastings ([email protected])
http://www.hlxcommunity.com
HLstatsX Community Edition is a continuation of
HLstatsX Community Edition is a continuation of
ELstatsNEO - Real-time player and clan rankings and statistics
Copyleft (L) 2008-20XX Malte Bayer ([email protected])
http://ovrsized.neo-soft.org/
Expand All @@ -18,7 +18,7 @@
HLstats - Real-time player and clan rankings and statistics for Half-Life
http://sourceforge.net/projects/hlstats/
Copyright (C) 2001 Simon Garner
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
Expand Down Expand Up @@ -49,19 +49,29 @@

if (isset($_POST['confirm']))
{
$convert_to = 'utf8_general_ci';
$character_set= 'utf8';
// leo - change to utf8mb4_unicode_ci
//$convert_to = 'utf8_general_ci';
$convert_to = 'utf8mb4_unicode_ci';

// leo - change to utf8mb4
//$character_set= 'utf8';
$character_set= 'utf8mb4';

if ($_POST['printonly'] > 0) {
echo '<strong>Run these statements against your MySql database</strong><br><br>';
echo "ALTER DATABASE `".DB_NAME."` DEFAULT CHARACTER SET $character_set COLLATE $convert_to;<br>";
$rs_tables = $db->query('SHOW TABLES') or die(mysql_error());
$rs_tables = $db->query('SHOW TABLES') or die(mysqli_error());
while ($row_tables = $db->fetch_row($rs_tables))
{
$table = mysql_real_escape_string($row_tables[0]);
// leo - remove escape because no use!
//$table = mysqli_real_escape_string($row_tables);
$table = $row_tables[0];
echo "ALTER TABLE `$table` DEFAULT CHARACTER SET $character_set;<br>";
$rs = $db->query("SHOW FULL FIELDS FROM `$table` WHERE collation is not null AND collation <> 'utf8_general_ci'") or die(mysql_error());
while ($row=mysql_fetch_assoc($rs))

// leo - remove hardcoded collation, use variable
//$rs = $db->query("SHOW FULL FIELDS FROM `$table` WHERE collation is not null AND collation <> 'utf8_general_ci'") or die(mysqli_error());
$rs = $db->query("SHOW FULL FIELDS FROM `$table` WHERE collation is not null AND collation <> '$convert_to'") or die(mysqli_error());
while ($row=mysqli_fetch_assoc($rs))
{
if ($row['Collation'] == '')
continue;
Expand All @@ -74,29 +84,34 @@
else if ( $row['Default'] === NULL )
$default = ' DEFAULT NULL';
else if ($row['Default']!='')
$default = " DEFAULT '".mysql_real_escape_string($row['Default'])."'";
$default = " DEFAULT '".mysqli_real_escape_string($row['Default'])."'";
else
$default = '';
$field = mysql_real_escape_string($row['Field']);

$field = mysqli_real_escape_string($row['Field']);
echo "ALTER TABLE `$table` CHANGE `$field` `$field` $row[Type] CHARACTER SET $character_set COLLATE $convert_to $nullable $default;<br>";
}
}
} else {
echo "Converting database, table, and row collations to utf8:<ul>\n";
set_time_limit(0);
// leo - remove hardcoded collation, use variable
//echo "Converting database, table, and row collations to utf8:<ul>\n";
echo "Converting database, table, and row collations to $character_set:<ul>\n";
set_time_limit(0);
echo '<li>Changing '.DB_NAME.' default character set and collation... ';
$db->query("ALTER DATABASE `".DB_NAME."` DEFAULT CHARACTER SET $character_set COLLATE $convert_to;")or die(mysql_error());
//$rs = $db->query("SHOW FULL FIELDS FROM `$table` WHERE collation is not null AND collation <> 'utf8_general_ci'") or die(mysqli_error());
$rs = $db->query("SHOW FULL FIELDS FROM `$table` WHERE collation is not null AND collation <> '$convert_to'") or die(mysqli_error());
echo 'OK';
$rs_tables = $db->query('SHOW TABLES') or die(mysql_error());
$rs_tables = $db->query('SHOW TABLES') or die(mysqli_error());
while ($row_tables = $db->fetch_row($rs_tables))
{
$table = mysql_real_escape_string($row_tables[0]);
// leo - remove escape because no use!
//$table = mysqli_real_escape_string($row_tables);
$table = $row_tables[0];
echo "<li>Converting Table: $table ... ";
$db->query("ALTER TABLE `$table` DEFAULT CHARACTER SET $character_set;");
echo 'OK';
$rs = $db->query("SHOW FULL FIELDS FROM `$table` WHERE collation is not null AND collation <> 'utf8_general_ci'") or die(mysql_error());
while ($row=mysql_fetch_assoc($rs))
$rs = $db->query("SHOW FULL FIELDS FROM `$table` WHERE collation is not null AND collation <> 'utf8_general_ci'") or die(mysqli_error());
while ($row=mysqli_fetch_assoc($rs))
{
if ($row['Collation'] == '')
continue;
Expand All @@ -109,32 +124,36 @@
else if ( $row['Default'] === NULL )
$default = ' DEFAULT NULL';
else if ($row['Default']!='')
$default = " DEFAULT '".mysql_real_escape_string($row['Default'])."'";
// leo - remove escape because no use!
//$default = " DEFAULT '".mysqli_real_escape_string($row['Default'])."'";
$default = " DEFAULT '".$row['Default']."'";
else
$default = '';

$field = mysql_real_escape_string($row['Field']);

// leo - remove escape because no use!
//$field = mysqli_real_escape_string($row['Field']);
$field = $row['Field'];
echo "<li>Converting Table: $table Column: $field ... ";
$db->query("ALTER TABLE `$table` CHANGE `$field` `$field` $row[Type] CHARACTER SET $character_set COLLATE $convert_to $nullable $default;");
echo 'OK';
}
}
echo '</ul>';

echo 'Done.<p>';
}

} else {
?>

?>

<form method="POST">
<table width="60%" align="center" border=0 cellspacing=0 cellpadding=0 class="border">

<tr>
<td>
<table width="100%" border=0 cellspacing=1 cellpadding=10>

<tr class="bg1">
<td class="fNormal">

Expand All @@ -149,7 +168,7 @@
<center><input type="submit" value="Generate commands and do the above"></center>
</td>
</tr>

</table></td>
</tr>

Expand All @@ -158,5 +177,4 @@

<?php
}
?>

?>

0 comments on commit 1670f52

Please sign in to comment.