<?PHP
$dsn = 'mysql:dbname=theforgottenserver;host=127.0.0.1';
$user = 'root';
$password = 'secretPassword';
try
{
$SQL = new PDO($dsn, $user, $password);
$result = $SQL->query('SELECT unitedItems.serial, COUNT(1) AS duplicatesCount FROM (SELECT substring(CONVERT(`attributes` USING latin1) , LOCATE(\'serial\', CONVERT(`attributes` USING latin1))+11, 29) AS serial FROM `player_items` WHERE LOCATE(\'serial\', CONVERT(`attributes` USING latin1)) != 0
UNION ALL
SELECT substring(CONVERT(`attributes` USING latin1) , LOCATE(\'serial\', CONVERT(`attributes` USING latin1))+11, 29) AS serial FROM `player_depotitems` WHERE LOCATE(\'serial\', CONVERT(`attributes` USING latin1)) != 0
UNION ALL
SELECT substring(CONVERT(`attributes` USING latin1) , LOCATE(\'serial\', CONVERT(`attributes` USING latin1))+11, 29) AS serial FROM `tile_items` WHERE LOCATE(\'serial\', CONVERT(`attributes` USING latin1)) != 0) unitedItems GROUP BY unitedItems.serial HAVING COUNT(1) > 1;')->fetchAll();
if(count($result) > 0) // there are some duplicated
{
$duplicatedSerials = array();
$duplicatedSerialsClear = array();
$duplicatedCount = 0;
foreach($result as $row)
{
$duplicatedSerials[] = $SQL->quote($row['serial']);
$duplicatedSerialsClear[] = $row['serial'];
$duplicatedCount += $row['duplicatesCount'];
}
$duplicatedString = implode(', ', $duplicatedSerials);
$SQL->query('DELETE FROM `player_items` WHERE LOCATE(\'serial\', CONVERT(`attributes` USING latin1)) != 0 AND substring(CONVERT(`attributes` USING latin1) , LOCATE(\'serial\', CONVERT(`attributes` USING latin1))+11, 29) IN (' . $duplicatedString . ');');
$SQL->query('DELETE FROM `player_depotitems` WHERE LOCATE(\'serial\', CONVERT(`attributes` USING latin1)) != 0 AND substring(CONVERT(`attributes` USING latin1) , LOCATE(\'serial\', CONVERT(`attributes` USING latin1))+11, 29) IN (' . $duplicatedString . ');');
$SQL->query('DELETE FROM `tile_items` WHERE LOCATE(\'serial\', CONVERT(`attributes` USING latin1)) != 0 AND substring(CONVERT(`attributes` USING latin1) , LOCATE(\'serial\', CONVERT(`attributes` USING latin1))+11, 29) IN (' . $duplicatedString . ');');
file_put_contents('data/log/duplicates.txt', "\n" . date(DATE_RFC822) . " - " . $duplicatedCount . " duplicated items found. Serials:\n" . implode("\n", $duplicatedSerialsClear) . "\n", FILE_APPEND);
}
else
{
file_put_contents('data/log/duplicates.txt', "\n" . date(DATE_RFC822) . " - 0 duplicated items found.\n", FILE_APPEND);
}
}
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}
?>