Merge branch 'master' of dev.sourcefabric.org:campcaster

This commit is contained in:
naomiaro 2010-09-22 10:43:55 -04:00
commit a3e8e41f33
8 changed files with 197 additions and 276 deletions

View File

@ -200,15 +200,6 @@ function camp_import_audio_file($p_filepath, $p_importMode = null, $p_testOnly =
echo "Importing: [".sprintf("%05d",$g_fileCount+1)."] $p_filepath\n";
$metadata = camp_get_audio_metadata($p_filepath, $p_testOnly);
if (PEAR::isError($metadata)) {
import_err($metadata);
return;
}
// bsSetMetadataBatch doesnt like these values
unset($metadata['audio']);
unset($metadata['playtime_seconds']);
if (!$p_testOnly) {
if ($p_importMode == "copy") {
$doCopyFiles = true;
@ -216,36 +207,15 @@ function camp_import_audio_file($p_filepath, $p_importMode = null, $p_testOnly =
$doCopyFiles = false;
}
$values = array(
"filename" => $metadata['ls:filename'],
"filepath" => $p_filepath,
"metadata" => "$STORAGE_SERVER_PATH/var/emptyMdata.xml",
"gunid" => NULL,
"filetype" => "audioclip",
"md5" => $md5sum,
"mime" => $metadata['dc:format']
);
// $timeBegin = microtime(true);
$storedFile = $greenbox->bsPutFile($values, $doCopyFiles);
if (PEAR::isError($storedFile)) {
import_err($storedFile, "Error in bsPutFile()");
echo var_export($metadata)."\n";
return;
}
$id = $storedFile->getId();
// $timeEnd = microtime(true);
// echo " * Store file time: ".($timeEnd-$timeBegin)."\n";
// Note: the bsSetMetadataBatch() takes up .25 of a second
// on my 3Ghz computer. We should try to speed this up.
// $timeBegin = microtime(true);
$r = $greenbox->bsSetMetadataBatch($id, $metadata);
if (PEAR::isError($r)) {
import_err($r, "Error in bsSetMetadataBatch()");
echo var_export($metadata)."\n";
return;
}
// $timeEnd = microtime(true);
// echo " * Metadata store time: ".($timeEnd-$timeBegin)."\n";
} else {
echo "==========================================================================\n";
echo "METADATA\n";

View File

@ -101,34 +101,16 @@ class BasicStor {
/**
* Store new file in the storage
*
* @param int $p_parentId
* Parent id
* @param array $p_values
* See StoredFile::Insert() for details.
* @param boolean $copyMedia
* copy the media file if true, make symlink if false
* @return int|PEAR_Error
* ID of the StoredFile that was created.
* @return StoredFile|PEAR_Error
* The StoredFile that was created.
*/
public function bsPutFile($p_values, $p_copyMedia=TRUE)
{
if (!isset($p_values['filetype']) || !isset($p_values['filename'])) {
return NULL;
}
$ftype = strtolower($p_values['filetype']);
$storedFile = StoredFile::Insert($p_values, $p_copyMedia);
if (PEAR::isError($storedFile)) {
$res = BasicStor::RemoveObj($id);
// catch constraint violations
switch ($storedFile->getCode()) {
case -3:
return PEAR::raiseError(
"BasicStor::bsPutFile: gunid duplication",
GBERR_GUNID);
default:
return $storedFile;
}
}
return $storedFile;
} // fn bsPutFile
@ -857,55 +839,34 @@ class BasicStor {
* Metadata element identification (e.g. dc:title)
* @param string $value
* value to store, if NULL then delete record
* @param string $lang
* xml:lang value for select language version
* @param int $mid
* (optional on unique elements) metadata record id
* @param string $container
* container element name for insert
* @param boolean $regen
* flag, if true, regenerate XML file
* @return boolean
*/
public static function bsSetMetadataValue($p_id, $p_category, $p_value)/*,
$lang=NULL, $mid=NULL, $container='metadata', $regen=TRUE)*/
public static function bsSetMetadataValue($p_id, $p_category, $p_value)
{
global $CC_CONFIG, $CC_DBC;
if (!is_string($p_category) || is_array($p_value)) {
return FALSE;
}
if (is_a($p_id, "StoredFile")) {
$storedFile =& $p_id;
} else {
$storedFile = StoredFile::Recall($p_id);
if (is_null($storedFile) || PEAR::isError($storedFile)) {
return $storedFile;
}
}
if ($p_category == 'dcterms:extent') {
$p_value = BasicStor::NormalizeExtent($p_value);
}
$columnName = BasicStor::xmlCategoryToDbColumn($p_category); // Get column name
global $CC_CONFIG, $CC_DBC;
if (!is_string($p_category) || is_array($p_value)) {
return FALSE;
}
if (is_a($p_id, "StoredFile")) {
$p_id = $p_id->getId();
}
if ($p_category == 'dcterms:extent') {
$p_value = BasicStor::NormalizeExtent($p_value);
}
$columnName = BasicStor::xmlCategoryToDbColumn($p_category); // Get column name
if (!is_null($columnName)) {
$escapedValue = pg_escape_string($p_value);
$sql = "UPDATE ".$CC_CONFIG["filesTable"]
." SET $columnName='$escapedValue'"
." WHERE id=".$storedFile->getId();
//var_dump($sql);
//$res = $storedFile->md->setMetadataValue($category, $value, $lang, $mid, $container);
$res = $CC_DBC->query($sql);
if (PEAR::isError($res)) {
return $res;
}
if (!is_null($columnName)) {
$escapedValue = pg_escape_string($p_value);
$sql = "UPDATE ".$CC_CONFIG["filesTable"]
." SET $columnName='$escapedValue'"
." WHERE id=$p_id";
//var_dump($sql);
$res = $CC_DBC->query($sql);
if (PEAR::isError($res)) {
return $res;
}
// if ($regen) {
// $r = $storedFile->md->regenerateXmlFile();
// if (PEAR::isError($r)) {
// return $r;
// }
// }
// return $res;
}
return TRUE;
}
@ -936,46 +897,37 @@ class BasicStor {
* @param array $values
* array of key/value pairs
* (e.g. 'dc:title'=>'New title')
* @param string $lang
* xml:lang value for select language version
* @param string $container
* Container element name for insert
* @param boolean $regen
* flag, if true, regenerate XML file
* @return boolean
*/
public static function bsSetMetadataBatch(
$id, $values, $lang=NULL, $container='metadata', $regen=TRUE)
public static function bsSetMetadataBatch($id, $values)
{
if (!is_array($values)) {
$values = array($values);
global $CC_CONFIG, $CC_DBC;
if (!is_array($values)) {
$values = array($values);
}
if (is_a($id, "StoredFile")) {
$storedFile =& $id;
} else {
$storedFile = StoredFile::Recall($id);
if (is_null($storedFile) || PEAR::isError($storedFile)) {
return $storedFile;
}
}
foreach ($values as $category => $oneValue) {
$columnName = BasicStor::xmlCategoryToDbColumn($category);
if (!is_null($columnName)) {
if ($category == 'dcterms:extent') {
$oneValue = BasicStor::NormalizeExtent($oneValue);
}
$escapedValue = pg_escape_string($oneValue);
$sqlValues[] = "$columnName = '$escapedValue'";
}
if (is_a($id, "StoredFile")) {
$storedFile =& $id;
} else {
$storedFile = StoredFile::Recall($id);
if (is_null($storedFile) || PEAR::isError($storedFile)) {
return $storedFile;
}
}
foreach ($values as $category => $oneValue) {
$res = BasicStor::bsSetMetadataValue($storedFile, $category,
$oneValue/*, $lang, NULL, $container, FALSE*/);
if (PEAR::isError($res)) {
return $res;
}
}
// if ($regen) {
// $storedFile = StoredFile::Recall($id);
// if (is_null($storedFile) || PEAR::isError($storedFile)) {
// return $storedFile;
// }
// $r = $storedFile->md->regenerateXmlFile();
// if (PEAR::isError($r)) {
// return $r;
// }
// }
return TRUE;
}
$sql = "UPDATE ".$CC_CONFIG["filesTable"]
." SET ".join(",", $sqlValues)
." WHERE id=$id";
$CC_DBC->query($sql);
return TRUE;
}
/**
@ -1869,7 +1821,7 @@ class BasicStor {
$res = preg_match("|^([0-9a-fA-F]{16})?$|", $p_gunid);
return $res;
}
/**
* Set playlist edit flag
*

View File

@ -44,13 +44,14 @@ class ScheduleItem {
// }
/**
* Add a music clip or playlist to the schedule.
*
* @param $p_audioFileId
* @param $p_playlistId
* @param $p_datetime
* @param $p_options
* @return int|null
* Return null if the item could not be added.
* @return int|PEAR_Error
* Return PEAR_Error if the item could not be added.
*/
public function add($p_datetime, $p_audioFileId = null, $p_playlistId = null, $p_options = null) {
global $CC_CONFIG, $CC_DBC;
@ -60,14 +61,17 @@ class ScheduleItem {
// Load existing track
$track = StoredFile::Recall($p_audioFileId);
if (is_null($track)) {
return null;
return new PEAR_Error("Could not find audio track.");
}
// Check if there are any conflicts with existing entries
$metadata = $track->getMetadata();
$length = trim($metadata["length"]);
if (empty($length)) {
return new PEAR_Error("Length is empty.");
}
if (!Schedule::isScheduleEmptyInRange($p_datetime, $length)) {
return null;
return new PEAR_Error("Schedule conflict.");
}
// Insert into the table
@ -78,7 +82,11 @@ class ScheduleItem {
." VALUES ($id, 0, TIMESTAMP '$p_datetime', "
." (TIMESTAMP '$p_datetime' + INTERVAL '$length'),"
." {$this->groupId}, $p_audioFileId)";
$CC_DBC->query($sql);
$result = $CC_DBC->query($sql);
if (PEAR::isError($result)) {
var_dump($sql);
return $result;
}
return $this->groupId;
} elseif (!is_null($p_playlistId)){
@ -136,9 +144,14 @@ class Schedule {
*
* @param string $p_datetime
* @param string $p_length
*
* @return boolean|PEAR_Error
*/
public static function isScheduleEmptyInRange($p_datetime, $p_length) {
global $CC_CONFIG, $CC_DBC;
if (empty($p_length)) {
return new PEAR_Error("Schedule::isSchedulerEmptyInRange: param p_length is empty.");
}
$sql = "SELECT COUNT(*) FROM ".$CC_CONFIG["scheduleTable"]
." WHERE (starts <= '$p_datetime') "
." AND (ends >= (TIMESTAMP '$p_datetime' + INTERVAL '$p_length'))";

View File

@ -459,52 +459,70 @@ class StoredFile {
}
}
}
/* ========= 'factory' methods - should be called to construct StoredFile */
/**
* Create instance of StoredFile object and insert new file
*
* @param array $p_values
* "id" - optional, local object id in the tree
* "gunid" - optional, unique id, for insert file with gunid
* "filename" - optional
* "filepath" - local path to media file, not needed for Playlist
* "metadata" - local path to metadata XML file or XML string
* "filetype" - internal file type
* "mime" - MIME type, highly recommended to pass in
* "md5" - MD5 sum, highly recommended to pass in
* "filepath" - required, local path to media file
* "id" - optional, local object id, will be generated if not given
* "gunid" - optional, unique id, for insert file with gunid, will be generated if not given
* "filename" - optional, will use "filepath" if not given
* "metadata" - optional, array of extra metadata, will be automatically calculated if not given.
* "mime" - optional, MIME type, highly recommended to pass in, will be automatically calculated if not given.
* "md5" - optional, MD5 sum, highly recommended to pass in, will be automatically calculated if not given.
*
* @param boolean $p_copyMedia
* copy the media file if true, make symlink if false
*
* @return StoredFile|NULL|PEAR_Error
*/
public static function Insert($p_values, $p_copyMedia=TRUE)
{
global $CC_CONFIG, $CC_DBC;
if (!isset($p_values["filepath"])) {
return new PEAR_Error("StoredFile::Insert: filepath not set.");
}
if (!file_exists($p_values['filepath'])) {
return PEAR::raiseError("StoredFile::Insert: ".
"media file not found ({$p_values['filepath']})");
}
$gunid = isset($p_values['gunid'])?$p_values['gunid']:NULL;
// Create the StoredFile object
$storedFile = new StoredFile($gunid);
$storedFile->name = isset($p_values['filename']) ? $p_values['filename'] : $storedFile->gunid;
// Get metadata
if (isset($p_values["metadata"])) {
$metadata = $p_values['metadata'];
} else {
$metadata = camp_get_audio_metadata($p_values["filepath"]);
}
$storedFile->name = isset($p_values['filename']) ? $p_values['filename'] : $p_values["filepath"];
// NOTE: POSTGRES-SPECIFIC KEYWORD "DEFAULT" BEING USED, WOULD BE "NULL" IN MYSQL
$storedFile->id = isset($p_values['id']) && is_integer($p_values['id'])?"'".$p_values['id']."'":'DEFAULT';
$storedFile->ftype = $p_values['filetype'];
if ($storedFile->ftype == 'playlist') {
$storedFile->mime = 'application/smil';
} else {
$storedFile->mime = (isset($p_values["mime"]) ? $p_values["mime"] : NULL );
}
# $storedFile->filepath = $p_values['filepath'];
$storedFile->ftype = isset($p_values['filetype']) ? strtolower($p_values['filetype']) : "audioclip";
$storedFile->mime = (isset($p_values["mime"]) ? $p_values["mime"] : NULL );
// $storedFile->filepath = $p_values['filepath'];
if (isset($p_values['md5'])) {
$storedFile->md5 = $p_values['md5'];
} elseif (file_exists($p_values['filepath'])) {
# echo "StoredFile::Insert: WARNING: Having to recalculate MD5 value\n";
//echo "StoredFile::Insert: WARNING: Having to recalculate MD5 value\n";
$storedFile->md5 = md5_file($p_values['filepath']);
}
// Check for duplicates -- return duplicate
$duplicate = StoredFile::RecallByMd5($storedFile->md5);
if ($duplicate) {
return $duplicate;
}
$storedFile->exists = FALSE;
$emptyState = TRUE;
// Insert record into the database
$escapedName = pg_escape_string($storedFile->name);
@ -528,52 +546,22 @@ class StoredFile {
$sql = "SELECT currval('".$CC_CONFIG["filesSequence"]."_seq')";
$storedFile->id = $CC_DBC->getOne($sql);
}
// Insert metadata
$metadata = $p_values['metadata'];
BasicStor::bsSetMetadataBatch($storedFile->id, $metadata);
// $mdataLoc = ($metadata[0]=="/")? "file":"string";
// for non-absolute paths:
// $mdataLoc = ($metadata[0]!="<")? "file":"string";
// if (is_null($metadata) || ($metadata == '') ) {
// $metadata = dirname(__FILE__).'/emptyMdata.xml';
// $mdataLoc = 'file';
// } else {
// $emptyState = FALSE;
// }
// if ( ($mdataLoc == 'file') && !file_exists($metadata)) {
// return PEAR::raiseError("StoredFile::Insert: ".
// "metadata file not found ($metadata)");
// }
// $res = $storedFile->md->insert($metadata, $mdataLoc, $storedFile->ftype);
// if (PEAR::isError($res)) {
// $CC_DBC->query("ROLLBACK");
// return $res;
// }
// Save media file
if (!empty($p_values['filepath'])) {
if (!file_exists($p_values['filepath'])) {
return PEAR::raiseError("StoredFile::Insert: ".
"media file not found ({$p_values['filepath']})");
}
$res = $storedFile->addFile($p_values['filepath'], $p_copyMedia);
if (PEAR::isError($res)) {
echo "StoredFile::Insert: ERROR adding file: '".$res->getMessage()."'\n";
$CC_DBC->query("ROLLBACK");
return $res;
}
if (empty($storedFile->mime)) {
# echo "StoredFile::Insert: WARNING: Having to recalculate MIME value\n";
$storedFile->setMime($storedFile->getMime());
}
$emptyState = FALSE;
$res = $storedFile->addFile($p_values['filepath'], $p_copyMedia);
if (PEAR::isError($res)) {
echo "StoredFile::Insert: ERROR adding file: '".$res->getMessage()."'\n";
$CC_DBC->query("ROLLBACK");
return $res;
}
if (empty($storedFile->mime)) {
//echo "StoredFile::Insert: WARNING: Having to recalculate MIME value\n";
$storedFile->setMime($storedFile->getMime());
}
// Save state
if (!$emptyState) {
$res = $storedFile->setState('ready');
}
$storedFile->setState('ready');
// Commit changes
$res = $CC_DBC->query("COMMIT");
@ -1160,7 +1148,7 @@ class StoredFile {
* Returns gunIds of the playlists the stored file is in.
* TODO update this to work with new tables.
*/
/*
public function getPlaylists() {
global $CC_CONFIG, $CC_DBC;
@ -1176,7 +1164,7 @@ class StoredFile {
return $playlists;
}
*/
*/
/**

View File

@ -4,11 +4,11 @@ require_once('DB.php');
require_once('PHPUnit.php');
require_once 'BasicStorTests.php';
require_once 'SchedulerTests.php';
require_once 'PlayListTests.php';
//require_once 'PlayListTests.php';
$suite = new PHPUnit_TestSuite("BasicStorTest");
$suite->addTestSuite("SchedulerTests");
$suite->addTestSuite("PlayListTests");
//$suite->addTestSuite("PlayListTests");
$result = PHPUnit::run($suite);
echo $result->toString();

View File

@ -42,41 +42,27 @@ class BasicStorTest extends PHPUnit_TestCase {
//$this->assertTrue(FALSE);
}
function testPutFile() {
function testDeleteAndPutFile() {
$STORAGE_SERVER_PATH = dirname(__FILE__)."/../../";
$filePath = dirname(__FILE__)."/ex1.mp3";
$md5sum = md5_file($filePath);
$metadata = camp_get_audio_metadata($filePath);
if (PEAR::isError($metadata)) {
$this->fail($metadata->getMessage());
return;
$md5 = md5_file($filePath);
$duplicate = StoredFile::RecallByMd5($md5);
if ($duplicate) {
$duplicate->delete();
}
// bsSetMetadataBatch doesnt like these values
unset($metadata['audio']);
unset($metadata['playtime_seconds']);
$values = array(
"filename" => $metadata['ls:filename'],
"filepath" => $filePath,
"metadata" => "$STORAGE_SERVER_PATH/var/emptyMdata.xml",
"gunid" => NULL,
"filetype" => "audioclip",
"md5" => $md5sum,
"mime" => $metadata['dc:format']
);
$values = array("filepath" => $filePath);
$storedFile = $this->greenbox->bsPutFile($values, false);
$this->assertFalse(PEAR::isError($storedFile));
if (PEAR::isError($storedFile)) {
$this->fail("Failed to create StoredFile: ".$storedFile->getMessage());
return;
}
$id = $storedFile->getId();
if (!is_numeric($id)) {
$this->fail("StoredFile not created correctly. id = ".$id);
return;
}
$r = $this->greenbox->bsSetMetadataBatch($storedFile, $metadata);
if (PEAR::isError($r)) {
$this->fail($r->getMessage());
//echo var_export($metadata)."\n";
return;
}
}

View File

@ -3,68 +3,80 @@ require_once(dirname(__FILE__)."/../Schedule.php");
class SchedulerTests extends PHPUnit_TestCase {
private $groupIdCreated;
private $storedFileId;
private $groupIdCreated;
private $storedFile;
function setup() {
$this->storedFileId = '192';
function setup() {
global $CC_CONFIG, $CC_DBC;
// Clear the files table
$sql = "DELETE FROM ".$CC_CONFIG["filesTable"];
$CC_DBC->query($sql);
// Add a file
$values = array("filepath" => dirname(__FILE__)."/test10001.mp3");
$this->storedFile = StoredFile::Insert($values, false);
// Clear the schedule table
$sql = "DELETE FROM ".$CC_CONFIG["scheduleTable"];
$CC_DBC->query($sql);
}
function testDateToId() {
$dateStr = "2006-04-02 10:20:08.123456";
$id = ScheduleItem::dateToId($dateStr);
$expected = "20060402102008123";
if ($id != $expected) {
$this->fail("Did not convert date to ID correctly #1: $id != $expected");
}
function testDateToId() {
$dateStr = "2006-04-02 10:20:08.123456";
$id = ScheduleItem::dateToId($dateStr);
$expected = "20060402102008123";
if ($id != $expected) {
$this->fail("Did not convert date to ID correctly #1: $id != $expected");
}
$dateStr = "2006-04-02 10:20:08";
$id = ScheduleItem::dateToId($dateStr);
$expected = "20060402102008000";
if ($id != $expected) {
$this->fail("Did not convert date to ID correctly #2: $id != $expected");
}
}
$dateStr = "2006-04-02 10:20:08";
$id = ScheduleItem::dateToId($dateStr);
$expected = "20060402102008000";
if ($id != $expected) {
$this->fail("Did not convert date to ID correctly #2: $id != $expected");
}
function testAddAndRemove() {
$i = new ScheduleItem();
$this->groupIdCreated = $i->add('2010-10-10 01:30:23', $this->storedFile->getId());
if (PEAR::isError($this->groupIdCreated)) {
$this->fail("Failed to create scheduled item: ". $this->groupIdCreated->getMessage());
}
function testAddAndRemove() {
$i = new ScheduleItem();
$this->groupIdCreated = $i->add('2010-10-10 01:30:23', $this->storedFileId);
if (!is_numeric($this->groupIdCreated)) {
$this->fail("Failed to create scheduled item.");
}
$i = new ScheduleItem($this->groupIdCreated);
$result = $i->remove();
if ($result != 1) {
$this->fail("Did not remove item.");
}
$i = new ScheduleItem($this->groupIdCreated);
$result = $i->remove();
if ($result != 1) {
$this->fail("Did not remove item.");
}
}
function testIsScheduleEmptyInRange() {
$i = new ScheduleItem();
$this->groupIdCreated = $i->add('2011-10-10 01:30:23', $this->storedFileId);
if (Schedule::isScheduleEmptyInRange('2011-10-10 01:30:23', '00:00:01.432153')) {
$this->fail("Reporting empty schedule when it isnt.");
}
$i->remove();
if (!Schedule::isScheduleEmptyInRange('2011-10-10 01:30:23', '00:00:01.432153')) {
$this->fail("Reporting booked schedule when it isnt.");
}
function testIsScheduleEmptyInRange() {
$i = new ScheduleItem();
$this->groupIdCreated = $i->add('2011-10-10 01:30:23', $this->storedFile->getId());
if (Schedule::isScheduleEmptyInRange('2011-10-10 01:30:23', '00:00:01.432153')) {
$this->fail("Reporting empty schedule when it isnt.");
}
$i->remove();
if (!Schedule::isScheduleEmptyInRange('2011-10-10 01:30:23', '00:00:01.432153')) {
$this->fail("Reporting booked schedule when it isnt.");
}
}
function testGetItems() {
$i1 = new ScheduleItem();
$groupId1 = $i1->add('2008-01-01 12:00:00.000', $this->storedFileId);
$i2 = new ScheduleItem();
$i2->addAfter($groupId1, $this->storedFileId);
$items = Schedule::GetItems("2008-01-01", "2008-01-02");
if (count($items) != 2) {
$this->fail("Wrong number of items returned.");
return;
}
$i1->remove();
$i2->remove();
function testGetItems() {
$i1 = new ScheduleItem();
$groupId1 = $i1->add('2008-01-01 12:00:00.000', $this->storedFile->getId());
$i2 = new ScheduleItem();
$i2->addAfter($groupId1, $this->storedFile->getId());
$items = Schedule::GetItems("2008-01-01", "2008-01-02");
if (count($items) != 2) {
$this->fail("Wrong number of items returned.");
return;
}
$i1->remove();
$i2->remove();
}
}
?>