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

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