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.

artists.class.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 = "Various".($IncludeHyphen?' – ':'');
  109. }
  110. return $link;
  111. } else {
  112. return '';
  113. }
  114. }
  115. /**
  116. * Formats a single artist name.
  117. *
  118. * @param array $Artist an array of the form ('id'=>ID, 'name'=>Name)
  119. * @param boolean $MakeLink If true, links to the artist page.
  120. * @param boolean $Escape If false and $MakeLink is false, returns the unescaped, unadorned artist name.
  121. * @return string Formatted artist name.
  122. */
  123. public static function display_artist($Artist, $MakeLink = true, $Escape = true)
  124. {
  125. if ($MakeLink && !$Escape) {
  126. error('Invalid parameters to Artists::display_artist()');
  127. } elseif ($MakeLink) {
  128. return '<a href="artist.php?id='.$Artist['id'].'" dir="ltr">'.display_str($Artist['name']).'</a>';
  129. } elseif ($Escape) {
  130. return display_str($Artist['name']);
  131. } else {
  132. return $Artist['name'];
  133. }
  134. }
  135. /**
  136. * Deletes an artist and their requests, wiki, and tags.
  137. * Does NOT delete their torrents.
  138. *
  139. * @param int $ArtistID
  140. */
  141. public static function delete_artist($ArtistID)
  142. {
  143. $QueryID = G::$DB->get_query_id();
  144. G::$DB->query("
  145. SELECT Name
  146. FROM artists_group
  147. WHERE ArtistID = ".$ArtistID);
  148. list($Name) = G::$DB->next_record(MYSQLI_NUM, false);
  149. // Delete requests
  150. G::$DB->query("
  151. SELECT RequestID
  152. FROM requests_artists
  153. WHERE ArtistID = $ArtistID
  154. AND ArtistID != 0");
  155. $Requests = G::$DB->to_array();
  156. foreach ($Requests as $Request) {
  157. list($RequestID) = $Request;
  158. G::$DB->query('DELETE FROM requests WHERE ID='.$RequestID);
  159. G::$DB->query('DELETE FROM requests_votes WHERE RequestID='.$RequestID);
  160. G::$DB->query('DELETE FROM requests_tags WHERE RequestID='.$RequestID);
  161. G::$DB->query('DELETE FROM requests_artists WHERE RequestID='.$RequestID);
  162. }
  163. // Delete artist
  164. G::$DB->query('DELETE FROM artists_group WHERE ArtistID='.$ArtistID);
  165. G::$Cache->decrement('stats_artist_count');
  166. // Delete wiki revisions
  167. G::$DB->query('DELETE FROM wiki_artists WHERE PageID='.$ArtistID);
  168. // Delete tags
  169. G::$DB->query('DELETE FROM artists_tags WHERE ArtistID='.$ArtistID);
  170. // Delete artist comments, subscriptions and quote notifications
  171. Comments::delete_page('artist', $ArtistID);
  172. G::$Cache->delete_value('artist_'.$ArtistID);
  173. G::$Cache->delete_value('artist_groups_'.$ArtistID);
  174. // Record in log
  175. if (!empty(G::$LoggedUser['Username'])) {
  176. $Username = G::$LoggedUser['Username'];
  177. } else {
  178. $Username = 'System';
  179. }
  180. Misc::write_log("Artist $ArtistID ($Name) was deleted by $Username");
  181. G::$DB->set_query_id($QueryID);
  182. }
  183. /**
  184. * Remove LRM (left-right-marker) and trims, because people copypaste carelessly.
  185. * If we don't do this, we get seemingly duplicate artist names.
  186. * TODO: make stricter, e.g. on all whitespace characters or Unicode normalisation
  187. *
  188. * @param string $ArtistName
  189. */
  190. public static function normalise_artist_name($ArtistName)
  191. {
  192. // \u200e is &lrm;
  193. $ArtistName = trim($ArtistName);
  194. $ArtistName = preg_replace('/^(\xE2\x80\x8E)+/', '', $ArtistName);
  195. $ArtistName = preg_replace('/(\xE2\x80\x8E)+$/', '', $ArtistName);
  196. return trim(preg_replace('/ +/', ' ', $ArtistName));
  197. }
  198. }