admin function added: clean database

This commit is contained in:
Matt Nadareski
2016-02-29 01:45:02 -08:00
parent 08462da913
commit bfcf8b2f6e
2 changed files with 66 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
<?php
/*
Clean duplicates and orphaned checksums from the database
*/
//http://stackoverflow.com/questions/18932/how-can-i-remove-duplicate-rows
// Delete dupes from games
$query = "DELETE games
FROM games LEFT OUTER JOIN (
SELECT MIN(id) as id, system, name, parent, source
FROM games
GROUP BY system, name, parent, source
) as KeepRows ON
games.id = KeepRows.id
WHERE KeepRows.id IS NULL";
$result = mysqli_query($link, $query);
if (gettype($result) == "boolean" && $result)
{
echo "Deleting game dupes succeeded!<br/>\n";
}
else
{
echo "Deleting game dupes failed!<br/>\n".mysqli_error($link);
}
// Delete dupes from files
$query = "DELETE files
FROM files LEFT OUTER JOIN (
SELECT MIN(id) as id, setid, name, type, lastupdated
FROM files
GROUP BY setid, name, type, lastupdated
) as KeepRows ON
files.id = KeepRows.id
WHERE KeepRows.id IS NULL";
if (gettype($result) == "boolean" && $result)
{
echo "Deleting file dupes succeeded!<br/>\n";
}
else
{
echo "Deleting file dupes failed!<br/>\n".mysqli_error($link);
}
// Delete orphaned checksums
$query = "DELETE FROM checksums
WHERE NOT EXISTS (
SELECT *
FROM files
WHERE checksums.file = files.id)";
$query = "SELECT * FROM sources";
$result = mysqli_query($link, $query);
if (gettype($result) == "boolean" && $result)
{
echo "Deleting checksum orphans succeeded!<br/>\n";
}
else
{
echo "Deleting checksum orphans failed!<br/>\n".mysqli_error($link);
}
?>

View File

@@ -44,6 +44,7 @@ Administrative Functions Homepage
<li><a href='?page=onlinecheck'>Check for new files online</a></li>
<li><a href='?page=getmamenames'>Generate an array of names for all MAME softlists</a></li>
<li><a href='?page=parsenointro'>Generate an array of names for all No-Intro sources</a></li>
<li><a href='?page=clean'>Clean the database of dupes and orphans</a></li>
</ol>";
}