diff options
author | Paul Arthur <paul.arthur@flowerysong.com> | 2013-01-28 11:09:49 -0500 |
---|---|---|
committer | Paul Arthur <paul.arthur@flowerysong.com> | 2013-03-28 16:56:21 -0400 |
commit | 744e6bb99404b8455024488472a1ad02e5c87025 (patch) | |
tree | 02b23c011984d5a25e762f015568585a9cb8409f /lib/class/artist.class.php | |
parent | eee48871e9071db686b1db89786da2968a8857cf (diff) | |
download | ampache-744e6bb99404b8455024488472a1ad02e5c87025.tar.gz ampache-744e6bb99404b8455024488472a1ad02e5c87025.tar.bz2 ampache-744e6bb99404b8455024488472a1ad02e5c87025.zip |
Make remote catalogs actually work
Diffstat (limited to 'lib/class/artist.class.php')
-rw-r--r-- | lib/class/artist.class.php | 82 |
1 files changed, 81 insertions, 1 deletions
diff --git a/lib/class/artist.class.php b/lib/class/artist.class.php index caf24335..b9523ef3 100644 --- a/lib/class/artist.class.php +++ b/lib/class/artist.class.php @@ -33,6 +33,7 @@ class Artist extends database_object { // Constructed vars public $_fake = false; // Set if construct_from_array() used + private static $_mapcache = array(); /** * Artist @@ -275,6 +276,85 @@ class Artist extends database_object { } // format /** + * check + * + * Checks for an existing artist; if none exists, insert one. + */ + public static function check($name, $mbid = null, $readonly = false) { + $trimmed = Catalog::trim_prefix(trim($name)); + $name = $trimmed['string']; + $prefix = $trimmed['prefix']; + + if (!$name) { + $name = T_('Unknown (Orphaned)'); + $prefix = null; + } + + if (isset(self::$_mapcache[$name][$mbid])) { + return self::$_mapcache[$name][$mbid]; + } + + $exists = false; + + $sql = 'SELECT `id` FROM `artist` WHERE `mbid` = ?'; + $db_results = Dba::read($sql, array($mbid)); + + if ($row = Dba::fetch_assoc($db_results)) { + $id = $row['id']; + $exists = true; + } + else { + $sql = 'SELECT `id`, `mbid` FROM `artist` WHERE `name` LIKE ?'; + $db_results = Dba::read($sql, array($name)); + + while ($row = Dba::fetch_assoc($db_results)) { + $key = $row['mbid'] ?: 'null'; + $id_array[$key] = $row['id']; + } + + if (isset($id_array)) { + if ($mbid) { + if (isset($id_array['null']) && !$readonly) { + $sql = 'UPDATE `artist` SET `mbid` = ? WHERE `id` = ?'; + Dba::write($sql, array($mbid, $id_array['null'])); + } + if (isset($id_array['null'])) { + $id = $id_array['null']; + $exists = true; + } + } + else { + // Pick one at random + $id = array_shift($id_array); + $exists = true; + } + } + } + + if ($exists) { + self::$_mapcache[$name][$mbid] = $id; + return $id; + } + + if ($readonly) { + return null; + } + + $sql = 'INSERT INTO `artist` (`name`, `prefix`, `mbid`) ' . + 'VALUES(?, ?, ?)'; + + $db_results = Dba::write($sql, array($name, $prefix, $mbid)); + if (!$db_results) { + return null; + } + $id = Dba::insert_id(); + + self::$_mapcache[$name][$mbid] = $id; + return $id; + + } + + /** * update * This takes a key'd array of data and updates the current artist * it will flag songs as neeed @@ -284,7 +364,7 @@ class Artist extends database_object { // Save our current ID $current_id = $this->id; - $artist_id = Catalog::check_artist($data['name'], $this->mbid); + $artist_id = self::check($data['name'], $this->mbid); // If it's changed we need to update if ($artist_id != $this->id) { |