diff options
author | Karl 'vollmerk' Vollmer <vollmer@ampache.org> | 2008-05-19 05:40:26 +0000 |
---|---|---|
committer | Karl 'vollmerk' Vollmer <vollmer@ampache.org> | 2008-05-19 05:40:26 +0000 |
commit | 8ed4195e450067975d6be5ff5588c5c228b538af (patch) | |
tree | 566c3e5db2e266aa40a6e0762b8fcf1da8e835b9 | |
parent | f3233e12d81813b2574af831472fb4136066f8ee (diff) | |
download | ampache-8ed4195e450067975d6be5ff5588c5c228b538af.tar.gz ampache-8ed4195e450067975d6be5ff5588c5c228b538af.tar.bz2 ampache-8ed4195e450067975d6be5ff5588c5c228b538af.zip |
made the cataloging process gather genre and use it as a tag, browse genre also removed
-rwxr-xr-x | docs/CHANGELOG | 1 | ||||
-rw-r--r-- | lib/class/album.class.php | 1 | ||||
-rw-r--r-- | lib/class/catalog.class.php | 93 | ||||
-rw-r--r-- | lib/class/tag.class.php | 179 | ||||
-rw-r--r-- | templates/sidebar_browse.inc.php | 4 |
5 files changed, 245 insertions, 33 deletions
diff --git a/docs/CHANGELOG b/docs/CHANGELOG index bbef2ea1..5f896be7 100755 --- a/docs/CHANGELOG +++ b/docs/CHANGELOG @@ -4,6 +4,7 @@ -------------------------------------------------------------------------- v.3.5-Alpha1 + - Genre Tag is now used as a 'Tag', Browse Genre removed - Ignore getid3() iconv stuff doesn't seem to work - Improved fix_filenames.inc, tries a translation first then strips invalid characters diff --git a/lib/class/album.class.php b/lib/class/album.class.php index cb90a518..d689a74d 100644 --- a/lib/class/album.class.php +++ b/lib/class/album.class.php @@ -98,6 +98,7 @@ class Album extends database_object { * with a single query */ public static function build_cache($ids) { + $idlist = '(' . implode(',', $ids) . ')'; $sql = "SELECT * FROM `album` WHERE `id` IN $idlist"; diff --git a/lib/class/catalog.class.php b/lib/class/catalog.class.php index b267401a..e9ecc100 100644 --- a/lib/class/catalog.class.php +++ b/lib/class/catalog.class.php @@ -44,7 +44,8 @@ class Catalog { // Used in functions private static $albums = array(); private static $artists = array(); - private static $genres = array(); + private static $genres = array(); //FIXME + private static $tags = array(); private static $_art_albums = array(); /** @@ -117,7 +118,6 @@ class Catalog { $this->f_update = $this->last_update ? date('d/m/Y h:i',$this->last_update) : _('Never'); $this->f_add = $this->last_add ? date('d/m/Y h:i',$this->last_add) : _('Never'); - } // format /** @@ -1507,6 +1507,32 @@ class Catalog { } // clean_genres /** + * clean_tags + * This cleans out tag_maps that are not assoicated with a 'living' object + * and then cleans the tags that have no maps + */ + public static function clean_tags() { + + $sql = "DELETE FROM `tag_map` USING `tag_map` LEFT JOIN `song` ON `song`.`id`=`tag_map`.`object_id` " . + "WHERE `tag_map`.`object_type`='song' AND `song`.`id` IS NULL"; + $db_results = Dba::query($sql); + + $sql = "DELETE FROM `tag_map` USING `tag_map` LEFT JOIN `album` ON `album`.`id`=`tag_map`.`object_id` " . + "WHERE `tag_map`.`object_type`='album' AND `album`.`id` IS NULL"; + $db_results = Dba::query($sql); + + $sql = "DELETE FROM `tag_map` USING `tag_map` LEFT JOIN `artist` ON `artist`.`id`=`tag_map`.`object_id` " . + "WHERE `tag_map`.`object_type`='artist' AND `artist`.`id` IS NULL"; + $db_results = Dba::query($sql); + + // Now nuke the tags themselves + $sql = "DELETE FROM `tag` USING `tag` LEFT JOIN `tag_map` ON `tag`.`id`=`tag_map`.`tag_id` " . + "WHERE `tag_map`.`id` IS NULL"; + $db_results = Dba::query($sql); + + } // clean_tags + + /** * clean_shoutbox * This cleans out any shoutbox items that are now orphaned */ @@ -1753,16 +1779,17 @@ class Catalog { * This is a wrapper function for all of the different cleaning * functions, it runs them in the correct order and takes a catalog_id */ - public static function clean($catalog_id) { - - self::clean_albums($catalog_id); - self::clean_artists($catalog_id); - self::clean_genres($catalog_id); - self::clean_flagged($catalog_id); - self::clean_stats($catalog_id); - self::clean_ext_info($catalog_id); - self::clean_playlists($catalog_id); - self::clean_shoutbox($catalog_id); + public static function clean() { + + self::clean_albums(); + self::clean_artists(); + self::clean_genres(); + self::clean_flagged(); + self::clean_stats(); + self::clean_ext_info(); + self::clean_playlists(); + self::clean_shoutbox(); + self::clean_tags(); } // clean @@ -1994,6 +2021,46 @@ class Catalog { return $insert_id; } // check_genre + + /** + * check_tag + * This checks the tag we've been passed (name) + * and sees if it exists, and if so if it's mapped + * to this object, this is only done for songs for now + */ + public static function check_tag($value,$object_id,$object_type='song') { + + // First see if the tag exists at all + $tag_id = self::$tags[$value]; + + if ($tag_id) { + + // At least we know the tag but sadly we still have to check the map + $tag = new Tag($tag_id); + if ($tag->has_object($object_id,$object_type)) { + return $tag->id; + } + // Oooh well time to add it + Tag::add_tag_map($tag->id,$object_type,$object_id); + + return $tag->id; + + } // if cached already + + // Clean it up and try to create it + $value = Tag::clean_tag($value); + $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'); } + else { $insert_id = $tag->id; } + + // Add to the cache + self::$tags[$value] = $insert_id; + + return $insert_id; + + } // check_tag /** * check_title @@ -2066,6 +2133,8 @@ class Catalog { $song_id = Dba::insert_id(); + self::check_tag($genre,$song_id); + /* Add the EXT information */ $sql = "INSERT INTO `song_data` (`song_id`,`comment`,`lyrics`) " . " VALUES ('$song_id','$comment','$lyrics')"; diff --git a/lib/class/tag.class.php b/lib/class/tag.class.php index 7964c549..1e3ba1d0 100644 --- a/lib/class/tag.class.php +++ b/lib/class/tag.class.php @@ -25,11 +25,117 @@ */ class Tag extends database_object { + public $id; + public $name; + + /** + * constructor + * This takes a tag id and returns all of the relevent information + */ + public function __construct($id) { + + if (!$id) { return false; } + + $info = $this->get_info($id); + + foreach ($info as $key=>$value) { + $this->$key = $value; + } // end foreach + + } // constructor + + /** + * construct_from_name + * This attempts to construct the tag from a name, rather then the ID + */ + public static function construct_from_name($name) { + + $name = Dba::escape($name); + + $sql = "SELECT * FROM `tag` WHERE `name`='$name'"; + $db_results = Dba::query($sql); + + $row = Dba::fetch_assoc($db_results); + + if (!$row['id']) { return false; } + + parent::add_to_cache('tag',$row['id'],$row); + + $tag = new Tag(0); + foreach ($row as $key=>$value) { + $tag->$key = $value; + } + + return $tag; + + } // construct_from_name + + /** + * get_info + * This takes the id and returns an array of information, checks the cache + * to see what's up + */ + private function get_info($id) { + + $id = intval($id); + + if (parent::is_cached('tag',$id)) { + return parent::get_from_cache('tag',$id); + } + + $sql = "SELECT * FROM `tag` WHERE `id`='$id'"; + $db_results = Dba::query($sql); + + $results = Dba::fetch_assoc($db_results); + + parent::add_to_cache('tag',$id,$results); + + return $results; + + } // get_info + + /** + * build_cache + * 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) { + + $idlist = '(' . implode(',',$ids) . ')'; + + $sql = "SELECT * FROM `tag` WHERE `id` IN $idlist"; + $db_results = Dba::query($sql); + + while ($row = Dba::fetch_assoc($db_results)) { + parent::add_to_cache('tag',$row['id'],$row); + } + + } // build_cache + + /** + * has_object + * This checks to see if the current tag element has the specified object + * of the specified type + */ + public function has_object($object_type,$object_id) { + + $object_type = self::validate_type($object_type); + $object_id = intval($object_id); + $tag_id = intval($this->id); + + $sql = "SELECT * FROM `tag_map` WHERE `object_type`='$object_type' AND `object_id`='$object_id' " . + " AND `tag_id`='$tag_id'"; + $db_results = Dba::query($sql); + + return Dba::num_rows($db_results); + + } // has_object + /** * add_tag * This function adds a new tag, for now we're going to limit the tagging a bit */ - public static function add_tag($type, $id, $tagval) { + public static function add_tag($type, $id, $tagval,$user='') { if (!self::validate_type($type)) { return false; @@ -37,11 +143,13 @@ class Tag extends database_object { if (!is_numeric($id)) { return false; } - if (!preg_match('/^[A-Za-z_]+$/',$tagval)) { - return false; - } + + // Clean it up and make it tagish + $tagval = self::clean_tag($tagval); + + if (!strlen($tagval)) { return false; } - $uid = intval($GLOBALS['user']->id); + $uid = $user ? intval($user) : intval($GLOBALS['user']->id); $tagval = Dba::escape($tagval); $type = Dba::escape($type); $id = intval($id); @@ -59,23 +167,44 @@ class Tag extends database_object { $insert_id = Dba::insert_id(); } - // Now make sure this isn't a duplicate - $sql = "SELECT * FROM `tag_map " . - "WHERE `tag_id`='$insert_id' AND `user`='$uid' AND `object_type`='$type' AND `object_id`='$id'"; - $db_results = Dba::query($sql); + self::add_tag_map($insert_id,$type,$id); - $row = Dba::fetch_assoc($db_results); + return $insert_id; + + } // add_tag - // Only insert it if the current tag for this user doesn't exist - if (!count($row)) { - $sql = "INSERT INTO `tag_map` (`tag_id`,`user`,`object_type`,`object_id`) " . - "VALUES ('$tid','$uid','$type','$id')"; - $db_results = Dba::query($sql); + /** + * 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) { + + $uid = intval($GLOBALS['user']->id); + $tag_id = intval($tag_id); + $type = self::validate_type($object_type); + $id = intval($object_id); + + // Now make sure this isn't a duplicate + $sql = "SELECT * FROM `tag_map " . + "WHERE `tag_id`='$insert_id' AND `user`='$uid' AND `object_type`='$type' AND `object_id`='$id'"; + $db_results = Dba::query($sql); + + $row = Dba::fetch_assoc($db_results); + + // Only insert it if the current tag for this user doesn't exist + if (!count($row)) { + $sql = "INSERT INTO `tag_map` (`tag_id`,`user`,`object_type`,`object_id`) " . + "VALUES ('$tag_id','$uid','$type','$id')"; + $db_results = Dba::query($sql); + $insert_id = Dba::insert_id(); + } + else { + $insert_id = $row['id']; } - return true; + return $insert_id; - } // add_tag + } // add_tag_map /** * get_many_tags @@ -205,11 +334,23 @@ return array(); $valid_array = array('song','artist','album'); - if (in_array($type,$valid_array)) { return true; } + if (in_array($type,$valid_array)) { return $type; } return false; } // validate_type -} // end of TagCloud class + /** + * clean_tag + * This takes a string and makes it Tagish + */ + public static function clean_tag($value) { + + $tag = preg_replace("/[^\w\_\-]/","",$value); + + return $tag; + + } // clean_tag + +} // end of Tag class ?> diff --git a/templates/sidebar_browse.inc.php b/templates/sidebar_browse.inc.php index 7bef4348..0867cde8 100644 --- a/templates/sidebar_browse.inc.php +++ b/templates/sidebar_browse.inc.php @@ -1,7 +1,7 @@ <?php /* - Copyright (c) 2001 - 2007 Ampache.org + Copyright (c) Ampache.org All rights reserved. This program is free software; you can redistribute it and/or @@ -33,7 +33,7 @@ $allowed_filters = Browse::get_allowed_filters(); <li id="sb_browse_bb_SongTitle"><a href="<?php echo $web_path; ?>/browse.php?action=song"><?php echo _('Song Title'); ?></a></li> <li id="sb_browse_bb_Album"><a href="<?php echo $web_path; ?>/browse.php?action=album"><?php echo _('Albums'); ?></a></li> <li id="sb_browse_bb_Artist"><a href="<?php echo $web_path; ?>/browse.php?action=artist"><?php echo _('Artist'); ?></a></li> - <li id="sb_browse_bb_Genre"><a href="<?php echo $web_path; ?>/browse.php?action=genre"><?php echo _('Genre'); ?></a></li> + <li id="sb_browse_bb_Tags"><a href="<?php echo $web_path; ?>/browse.php?action=tag"><?php echo _('Tag Cloud'); ?></a></li> <li id="sb_browse_bb_Playlist"><a href="<?php echo $web_path; ?>/browse.php?action=playlist"><?php echo _('Playlist'); ?></a></li> <li id="sb_browse_bb_RadioStation"><a href="<?php echo $web_path; ?>/browse.php?action=live_stream"><?php echo _('Radio Stations'); ?></a></li> </ul> |