Fixed some API stuff to return the right values for pypo.

Created a test script to schedule a test file one minute after running it.
Moved the database connection initialization to conf.php instead of in
ui_conf.php.
Changed some includes to rely on the pear path instead of specifying it
directly in the include.  This will make it easier to use the system
defaults (for Ubuntu/Debian for example).
This commit is contained in:
paul.baranowski 2010-11-23 19:21:05 -05:00
parent ef8b8dc54d
commit 8e7e0226e6
11 changed files with 289 additions and 68 deletions

View file

@ -57,16 +57,23 @@ class Playlist {
private $categories = array("dc:title" => "DbName", "dc:creator" => "DbCreator", "dc:description" => "DbDescription", "dcterms:extent" => "length");
/**
* @param string $p_gunid
*/
public function __construct($p_gunid=NULL)
{
}
public static function Insert($p_values)
/**
* @param array $p_name
* The name of the playlist
*/
private static function Insert($p_name = null)
{
// Create the StoredPlaylist object
$storedPlaylist = new Playlist();
$storedPlaylist->name = isset($p_values['filename']) ? $p_values['filename'] : date("H:i:s");
$storedPlaylist->name = !empty($p_name) ? $p_name : date("H:i:s");
$storedPlaylist->mtime = new DateTime("now");
$pl = new CcPlaylist();
@ -325,8 +332,9 @@ class Playlist {
->findPK($this->id)
->computeLastPosition();
if(is_null($res))
return 0;
if(is_null($res)) {
return 0;
}
return $res + 1;
}
@ -355,8 +363,9 @@ class Playlist {
->findPK($this->id)
->computeLength();
if(is_null($res))
return '00:00:00.000000';
if(is_null($res)) {
return '00:00:00.000000';
}
return $res;
}
@ -370,12 +379,19 @@ class Playlist {
*/
public function create($fname=NULL)
{
$values = array("filename" => $fname);
$pl_id = Playlist::Insert($values);
$pl_id = Playlist::Insert($fname);
$this->id = $pl_id;
return $this->id;
}
public static function findPlaylistByName($p_name)
{
$res = CcPlaylistQuery::create()->findByDbName($p_name);
return $res;
}
/**
* Lock playlist for edit
*

View file

@ -213,11 +213,13 @@ class Schedule {
}
/**
* Return true if there is nothing in the schedule for the given times.
* Return true if there is nothing in the schedule for the given start time
* up to the length of time after that.
*
* @param string $p_datetime
* In the format YYYY-MM-DD HH:MM:SS.mmmmmm
* @param string $p_length
*
* In the format HH:MM:SS.mmmmmm
* @return boolean|PEAR_Error
*/
public static function isScheduleEmptyInRange($p_datetime, $p_length) {
@ -268,7 +270,8 @@ class Schedule {
* "start"/"starts" (aliases to the same thing) as YYYY-MM-DD HH:MM:SS.nnnnnn
* "end"/"ends" (aliases to the same thing) as YYYY-MM-DD HH:MM:SS.nnnnnn
* "group_id"/"id" (aliases to the same thing)
* "clip_length" (for playlists only, this is the length of the entire playlist)
* "clip_length" (for audio clips this is the length of the audio clip,
* for playlists this is the length of the entire playlist)
* "name" (playlist only)
* "creator" (playlist only)
* "file_id" (audioclip only)
@ -339,18 +342,85 @@ class Schedule {
}
private static function CcTimeToPypoTime($p_time) {
/**
* Convert a time string in the format "YYYY-MM-DD HH:mm:SS"
* to "YYYY-MM-DD-HH-mm-SS".
*
* @param string $p_time
* @return string
*/
private static function CcTimeToPypoTime($p_time)
{
$p_time = substr($p_time, 0, 19);
$p_time = str_replace(" ", "-", $p_time);
$p_time = str_replace(":", "-", $p_time);
return $p_time;
}
private static function PypoTimeToCcTime($p_time) {
/**
* Convert a time string in the format "YYYY-MM-DD-HH-mm-SS" to
* "YYYY-MM-DD HH:mm:SS".
*
* @param string $p_time
* @return string
*/
private static function PypoTimeToCcTime($p_time)
{
$t = explode("-", $p_time);
return $t[0]."-".$t[1]."-".$t[2]." ".$t[3].":".$t[4].":00";
}
/**
* Converts a time value as a string (with format HH:MM:SS.mmmmmm) to
* millisecs.
*
* @param string $p_time
* @return int
*/
private static function WallTimeToMillisecs($p_time)
{
$t = explode(":", $p_time);
$millisecs = 0;
if (strpos($t[2], ".")) {
$secParts = explode(".", $t[2]);
$millisecs = $secParts[1];
$millisecs = substr($millisecs, 0, 3);
$millisecs = intval($millisecs);
$seconds = intval($secParts[0]);
} else {
$seconds = intval($t[2]);
}
$ret = $millisecs + ($seconds * 1000) + ($t[1] * 60 * 1000) + ($t[0] * 60 * 60 * 1000);
return $ret;
}
/**
* Compute the difference between two times in the format "HH:MM:SS.mmmmmm".
* Note: currently only supports calculating millisec differences.
*
* @param string $p_time1
* @param string $p_time2
* @return double
*/
private static function TimeDiff($p_time1, $p_time2)
{
$parts1 = explode(".", $p_time1);
$parts2 = explode(".", $p_time2);
$diff = 0;
if ( (count($parts1) > 1) && (count($parts2) > 1) ) {
$millisec1 = substr($parts1[1], 0, 3);
$millisec1 = str_pad($millisec1, 3, "0");
$millisec1 = intval($millisec1);
$millisec2 = substr($parts2[1], 0, 3);
$millisec2 = str_pad($millisec2, 3, "0");
$millisec2 = intval($millisec2);
$diff = abs(millisec1 - millisec2)/1000;
}
return $diff;
}
/**
* Export the schedule in json formatted for pypo (the liquidsoap scheduler)
*
@ -404,14 +474,20 @@ class Schedule {
{
$storedFile = StoredFile::Recall($item["file_id"]);
$uri = $storedFile->getFileUrl();
// For pypo, a cueout of zero means no cueout
$cueOut = "0";
if (Schedule::TimeDiff($item["cue_out"], $item["clip_length"]) > 0.001) {
$cueOut = Schedule::WallTimeToMillisecs($item["cue_out"]);
}
$medias[] = array(
'id' => $storedFile->getGunid(), //$item["file_id"],
'uri' => $uri,
'fade_in' => $item["fade_in"],
'fade_out' => $item["fade_out"],
'fade_in' => Schedule::WallTimeToMillisecs($item["fade_in"]),
'fade_out' => Schedule::WallTimeToMillisecs($item["fade_out"]),
'fade_cross' => 0,
'cue_in' => $item["cue_in"],
'cue_out' => $item["cue_out"],
'cue_in' => Schedule::WallTimeToMillisecs($item["cue_in"]),
'cue_out' => $cueOut
);
}
$playlist['medias'] = $medias;
@ -425,6 +501,24 @@ class Schedule {
print json_encode($result);
}
/**
* Remove all items from the schedule in the given range.
*
* @param string $p_start
* In the format YYYY-MM-DD HH:MM:SS.nnnnnn
* @param string $p_end
* In the format YYYY-MM-DD HH:MM:SS.nnnnnn
*/
public static function RemoveItemsInRange($p_start, $p_end)
{
$items = Schedule::GetItems($p_start, $p_end);
foreach ($items as $item) {
$scheduleGroup = new ScheduleGroup($item["group_id"]);
$scheduleGroup->remove();
}
}
}
?>

View file

@ -835,6 +835,25 @@ class StoredFile {
}
/**
* Find and return the first exact match for the original file name
* that was used on import.
* @param string $p_name
*/
public static function findByOriginalName($p_name)
{
global $CC_CONFIG, $CC_DBC;
$sql = "SELECT id FROM ".$CC_CONFIG["filesTable"]
." WHERE name='".pg_escape_string($p_name)."'";
$id = $CC_DBC->getOne($sql);
if (is_numeric($id)) {
return StoredFile::Recall($id);
} else {
return NULL;
}
}
/**
* Delete and insert media file
*

22
backend/tests/pdoTest.php Normal file
View file

@ -0,0 +1,22 @@
<?php
require_once(__DIR__.'/../../3rd_party/php/propel/runtime/lib/Propel.php');
// Initialize Propel with the runtime configuration
Propel::init(__DIR__."/../propel-db/build/conf/campcaster-conf.php");
// Add the generated 'classes' directory to the include path
set_include_path(__DIR__."/../propel-db/build/classes" . PATH_SEPARATOR . get_include_path());
$con = Propel::getConnection("campcaster");
$sql = "SELECT COUNT(*) FROM cc_schedule WHERE (starts >= '2010-01-01 00:00:00.000') "
." AND (ends <= (TIMESTAMP '2011-01-01 00:00:00.000' + INTERVAL '01:00:00.123456'))";
$rows1 = $con->query($sql);
var_dump($rows1->fetchAll());
$sql2 = "SELECT COUNT(*) FROM cc_playlistcontents";
$rows2 = $con->query($sql2);
var_dump($rows2->fetchAll());
$sql3 = "SELECT TIMESTAMP '2011-01-01 00:00:00.000' + INTERVAL '01:00:00.123456'";
$result3 = $con->query($sql3);
var_dump($result3->fetchAll());
?>

View file

@ -0,0 +1,70 @@
<?php
require_once '../../conf.php';
require_once '../Playlist.php';
require_once '../StoredFile.php';
require_once(__DIR__.'/../../3rd_party/php/propel/runtime/lib/Propel.php');
// Initialize Propel with the runtime configuration
Propel::init(__DIR__."/../propel-db/build/conf/campcaster-conf.php");
// Add the generated 'classes' directory to the include path
set_include_path(__DIR__."/../propel-db/build/classes" . PATH_SEPARATOR . get_include_path());
$playlistName = "pypo_playlist_test";
$minutesFromNow = 1;
echo " ************************************************************** \n";
echo " This script schedules a playlist to play $minutesFromNow minute(s) from now.\n";
echo " This is a utility to help you debug the scheduler.\n";
echo " ************************************************************** \n";
echo "\n";
echo "Deleting playlists with the name '$playlistName'...";
// Delete any old playlists
$pl2 = Playlist::findPlaylistByName($playlistName);
foreach ($pl2 as $playlist) {
//var_dump($playlist);
$playlist->delete();
}
echo "done.\n";
// Create a new playlist
echo "Creating new playlist '$playlistName'...";
$pl = new Playlist();
$pl->create($playlistName);
// Add a media clip
$mediaFile = StoredFile::findByOriginalName("test10001.mp3");
if (is_null($mediaFile)) {
echo "Adding test audio clip to the database.\n";
$v = array("filepath" => __DIR__."/test10001.mp3");
$mediaFile = StoredFile::Insert($v);
}
$pl->addAudioClip($mediaFile->getId());
echo "done.\n";
//$pl2 = Playlist::findPlaylistByName("pypo_playlist_test");
//var_dump($pl2);
// Get current time
// In the format YYYY-MM-DD HH:MM:SS.nnnnnn
$startTime = date("Y-m-d H:i:s");
$endTime = date("Y-m-d H:i:s", time()+(60*60));
echo "Removing everything from the scheduler between $startTime and $endTime...";
// Scheduler: remove any playlists for the next hour
Schedule::RemoveItemsInRange($startTime, $endTime);
// Check for succcess
$scheduleClear = Schedule::isScheduleEmptyInRange($startTime, "01:00:00");
if (!$scheduleClear) {
echo "\nERROR: Schedule could not be cleared.\n\n";
var_dump(Schedule::GetItems($startTime, $endTime));
exit;
}
echo "done.\n";
// Schedule the playlist for two minutes from now
echo "Scheduling new playlist...\n";
$playTime = date("Y-m-d H:i:s", time()+(60*$minutesFromNow));
$scheduleGroup = new ScheduleGroup();
$scheduleGroup->add($playTime, null, $pl->getId());
echo " SUCCESS: Playlist scheduled at $playTime\n\n";
?>

View file

@ -41,7 +41,7 @@ $values = array(
"gunid" => $gunid,
"filetype" => "audioclip"
);
$storedFile = $gb->bsPutFile($values);
$storedFile = StoredFile::Insert($values);
if (PEAR::isError($storedFile)) {
if ($storedFile->getCode()!=GBERR_GUNID) {
echo "ERROR: ".$storedFile->getMessage()."\n";