diff options
Diffstat (limited to 'bin/sort_files.inc')
-rw-r--r-- | bin/sort_files.inc | 380 |
1 files changed, 190 insertions, 190 deletions
diff --git a/bin/sort_files.inc b/bin/sort_files.inc index a8020040..5eb95f10 100644 --- a/bin/sort_files.inc +++ b/bin/sort_files.inc @@ -1,5 +1,5 @@ <?php -/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */ +/* vim:set softtabstop=4 shiftwidth=4 expandtab: */ /** * * LICENSE: GNU General Public License, version 2 (GPLv2) @@ -55,40 +55,40 @@ $catalogs = array(); while ($r = Dba::fetch_row($db_results)) { - $catalog = new Catalog($r['0']); - $songs = $catalog->get_catalog_files(); - - printf (T_('Starting Catalog: %s'), stripslashes($catalog->name)); - echo "\n"; - - /* Foreach through each file and find it a home! */ - foreach ($songs as $song) { - /* Find this poor song a home */ - $song->format(); - $song->format_pattern(); - $directory = sort_find_home($song,$catalog->sort_pattern,$catalog->path); - $filename = $song->f_file; - $fullpath = $directory . "/" . $filename; - - /* Check for Demo Mode */ - if ($test_mode) { - /* We're just talking here... no work */ - echo T_("Moving File..."); - echo "\n\t"; - printf (T_('Source: %s'), $song->file); - echo "\n\t"; - printf (T_('Dest: %s'), $fullpath); - echo "\n"; - flush(); - } - /* We need to actually do the moving (fake it if we are testing) - * Don't try to move it, if it's already the same friggin thing! - */ - if ($song->file != $fullpath && strlen($fullpath)) { - sort_move_file($song,$fullpath); - } - - } // end foreach song + $catalog = new Catalog($r['0']); + $songs = $catalog->get_catalog_files(); + + printf (T_('Starting Catalog: %s'), stripslashes($catalog->name)); + echo "\n"; + + /* Foreach through each file and find it a home! */ + foreach ($songs as $song) { + /* Find this poor song a home */ + $song->format(); + $song->format_pattern(); + $directory = sort_find_home($song,$catalog->sort_pattern,$catalog->path); + $filename = $song->f_file; + $fullpath = $directory . "/" . $filename; + + /* Check for Demo Mode */ + if ($test_mode) { + /* We're just talking here... no work */ + echo T_("Moving File..."); + echo "\n\t"; + printf (T_('Source: %s'), $song->file); + echo "\n\t"; + printf (T_('Dest: %s'), $fullpath); + echo "\n"; + flush(); + } + /* We need to actually do the moving (fake it if we are testing) + * Don't try to move it, if it's already the same friggin thing! + */ + if ($song->file != $fullpath && strlen($fullpath)) { + sort_move_file($song,$fullpath); + } + + } // end foreach song } // end foreach catalogs @@ -100,47 +100,47 @@ while ($r = Dba::fetch_row($db_results)) { */ function sort_find_home($song,$sort_pattern,$base) { - $home = rtrim($base,"\/"); - $home = rtrim($home,"\\"); + $home = rtrim($base,"\/"); + $home = rtrim($home,"\\"); - /* Create the filename that this file should have */ - $album = sort_clean_name($song->f_album_full); - $artist = sort_clean_name($song->f_artist_full); - $track = sort_clean_name($song->track); - $title = sort_clean_name($song->title); - $year = sort_clean_name($song->year); - $comment = sort_clean_name($song->comment); + /* Create the filename that this file should have */ + $album = sort_clean_name($song->f_album_full); + $artist = sort_clean_name($song->f_artist_full); + $track = sort_clean_name($song->track); + $title = sort_clean_name($song->title); + $year = sort_clean_name($song->year); + $comment = sort_clean_name($song->comment); - /* Do the various check */ - $album_object = new Album($song->album); - $album_object->format(); - if ($album_object->artist_count != '1') { - $artist = "Various"; - } + /* Do the various check */ + $album_object = new Album($song->album); + $album_object->format(); + if ($album_object->artist_count != '1') { + $artist = "Various"; + } - /* IF we want a,b,c,d we need to know the first element */ - if ($GLOBALS['alphabet_prefix']) { - $sort_pattern = preg_replace("/\/?%o\//","",$sort_pattern); - $first_element = substr($sort_pattern,0,2); - $element = sort_element_name($first_element); - if (!$element) { $alphabet = 'ZZ'; } - else { $alphabet = strtoupper(substr(${$element},0,1)); } - $alphabet = preg_replace("/[^A-Za-z0-9]/","ZZ",$alphabet); + /* IF we want a,b,c,d we need to know the first element */ + if ($GLOBALS['alphabet_prefix']) { + $sort_pattern = preg_replace("/\/?%o\//","",$sort_pattern); + $first_element = substr($sort_pattern,0,2); + $element = sort_element_name($first_element); + if (!$element) { $alphabet = 'ZZ'; } + else { $alphabet = strtoupper(substr(${$element},0,1)); } + $alphabet = preg_replace("/[^A-Za-z0-9]/","ZZ",$alphabet); - $home .= "/$alphabet"; - } + $home .= "/$alphabet"; + } - /* Replace everything we can find */ - $replace_array = array('%a','%A','%t','%T','%y','%c'); - $content_array = array($artist,$album,$title,$track,$year,$comment); - $sort_pattern = str_replace($replace_array,$content_array,$sort_pattern); + /* Replace everything we can find */ + $replace_array = array('%a','%A','%t','%T','%y','%c'); + $content_array = array($artist,$album,$title,$track,$year,$comment); + $sort_pattern = str_replace($replace_array,$content_array,$sort_pattern); - /* Remove non A-Z0-9 chars */ - $sort_pattern = preg_replace("[^\\\/A-Za-z0-9\-\_\ \'\,\(\)]","_",$sort_pattern); + /* Remove non A-Z0-9 chars */ + $sort_pattern = preg_replace("[^\\\/A-Za-z0-9\-\_\ \'\,\(\)]","_",$sort_pattern); - $home .= "/$sort_pattern"; + $home .= "/$sort_pattern"; - return $home; + return $home; } // sort_find_home @@ -150,27 +150,27 @@ function sort_find_home($song,$sort_pattern,$base) { */ function sort_element_name($key) { - switch ($key) { - case '%t': - return 'title'; - break; - case '%T': - return 'track'; - break; - case '%a': - return 'artist'; - break; - case '%A': - return 'album'; - break; - case '%y': - return 'year'; - break; - default: - break; - } // switch on key - - return false; + switch ($key) { + case '%t': + return 'title'; + break; + case '%T': + return 'track'; + break; + case '%a': + return 'artist'; + break; + case '%A': + return 'album'; + break; + case '%y': + return 'year'; + break; + default: + break; + } // switch on key + + return false; } // sort_element_name @@ -183,14 +183,14 @@ function sort_element_name($key) { */ function sort_clean_name($string) { - /* First remove any / or \ chars */ - $string = preg_replace('/[\/\\\]/','-',$string); + /* First remove any / or \ chars */ + $string = preg_replace('/[\/\\\]/','-',$string); - $string = str_replace(':',' ',$string); + $string = str_replace(':',' ',$string); - $string = preg_replace('/[\!\:\*]/','_',$string); + $string = preg_replace('/[\!\:\*]/','_',$string); - return $string; + return $string; } // sort_clean_name @@ -206,102 +206,102 @@ function sort_clean_name($string) { */ function sort_move_file($song,$fullname) { - $old_dir = dirname($song->file); - - $info = pathinfo($fullname); - - $directory = $info['dirname']; - $file = $info['basename']; - $data = preg_split("/[\/\\\]/",$directory); - $path = ''; - - /* We not need the leading / */ - unset($data[0]); - - foreach ($data as $dir) { - - $dir = sort_clean_name($dir); - $path .= "/" . $dir; - - /* We need to check for the existance of this directory */ - if (!is_dir($path)) { - if ($GLOBALS['test_mode']) { - echo "\t"; - printf (T_('Making %s Directory'), $path); - echo "\n"; - } - else { - debug_event('mkdir',"Creating $path directory",'5'); - $results = mkdir($path); - if (!$results) { - printf (T_('Error: Unable to create %s move failed'), $path); - echo "\n"; - return false; - } - } // else we aren't in test mode - } // if it's not a dir - - } // foreach dir - - /* Now that we've got the correct directory structure let's try to copy it */ - if ($GLOBALS['test_mode']) { - echo "\t"; - // HINT: %1$s: file, %2$s: directory - printf (T_('Copying %1$s to %2$s'), $file, $directory); - echo "\n"; - $sql = "UPDATE song SET file='" . Dba::escape($fullname) . "' WHERE id='" . Dba::escape($song->id) . "'"; - echo "\tSQL: $sql\n"; - flush(); - } - else { - - /* Check for file existance */ - if (file_exists($fullname)) { - debug_event('file exists','Error: $fullname already exists','1'); - printf (T_('Error: %s already exists'), $filename); - echo "\n"; - return false; - } - - $results = copy($song->file,$fullname); - debug_event('copy','Copied ' . $song->file . ' to ' . $fullname,'5'); - - /* Look for the folder art and copy that as well */ - if (!Config::get('album_art_preferred_filename') OR strstr(Config::get('album_art_preferred_filename'),"%")) { - $folder_art = $directory . '/folder.jpg'; - $old_art = $old_dir . '/folder.jpg'; - } - else { - $folder_art = $directory . "/" . sort_clean_name(Config::get('album_art_preferred_filename')); - $old_art = $old_dir . "/" . sort_clean_name(Config::get('album_art_preferred_filename')); - } - - debug_event('copy_art','Copied ' . $old_art . ' to ' . $folder_art,'5'); - @copy($old_art,$folder_art); - - if (!$results) { printf (T_('Error: Unable to copy file to %s'), $fullname); echo "\n"; return false; } - - /* Check the filesize */ - $new_sum = filesize($fullname); - $old_sum = filesize($song->file); - - if ($new_sum != $old_sum OR !$new_sum) { - printf (T_('Error: Size Inconsistency, not deleting %s'), $song->file); - echo "\n"; - return false; - } // end if sum's don't match - - /* If we've made it this far it should be safe */ - $results = unlink($song->file); - if (!$results) { printf (T_('Error: Unable to delete %s'), $song->file); echo "\n"; } - - /* Update the catalog */ - $sql = "UPDATE song SET file='" . Dba::escape($fullname) . "' WHERE id='" . Dba::escape($song->id) . "'"; - $db_results = Dba::write($sql); - - } // end else - - return true; + $old_dir = dirname($song->file); + + $info = pathinfo($fullname); + + $directory = $info['dirname']; + $file = $info['basename']; + $data = preg_split("/[\/\\\]/",$directory); + $path = ''; + + /* We not need the leading / */ + unset($data[0]); + + foreach ($data as $dir) { + + $dir = sort_clean_name($dir); + $path .= "/" . $dir; + + /* We need to check for the existance of this directory */ + if (!is_dir($path)) { + if ($GLOBALS['test_mode']) { + echo "\t"; + printf (T_('Making %s Directory'), $path); + echo "\n"; + } + else { + debug_event('mkdir',"Creating $path directory",'5'); + $results = mkdir($path); + if (!$results) { + printf (T_('Error: Unable to create %s move failed'), $path); + echo "\n"; + return false; + } + } // else we aren't in test mode + } // if it's not a dir + + } // foreach dir + + /* Now that we've got the correct directory structure let's try to copy it */ + if ($GLOBALS['test_mode']) { + echo "\t"; + // HINT: %1$s: file, %2$s: directory + printf (T_('Copying %1$s to %2$s'), $file, $directory); + echo "\n"; + $sql = "UPDATE song SET file='" . Dba::escape($fullname) . "' WHERE id='" . Dba::escape($song->id) . "'"; + echo "\tSQL: $sql\n"; + flush(); + } + else { + + /* Check for file existance */ + if (file_exists($fullname)) { + debug_event('file exists','Error: $fullname already exists','1'); + printf (T_('Error: %s already exists'), $filename); + echo "\n"; + return false; + } + + $results = copy($song->file,$fullname); + debug_event('copy','Copied ' . $song->file . ' to ' . $fullname,'5'); + + /* Look for the folder art and copy that as well */ + if (!Config::get('album_art_preferred_filename') OR strstr(Config::get('album_art_preferred_filename'),"%")) { + $folder_art = $directory . '/folder.jpg'; + $old_art = $old_dir . '/folder.jpg'; + } + else { + $folder_art = $directory . "/" . sort_clean_name(Config::get('album_art_preferred_filename')); + $old_art = $old_dir . "/" . sort_clean_name(Config::get('album_art_preferred_filename')); + } + + debug_event('copy_art','Copied ' . $old_art . ' to ' . $folder_art,'5'); + @copy($old_art,$folder_art); + + if (!$results) { printf (T_('Error: Unable to copy file to %s'), $fullname); echo "\n"; return false; } + + /* Check the filesize */ + $new_sum = filesize($fullname); + $old_sum = filesize($song->file); + + if ($new_sum != $old_sum OR !$new_sum) { + printf (T_('Error: Size Inconsistency, not deleting %s'), $song->file); + echo "\n"; + return false; + } // end if sum's don't match + + /* If we've made it this far it should be safe */ + $results = unlink($song->file); + if (!$results) { printf (T_('Error: Unable to delete %s'), $song->file); echo "\n"; } + + /* Update the catalog */ + $sql = "UPDATE song SET file='" . Dba::escape($fullname) . "' WHERE id='" . Dba::escape($song->id) . "'"; + $db_results = Dba::write($sql); + + } // end else + + return true; } // sort_move_file |