BioTorrents.de’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.

artists.class.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. class Artists
  3. {
  4. /**
  5. * Given an array of GroupIDs, return their associated artists.
  6. *
  7. * @param array $GroupIDs
  8. * @return an array of the following form:
  9. * GroupID => {
  10. * [ArtistType] => {
  11. * id, name, aliasid
  12. * }
  13. * }
  14. * ArtistType is an int. It can be:
  15. * 1 => Main artist
  16. * 2 => Guest artist
  17. * 4 => Composer
  18. * 5 => Conductor
  19. * 6 => DJ
  20. */
  21. public static function get_artists($GroupIDs)
  22. {
  23. $Results = [];
  24. $DBs = [];
  25. foreach ($GroupIDs as $GroupID) {
  26. if (!is_number($GroupID)) {
  27. continue;
  28. }
  29. $Artists = G::$Cache->get_value('groups_artists_'.$GroupID);
  30. if (is_array($Artists)) {
  31. $Results[$GroupID] = $Artists;
  32. } else {
  33. $DBs[] = $GroupID;
  34. }
  35. }
  36. if (count($DBs) > 0) {
  37. $IDs = implode(',', $DBs);
  38. if (empty($IDs)) {
  39. $IDs = "null";
  40. }
  41. $QueryID = G::$DB->get_query_id();
  42. G::$DB->query("
  43. SELECT ta.GroupID,
  44. ta.ArtistID,
  45. ag.Name
  46. FROM torrents_artists AS ta
  47. JOIN artists_group AS ag ON ta.ArtistID = ag.ArtistID
  48. WHERE ta.GroupID IN ($IDs)
  49. ORDER BY ta.GroupID ASC,
  50. ag.Name ASC;");
  51. while (list($GroupID, $ArtistID, $ArtistName) = G::$DB->next_record(MYSQLI_BOTH, false)) {
  52. $Results[$GroupID][] = array('id' => $ArtistID, 'name' => $ArtistName);
  53. $New[$GroupID][] = array('id' => $ArtistID, 'name' => $ArtistName);
  54. }
  55. G::$DB->set_query_id($QueryID);
  56. foreach ($DBs as $GroupID) {
  57. if (isset($New[$GroupID])) {
  58. G::$Cache->cache_value('groups_artists_'.$GroupID, $New[$GroupID]);
  59. } else {
  60. G::$Cache->cache_value('groups_artists_'.$GroupID, []);
  61. }
  62. }
  63. $Missing = array_diff($GroupIDs, array_keys($Results));
  64. if (!empty($Missing)) {
  65. $Results += array_fill_keys($Missing, []);
  66. }
  67. }
  68. return $Results;
  69. }
  70. /**
  71. * Convenience function for get_artists, when you just need one group.
  72. *
  73. * @param int $GroupID
  74. * @return array - see get_artists
  75. */
  76. public static function get_artist($GroupID)
  77. {
  78. $Results = Artists::get_artists(array($GroupID));
  79. return $Results[$GroupID];
  80. }
  81. /**
  82. * Format an array of artists for display.
  83. * todo: Revisit the logic of this, see if we can helper-function the copypasta.
  84. *
  85. * @param array Artists an array of the form output by get_artists
  86. * @param boolean $MakeLink if true, the artists will be links, if false, they will be text.
  87. * @param boolean $IncludeHyphen if true, appends " - " to the end.
  88. * @param $Escape if true, output will be escaped. Think carefully before setting it false.
  89. */
  90. public static function display_artists($Artists, $MakeLink = true, $IncludeHyphen = true, $Escape = true)
  91. {
  92. if (!empty($Artists)) {
  93. $ampersand = ($Escape) ? ' &amp; ' : ' & ';
  94. $link = '';
  95. switch (count($Artists)) {
  96. case 0:
  97. break;
  98. case 3:
  99. $link .= Artists::display_artist($Artists[2], $MakeLink, $Escape). ", ";
  100. // no break
  101. case 2:
  102. $link .= Artists::display_artist($Artists[1], $MakeLink, $Escape). ", ";
  103. // no break
  104. case 1:
  105. $link .= Artists::display_artist($Artists[0], $MakeLink, $Escape).($IncludeHyphen? ' – ':'');
  106. break;
  107. default:
  108. $link = Artists::display_artist($Artists[0], $MakeLink, $Escape).' et al.'.($IncludeHyphen? ' – ':'');
  109. #$link = "Various".($IncludeHyphen?' – ':'');
  110. }
  111. return $link;
  112. } else {
  113. return '';
  114. }
  115. }
  116. /**
  117. * Formats a single artist name.
  118. *
  119. * @param array $Artist an array of the form ('id'=>ID, 'name'=>Name)
  120. * @param boolean $MakeLink If true, links to the artist page.
  121. * @param boolean $Escape If false and $MakeLink is false, returns the unescaped, unadorned artist name.
  122. * @return string Formatted artist name.
  123. */
  124. public static function display_artist($Artist, $MakeLink = true, $Escape = true)
  125. {
  126. if ($MakeLink && !$Escape) {
  127. error('Invalid parameters to Artists::display_artist()');
  128. } elseif ($MakeLink) {
  129. return '<a href="artist.php?id='.$Artist['id'].'" dir="ltr">'.display_str($Artist['name']).'</a>';
  130. } elseif ($Escape) {
  131. return display_str($Artist['name']);
  132. } else {
  133. return $Artist['name'];
  134. }
  135. }
  136. /**
  137. * Deletes an artist and their requests, wiki, and tags.
  138. * Does NOT delete their torrents.
  139. *
  140. * @param int $ArtistID
  141. */
  142. public static function delete_artist($ArtistID)
  143. {
  144. $QueryID = G::$DB->get_query_id();
  145. G::$DB->query("
  146. SELECT Name
  147. FROM artists_group
  148. WHERE ArtistID = ".$ArtistID);
  149. list($Name) = G::$DB->next_record(MYSQLI_NUM, false);
  150. // Delete requests
  151. G::$DB->query("
  152. SELECT RequestID
  153. FROM requests_artists
  154. WHERE ArtistID = $ArtistID
  155. AND ArtistID != 0");
  156. $Requests = G::$DB->to_array();
  157. foreach ($Requests as $Request) {
  158. list($RequestID) = $Request;
  159. G::$DB->query('DELETE FROM requests WHERE ID='.$RequestID);
  160. G::$DB->query('DELETE FROM requests_votes WHERE RequestID='.$RequestID);
  161. G::$DB->query('DELETE FROM requests_tags WHERE RequestID='.$RequestID);
  162. G::$DB->query('DELETE FROM requests_artists WHERE RequestID='.$RequestID);
  163. }
  164. // Delete artist
  165. G::$DB->query('DELETE FROM artists_group WHERE ArtistID='.$ArtistID);
  166. G::$Cache->decrement('stats_artist_count');
  167. // Delete wiki revisions
  168. G::$DB->query('DELETE FROM wiki_artists WHERE PageID='.$ArtistID);
  169. // Delete tags
  170. G::$DB->query('DELETE FROM artists_tags WHERE ArtistID='.$ArtistID);
  171. // Delete artist comments, subscriptions and quote notifications
  172. Comments::delete_page('artist', $ArtistID);
  173. G::$Cache->delete_value('artist_'.$ArtistID);
  174. G::$Cache->delete_value('artist_groups_'.$ArtistID);
  175. // Record in log
  176. if (!empty(G::$LoggedUser['Username'])) {
  177. $Username = G::$LoggedUser['Username'];
  178. } else {
  179. $Username = 'System';
  180. }
  181. Misc::write_log("Artist $ArtistID ($Name) was deleted by $Username");
  182. G::$DB->set_query_id($QueryID);
  183. }
  184. /**
  185. * Remove LRM (left-right-marker) and trims, because people copypaste carelessly.
  186. * If we don't do this, we get seemingly duplicate artist names.
  187. * todo: make stricter, e.g. on all whitespace characters or Unicode normalisation
  188. *
  189. * @param string $ArtistName
  190. */
  191. public static function normalise_artist_name($ArtistName)
  192. {
  193. // \u200e is &lrm;
  194. $ArtistName = trim($ArtistName);
  195. $ArtistName = preg_replace('/^(\xE2\x80\x8E)+/', '', $ArtistName);
  196. $ArtistName = preg_replace('/(\xE2\x80\x8E)+$/', '', $ArtistName);
  197. return trim(preg_replace('/ +/', ' ', $ArtistName));
  198. }
  199. }