query("SELECT Name FROM artists_group WHERE ArtistID='$ArtistID'"); if($DB->record_count()==0) { error(404); } list($OldName) = $DB->next_record(MYSQLI_NUM, false); if($OldName == $NewName) { error('The new name is identical to the old name.'); } $DB->query("SELECT AliasID FROM artists_alias WHERE Name='".db_string($OldName)."' AND ArtistID='$ArtistID'"); list($OldAliasID) = $DB->next_record(MYSQLI_NUM, false); if(!$OldAliasID) { error('Could not find old alias ID'); } $DB->query("SELECT AliasID, ArtistID FROM artists_alias WHERE name LIKE '".db_string($NewName, true)."'"); list($TargetAliasID, $TargetArtistID) = $DB->next_record(MYSQLI_NUM, false); if(!$TargetAliasID || $TargetAliasID==$OldAliasID) { // no merge, just rename $DB->query("INSERT INTO artists_alias (ArtistID, Name, Redirect, UserID) VALUES ($ArtistID, '".db_string($NewName)."', '0', '$LoggedUser[ID]')"); $TargetAliasID = $DB->inserted_id(); $DB->query("UPDATE artists_alias SET Redirect='$TargetAliasID' WHERE AliasID='$OldAliasID'"); $DB->query("UPDATE artists_group SET Name='".db_string($NewName)."' WHERE ArtistID='$ArtistID'"); $DB->query("SELECT GroupID FROM torrents_artists WHERE AliasID='$OldAliasID'"); $Groups = $DB->collect('GroupID'); $DB->query("UPDATE IGNORE torrents_artists SET AliasID='$TargetAliasID' WHERE AliasID='$OldAliasID'"); $DB->query("DELETE FROM torrents_artists WHERE AliasID='$OldAliasID'"); if(!empty($Groups)) { foreach($Groups as $GroupID) { $Cache->delete_value('groups_artists_'.$GroupID); // Delete group artist cache update_hash($GroupID); } } $TargetArtistID = $ArtistID; } else { // Merge stuff $DB->query("UPDATE artists_alias SET Redirect='$TargetAliasID',ArtistID='$TargetArtistID' WHERE AliasID='$OldAliasID'"); $DB->query("UPDATE artists_alias SET Redirect='0' WHERE AliasID='$TargetAliasID'"); if($ArtistID != $TargetArtistID) { $DB->query("UPDATE artists_alias SET ArtistID='$TargetArtistID' WHERE ArtistID='$ArtistID'"); $DB->query("DELETE FROM artists_group WHERE ArtistID='$ArtistID'"); } else { $DB->query("UPDATE artists_group SET Name='".db_string($NewName)."' WHERE ArtistID='$ArtistID'"); } $DB->query("SELECT GroupID FROM torrents_artists WHERE AliasID='$OldAliasID'"); $Groups = $DB->collect('GroupID'); $DB->query("UPDATE IGNORE torrents_artists SET AliasID='$TargetAliasID',ArtistID='$TargetArtistID' WHERE AliasID='$OldAliasID'"); $DB->query("DELETE FROM torrents_artists WHERE AliasID='$OldAliasID'"); if(!empty($Groups)) { foreach($Groups as $GroupID) { $Cache->delete_value('groups_artists_'.$GroupID); update_hash($GroupID); } } if($ArtistID != $TargetArtistID) { $DB->query("SELECT GroupID FROM torrents_artists WHERE ArtistID='$ArtistID'"); $Groups = $DB->collect('GroupID'); $DB->query("UPDATE IGNORE torrents_artists SET ArtistID='$TargetArtistID' WHERE ArtistID='$ArtistID'"); $DB->query("DELETE FROM torrents_artists WHERE ArtistID='$ArtistID'"); if(!empty($Groups)) { foreach($Groups as $GroupID) { $Cache->delete_value('groups_artists_'.$GroupID); update_hash($GroupID); } } } } $Cache->delete_value('artist_'.$ArtistID); // Clear torrent caches $DB->query("SELECT GroupID FROM torrents_artists WHERE ArtistID='$ArtistID'"); while(list($GroupID) = $DB->next_record()) { $Cache->delete_value('torrents_details_'.$GroupID); } $Cache->delete_value('artist_'.$ArtistID); $Cache->delete_value('artist_'.$TargetArtistID); header('Location: artist.php?id='.$TargetArtistID); ?>