diff options
author | Karl 'vollmerk' Vollmer <vollmer@ampache.org> | 2008-05-26 04:08:45 +0000 |
---|---|---|
committer | Karl 'vollmerk' Vollmer <vollmer@ampache.org> | 2008-05-26 04:08:45 +0000 |
commit | 13ae6a0371ae26023880a0ad69b3b1587db8dd76 (patch) | |
tree | ec0f94a7ee29fb4d34056f3e5a79bd620ad93161 | |
parent | 9803be045ba3895cc981d5e7e22fd80f80d6544d (diff) | |
download | ampache-13ae6a0371ae26023880a0ad69b3b1587db8dd76.tar.gz ampache-13ae6a0371ae26023880a0ad69b3b1587db8dd76.tar.bz2 ampache-13ae6a0371ae26023880a0ad69b3b1587db8dd76.zip |
show the top 2 tags on songs, sync fixes from /branches/3.4 improved caching on song object
-rw-r--r-- | lib/class/album.class.php | 2 | ||||
-rw-r--r-- | lib/class/catalog.class.php | 4 | ||||
-rw-r--r-- | lib/class/dba.class.php | 93 | ||||
-rw-r--r-- | lib/class/song.class.php | 38 | ||||
-rw-r--r-- | lib/class/tag.class.php | 26 | ||||
-rw-r--r-- | lib/class/vauth.class.php | 2 |
6 files changed, 110 insertions, 55 deletions
diff --git a/lib/class/album.class.php b/lib/class/album.class.php index d689a74d..ad0f6ef5 100644 --- a/lib/class/album.class.php +++ b/lib/class/album.class.php @@ -276,7 +276,7 @@ class Album extends database_object { public function get_art() { // Attempt to get the resized art first - //$art = $this->get_resized_db_art(); + $art = $this->get_resized_db_art(); if (!is_array($art)) { $art = $this->get_db_art(); diff --git a/lib/class/catalog.class.php b/lib/class/catalog.class.php index 22ef5ff2..0a7df3eb 100644 --- a/lib/class/catalog.class.php +++ b/lib/class/catalog.class.php @@ -1946,7 +1946,7 @@ debug_event('prefix',$prefix_pattern,'3'); return $tag->id; } // Oooh well time to add it - Tag::add_tag_map($tag->id,$object_type,$object_id); + Tag::add_tag_map($tag->id,$object_type,$object_id,'0'); return $tag->id; @@ -1957,7 +1957,7 @@ debug_event('prefix',$prefix_pattern,'3'); $tag = Tag::construct_from_name($value); // Figure out the ID so we can cache it - if (!$tag) { $insert_id = Tag::add_tag($object_type,$object_id,$value,'-1'); } + if (!$tag) { $insert_id = Tag::add_tag($object_type,$object_id,$value,'0'); } else { $insert_id = $tag->id; } // Add to the cache diff --git a/lib/class/dba.class.php b/lib/class/dba.class.php index a4685f21..4f97f6db 100644 --- a/lib/class/dba.class.php +++ b/lib/class/dba.class.php @@ -174,15 +174,17 @@ class Dba { // Do this later } + $dbh = mysql_connect($hostname,$username,$password); if (!$dbh) { debug_event('Database','Error unable to connect to database' . mysql_error(),'1'); } + + $data = self::translate_to_mysqlcharset(Config::get('site_charset')); if (function_exists('mysql_set_charset')) { - $sql_charset = str_replace("-","",Config::get('site_charset')); - $charset = mysql_set_charset($sql_charset,$dbh); + $charset = mysql_set_charset($data['charset'],$dbh); } else { - $sql = "SET NAMES " . mysql_real_escape_string(str_replace("-","",Config::get('site_charset'))); + $sql = "SET NAMES " . mysql_real_escape_string($data['charset']); $charset = mysql_query($sql,$dbh); } if (!$charset) { debug_event('Database','Error unable to set connection charset, function missing or set failed','1'); } @@ -190,7 +192,7 @@ class Dba { $select_db = mysql_select_db($database,$dbh); if (!$select_db) { debug_event('Database','Error unable to select ' . $database . ' error ' . mysql_error(),'1'); } - if ($_REQUEST['profiling']) { + if (Config::get('sql_profiling')) { mysql_query('set profiling=1', $dbh); mysql_query('set profiling_history_size=50', $dbh); mysql_query('set query_cache_type=0', $dbh); @@ -276,54 +278,69 @@ class Dba { } // auto_init /** - * reset_db_charset - * This cruises through the database and trys to set the charset to the current - * site charset, this is an admin function that can be run by an administrator - * this can mess up data if you switch between charsets that are not overlapping - * a catalog verify must be re-run to correct them - */ - public static function reset_db_charset() { + * translate_to_mysqlcharset + * This translates the specified charset to a mysqlcharset, stupid ass mysql + * demands that it's charset list is different! + */ + public static function translate_to_mysqlcharset($charset) { // MySQL translte real charset names into fancy smancy MySQL land names - switch (strtoupper(Config::get('site_charset'))) { + switch (strtoupper($charset)) { case 'CP1250': case 'WINDOWS-1250': case 'WINDOWS-1252': $target_charset = 'cp1250'; $target_collation = 'cp1250_general_ci'; - break; - case 'ISO-8859': - case 'ISO-8859-2': - $target_charset = 'latin2'; - $target_collation = 'latin2_general_ci'; - break; - case 'ISO-8859-1': - $target_charset = 'latin1'; - $target_charset = 'latin1_general_ci'; - break; + break; + case 'ISO-8859': + case 'ISO-8859-2': + $target_charset = 'latin2'; + $target_collation = 'latin2_general_ci'; + break; + case 'ISO-8859-1': + $target_charset = 'latin1'; + $target_charset = 'latin1_general_ci'; + break; case 'EUC-KR': $target_charset = 'euckr'; $target_collation = 'euckr_korean_ci'; - break; - case 'CP932': - $target_charset = 'sjis'; - $target_collation = 'sjis_japanese_ci'; - break; - case 'KOI8-U': - $target_charset = 'koi8u'; - $target_collation = 'koi8u_general_ci'; - break; - case 'KOI8-R': + break; + case 'CP932': + $target_charset = 'sjis'; + $target_collation = 'sjis_japanese_ci'; + break; + case 'KOI8-U': + $target_charset = 'koi8u'; + $target_collation = 'koi8u_general_ci'; + break; + case 'KOI8-R': $target_charset = 'koi8r'; - $target_collation = 'koi8r_general_ci'; - break; - default; + $target_collation = 'koi8r_general_ci'; + break; + default; case 'UTF-8': - $target_charset = 'utf8'; - $target_collation = 'utf8_unicode_ci'; - break; + $target_charset = 'utf8'; + $target_collation = 'utf8_unicode_ci'; + break; } // end mysql charset translation + return array('charset'=>$target_charset,'collation'=>$target_collation); + + } // translate_to_mysqlcharset + + /** + * reset_db_charset + * This cruises through the database and trys to set the charset to the current + * site charset, this is an admin function that can be run by an administrator + * this can mess up data if you switch between charsets that are not overlapping + * a catalog verify must be re-run to correct them + */ + public static function reset_db_charset() { + + $translated_charset = self::translate_to_mysqlcharset(Config::get('site_charset')); + $target_charset = $translated_charset['charset']; + $target_collation = $translated_charset['collation']; + // Alter the charset for the entire database $sql = "ALTER DATABASE `" . Config::get('database_name') . "` DEFAULT CHARACTER SET $target_charset COLLATE $target_collation"; $db_results = Dba::query($sql); diff --git a/lib/class/song.class.php b/lib/class/song.class.php index 0a20e4b7..f38c7985 100644 --- a/lib/class/song.class.php +++ b/lib/class/song.class.php @@ -82,19 +82,30 @@ class Song extends database_object { // Song data cache $sql = "SELECT song.id,file,catalog,album,year,artist,". - "title,bitrate,rate,mode,size,time,track,played,song.enabled,update_time,". - "addition_time FROM `song` WHERE `song`.`id` IN - $idlist"; + "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::query($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']; } Artist::build_cache($artists); Album::build_cache($albums); + Tag::build_cache($tags); + + // Build a cache for the song's extended table + $sql = "SELECT * FROM `song_data` WHERE `song_id` IN $idlist"; + $db_results = Dba::query($sql); + + while ($row = Dba::fetch_assoc($db_results)) { + parent::add_to_cache('song_data',$row['song_id'],$row); + } return true; @@ -133,14 +144,20 @@ class Song extends database_object { * current object */ public function _get_ext_info() { - global $song_data_cache; - if (isset($song_data_cache[intval($this->id)])) - return $song_data_cache[intval($this->id)]; - $sql = "SELECT * FROM song_data WHERE `song_id`='" . Dba::escape($this->id) . "'"; + + $id = intval($this->id); + + if (parent::is_cached('song_data',$id)) { + return parent::get_from_cache('song_data',$id); + } + + $sql = "SELECT * FROM song_data WHERE `song_id`='$id'"; $db_results = Dba::query($sql); $results = Dba::fetch_assoc($db_results); + parent::add_to_cache('song_data',$id,$results); + return $results; } // _get_ext_info @@ -652,7 +669,12 @@ class Song extends database_object { // Get the top tags $tags = Tag::get_top_tags('song',$this->id); - $this->f_tags = implode(', ',$tags); + foreach ($tags as $tag_id) { + $tag = new Tag($tag_id); + $this->f_tags .= $tag->name . ', '; + } + + $this->f_tags = rtrim($this->f_tags,', '); // Format the size $this->f_size = sprintf("%.2f",($this->size/1048576)); diff --git a/lib/class/tag.class.php b/lib/class/tag.class.php index 5c65455c..ff0bfa9c 100644 --- a/lib/class/tag.class.php +++ b/lib/class/tag.class.php @@ -99,7 +99,7 @@ class Tag extends database_object { * This takes an array of object ids and caches all of their information * in a single query, cuts down on the connections */ - public function build_cache($ids) { + public static function build_cache($ids) { $idlist = '(' . implode(',',$ids) . ')'; @@ -149,7 +149,7 @@ class Tag extends database_object { if (!strlen($tagval)) { return false; } - $uid = $user ? intval($user) : intval($GLOBALS['user']->id); + $uid = ($user == '') ? intval($user) : intval($GLOBALS['user']->id); $tagval = Dba::escape($tagval); $type = Dba::escape($type); $id = intval($id); @@ -177,9 +177,9 @@ class Tag extends database_object { * add_tag_map * This adds a specific tag to the map for specified object */ - public static function add_tag_map($tag_id,$object_type,$object_id) { + public static function add_tag_map($tag_id,$object_type,$object_id,$user='') { - $uid = intval($GLOBALS['user']->id); + $uid = ($user == '') ? intval($GLOBALS['user']->id) : intval($user); $tag_id = intval($tag_id); $type = self::validate_type($object_type); $id = intval($object_id); @@ -248,7 +248,23 @@ return array(); */ public static function get_top_tags($type,$object_id,$limit='2') { + $type = self::validate_type($type); + $object_id = intval($object_id); + $limit = intval($limit); + + $sql = "SELECT COUNT(`tag_map`.`id`) AS `count`,`tag`.`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`='$object_id' " . + "GROUP BY `tag_map`.`tag_id` ORDER BY `count` LIMIT $limit"; + $db_results = Dba::query($sql); + + $results = array(); + + while ($row = Dba::fetch_assoc($db_results)) { + $results[] = $row['id']; + } + return $results; } // get_top_tags @@ -356,7 +372,7 @@ return array(); */ public static function clean_tag($value) { - $tag = preg_replace("/[^\w\_\-\s]/","",$value); + $tag = preg_replace("/[^\w\_\-\s\&]/","",$value); return $tag; diff --git a/lib/class/vauth.class.php b/lib/class/vauth.class.php index b3892872..d5ee590d 100644 --- a/lib/class/vauth.class.php +++ b/lib/class/vauth.class.php @@ -98,7 +98,7 @@ class vauth { $sql = "UPDATE `session` SET `value`='$value', `expire`='$expire' WHERE `id`='$key'"; $db_results = Dba::query($sql); - debug_event('SESSION','Writing to ' . $key . ' with expire ' . $expire . ' DBError:' . Dba::error(),'6'); + debug_event('SESSION','Writing to ' . $key . ' with expire ' . $expire . ' ' . Dba::error(),'6'); return $db_results; |