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 6.5KB

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