Ejemplo n.º 1
0
function cmdLine(array $args)
{
    if ($args[1] === '-c') {
        //If the command line triggers monitor, load monitor functionality.
        if (isset($args[2])) {
            $GLOBALS['custom'] = $args[2];
        }
        releaseView();
    } else {
        if ($args[1] === '-a') {
            remArt($args[2]);
        } else {
            if ($args[1] === '-i') {
                $flacwriter = new Flacwriter();
                $flacwriter->setFile($args[2]);
                $res = json_encode($flacwriter->getMetadata());
                scanLog($res);
                print $res;
                die;
            } else {
                if ($args[1] === '-x') {
                    $ddexml = new Ddexml($args[2]);
                    if (isset($args[3])) {
                        if (isset($args[4])) {
                            $res = $ddexml->getProperty($args[3], $args[4]);
                        } else {
                            $res = $ddexml->getProperty($args[3]);
                        }
                    } else {
                        $res = $ddexml->xmlToJSON();
                    }
                    print json_encode($res, JSON_PRETTY_PRINT);
                    die;
                } else {
                    print "No option specified.";
                    die;
                }
            }
        }
    }
}
function metaToFlac(array $array, $depth = false)
{
    if (isset($GLOBALS['log'])) {
        $log = $GLOBALS['log'];
    } else {
        $log = new xmllog();
    }
    //Accepts an array of modified files then recursively applies changes to them.
    ifecho("metaToFlac called. Depth: " . $depth);
    $tracks = false;
    $xml = '';
    if ($depth == false) {
        $depth = DIRECTORY_SEPARATOR;
    }
    //Look for our XML first. Does not permit the presence of multiple XML files in one folder (because how do I know which is authoritative?)
    foreach ($array as $k => $v) {
        if ($k === 'xml') {
            if (count($v) > 1) {
                scanLog('Only one XML per folder is permitted.');
                break;
            } else {
                if (!isset($v[0])) {
                    continue;
                } else {
                    $xml = $depth . $v[0];
                    ifecho("Begin parsing " . $xml);
                    if (filesize($xml) > 12) {
                        $ddexml = new Ddexml($xml);
                        if ($album = $ddexml->parseRelease()) {
                            scanLog('Found release ' . $album->isrc . ' in ' . $xml);
                            $tracks = $album->getTracks();
                        }
                    }
                    break;
                }
            }
        }
    }
    if ($tracks) {
        //Establish temporary and completed paths. Create temp folder if it doesn't exist.
        if (isset($album->sku)) {
            $finalloc = $GLOBALS['cfg_processed_path'] . DIRECTORY_SEPARATOR . $album->sku;
        } elseif (isset($album->reference)) {
            $finalloc = $GLOBALS['cfg_processed_path'] . DIRECTORY_SEPARATOR . $album->reference;
        } else {
            trigger_error("Can't read release info from " . $xml);
            return false;
        }
        if (isset($album->sku)) {
            $tmploc = $GLOBALS['cfg_tmp_path'] . DIRECTORY_SEPARATOR . $album->sku;
        } elseif (isset($album->reference)) {
            $tmploc = $GLOBALS['cfg_tmp_path'] . DIRECTORY_SEPARATOR . $album->reference;
        } else {
            trigger_error("Can't read release info from " . $xml);
            return false;
        }
        if (!file_exists($GLOBALS['cfg_tmp_path'])) {
            mkdir($GLOBALS['cfg_tmp_path']);
        }
        if (!file_exists($GLOBALS['cfg_processed_path'])) {
            mkdir($GLOBALS['cfg_processed_path']);
        }
        if (!file_exists($finalloc)) {
            mkdir($finalloc);
        }
        if ($log->setLog($finalloc . DIRECTORY_SEPARATOR . "parse.log")) {
            $log->writeHead("DDEXML Process Log for " . $album->sku);
        } else {
            trigger_error("Couldn't create logfile " . $finalloc . DIRECTORY_SEPARATOR . "parse.log");
        }
        if (!file_exists($tmploc)) {
            mkdir($tmploc);
        }
        //Empty the temporary and final folder of old files if they're already there.
        $emptydir = glob($tmploc . DIRECTORY_SEPARATOR . "*");
        foreach ($emptydir as $eraseme) {
            if (is_file($eraseme)) {
                unlink($eraseme);
            }
        }
        $emptydir = glob($finalloc . DIRECTORY_SEPARATOR . "*");
        foreach ($emptydir as $eraseme) {
            if (is_file($eraseme)) {
                unlink($eraseme);
            }
        }
        $i = 1;
        foreach ($tracks as $track) {
            ifecho('Parsing album track ' . $i . ' of ' . count($tracks));
            //Now we process each track. First we'll look in primary folder, then in 'resources' sub-folder for the FLAC file itself.
            $locate = array_search_regex($track['isrc'], $array['flac']);
            if ($locate !== false) {
                $loc = $depth . $array['flac'][$locate];
                $file = $array['flac'][$locate];
            } else {
                ifecho($track['isrc'] . " not found. Searching in \\resources.");
                if (array_key_exists('resources', $array)) {
                    $locate = array_search_regex($track['isrc'], $array['resources']['flac']);
                    if ($locate !== false) {
                        $loc = $depth . 'resources' . DIRECTORY_SEPARATOR . $array['resources']['flac'][$locate];
                        $file = $array['resources']['flac'][$locate];
                    }
                }
            }
            if ($locate !== false) {
                ifecho('Track found at: ' . $loc);
                //Write metadata to the FLAC file if we find it.
                $remloc = $loc;
                //Copy the original FLAC file to a temp folder (NEVER write to the original!)
                copy($remloc, $tmploc . DIRECTORY_SEPARATOR . $file);
                //We instantiate an instance of the Flacwriter object, point it to the FLAC file and provide an array containing metadata.
                $flacwriter = new Flacwriter();
                $flacwriter->setFile($tmploc . DIRECTORY_SEPARATOR . $file);
                //Decide which attributes to write to file.
                $tagPack = array();
                foreach ($track as $k => $v) {
                    switch ($k) {
                        case 'artist':
                            $tagPack[$k] = $v;
                            break;
                        case 'title':
                            $tagPack[$k] = $v;
                            break;
                        case 'date':
                            $tagPack[$k] = $v;
                            break;
                        case 'tracknumber':
                            $tagPack[$k] = $v;
                            break;
                        case 'albumartist':
                            $tagPack[$k] = $v;
                            break;
                        case 'composer':
                            $tagPack[$k] = $v;
                            break;
                        case 'isrc':
                            $tagPack[$k] = $v;
                            break;
                        case 'album':
                            $tagPack[$k] = $v;
                            break;
                        case 'publisher':
                            $tagPack[$k] = $v;
                            break;
                        case 'copyright':
                            $tagPack[$k] = $v;
                            break;
                        case 'genre':
                            $tagPack[$k] = $v;
                            break;
                        case 'personnel':
                            $tagPack[$k] = $v;
                            break;
                        case 'comment':
                            $tagPack[$k] = $v;
                            break;
                        default:
                            break;
                    }
                }
                $wrote = $flacwriter->writeMetadata($tagPack, $tmploc . DIRECTORY_SEPARATOR . $file);
                foreach ($wrote as $wk => $wv) {
                    if (is_array($wv)) {
                        foreach ($wv as $wwk => $wwv) {
                            $log->writeLine('Wrote ' . $wk . ' as ' . $wwv . ' to ' . $file);
                        }
                    } else {
                        $log->writeLine('Wrote ' . $wk . ' as ' . $wv . ' to ' . $file);
                    }
                }
                //If all goes well, we move the track to the completed folder.
                if (copy($tmploc . DIRECTORY_SEPARATOR . $file, $finalloc . DIRECTORY_SEPARATOR . $file)) {
                    //Write success message to the log.
                    scanLog("Wrote metadata to {$finalloc}" . DIRECTORY_SEPARATOR . "{$file}");
                }
            }
            $i++;
        }
        //Final clean-up tasks. Moving other resources like cover art and sample clips over to the completed folder and removing temp files.
        ifecho('Track processing complete. Cleaning up...');
        copyResources($depth, $finalloc);
        $emptydir = glob($tmploc . DIRECTORY_SEPARATOR . "*");
        foreach ($emptydir as $eraseme) {
            if (is_file($eraseme)) {
                unlink($eraseme);
            }
        }
        rmdir($tmploc);
        ifecho('Completed clean up. Album ' . $album->sku . ' complete.');
        $log->writeMetaBlock($album);
        $log->closeLog($album->sku);
        unset($album, $tracks, $ddexml, $xml);
        return true;
    } else {
        trigger_error("No track data found in XML.");
        return false;
    }
}