diff options
author | momo-i <momo-i@ampache> | 2008-09-03 22:44:01 +0000 |
---|---|---|
committer | momo-i <momo-i@ampache> | 2008-09-03 22:44:01 +0000 |
commit | 01670c6abda538b5b40893f5015bfc4913382841 (patch) | |
tree | d20886a34636087129877d3284dd1701d42daee1 /lib/class | |
parent | 64cbf29ff39637f91ab938e8370b67a604c74f9a (diff) | |
download | ampache-01670c6abda538b5b40893f5015bfc4913382841.tar.gz ampache-01670c6abda538b5b40893f5015bfc4913382841.tar.bz2 ampache-01670c6abda538b5b40893f5015bfc4913382841.zip |
simplify code, remove large if else statement
Diffstat (limited to 'lib/class')
-rw-r--r-- | lib/class/artist.class.php | 38 | ||||
-rw-r--r-- | lib/class/rating.class.php | 64 | ||||
-rw-r--r-- | lib/class/song.class.php | 72 | ||||
-rw-r--r-- | lib/class/tag.class.php | 55 |
4 files changed, 109 insertions, 120 deletions
diff --git a/lib/class/artist.class.php b/lib/class/artist.class.php index ae742ded..837c2154 100644 --- a/lib/class/artist.class.php +++ b/lib/class/artist.class.php @@ -78,32 +78,30 @@ class Artist extends database_object { * this attempts to build a cache of the data from the passed albums all in one query */ public static function build_cache($ids,$extra=false) { - if($ids) { - $idlist = '(' . implode(',', $ids) . ')'; + if(!is_array($ids) OR !count($ids)) { return false; } - $sql = "SELECT * FROM `artist` WHERE `id` IN $idlist"; - $db_results = Dba::query($sql); + $idlist = '(' . implode(',', $ids) . ')'; + + $sql = "SELECT * FROM `artist` WHERE `id` IN $idlist"; + $db_results = Dba::query($sql); - while ($row = Dba::fetch_assoc($db_results)) { - parent::add_to_cache('artist',$row['id'],$row); - } + while ($row = Dba::fetch_assoc($db_results)) { + parent::add_to_cache('artist',$row['id'],$row); + } - // If we need to also pull the extra information, this is normally only used when we are doing the human display - if ($extra) { - $sql = "SELECT `song`.`artist`, COUNT(`song`.`id`) AS `song_count`, COUNT(DISTINCT `song`.`album`) AS `album_count`, SUM(`song`.`time`) AS `time` FROM `song` " . - "WHERE `song`.`artist` IN $idlist GROUP BY `song`.`artist`"; - $db_results = Dba::query($sql); + // If we need to also pull the extra information, this is normally only used when we are doing the human display + if ($extra) { + $sql = "SELECT `song`.`artist`, COUNT(`song`.`id`) AS `song_count`, COUNT(DISTINCT `song`.`album`) AS `album_count`, SUM(`song`.`time`) AS `time` FROM `song` " . + "WHERE `song`.`artist` IN $idlist GROUP BY `song`.`artist`"; + $db_results = Dba::query($sql); - while ($row = Dba::fetch_assoc($db_results)) { - parent::add_to_cache('artist_extra',$row['artist'],$row); - } + while ($row = Dba::fetch_assoc($db_results)) { + parent::add_to_cache('artist_extra',$row['artist'],$row); + } - } // end if extra + } // end if extra - return true; - } else { - return false; - } + return true; } // build_cache diff --git a/lib/class/rating.class.php b/lib/class/rating.class.php index d1b34822..045be750 100644 --- a/lib/class/rating.class.php +++ b/lib/class/rating.class.php @@ -65,45 +65,43 @@ class Rating extends database_object { */ public static function build_cache($type, $ids) { - if ($ids) { - $user_id = Dba::escape($GLOBALS['user']->id); + if (!is_array($ids) OR !count($ids)) { return false; } - $idlist = '(' . implode(',', $ids) . ')'; - $sql = "SELECT `rating`, `object_id`,`rating`.`rating` FROM `rating` WHERE `user`='$user_id' AND `object_id` IN $idlist " . - "AND `object_type`='$type'"; - $db_results = Dba::read($sql); + $user_id = Dba::escape($GLOBALS['user']->id); - while ($row = Dba::fetch_assoc($db_results)) { - $user[$row['object_id']] = $row['rating']; - } + $idlist = '(' . implode(',', $ids) . ')'; + $sql = "SELECT `rating`, `object_id`,`rating`.`rating` FROM `rating` WHERE `user`='$user_id' AND `object_id` IN $idlist " . + "AND `object_type`='$type'"; + $db_results = Dba::read($sql); + + while ($row = Dba::fetch_assoc($db_results)) { + $user[$row['object_id']] = $row['rating']; + } - $sql = "SELECT `rating`,`object_id` FROM `rating` WHERE `object_id` IN $idlist AND `object_type`='$type'"; - $db_results = Dba::read($sql); + $sql = "SELECT `rating`,`object_id` FROM `rating` WHERE `object_id` IN $idlist AND `object_type`='$type'"; + $db_results = Dba::read($sql); - while ($row = Dba::fetch_assoc($db_results)) { - $rating[$row['object_id']]['rating'] += $row['rating']; - $rating[$row['object_id']]['total']++; - } - - foreach ($ids as $id) { - parent::add_to_cache('rating_' . $type . '_user',$id,intval($user[$id])); - - // Do the bit of math required to store this - if (!isset($rating[$id])) { - $entry = array('average'=>'0','percise'=>'0'); - } - else { - $average = round($rating[$id]['rating']/$rating[$id]['total'],1); - $entry = array('average'=>floor($average),'percise'=>$average); - } - - parent::add_to_cache('rating_' . $type . '_all',$id,$entry); + while ($row = Dba::fetch_assoc($db_results)) { + $rating[$row['object_id']]['rating'] += $row['rating']; + $rating[$row['object_id']]['total']++; + } + + foreach ($ids as $id) { + parent::add_to_cache('rating_' . $type . '_user',$id,intval($user[$id])); + + // Do the bit of math required to store this + if (!isset($rating[$id])) { + $entry = array('average'=>'0','percise'=>'0'); + } + else { + $average = round($rating[$id]['rating']/$rating[$id]['total'],1); + $entry = array('average'=>floor($average),'percise'=>$average); } + + parent::add_to_cache('rating_' . $type . '_all',$id,$entry); + } - return true; - } else { - return false; - } + return true; } // build_cache diff --git a/lib/class/song.class.php b/lib/class/song.class.php index b524d126..6c97383f 100644 --- a/lib/class/song.class.php +++ b/lib/class/song.class.php @@ -78,46 +78,44 @@ class Song extends database_object { */ public static function build_cache($song_ids) { - if ($song_ids) { - $idlist = '(' . implode(',', $song_ids) . ')'; - - // Song data cache - $sql = "SELECT song.id,file,catalog,album,year,artist,". - "title,bitrate,rate,mode,size,time,track,played,song.enabled,update_time,tag_map.tag_id,". - "addition_time FROM `song` " . - "LEFT JOIN `tag_map` ON `tag_map`.`object_id`=`song`.`id` AND `tag_map`.`object_type`='song' " . - "WHERE `song`.`id` IN $idlist"; - $db_results = Dba::read($sql); + if (!is_array($song_ids) OR !count($song_ids)) { return false; } + + $idlist = '(' . implode(',', $song_ids) . ')'; - while ($row = Dba::fetch_assoc($db_results)) { - parent::add_to_cache('song',$row['id'],$row); - $artists[$row['artist']] = $row['artist']; - $albums[$row['album']] = $row['album']; - $tags[$row['tag_id']] = $row['tag_id']; - } - - Artist::build_cache($artists); - Album::build_cache($albums); - Tag::build_cache($tags); - Tag::build_map_cache('song',$song_ids); - - // If we're rating this then cache them as well - if (Config::get('ratings')) { - Rating::build_cache('song',$song_ids); - } + // Song data cache + $sql = "SELECT song.id,file,catalog,album,year,artist,". + "title,bitrate,rate,mode,size,time,track,played,song.enabled,update_time,tag_map.tag_id,". + "addition_time FROM `song` " . + "LEFT JOIN `tag_map` ON `tag_map`.`object_id`=`song`.`id` AND `tag_map`.`object_type`='song' " . + "WHERE `song`.`id` IN $idlist"; + $db_results = Dba::read($sql); + + while ($row = Dba::fetch_assoc($db_results)) { + parent::add_to_cache('song',$row['id'],$row); + $artists[$row['artist']] = $row['artist']; + $albums[$row['album']] = $row['album']; + $tags[$row['tag_id']] = $row['tag_id']; + } - // Build a cache for the song's extended table - $sql = "SELECT * FROM `song_data` WHERE `song_id` IN $idlist"; - $db_results = Dba::read($sql); + Artist::build_cache($artists); + Album::build_cache($albums); + Tag::build_cache($tags); + Tag::build_map_cache('song',$song_ids); - while ($row = Dba::fetch_assoc($db_results)) { - parent::add_to_cache('song_data',$row['song_id'],$row); - } - - return true; - } else { - return false; - } + // If we're rating this then cache them as well + if (Config::get('ratings')) { + Rating::build_cache('song',$song_ids); + } + + // Build a cache for the song's extended table + $sql = "SELECT * FROM `song_data` WHERE `song_id` IN $idlist"; + $db_results = Dba::read($sql); + + while ($row = Dba::fetch_assoc($db_results)) { + parent::add_to_cache('song_data',$row['song_id'],$row); + } + + return true; } // build_cache diff --git a/lib/class/tag.class.php b/lib/class/tag.class.php index ff02041d..a3fc2b1e 100644 --- a/lib/class/tag.class.php +++ b/lib/class/tag.class.php @@ -77,21 +77,18 @@ class Tag extends database_object { */ public static function build_cache($ids) { - if ($ids) { - $idlist = '(' . implode(',',$ids) . ')'; - - $sql = "SELECT * FROM `tag` WHERE `id` IN $idlist"; - $db_results = Dba::query($sql); + if (!is_array($ids) OR !count($ids)) { return false; } - while ($row = Dba::fetch_assoc($db_results)) { - parent::add_to_cache('tag',$row['id'],$row); - } + $idlist = '(' . implode(',',$ids) . ')'; + + $sql = "SELECT * FROM `tag` WHERE `id` IN $idlist"; + $db_results = Dba::query($sql); - return true; - } else { - return false; - } + while ($row = Dba::fetch_assoc($db_results)) { + parent::add_to_cache('tag',$row['id'],$row); + } + return true; } // build_cache /** @@ -100,29 +97,27 @@ class Tag extends database_object { */ public static function build_map_cache($type,$ids) { - if ($ids) { - $type = self::validate_type($type); - $idlist = '(' . implode(',',$ids) . ')'; + if (!is_array($ids) OR !count($ids)) { return false; } - $sql = "SELECT COUNT(`tag_map`.`id`) AS `count`,`tag`.`id`,`tag_map`.`object_id` FROM `tag_map` " . - "INNER JOIN `tag` ON `tag`.`id`=`tag_map`.`tag_id` " . - "WHERE `tag_map`.`object_type`='$type' AND `tag_map`.`object_id` IN $idlist " . - "GROUP BY `tag_map`.`object_id` ORDER BY `count` DESC"; - $db_results = Dba::query($sql); + $type = self::validate_type($type); + $idlist = '(' . implode(',',$ids) . ')'; - while ($row = Dba::fetch_assoc($db_results)) { - $tags[$row['object_id']][] = $row; - } - - foreach ($tags as $id=>$entry) { - parent::add_to_cache('tag_map_' . $type,$id,$entry); - } + $sql = "SELECT COUNT(`tag_map`.`id`) AS `count`,`tag`.`id`,`tag_map`.`object_id` FROM `tag_map` " . + "INNER JOIN `tag` ON `tag`.`id`=`tag_map`.`tag_id` " . + "WHERE `tag_map`.`object_type`='$type' AND `tag_map`.`object_id` IN $idlist " . + "GROUP BY `tag_map`.`object_id` ORDER BY `count` DESC"; + $db_results = Dba::query($sql); - return true; - } else { - return false; + while ($row = Dba::fetch_assoc($db_results)) { + $tags[$row['object_id']][] = $row; } + foreach ($tags as $id=>$entry) { + parent::add_to_cache('tag_map_' . $type,$id,$entry); + } + + return true; + } // build_map_cache /** |