From 1069d54c85149ff1cb51563d6a1fc471695f8b47 Mon Sep 17 00:00:00 2001 From: Paul Arthur Date: Fri, 25 Jan 2013 21:08:24 -0500 Subject: Move clean functions into their respective classes Uniformly name them gc() --- admin/flag.php | 4 +- lib/class/album.class.php | 16 +++- lib/class/art.class.php | 6 +- lib/class/artist.class.php | 14 ++- lib/class/catalog.class.php | 186 +++--------------------------------- lib/class/flag.class.php | 9 ++ lib/class/playlist.class.php | 9 ++ lib/class/rating.class.php | 11 +++ lib/class/shoutbox.class.php | 11 +++ lib/class/song.class.php | 9 ++ lib/class/stats.class.php | 13 ++- lib/class/stream.class.php | 10 +- lib/class/stream_playlist.class.php | 2 +- lib/class/tag.class.php | 29 ++++++ lib/class/tmpplaylist.class.php | 5 +- lib/class/vauth.class.php | 2 +- lib/ui.lib.php | 2 +- 17 files changed, 142 insertions(+), 196 deletions(-) diff --git a/admin/flag.php b/admin/flag.php index 9cce736d..9f3d0c28 100644 --- a/admin/flag.php +++ b/admin/flag.php @@ -118,7 +118,7 @@ switch ($_REQUEST['action']) { } // end foreach songs // Clean out the old album - $catalog->clean_albums(); + Album::gc(); show_confirmation(T_('Album Updated'),'',Config::get('web_path') . '/admin/index.php'); @@ -160,7 +160,7 @@ switch ($_REQUEST['action']) { } // end foreach songs // Clean out the old artist(s) - $catalog->clean_artists(); + Artist::gc(); show_confirmation(T_('Artist Updated'),'',Config::get('web_path') . '/admin/index.php'); diff --git a/lib/class/album.class.php b/lib/class/album.class.php index c32cf007..16ef541d 100644 --- a/lib/class/album.class.php +++ b/lib/class/album.class.php @@ -88,6 +88,15 @@ class Album extends database_object { } // construct_from_array + /** + * gc + * + * Cleans out unused albums + */ + public static function gc() { + Dba::write('DELETE FROM `album` USING `album` LEFT JOIN `song` ON `song`.`album` = `album`.`id` WHERE `song`.`id` IS NULL'); + } + /** * build_cache * This takes an array of object ids and caches all of their information @@ -305,7 +314,7 @@ class Album extends database_object { Song::update_artist($artist,$song_id); } $updated = 1; - Catalog::clean_artists(); + Artist::gc(); } $album_id = Catalog::check_album($name,$year,$disk,$mbid); @@ -317,7 +326,7 @@ class Album extends database_object { } $current_id = $album_id; $updated = 1; - Catalog::clean_albums(); + self::gc(); } if ($updated) { @@ -326,7 +335,8 @@ class Album extends database_object { Flag::add($song_id,'song','retag','Interface Album Update'); Song::update_utime($song_id); } // foreach song of album - Catalog::clean_stats(); + Stats::gc(); + Rating::gc(); } // if updated diff --git a/lib/class/art.class.php b/lib/class/art.class.php index c18c9313..0e23463f 100644 --- a/lib/class/art.class.php +++ b/lib/class/art.class.php @@ -552,10 +552,10 @@ class Art extends database_object { /** - * clean + * gc * This cleans up art that no longer has a corresponding object */ - public static function clean() { + public static function gc() { // iterate over our types and delete the images foreach (array('album', 'artist') as $type) { $sql = "DELETE FROM `image` USING `image` LEFT JOIN `" . @@ -564,7 +564,7 @@ class Art extends database_object { $type . "' AND `" . $type . "`.`id` IS NULL"; $db_results = Dba::write($sql); } // foreach - } // clean + } /** * gather diff --git a/lib/class/artist.class.php b/lib/class/artist.class.php index 02f8a90a..d4e2df76 100644 --- a/lib/class/artist.class.php +++ b/lib/class/artist.class.php @@ -75,6 +75,15 @@ class Artist extends database_object { } // construct_from_array + /** + * gc + * + * This cleans out unused artists + */ + public static function gc() { + Dba::write('DELETE FROM `artist` USING `artist` LEFT JOIN `song` ON `song`.`artist` = `artist`.`id` WHERE `song`.`id` IS NULL'); + } + /** * this attempts to build a cache of the data from the passed albums all in one query */ @@ -285,7 +294,7 @@ class Artist extends database_object { } $updated = 1; $current_id = $artist_id; - Catalog::clean_artists(); + self::gc(); } // end if it changed if ($updated) { @@ -293,7 +302,8 @@ class Artist extends database_object { Flag::add($song_id,'song','retag','Interface Artist Update'); Song::update_utime($song_id); } - Catalog::clean_stats(); + Stats::gc(); + Rating::gc(); } // if updated return $current_id; diff --git a/lib/class/catalog.class.php b/lib/class/catalog.class.php index 0807a351..ca4b4a05 100644 --- a/lib/class/catalog.class.php +++ b/lib/class/catalog.class.php @@ -1399,172 +1399,6 @@ class Catalog extends database_object { } //_clean_chunk - /** - * clean_tags - * This cleans out tag_maps that are not associated 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::write($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::write($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::write($sql); - - $sql = "DELETE FROM `tag_map` USING `tag_map` LEFT JOIN `video` ON `video`.`id`=`tag_map`.`object_id` " . - "WHERE `tag_map`.`object_type`='video' AND `video`.`id` IS NULL"; - $db_results = Dba::write($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::write($sql); - - } // clean_tags - - /** - * clean_shoutbox - * This cleans out any shoutbox items that are now orphaned - */ - public static function clean_shoutbox() { - - // Clean songs - $sql = "DELETE FROM `user_shout` USING `user_shout` LEFT JOIN `song` ON `song`.`id`=`user_shout`.`object_id` " . - "WHERE `song`.`id` IS NULL AND `user_shout`.`object_type`='song'"; - $db_results = Dba::write($sql); - - // Clean albums - $sql = "DELETE FROM `user_shout` USING `user_shout` LEFT JOIN `album` ON `album`.`id`=`user_shout`.`object_id` " . - "WHERE `album`.`id` IS NULL AND `user_shout`.`object_type`='album'"; - $db_results = Dba::write($sql); - - // Clean artists - $sql = "DELETE FROM `user_shout` USING `user_shout` LEFT JOIN `artist` ON `artist`.`id`=`user_shout`.`object_id` " . - "WHERE `artist`.`id` IS NULL AND `user_shout`.`object_type`='artist'"; - $db_results = Dba::write($sql); - - - } // clean_shoutbox - - /** - * clean_albums - *This function cleans out unused albums - */ - public static function clean_albums() { - - /* Do a complex delete to get albums where there are no songs */ - $sql = "DELETE FROM album USING album LEFT JOIN song ON song.album = album.id WHERE song.id IS NULL"; - $db_results = Dba::write($sql); - - } // clean_albums - - /** - * clean_flagged - * This functions cleans ou unused flagged items - */ - public static function clean_flagged() { - - /* Do a complex delete to get flagged items where the songs are now gone */ - $sql = "DELETE FROM flagged USING flagged LEFT JOIN song ON song.id = flagged.object_id WHERE song.id IS NULL AND object_type='song'"; - $db_results = Dba::write($sql); - - } // clean_flagged - - /** - * clean_artists - * This function cleans out unused artists - */ - public static function clean_artists() { - - /* Do a complex delete to get artists where there are no songs */ - $sql = "DELETE FROM artist USING artist LEFT JOIN song ON song.artist = artist.id WHERE song.id IS NULL"; - $db_results = Dba::write($sql); - - } //clean_artists - - /** - * clean_playlists - * cleans out dead files from playlists - */ - public static function clean_playlists() { - - /* Do a complex delete to get playlist songs where there are no songs */ - $sql = "DELETE FROM `playlist_data` USING `playlist_data` LEFT JOIN `song` ON `song`.`id` = `playlist_data`.`object_id` " . - "WHERE `song`.`file` IS NULL AND `playlist_data`.`object_type`='song'"; - $db_results = Dba::write($sql); - - // Clear TMP Playlist information as well - $sql = "DELETE FROM `tmp_playlist_data` USING `tmp_playlist_data` LEFT JOIN `song` ON `tmp_playlist_data`.`object_id` = `song`.`id` " . - "WHERE `song`.`id` IS NULL"; - $db_results = Dba::write($sql); - - } // clean_playlists - - /** - * clean_ext_info - * This function clears any ext_info that no longer has a parent - */ - public static function clean_ext_info() { - - $sql = "DELETE FROM `song_data` USING `song_data` LEFT JOIN `song` ON `song`.`id` = `song_data`.`song_id` " . - "WHERE `song`.`id` IS NULL"; - $db_results = Dba::write($sql); - - } // clean_ext_info - - /** - * clean_stats - * This functions removes stats for songs/albums that no longer exist - */ - public static function clean_stats() { - - // Crazy SQL Mojo to remove stats where there are no songs - $sql = "DELETE FROM object_count USING object_count LEFT JOIN song ON song.id=object_count.object_id WHERE object_type='song' AND song.id IS NULL"; - $db_results = Dba::write($sql); - - // Crazy SQL Mojo to remove stats where there are no albums - $sql = "DELETE FROM object_count USING object_count LEFT JOIN album ON album.id=object_count.object_id WHERE object_type='album' AND album.id IS NULL"; - $db_results = Dba::write($sql); - - // Crazy SQL Mojo to remove stats where ther are no artists - $sql = "DELETE FROM object_count USING object_count LEFT JOIN artist ON artist.id=object_count.object_id WHERE object_type='artist' AND artist.id IS NULL"; - $db_results = Dba::write($sql); - - // Delete the live_stream stat information - $sql = "DELETE FROM object_count USING object_count LEFT JOIN live_stream ON live_stream.id=object_count.object_id WHERE object_type='live_stream' AND live_stream.id IS NULL"; - $db_results = Dba::write($sql); - - // Clean the stats - $sql = "DELETE FROM `object_count` USING `object_count` LEFT JOIN `video` ON `video`.`id`=`object_count`.`object_id` " . - "WHERE `object_count`.`object_type`='video' AND `video`.`id` IS NULL"; - $db_results = Dba::write($sql); - - // Delete Song Ratings information - $sql = "DELETE FROM rating USING rating LEFT JOIN song ON song.id=rating.object_id WHERE object_type='song' AND song.id IS NULL"; - $db_results = Dba::write($sql); - - // Delete Album Rating Information - $sql = "DELETE FROM rating USING rating LEFT JOIN album ON album.id=rating.object_id WHERE object_type='album' AND album.id IS NULL"; - $db_results = Dba::write($sql); - - // Delete Artist Rating Information - $sql = "DELETE FROM rating USING rating LEFT JOIN artist ON artist.id=rating.object_id WHERE object_type='artist' AND artist.id IS NULL"; - $db_results = Dba::write($sql); - - // Delete the Video Rating Informations - $sql = "DELETE FROM `rating` USING `rating` LEFT JOIN `video` ON `video`.`id`=`rating`.`object_id` " . - "WHERE `rating`.`object_type`='video' AND `video`.`id` IS NULL"; - $db_results = Dba::write($sql); - - } // clean_stats - /** * verify_catalog * This function compares the DB's information with the ID3 tags @@ -1675,15 +1509,17 @@ class Catalog extends database_object { public static function clean() { debug_event('catalog', 'Database cleanup started', 5, 'ampache-catalog'); - self::clean_albums(); - self::clean_artists(); - Art::clean(); - self::clean_flagged(); - self::clean_stats(); - self::clean_ext_info(); - self::clean_playlists(); - self::clean_shoutbox(); - self::clean_tags(); + Song::gc(); + Album::gc(); + Artist::gc(); + Art::gc(); + Flag::gc(); + Stats::gc(); + Rating::gc(); + Playlist::gc(); + tmpPlaylist::gc(); + shoutBox::gc(); + Tag::gc(); debug_event('catalog', 'Database cleanup ended', 5, 'ampache-catalog'); } // clean diff --git a/lib/class/flag.class.php b/lib/class/flag.class.php index 65789686..ed1fef06 100644 --- a/lib/class/flag.class.php +++ b/lib/class/flag.class.php @@ -58,6 +58,15 @@ class Flag extends database_object { } // Constructor + /** + * gc + * + * This cleans out unused flagged items + */ + public static function gc() { + Dba::write("DELETE FROM `flagged` USING `flagged` LEFT JOIN `song` ON `song`.`id` = `flagged`.`object_id` WHERE `song`.`id` IS NULL AND `object_type` = 'song'"); + } + /** * build_cache * This takes an array of ids and builds up a nice little cache diff --git a/lib/class/playlist.class.php b/lib/class/playlist.class.php index fbe2d908..20035f2b 100644 --- a/lib/class/playlist.class.php +++ b/lib/class/playlist.class.php @@ -51,6 +51,15 @@ class Playlist extends playlist_object { } // Playlist + /** + * gc + * + * Clean dead items out of playlists + */ + public static function gc() { + Dba::write("DELETE FROM `playlist_data` USING `playlist_data` LEFT JOIN `song` ON `song`.`id` = `playlist_data`.`object_id` WHERE `song`.`file` IS NULL AND `playlist_data`.`object_type`='song'"); + } + /** * build_cache * This is what builds the cache from the objects diff --git a/lib/class/rating.class.php b/lib/class/rating.class.php index 616baad6..0943de2f 100644 --- a/lib/class/rating.class.php +++ b/lib/class/rating.class.php @@ -48,6 +48,17 @@ class Rating extends database_object { } // Constructor + /** + * gc + * + * Remove ratings for items that no longer exist. + */ + public static function gc() { + foreach(array('song', 'album', 'artist', 'video') as $object_type) { + Dba::write("DELETE FROM `rating` USING `rating` LEFT JOIN `$object_type` ON `$object_type`.`id` = `rating`.`object_type` WHERE `object_type` = '$object_type` AND `$object_type`.`id` IS NULL"); + } + } + /** * build_cache * This attempts to get everything we'll need for this page load in a diff --git a/lib/class/shoutbox.class.php b/lib/class/shoutbox.class.php index ce85813d..3cface18 100644 --- a/lib/class/shoutbox.class.php +++ b/lib/class/shoutbox.class.php @@ -59,6 +59,17 @@ class shoutBox { } // _get_info + /** + * gc + * + * Cleans out orphaned shoutbox items + */ + public static function gc() { + foreach(array('song', 'album', 'artist') as $object_type) { + Dba::write("DELETE FROM `user_shout` USING `user_shout` LEFT JOIN `$object_type` ON `$object_type`.`id` = `user_shout`.`object_id` WHERE `$object_type`.`id` IS NULL AND `user_shout`.`object_type` = '$object_type'"); + } + } + /** * get_top * This returns the top user_shouts, shoutbox objects are always shown regardless and count against the total diff --git a/lib/class/song.class.php b/lib/class/song.class.php index 72ab88f9..8e48f87c 100644 --- a/lib/class/song.class.php +++ b/lib/class/song.class.php @@ -74,6 +74,15 @@ class Song extends database_object implements media { } // constructor + /** + * gc + * + * Cleans up the song_data table + */ + public static function gc() { + Dba::write('DELETE FROM `song_data` USING `song_data` LEFT JOIN `song` ON `song`.`id` = `song_data`.`song_id` WHERE `song`.`id` IS NULL'); + } + /** * build_cache * diff --git a/lib/class/stats.class.php b/lib/class/stats.class.php index e92ed994..4ff676e9 100644 --- a/lib/class/stats.class.php +++ b/lib/class/stats.class.php @@ -55,7 +55,18 @@ class Stats { */ public static function clear() { Dba::write('TRUNCATE `object_count`'); - Dba::write('UPDATE `song` SET `played` = 0'; + Dba::write('UPDATE `song` SET `played` = 0'); + } + + /** + * gc + * + * This removes stats for things that no longer exist. + */ + public static function gc() { + foreach(array('song', 'album', 'artist', 'live_stream', 'video') as $object_type) { + Dba::write("DELETE FROM `object_count` USING `object_count` LEFT JOIN `$object_type` ON `$object_type`.`id` = `object_count`.`object_id` WHERE `object_type` = '$object_type' AND `$object_type`.`id` IS NULL"); + } } /** diff --git a/lib/class/stream.class.php b/lib/class/stream.class.php index fd8c23c7..428f5a4a 100644 --- a/lib/class/stream.class.php +++ b/lib/class/stream.class.php @@ -101,19 +101,19 @@ class Stream { } // session_exists /** - * gc_session + * gc * This function performes the garbage collection stuff, run on extend * and on now playing refresh. */ - public static function gc_session() { + public static function gc() { $time = time(); $sql = "DELETE FROM `session_stream` WHERE `expire` < '$time'"; $db_results = Dba::write($sql); - Stream_Playlist::clean(); + Stream_Playlist::gc(); - } // gc_session + } /** * extend_session @@ -132,7 +132,7 @@ class Stream { "WHERE `id`='$sid'"; $db_results = Dba::write($sql); - self::gc_session(); + self::gc(); return true; diff --git a/lib/class/stream_playlist.class.php b/lib/class/stream_playlist.class.php index a8e66c62..917b543c 100644 --- a/lib/class/stream_playlist.class.php +++ b/lib/class/stream_playlist.class.php @@ -85,7 +85,7 @@ class Stream_Playlist { return Dba::write($sql); } - public static function clean() { + public static function gc() { $sql = 'DELETE FROM `stream_playlist` ' . 'USING `stream_playlist` LEFT JOIN `session_stream` ' . 'ON `session_stream`.`id`=`stream_playlist`.`sid` ' . diff --git a/lib/class/tag.class.php b/lib/class/tag.class.php index 2e76fa04..36c25648 100644 --- a/lib/class/tag.class.php +++ b/lib/class/tag.class.php @@ -269,6 +269,35 @@ class Tag extends database_object { } // add_tag_map + /** + * gc + * + * This cleans out tag_maps that are obsolete and then removes tags that + * have no maps. + */ + public static function gc() { + $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::write($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::write($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::write($sql); + + $sql = "DELETE FROM `tag_map` USING `tag_map` LEFT JOIN `video` ON `video`.`id`=`tag_map`.`object_id` " . + "WHERE `tag_map`.`object_type`='video' AND `video`.`id` IS NULL"; + $db_results = Dba::write($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::write($sql); + } + /** * tag_exists * This checks to see if a tag exists, this has nothing to do with objects or maps diff --git a/lib/class/tmpplaylist.class.php b/lib/class/tmpplaylist.class.php index f216a7d0..2da411cf 100644 --- a/lib/class/tmpplaylist.class.php +++ b/lib/class/tmpplaylist.class.php @@ -278,12 +278,13 @@ class tmpPlaylist extends database_object { } // session_clean /** - * clean + * gc * This cleans up old data */ - public static function clean() { + public static function gc() { self::prune_playlists(); self::prune_tracks(); + Dba::write("DELETE FROM `tmp_playlist_data` USING `tmp_playlist_data` LEFT JOIN `song` ON `tmp_playlist_data`.`object_id` = `song`.`id` WHERE `song`.`id` IS NULL"); } /** diff --git a/lib/class/vauth.class.php b/lib/class/vauth.class.php index b1c30975..4939c5c2 100644 --- a/lib/class/vauth.class.php +++ b/lib/class/vauth.class.php @@ -143,7 +143,7 @@ class vauth { // Also clean up things that use sessions as keys Query::clean(); - tmpPlaylist::clean(); + tmpPlaylist::gc(); return true; diff --git a/lib/ui.lib.php b/lib/ui.lib.php index ae39990d..0b56b221 100644 --- a/lib/ui.lib.php +++ b/lib/ui.lib.php @@ -759,7 +759,7 @@ function print_bool($value) { */ function show_now_playing() { - Stream::gc_session(); + Stream::gc(); Stream::gc_now_playing(); $web_path = Config::get('web_path'); -- cgit