Oppaitime's version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

merge.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?
  2. if (!check_perms('torrents_edit')) {
  3. error(403);
  4. }
  5. $GroupID = $_POST['groupid'];
  6. $OldGroupID = $GroupID;
  7. $NewGroupID = db_string($_POST['targetgroupid']);
  8. if (!$GroupID || !is_number($GroupID)) {
  9. error(404);
  10. }
  11. if (!$NewGroupID || !is_number($NewGroupID)) {
  12. error(404);
  13. }
  14. if ($NewGroupID == $GroupID) {
  15. error('Old group ID is the same as new group ID!');
  16. }
  17. $DB->query("
  18. SELECT CategoryID, Name
  19. FROM torrents_group
  20. WHERE ID = '$NewGroupID'");
  21. if (!$DB->has_results()) {
  22. error('Target group does not exist.');
  23. }
  24. list($CategoryID, $NewName) = $DB->next_record();
  25. /*
  26. if ($Categories[$CategoryID - 1] != 'Music') {
  27. error('Only music groups can be merged.');
  28. }
  29. */
  30. $DB->query("
  31. SELECT Name
  32. FROM torrents_group
  33. WHERE ID = $GroupID");
  34. list($Name) = $DB->next_record();
  35. // Everything is legit, let's just confim they're not retarded
  36. if (empty($_POST['confirm'])) {
  37. $Artists = Artists::get_artists(array($GroupID, $NewGroupID));
  38. View::show_header();
  39. ?>
  40. <div class="center thin">
  41. <div class="header">
  42. <h2>Merge Confirm!</h2>
  43. </div>
  44. <div class="box pad">
  45. <form class="confirm_form" name="torrent_group" action="torrents.php" method="post">
  46. <input type="hidden" name="action" value="merge" />
  47. <input type="hidden" name="auth" value="<?=$LoggedUser['AuthKey']?>" />
  48. <input type="hidden" name="confirm" value="true" />
  49. <input type="hidden" name="groupid" value="<?=$GroupID?>" />
  50. <input type="hidden" name="targetgroupid" value="<?=$NewGroupID?>" />
  51. <h3>You are attempting to merge the group:</h3>
  52. <ul>
  53. <li><?= Artists::display_artists($Artists[$GroupID], true, false)?> - <a href="torrents.php?id=<?=$GroupID?>"><?=$Name?></a></li>
  54. </ul>
  55. <h3>Into the group:</h3>
  56. <ul>
  57. <li><?= Artists::display_artists($Artists[$NewGroupID], true, false)?> - <a href="torrents.php?id=<?=$NewGroupID?>"><?=$NewName?></a></li>
  58. </ul>
  59. <input type="submit" value="Confirm" />
  60. </form>
  61. </div>
  62. </div>
  63. <?
  64. View::show_footer();
  65. } else {
  66. authorize();
  67. // Votes ninjutsu. This is so annoyingly complicated.
  68. // 1. Get a list of everybody who voted on the old group and clear their cache keys
  69. $DB->query("
  70. SELECT UserID
  71. FROM users_votes
  72. WHERE GroupID = '$GroupID'");
  73. while (list($UserID) = $DB->next_record()) {
  74. $Cache->delete_value("voted_albums_$UserID");
  75. }
  76. // 2. Update the existing votes where possible, clear out the duplicates left by key
  77. // conflicts, and update the torrents_votes table
  78. $DB->query("
  79. UPDATE IGNORE users_votes
  80. SET GroupID = '$NewGroupID'
  81. WHERE GroupID = '$GroupID'");
  82. $DB->query("
  83. DELETE FROM users_votes
  84. WHERE GroupID = '$GroupID'");
  85. $DB->query("
  86. INSERT INTO torrents_votes (GroupID, Ups, Total, Score)
  87. SELECT $NewGroupID, UpVotes, TotalVotes, VoteScore
  88. FROM (
  89. SELECT
  90. IFNULL(SUM(IF(Type = 'Up', 1, 0)), 0) As UpVotes,
  91. COUNT(1) AS TotalVotes,
  92. binomial_ci(IFNULL(SUM(IF(Type = 'Up', 1, 0)), 0), COUNT(1)) AS VoteScore
  93. FROM users_votes
  94. WHERE GroupID = $NewGroupID
  95. GROUP BY GroupID
  96. ) AS a
  97. ON DUPLICATE KEY UPDATE
  98. Ups = a.UpVotes,
  99. Total = a.TotalVotes,
  100. Score = a.VoteScore;");
  101. // 3. Clear the votes_pairs keys!
  102. $DB->query("
  103. SELECT v2.GroupID
  104. FROM users_votes AS v1
  105. INNER JOIN users_votes AS v2 USING (UserID)
  106. WHERE (v1.Type = 'Up' OR v2.Type = 'Up')
  107. AND (v1.GroupID IN($GroupID, $NewGroupID))
  108. AND (v2.GroupID NOT IN($GroupID, $NewGroupID));");
  109. while (list($CacheGroupID) = $DB->next_record()) {
  110. $Cache->delete_value("vote_pairs_$CacheGroupID");
  111. }
  112. // 4. Clear the new groups vote keys
  113. $Cache->delete_value("votes_$NewGroupID");
  114. $DB->query("
  115. UPDATE torrents
  116. SET GroupID = '$NewGroupID'
  117. WHERE GroupID = '$GroupID'");
  118. $DB->query("
  119. UPDATE wiki_torrents
  120. SET PageID = '$NewGroupID'
  121. WHERE PageID = '$GroupID'");
  122. //Comments
  123. Comments::merge('torrents', $OldGroupID, $NewGroupID);
  124. //Collages
  125. $DB->query("
  126. SELECT CollageID
  127. FROM collages_torrents
  128. WHERE GroupID = '$OldGroupID'"); // Select all collages that contain edited group
  129. while (list($CollageID) = $DB->next_record()) {
  130. $DB->query("
  131. UPDATE IGNORE collages_torrents
  132. SET GroupID = '$NewGroupID'
  133. WHERE GroupID = '$OldGroupID'
  134. AND CollageID = '$CollageID'"); // Change collage group ID to new ID
  135. $DB->query("
  136. DELETE FROM collages_torrents
  137. WHERE GroupID = '$OldGroupID'
  138. AND CollageID = '$CollageID'");
  139. $Cache->delete_value("collage_$CollageID");
  140. }
  141. $Cache->delete_value("torrent_collages_$NewGroupID");
  142. $Cache->delete_value("torrent_collages_personal_$NewGroupID");
  143. // Requests
  144. $DB->query("
  145. SELECT ID
  146. FROM requests
  147. WHERE GroupID = '$OldGroupID'");
  148. $Requests = $DB->collect('ID');
  149. $DB->query("
  150. UPDATE requests
  151. SET GroupID = '$NewGroupID'
  152. WHERE GroupID = '$OldGroupID'");
  153. foreach ($Requests as $RequestID) {
  154. $Cache->delete_value("request_$RequestID");
  155. }
  156. $Cache->delete_value('requests_group_'.$NewGroupID);
  157. Torrents::delete_group($GroupID);
  158. Torrents::write_group_log($NewGroupID, 0, $LoggedUser['ID'], "Merged Group $GroupID ($Name) to $NewGroupID ($NewName)", 0);
  159. $DB->query("
  160. UPDATE group_log
  161. SET GroupID = $NewGroupID
  162. WHERE GroupID = $GroupID");
  163. $GroupID = $NewGroupID;
  164. $DB->query("
  165. SELECT ID
  166. FROM torrents
  167. WHERE GroupID = '$OldGroupID'");
  168. while (list($TorrentID) = $DB->next_record()) {
  169. $Cache->delete_value("torrent_download_$TorrentID");
  170. }
  171. $Cache->delete_value("torrents_details_$GroupID");
  172. $Cache->delete_value("groups_artists_$GroupID");
  173. Torrents::update_hash($GroupID);
  174. header("Location: torrents.php?id=" . $GroupID);
  175. }
  176. ?>