42
42
$prefix = realpath($path . '/../');
43
43
require_once $prefix . '/lib/init.php';
45
47
/* First Clean the catalog to we don't try to write anything we shouldn't */
47
$sql = "SELECT id FROM catalog WHERE catalog_type='local'";
48
$db_results = mysql_query($sql, dbh());
49
$sql = "SELECT `id` FROM `catalog` WHERE `catalog_type`='local'";
50
$db_results = Dba::query($sql);
50
52
$catalogs = array();
52
while ($r = mysql_fetch_row($db_results)) {
54
while ($r = Dba::fetch_row($db_results)) {
54
57
$catalog = new Catalog($r['0']);
55
58
$songs = $catalog->get_catalog_files();
59
62
/* Foreach through each file and find it a home! */
60
63
foreach ($songs as $song) {
61
64
/* Find this poor song a home */
66
$song->format_pattern();
63
67
$directory = sort_find_home($song,$catalog->sort_pattern,$catalog->path);
64
$filename = sort_find_filename($song,$catalog->rename_pattern);
68
$filename = $song->f_file;
65
69
$fullpath = $directory . "/" . $filename;
67
71
/* Check for Demo Mode */
87
89
/************** FUNCTIONS *****************/
90
* This gets the filename that this file should have, it takes the rename pattern of the catalog
91
* along with the song object. Nothing Special Here
93
function sort_find_filename($song,$rename_pattern) {
95
$extension = ltrim(substr($song->file,strlen($song->file)-4,4),".");
97
/* Create the filename that this file should have */
98
$album = sort_clean_name($song->f_album_full);
99
$artist = sort_clean_name($song->f_artist_full);
100
$genre = sort_clean_name($song->f_genre);
101
$track = sort_clean_name($song->track);
102
$title = sort_clean_name($song->title);
103
$year = sort_clean_name($song->year);
105
/* Start replacing stuff */
106
$replace_array = array('%a','%A','%t','%T','%y','%g');
107
$content_array = array($artist,$album,$title,$track,$year,$genre);
109
$rename_pattern = str_replace($replace_array,$content_array,$rename_pattern);
111
$rename_pattern = preg_replace("[\-\:\!]","_",$rename_pattern);
113
return $rename_pattern . "." . $extension;
115
} // sort_find_filename
119
92
* Get the directory for this file from the catalog and the song info using the sort_pattern
120
93
* takes into account various artists and the alphabet_prefix
266
240
/* Now that we've got the correct directory structure let's try to copy it */
267
241
if ($GLOBALS['test_mode']) {
268
242
echo "\tCopying $file to $directory\n";
269
$sql = "UPDATE song SET file='" . sql_escape($fullname) . "' WHERE id='" . sql_escape($song->id) . "'";
243
$sql = "UPDATE song SET file='" . Dba::escape($fullname) . "' WHERE id='" . Dba::escape($song->id) . "'";
270
244
echo "\tSQL: $sql\n";
282
257
debug_event('copy','Copied ' . $song->file . ' to ' . $fullname,'5');
284
259
/* Look for the folder art and copy that as well */
285
if (!conf('album_art_preferred_filename')) {
260
if (!Config::get('album_art_preferred_filename')) {
286
261
$folder_art = $directory . '/folder.jpg';
287
262
$old_art = $old_dir . '/folder.jpg';
290
$folder_art = $directory . "/" . conf('album_art_preferred_filename');
291
$old_art = $old_dir . "/" . conf('album_art_preferred_filename');
265
$folder_art = $directory . "/" . Config::get('album_art_preferred_filename');
266
$old_art = $old_dir . "/" . Config::get('album_art_preferred_filename');
294
269
debug_event('copy_art','Copied ' . $old_art . ' to ' . $folder_art,'5');
310
285
if (!$results) { echo "Error: Unable to delete " . $song->file . "\n"; }
312
287
/* Update the catalog */
313
$sql = "UPDATE song SET file='" . sql_escape($fullname) . "' WHERE id='" . sql_escape($song->id) . "'";
314
$db_results = mysql_query($sql, dbh());
288
$sql = "UPDATE song SET file='" . Dba::escape($fullname) . "' WHERE id='" . Dba::escape($song->id) . "'";
289
$db_results = Dba::query($sql);