Changeset 1740

Show
Ignore:
Timestamp:
09/03/08 15:44:01 (3 months ago)
Author:
momo-i
Message:

simplify code, remove large if else statement

Location:
trunk/lib/class
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/class/artist.class.php

    r1736 r1740  
    7979         */ 
    8080        public static function build_cache($ids,$extra=false) { 
    81                 if($ids) { 
    82                         $idlist = '(' . implode(',', $ids) . ')'; 
    83  
    84                         $sql = "SELECT * FROM `artist` WHERE `id` IN $idlist"; 
    85                         $db_results = Dba::query($sql); 
    86  
    87                         while ($row = Dba::fetch_assoc($db_results)) { 
    88                                 parent::add_to_cache('artist',$row['id'],$row);  
    89                         } 
    90  
    91                         // If we need to also pull the extra information, this is normally only used when we are doing the human display 
    92                         if ($extra) {  
    93                                 $sql = "SELECT `song`.`artist`, COUNT(`song`.`id`) AS `song_count`, COUNT(DISTINCT `song`.`album`) AS `album_count`, SUM(`song`.`time`) AS `time` FROM `song` " . 
    94                                         "WHERE `song`.`artist` IN $idlist GROUP BY `song`.`artist`"; 
    95                                 $db_results = Dba::query($sql); 
    96  
    97                                 while ($row = Dba::fetch_assoc($db_results)) {  
    98                                         parent::add_to_cache('artist_extra',$row['artist'],$row);  
    99                                 }  
    100  
    101                         } // end if extra 
    102  
    103                         return true; 
    104                 } else { 
    105                         return false; 
     81                if(!is_array($ids) OR !count($ids)) { return false; } 
     82 
     83                $idlist = '(' . implode(',', $ids) . ')'; 
     84 
     85                $sql = "SELECT * FROM `artist` WHERE `id` IN $idlist"; 
     86                $db_results = Dba::query($sql); 
     87 
     88                while ($row = Dba::fetch_assoc($db_results)) { 
     89                        parent::add_to_cache('artist',$row['id'],$row);  
    10690                } 
     91 
     92                // If we need to also pull the extra information, this is normally only used when we are doing the human display 
     93                if ($extra) {  
     94                        $sql = "SELECT `song`.`artist`, COUNT(`song`.`id`) AS `song_count`, COUNT(DISTINCT `song`.`album`) AS `album_count`, SUM(`song`.`time`) AS `time` FROM `song` " . 
     95                        "WHERE `song`.`artist` IN $idlist GROUP BY `song`.`artist`"; 
     96                        $db_results = Dba::query($sql); 
     97 
     98                        while ($row = Dba::fetch_assoc($db_results)) {  
     99                                parent::add_to_cache('artist_extra',$row['artist'],$row);  
     100                        }  
     101 
     102                } // end if extra 
     103 
     104                return true; 
    107105 
    108106        } // build_cache 
  • trunk/lib/class/rating.class.php

    r1736 r1740  
    6666        public static function build_cache($type, $ids) { 
    6767                 
    68                 if ($ids) { 
    69                         $user_id = Dba::escape($GLOBALS['user']->id);  
    70  
    71                         $idlist = '(' . implode(',', $ids) . ')'; 
    72                         $sql = "SELECT `rating`, `object_id`,`rating`.`rating` FROM `rating` WHERE `user`='$user_id' AND `object_id` IN $idlist " .  
    73                                 "AND `object_type`='$type'"; 
    74                         $db_results = Dba::read($sql); 
    75  
    76                         while ($row = Dba::fetch_assoc($db_results)) { 
    77                                 $user[$row['object_id']] = $row['rating'];  
    78                         } 
    79                  
    80                         $sql = "SELECT `rating`,`object_id` FROM `rating` WHERE `object_id` IN $idlist AND `object_type`='$type'";  
    81                         $db_results = Dba::read($sql);  
    82                  
    83                         while ($row = Dba::fetch_assoc($db_results)) {  
    84                                 $rating[$row['object_id']]['rating'] += $row['rating'];  
    85                                 $rating[$row['object_id']]['total']++;  
    86                         }  
    87  
    88                         foreach ($ids as $id) {  
    89                                 parent::add_to_cache('rating_' . $type . '_user',$id,intval($user[$id]));  
    90  
    91                                 // Do the bit of math required to store this 
    92                                 if (!isset($rating[$id])) {  
    93                                         $entry = array('average'=>'0','percise'=>'0');  
    94                                 }  
    95                                 else {  
    96                                         $average = round($rating[$id]['rating']/$rating[$id]['total'],1);  
    97                                         $entry = array('average'=>floor($average),'percise'=>$average);  
    98                                 }  
    99                                  
    100                                 parent::add_to_cache('rating_' . $type . '_all',$id,$entry);  
     68                if (!is_array($ids) OR !count($ids)) { return false; } 
     69 
     70                $user_id = Dba::escape($GLOBALS['user']->id);  
     71 
     72                $idlist = '(' . implode(',', $ids) . ')'; 
     73                $sql = "SELECT `rating`, `object_id`,`rating`.`rating` FROM `rating` WHERE `user`='$user_id' AND `object_id` IN $idlist " .  
     74                        "AND `object_type`='$type'"; 
     75                $db_results = Dba::read($sql); 
     76 
     77                while ($row = Dba::fetch_assoc($db_results)) { 
     78                        $user[$row['object_id']] = $row['rating'];  
     79                } 
     80                 
     81                $sql = "SELECT `rating`,`object_id` FROM `rating` WHERE `object_id` IN $idlist AND `object_type`='$type'";  
     82                $db_results = Dba::read($sql);  
     83                 
     84                while ($row = Dba::fetch_assoc($db_results)) {  
     85                        $rating[$row['object_id']]['rating'] += $row['rating'];  
     86                        $rating[$row['object_id']]['total']++;  
     87                }  
     88 
     89                foreach ($ids as $id) {  
     90                        parent::add_to_cache('rating_' . $type . '_user',$id,intval($user[$id]));  
     91 
     92                        // Do the bit of math required to store this 
     93                        if (!isset($rating[$id])) {  
     94                                $entry = array('average'=>'0','percise'=>'0');  
    10195                        }  
    102  
    103                         return true;  
    104                 } else { 
    105                         return false; 
    106                 } 
     96                        else {  
     97                                $average = round($rating[$id]['rating']/$rating[$id]['total'],1);  
     98                                $entry = array('average'=>floor($average),'percise'=>$average);  
     99                        }  
     100                         
     101                        parent::add_to_cache('rating_' . $type . '_all',$id,$entry);  
     102                }  
     103 
     104                return true;  
    107105 
    108106        } // build_cache 
  • trunk/lib/class/song.class.php

    r1738 r1740  
    7979        public static function build_cache($song_ids) { 
    8080 
    81                 if ($song_ids) { 
    82                         $idlist = '(' . implode(',', $song_ids) . ')'; 
     81                if (!is_array($song_ids) OR !count($song_ids)) { return false; } 
     82 
     83                $idlist = '(' . implode(',', $song_ids) . ')'; 
    8384           
    84                         // Song data cache 
    85                         $sql = "SELECT song.id,file,catalog,album,year,artist,". 
    86                                         "title,bitrate,rate,mode,size,time,track,played,song.enabled,update_time,tag_map.tag_id,". 
    87                                         "addition_time FROM `song` " . 
    88                                         "LEFT JOIN `tag_map` ON `tag_map`.`object_id`=`song`.`id` AND `tag_map`.`object_type`='song' " .  
    89                                         "WHERE `song`.`id` IN $idlist"; 
    90                         $db_results = Dba::read($sql); 
    91            
    92                         while ($row = Dba::fetch_assoc($db_results)) { 
    93                                 parent::add_to_cache('song',$row['id'],$row);  
    94                                 $artists[$row['artist']]        = $row['artist'];  
    95                                 $albums[$row['album']]          = $row['album'];  
    96                                 $tags[$row['tag_id']]           = $row['tag_id'];  
    97                         } 
    98          
    99                         Artist::build_cache($artists); 
    100                         Album::build_cache($albums);  
    101                         Tag::build_cache($tags);  
    102                         Tag::build_map_cache('song',$song_ids);  
    103  
    104                         // If we're rating this then cache them as well 
    105                         if (Config::get('ratings')) {  
    106                                 Rating::build_cache('song',$song_ids);  
    107                         }  
    108  
    109                         // Build a cache for the song's extended table 
    110                         $sql = "SELECT * FROM `song_data` WHERE `song_id` IN $idlist";  
    111                         $db_results = Dba::read($sql);  
    112  
    113                         while ($row = Dba::fetch_assoc($db_results)) {  
    114                                 parent::add_to_cache('song_data',$row['song_id'],$row);  
    115                         }  
    116   
    117                         return true; 
    118                 } else { 
    119                         return false; 
    120                 } 
     85                // Song data cache 
     86                $sql = "SELECT song.id,file,catalog,album,year,artist,". 
     87                                "title,bitrate,rate,mode,size,time,track,played,song.enabled,update_time,tag_map.tag_id,". 
     88                                "addition_time FROM `song` " . 
     89                                "LEFT JOIN `tag_map` ON `tag_map`.`object_id`=`song`.`id` AND `tag_map`.`object_type`='song' " .  
     90                                "WHERE `song`.`id` IN $idlist"; 
     91                $db_results = Dba::read($sql); 
     92 
     93                while ($row = Dba::fetch_assoc($db_results)) { 
     94                        parent::add_to_cache('song',$row['id'],$row);  
     95                        $artists[$row['artist']]        = $row['artist'];  
     96                        $albums[$row['album']]          = $row['album'];  
     97                        $tags[$row['tag_id']]           = $row['tag_id'];  
     98                } 
     99 
     100                Artist::build_cache($artists); 
     101                Album::build_cache($albums);  
     102                Tag::build_cache($tags);  
     103                Tag::build_map_cache('song',$song_ids);  
     104 
     105                // If we're rating this then cache them as well 
     106                if (Config::get('ratings')) {  
     107                        Rating::build_cache('song',$song_ids);  
     108                }  
     109 
     110                // Build a cache for the song's extended table 
     111                $sql = "SELECT * FROM `song_data` WHERE `song_id` IN $idlist";  
     112                $db_results = Dba::read($sql);  
     113 
     114                while ($row = Dba::fetch_assoc($db_results)) {  
     115                        parent::add_to_cache('song_data',$row['song_id'],$row);  
     116                }  
     117 
     118                return true; 
    121119  
    122120        } // build_cache 
  • trunk/lib/class/tag.class.php

    r1736 r1740  
    7878        public static function build_cache($ids) {  
    7979 
    80                 if ($ids) { 
    81                         $idlist = '(' . implode(',',$ids) . ')';  
    82                  
    83                         $sql = "SELECT * FROM `tag` WHERE `id` IN $idlist";  
    84                         $db_results = Dba::query($sql);  
    85  
    86                         while ($row = Dba::fetch_assoc($db_results)) {  
    87                                 parent::add_to_cache('tag',$row['id'],$row);  
    88                         }  
    89  
    90                         return true; 
    91                 } else { 
    92                         return false; 
    93                 } 
    94  
     80                if (!is_array($ids) OR !count($ids)) { return false; } 
     81 
     82                $idlist = '(' . implode(',',$ids) . ')';  
     83         
     84                $sql = "SELECT * FROM `tag` WHERE `id` IN $idlist";  
     85                $db_results = Dba::query($sql);  
     86 
     87                while ($row = Dba::fetch_assoc($db_results)) {  
     88                        parent::add_to_cache('tag',$row['id'],$row);  
     89                }  
     90 
     91                return true; 
    9592        } // build_cache 
    9693 
     
    10198        public static function build_map_cache($type,$ids) {  
    10299 
    103                 if ($ids) { 
    104                         $type = self::validate_type($type); 
    105                         $idlist = '(' . implode(',',$ids) . ')';  
    106  
    107                         $sql = "SELECT COUNT(`tag_map`.`id`) AS `count`,`tag`.`id`,`tag_map`.`object_id` FROM `tag_map` " . 
    108                                 "INNER JOIN `tag` ON `tag`.`id`=`tag_map`.`tag_id` " . 
    109                                 "WHERE `tag_map`.`object_type`='$type' AND `tag_map`.`object_id` IN $idlist " . 
    110                                 "GROUP BY `tag_map`.`object_id` ORDER BY `count` DESC"; 
    111                         $db_results = Dba::query($sql);  
    112  
    113                         while ($row = Dba::fetch_assoc($db_results)) {  
    114                                 $tags[$row['object_id']][] = $row;  
    115                         } 
    116  
    117                         foreach ($tags as $id=>$entry) {         
    118                                 parent::add_to_cache('tag_map_' . $type,$id,$entry);  
    119                         }  
    120  
    121                         return true;  
    122                 } else { 
    123                         return false; 
    124                 } 
     100                if (!is_array($ids) OR !count($ids)) { return false; } 
     101 
     102                $type = self::validate_type($type); 
     103                $idlist = '(' . implode(',',$ids) . ')';  
     104 
     105                $sql = "SELECT COUNT(`tag_map`.`id`) AS `count`,`tag`.`id`,`tag_map`.`object_id` FROM `tag_map` " . 
     106                        "INNER JOIN `tag` ON `tag`.`id`=`tag_map`.`tag_id` " . 
     107                        "WHERE `tag_map`.`object_type`='$type' AND `tag_map`.`object_id` IN $idlist " . 
     108                        "GROUP BY `tag_map`.`object_id` ORDER BY `count` DESC"; 
     109                $db_results = Dba::query($sql);  
     110 
     111                while ($row = Dba::fetch_assoc($db_results)) {  
     112                        $tags[$row['object_id']][] = $row;  
     113                } 
     114 
     115                foreach ($tags as $id=>$entry) {         
     116                        parent::add_to_cache('tag_map_' . $type,$id,$entry);  
     117                }  
     118 
     119                return true;  
    125120 
    126121        } // build_map_cache