Gazelle/classes/artists.class.php

282 lines
8.7 KiB
PHP
Raw Normal View History

2012-10-11 08:00:15 +00:00
<?
class Artists {
/**
* Given an array of GroupIDs, return their associated artists.
*
* @param array $GroupIDs
* @return an array of the following form:
* GroupID => {
* [ArtistType] => {
* id, name, aliasid
* }
* }
* ArtistType is an int. It can be:
* 1 => Main artist
* 2 => Guest artist
* 4 => Composer
* 5 => Conductor
* 6 => DJ
*/
public static function get_artists($GroupIDs) {
global $Cache, $DB;
$Results = array();
$DBs = array();
2013-04-18 08:00:54 +00:00
foreach ($GroupIDs as $GroupID) {
if (!is_number($GroupID)) {
2012-10-11 08:00:15 +00:00
continue;
}
$Artists = $Cache->get_value('groups_artists_'.$GroupID);
2013-04-18 08:00:54 +00:00
if (is_array($Artists)) {
2012-10-11 08:00:15 +00:00
$Results[$GroupID] = $Artists;
} else {
$DBs[] = $GroupID;
}
}
2013-04-18 08:00:54 +00:00
if (count($DBs) > 0) {
2012-10-11 08:00:15 +00:00
$IDs = implode(',', $DBs);
2013-04-18 08:00:54 +00:00
if (empty($IDs)) {
2012-10-11 08:00:15 +00:00
$IDs = "null";
}
2013-06-06 08:01:03 +00:00
$DB->query("
SELECT ta.GroupID,
2012-10-11 08:00:15 +00:00
ta.ArtistID,
aa.Name,
ta.Importance,
ta.AliasID
FROM torrents_artists AS ta
JOIN artists_alias AS aa ON ta.AliasID = aa.AliasID
WHERE ta.GroupID IN ($IDs)
ORDER BY ta.GroupID ASC,
ta.Importance ASC,
aa.Name ASC;");
2013-06-18 08:00:48 +00:00
while (list($GroupID, $ArtistID, $ArtistName, $ArtistImportance, $AliasID) = $DB->next_record(MYSQLI_BOTH, false)) {
2012-10-11 08:00:15 +00:00
$Results[$GroupID][$ArtistImportance][] = array('id' => $ArtistID, 'name' => $ArtistName, 'aliasid' => $AliasID);
$New[$GroupID][$ArtistImportance][] = array('id' => $ArtistID, 'name' => $ArtistName, 'aliasid' => $AliasID);
}
2013-04-18 08:00:54 +00:00
foreach ($DBs as $GroupID) {
if (isset($New[$GroupID])) {
2012-10-11 08:00:15 +00:00
$Cache->cache_value('groups_artists_'.$GroupID, $New[$GroupID]);
}
else {
$Cache->cache_value('groups_artists_'.$GroupID, array());
}
}
$Missing = array_diff($GroupIDs, array_keys($Results));
2013-04-18 08:00:54 +00:00
if (!empty($Missing)) {
2012-10-11 08:00:15 +00:00
$Results += array_fill_keys($Missing, array());
}
}
return $Results;
}
/**
* Convenience function for get_artists, when you just need one group.
*
* @param int $GroupID
* @return array - see get_artists
*/
public static function get_artist($GroupID) {
$Results = Artists::get_artists(array($GroupID));
return $Results[$GroupID];
}
/**
* Format an array of artists for display.
* TODO: Revisit the logic of this, see if we can helper-function the copypasta.
*
* @param array Artists an array of the form output by get_artists
* @param boolean $MakeLink if true, the artists will be links, if false, they will be text.
* @param boolean $IncludeHyphen if true, appends " - " to the end.
* @param $Escape if true, output will be escaped. Think carefully before setting it false.
*/
public static function display_artists($Artists, $MakeLink = true, $IncludeHyphen = true, $Escape = true) {
2013-04-18 08:00:54 +00:00
if (!empty($Artists)) {
2012-10-11 08:00:15 +00:00
$ampersand = ($Escape) ? ' &amp; ' : ' & ';
$link = '';
2013-04-18 08:00:54 +00:00
$MainArtists = isset($Artists[1]) ? $Artists[1] : null;
$Guests = isset($Artists[2]) ? $Artists[2] : null;
$Composers = isset($Artists[4]) ? $Artists[4] : null;
$Conductors = isset($Artists[5]) ? $Artists[5] : null;
$DJs = isset($Artists[6]) ? $Artists[6] : null;
2012-10-11 08:00:15 +00:00
if ((count($MainArtists) + count($Conductors) + count($DJs) == 0) && (count($Composers) == 0)) {
return '';
}
// Various Composers is not needed and is ugly and should die
2013-04-18 08:00:54 +00:00
switch (count($Composers)) {
2012-10-11 08:00:15 +00:00
case 0:
break;
case 1:
$link .= Artists::display_artist($Composers[0], $MakeLink, $Escape);
break;
case 2:
$link .= Artists::display_artist($Composers[0], $MakeLink, $Escape).$ampersand.Artists::display_artist($Composers[1], $MakeLink, $Escape);
break;
}
if ((count($Composers) > 0) && (count($Composers) < 3) && (count($MainArtists) > 0)) {
$link .= ' performed by ';
}
2012-10-16 08:00:18 +00:00
$ComposerStr = $link;
2012-10-11 08:00:15 +00:00
2013-04-18 08:00:54 +00:00
switch (count($MainArtists)) {
2012-10-11 08:00:15 +00:00
case 0:
break;
case 1:
$link .= Artists::display_artist($MainArtists[0], $MakeLink, $Escape);
break;
case 2:
$link .= Artists::display_artist($MainArtists[0], $MakeLink, $Escape).$ampersand.Artists::display_artist($MainArtists[1], $MakeLink, $Escape);
break;
default:
$link .= 'Various Artists';
}
2013-04-18 08:00:54 +00:00
/*if (!empty($Guests) && (count($MainArtists) + count($Composers) > 0) && (count($MainArtists) + count($Composers) + count($Conductors) < 3)) {
switch (count($Guests)) {
2012-10-11 08:00:15 +00:00
case 1:
$link .= ' with '.Artists::display_artist($Guests[0], $MakeLink, $Escape);
break;
case 2:
$link .= ' with '.Artists::display_artist($Guests[0], $MakeLink, $Escape).$ampersand.Artists::display_artist($Guests[1], $MakeLink, $Escape);
break;
}
}*/
if ((count($Conductors) > 0) && (count($MainArtists) + count($Composers) > 0) && (count($Composers) < 3 || count($MainArtists) > 0)) {
$link .= ' under ';
}
2013-04-18 08:00:54 +00:00
switch (count($Conductors)) {
2012-10-11 08:00:15 +00:00
case 0:
break;
case 1:
$link .= Artists::display_artist($Conductors[0], $MakeLink, $Escape);
break;
case 2:
$link .= Artists::display_artist($Conductors[0], $MakeLink, $Escape).$ampersand.Artists::display_artist($Conductors[1], $MakeLink, $Escape);
break;
default:
$link .= ' Various Conductors';
}
if ((count($Composers) > 0) && (count($MainArtists) + count($Conductors) > 3) && (count($MainArtists) > 1) && (count($Conductors) > 1)) {
$link = $ComposerStr . 'Various Artists';
} elseif ((count($Composers) > 2) && (count($MainArtists) + count($Conductors) == 0)) {
$link = 'Various Composers';
}
// DJs override everything else
2013-04-18 08:00:54 +00:00
switch (count($DJs)) {
2012-10-11 08:00:15 +00:00
case 0:
break;
case 1:
$link = Artists::display_artist($DJs[0], $MakeLink, $Escape);
break;
case 2:
$link = Artists::display_artist($DJs[0], $MakeLink, $Escape).$ampersand.Artists::display_artist($DJs[1], $MakeLink, $Escape);
break;
default :
$link = 'Various DJs';
}
return $link.($IncludeHyphen?' - ':'');
} else {
return '';
}
}
/**
* Formats a single artist name.
*
* @param array $Artist an array of the form ('id'=>ID, 'name'=>Name)
* @param boolean $MakeLink If true, links to the artist page.
* @param boolean $Escape If false and $MakeLink is false, returns the unescaped, unadorned artist name.
* @return string Formatted artist name.
*/
public static function display_artist($Artist, $MakeLink = true, $Escape = true) {
if ($MakeLink && !$Escape) {
error('Invalid parameters to Artists::display_artist()');
} elseif ($MakeLink) {
return '<a href="artist.php?id='.$Artist['id'].'">'.display_str($Artist['name']).'</a>';
} elseif ($Escape) {
return display_str($Artist['name']);
} else {
return $Artist['name'];
}
}
/**
* Deletes an artist and their requests, wiki, and tags.
* Does NOT delete their torrents.
*
* @param int $ArtistID
*/
public static function delete_artist($ArtistID) {
global $DB, $LoggedUser, $Cache;
2013-06-06 08:01:03 +00:00
$DB->query("
SELECT Name
FROM artists_group
WHERE ArtistID = ".$ArtistID);
2012-10-11 08:00:15 +00:00
list($Name) = $DB->next_record(MYSQLI_NUM, false);
// Delete requests
2013-06-06 08:01:03 +00:00
$DB->query("
SELECT RequestID
FROM requests_artists
WHERE ArtistID = $ArtistID
AND ArtistID != 0");
2012-10-11 08:00:15 +00:00
$Requests = $DB->to_array();
2013-05-01 08:00:16 +00:00
foreach ($Requests AS $Request) {
2012-10-11 08:00:15 +00:00
list($RequestID) = $Request;
$DB->query('DELETE FROM requests WHERE ID='.$RequestID);
$DB->query('DELETE FROM requests_votes WHERE RequestID='.$RequestID);
$DB->query('DELETE FROM requests_tags WHERE RequestID='.$RequestID);
$DB->query('DELETE FROM requests_artists WHERE RequestID='.$RequestID);
}
// Delete artist
$DB->query('DELETE FROM artists_group WHERE ArtistID='.$ArtistID);
$DB->query('DELETE FROM artists_alias WHERE ArtistID='.$ArtistID);
$Cache->decrement('stats_artist_count');
// Delete wiki revisions
$DB->query('DELETE FROM wiki_artists WHERE PageID='.$ArtistID);
// Delete tags
$DB->query('DELETE FROM artists_tags WHERE ArtistID='.$ArtistID);
$Cache->delete_value('artist_'.$ArtistID);
2012-11-20 08:00:19 +00:00
$Cache->delete_value('artist_groups_'.$ArtistID);
2012-10-11 08:00:15 +00:00
// Record in log
2013-04-18 08:00:54 +00:00
if (!empty($LoggedUser['Username'])) {
2012-10-11 08:00:15 +00:00
$Username = $LoggedUser['Username'];
} else {
$Username = 'System';
}
2013-06-06 08:01:03 +00:00
Misc::write_log("Artist $ArtistID ($Name) was deleted by $Username");
2012-10-11 08:00:15 +00:00
}
/**
* Remove LRM (left-right-marker) and trims, because people copypaste carelessly.
* If we don't do this, we get seemingly duplicate artist names.
* TODO: make stricter, e.g. on all whitespace characters or Unicode normalisation
*
* @param string $ArtistName
*/
public static function normalise_artist_name($ArtistName) {
// \u200e is &lrm;
$ArtistName = trim($ArtistName);
$ArtistName = preg_replace('/^(\xE2\x80\x8E)+/', '', $ArtistName);
$ArtistName = preg_replace('/(\xE2\x80\x8E)+$/', '', $ArtistName);
return trim(preg_replace('/ +/', ' ', $ArtistName));
}
}
?>