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

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