Reformatted to Campware PHP coding conventions.

This commit is contained in:
paul 2006-10-17 16:22:18 +00:00
parent a988446e12
commit c63be46437
1 changed files with 102 additions and 49 deletions

View File

@ -7,29 +7,37 @@ ini_set('memory_limit', '64M');
header("Content-type: text/plain");
echo "\n#StorageServer import script:\n";
//echo date('H:i:s')."\n";
$start=intval(date('U'));
$start = intval(date('U'));
require_once 'conf.php';
require_once "$storageServerPath/var/conf.php";
require_once 'DB.php';
require_once "$storageServerPath/var/GreenBox.php";
require_once('conf.php');
require_once("$storageServerPath/var/conf.php");
require_once('DB.php');
require_once("$storageServerPath/var/GreenBox.php");
//PEAR::setErrorHandling(PEAR_ERROR_PRINT, "%s<hr>\n");
PEAR::setErrorHandling(PEAR_ERROR_RETURN);
//PEAR::setErrorHandling(PEAR_ERROR_DIE, "%s\n");
$dbc = DB::connect($config['dsn'], TRUE);
if(PEAR::isError($dbc)){ echo "ERROR: ".$dbc->getMessage()." ".$dbc->getUserInfo()."\n"; exit(1); }
if (PEAR::isError($dbc)) {
echo "ERROR: ".$dbc->getMessage()." ".$dbc->getUserInfo()."\n";
exit(1);
}
$dbc->setFetchMode(DB_FETCHMODE_ASSOC);
$gb = &new GreenBox($dbc, $config);
$testonly = (isset($argv[1]) && $argv[1] == '-t');
$errors=0;
$filecount=0;
function _err($r, $fn, $txt=''){
$errors = 0;
$filecount = 0;
function _err($r, $fn, $txt='')
{
global $errors;
if(PEAR::isError($r)) $msg = $r->getMessage()." ".$r->getUserInfo();
else $msg = $txt;
if (PEAR::isError($r)) {
$msg = $r->getMessage()." ".$r->getUserInfo();
} else {
$msg = $txt;
}
echo "ERROR\n $msg\n";
$errors++;
}
@ -90,35 +98,44 @@ $flds = array(
$titleKey = 'dc:title';
$r = $gb->getObjId('import', $gb->storId);
if(PEAR::isError($r)){ echo "ERROR: ".$r->getMessage()." ".$r->getUserInfo()."\n"; exit(1); }
if(is_null($r)){
if (PEAR::isError($r)) {
echo "ERROR: ".$r->getMessage()." ".$r->getUserInfo()."\n";
exit(1);
}
if (is_null($r)) {
$r = $gb->bsCreateFolder($gb->storId, 'import');
if(PEAR::isError($r)){ echo "ERROR: ".$r->getMessage()." ".$r->getUserInfo()."\n"; exit(1); }
if (PEAR::isError($r)) {
echo "ERROR: ".$r->getMessage()." ".$r->getUserInfo()."\n";
exit(1);
}
}
$parid = $r;
function addMdata($key, $val, $iEnc='iso-8859-1'){
function addMdata($key, $val, $iEnc='iso-8859-1')
{
global $mdata, $titleHaveSet, $titleKey;
#echo "$key($iEnc): $val\n";
if(!is_null($val)){
if (!is_null($val)) {
$data = $val;
$oEnc = 'UTF-8';
if(function_exists('iconv') && $iEnc != $oEnc){
if (function_exists('iconv') && $iEnc != $oEnc) {
$data = $r = @iconv($iEnc, $oEnc, $data);
if($r === FALSE){
if ($r === FALSE) {
echo "Warning: convert $key data to unicode failed\n";
$data = $val; // fallback
}
}
if($key == $titleKey) $titleHaveSet = TRUE;
if ($key == $titleKey) {
$titleHaveSet = TRUE;
}
$mdata[$key] = trim($data);
}
}
$stdin = fopen('php://stdin', 'r');
while($filename = fgets($stdin, 2048)){
while ($filename = fgets($stdin, 2048)) {
$filename = rtrim($filename);
if(!preg_match('/\.(ogg|wav|mp3|mpg|mpeg)$/', strtolower($filename), $var)){
if (!preg_match('/\.(ogg|wav|mp3|mpg|mpeg)$/', strtolower($filename), $var)) {
// echo "File extension not supported - skipping file\n";
continue;
}
@ -129,16 +146,27 @@ while($filename = fgets($stdin, 2048)){
$getID3 = new getID3;
$infoFromFile = $getID3->analyze("$filename");
//echo "\n".var_export($infoFromFile)."\n"; exit;
if(PEAR::isError($infoFromFile)){ _err($infoFromFile, $filename); continue; }
if(isset($infoFromFile['error'])){ _err(NULL, $filename, $infoFromFile['error']); continue; }
if (PEAR::isError($infoFromFile)) {
_err($infoFromFile, $filename);
continue;
}
if (isset($infoFromFile['error'])) {
_err(NULL, $filename, $infoFromFile['error']);
continue;
}
#if(!$infoFromFile['fileformat']){ echo "???\n"; continue; }
if(!$infoFromFile['bitrate']){ echo "not audio?\n"; continue; }
if (!$infoFromFile['bitrate']) {
echo "not audio?\n";
continue;
}
$mdata = array();
$titleHaveSet = FALSE;
foreach($flds as $key=>$getid3keys){
if($testonly) echo "$key\n";
foreach($getid3keys as $getid3key){
foreach ($flds as $key => $getid3keys) {
if ($testonly) {
echo "$key\n";
}
foreach ($getid3keys as $getid3key) {
// defaults:
$ignoreEnc = FALSE;
$dataPath = "";
@ -146,41 +174,63 @@ while($filename = fgets($stdin, 2048)){
$enc = "UTF-8";
extract($getid3key);
$vn = "\$infoFromFile$path$dataPath";
if($testonly) echo " $vn -> ";
if ($testonly) {
echo " $vn -> ";
}
eval("\$vnFl = isset($vn);");
if($vnFl){
if ($vnFl) {
eval("\$data = $vn;");
if($testonly) echo "$data\n";
if(!$ignoreEnc && $encPath != ""){
if ($testonly) {
echo "$data\n";
}
if (!$ignoreEnc && $encPath != "") {
$encVn = "\$infoFromFile$path$encPath";
eval("\$encVnFl = isset($encVn);");
if($encVnFl){
eval("\$enc = $encVn;");
}
if ($encVnFl) {
eval("\$enc = $encVn;");
}
}
if ($testonly) {
echo " ENC=$enc\n";
}
if($testonly) echo " ENC=$enc\n";
//addMdata($key, $data);
addMdata($key, $data, $enc);
break;
}else{
if($testonly) echo "\n";
} else {
if ($testonly) {
echo "\n";
}
}
}
}
if($testonly) var_dump($mdata);
if ($testonly) {
var_dump($mdata);
}
if(!$titleHaveSet || trim($mdata[$titleKey])=='') addMdata($titleKey, basename($filename));
if (!$titleHaveSet || trim($mdata[$titleKey]) == '') {
addMdata($titleKey, basename($filename));
}
if(!$testonly){
if (!$testonly) {
$r = $gb->bsPutFile($parid, $mdata['ls:filename'], "$filename", "$storageServerPath/var/emptyMdata.xml", NULL, 'audioclip');
if(PEAR::isError($r)){ _err($r, $filename); echo var_export($mdata)."\n"; continue; }
if (PEAR::isError($r)) {
_err($r, $filename);
echo var_export($mdata)."\n";
continue;
}
$id = $r;
$r = $gb->bsSetMetadataBatch($id, $mdata);
if(PEAR::isError($r)){ _err($r, $filename); echo var_export($mdata)."\n"; continue; }
}else{
var_dump($infoFromFile); echo"======================= ";
var_dump($mdata); echo"======================= ";
if (PEAR::isError($r)) {
_err($r, $filename);
echo var_export($mdata)."\n";
continue;
}
} else {
var_dump($infoFromFile);
echo"======================= ";
var_dump($mdata);
echo"======================= ";
}
echo "OK\n";
@ -188,10 +238,13 @@ while($filename = fgets($stdin, 2048)){
}
fclose($stdin);
$end = intval(date('U'));
$end = intval(date('U'));
//echo date('H:i:s')."\n";
$time = $end-$start;
if($time>0) $speed = round(($filecount+$errors)/$time, 1);
else $speed = "N/A";
$time = $end - $start;
if ($time > 0) {
$speed = round(($filecount+$errors)/$time, 1);
} else {
$speed = "N/A";
}
echo " Files ".($testonly ? "analyzed" : "imported").": $filecount, in $time s, $speed files/s, errors: $errors\n";
?>