~ubuntu-branches/ubuntu/karmic/ampache/karmic

« back to all changes in this revision

Viewing changes to bin/sort_files.inc

  • Committer: Bazaar Package Importer
  • Author(s): Charlie Smotherman
  • Date: 2008-05-15 18:44:23 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20080515184423-n47i81yk2s99iuci
Tags: 3.4-2
Included some upstream gather album art fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?php
2
2
/*
3
3
 
4
 
 Copyright (c) 2001 - 2006 Ampache.org
 
4
 Copyright (c) Ampache.org
5
5
 All rights reserved
6
6
 
7
7
 This program is free software; you can redistribute it and/or
42
42
$prefix = realpath($path . '/../'); 
43
43
require_once $prefix . '/lib/init.php';
44
44
 
 
45
ob_end_clean(); 
 
46
 
45
47
/* First Clean the catalog to we don't try to write anything we shouldn't */
46
48
 
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);
49
51
 
50
52
$catalogs = array();
51
53
 
52
 
while ($r = mysql_fetch_row($db_results)) { 
 
54
while ($r = Dba::fetch_row($db_results)) { 
 
55
 
53
56
 
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 */
62
 
                $song->format_song();
 
65
                $song->format();
 
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;
66
70
 
67
71
                /* Check for Demo Mode */
77
81
                        sort_move_file($song,$fullpath);
78
82
                }
79
83
 
80
 
 
81
 
 
82
84
        } // end foreach song
83
85
 
84
86
} // end foreach catalogs
86
88
 
87
89
/************** FUNCTIONS *****************/
88
90
/**
89
 
 * sort_find_filename 
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
92
 
 */
93
 
function sort_find_filename($song,$rename_pattern) { 
94
 
        
95
 
        $extension = ltrim(substr($song->file,strlen($song->file)-4,4),".");
96
 
        
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);
104
 
        
105
 
        /* Start replacing stuff */
106
 
        $replace_array = array('%a','%A','%t','%T','%y','%g');
107
 
        $content_array = array($artist,$album,$title,$track,$year,$genre);
108
 
        
109
 
        $rename_pattern = str_replace($replace_array,$content_array,$rename_pattern);
110
 
 
111
 
        $rename_pattern = preg_replace("[\-\:\!]","_",$rename_pattern);
112
 
 
113
 
        return $rename_pattern . "." . $extension;
114
 
 
115
 
} // sort_find_filename
116
 
 
117
 
/**
118
91
 * sort_find_home
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
134
107
 
135
108
        /* Do the various check */
136
109
        $album_object = new Album($song->album);
 
110
        $album_object->format(); 
137
111
        if ($album_object->artist_count != '1') { 
138
112
                $artist = "Various";
139
113
        }
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";
 
245
                flush(); 
271
246
        }
272
247
        else { 
273
248
 
282
257
                debug_event('copy','Copied ' . $song->file . ' to ' . $fullname,'5'); 
283
258
                
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';
288
263
                }
289
264
                else { 
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');
292
267
                }
293
268
 
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"; }
311
286
 
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);
315
290
 
316
291
        } // end else
317
292