CC-2225 airtime-clean-storage doesnt work

CC-2224 airtime-import checks if you can write to the stor directory even if you are linking

Had to rewrite the airtime-clean-storage script.
This commit is contained in:
Paul Baranowski 2011-04-25 16:48:34 -04:00
parent 0781f08148
commit 07633ea249
7 changed files with 162 additions and 117 deletions

View file

@ -113,6 +113,12 @@ class Playlist {
return TRUE; return TRUE;
} }
public static function deleteAll()
{
global $CC_CONFIG, $CC_DBC;
$sql = 'DELETE FROM '.$CC_CONFIG["playListTable"];
$CC_DBC->query($sql);
}
/** /**
* Delete the file from all playlists. * Delete the file from all playlists.

View file

@ -681,5 +681,11 @@ class Schedule {
return $result; return $result;
} }
public static function deleteAll()
{
global $CC_CONFIG, $CC_DBC;
$CC_DBC->query("TRUNCATE TABLE ".$CC_CONFIG["scheduleTable"]);
}
} }

View file

@ -1,6 +1,6 @@
<?php <?php
require_once("Playlist.php"); require_once("Playlist.php");
require_once(dirname(__FILE__)."/../../library/getid3/var/getid3.php"); require_once("getid3/var/getid3.php");
require_once("Schedule.php"); require_once("Schedule.php");
class Metadata { class Metadata {
@ -431,18 +431,22 @@ class StoredFile {
* *
* @param string $p_gunid * @param string $p_gunid
* globally unique id of file * globally unique id of file
* @param boolean $p_autoload
* if TRUE, automatically load the row from the DB
*/ */
public function __construct($p_gunid=NULL) public function __construct($p_gunid=NULL, $p_autoload=TRUE)
{ {
$this->gunid = $p_gunid; $this->gunid = $p_gunid;
if (empty($this->gunid)) { if (empty($this->gunid)) {
$this->gunid = StoredFile::generateGunid(); $this->gunid = StoredFile::generateGunid();
} }
else { else {
if ($p_autoload) {
$this->loadMetadata(); $this->loadMetadata();
$this->exists = is_file($this->filepath) && is_readable($this->filepath); $this->exists = is_file($this->filepath) && is_readable($this->filepath);
} }
} }
}
/** /**
* For testing only, do not use. * For testing only, do not use.
@ -730,7 +734,6 @@ class StoredFile {
return $storedFile; return $storedFile;
} }
/** /**
* Create instance of StoreFile object and recall existing file * Create instance of StoreFile object and recall existing file
* by gunid. * by gunid.
@ -757,6 +760,14 @@ class StoredFile {
} }
public static function GetAll()
{
global $CC_CONFIG, $CC_DBC;
$sql = "SELECT * FROM ".$CC_CONFIG["filesTable"];
$rows = $CC_DBC->GetAll($sql);
return $rows;
}
/** /**
* Generate the location to store the file. * Generate the location to store the file.
* It creates the subdirectory if needed. * It creates the subdirectory if needed.
@ -1205,6 +1216,31 @@ class StoredFile {
} }
public static function deleteById($p_id)
{
global $CC_CONFIG, $CC_DBC;
if (!is_numeric($p_id)) {
return FALSE;
}
$sql = "DELETE FROM ".$CC_CONFIG["filesTable"]." WHERE id=$p_id";
$res = $CC_DBC->query($sql);
if (PEAR::isError($res)) {
return $res;
}
return TRUE;
}
public static function deleteAll()
{
global $CC_CONFIG, $CC_DBC;
$files = StoredFile::getAll();
foreach ($files as $file) {
$media = StoredFile::Recall($file["id"]);
$media->delete();
}
}
/** /**
* Returns an array of playlist objects that this file is a part of. * Returns an array of playlist objects that this file is a part of.
* @return array * @return array

View file

@ -21,6 +21,14 @@
# #
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# This script cleans audio files in the Airtime storageServer. # This script cleans audio files in Airtime.
#
# Absolute path to this script
SCRIPT=`readlink -f $0`
# Absolute path this script is in
SCRIPTPATH=`dirname $SCRIPT`
invokePwd=$PWD
cd $SCRIPTPATH
php -q airtime-clean-storage.php "$@" || exit 1 php -q airtime-clean-storage.php "$@" || exit 1

View file

@ -8,118 +8,102 @@ if (isset($arr["DOCUMENT_ROOT"]) && ($arr["DOCUMENT_ROOT"] != "") ) {
echo "400 Not executable\r\n"; echo "400 Not executable\r\n";
exit(1); exit(1);
} }
set_include_path('/var/www/airtime/library' . PATH_SEPARATOR . get_include_path());
set_include_path('/var/www/airtime/application/models' . PATH_SEPARATOR . get_include_path());
require_once('/var/www/airtime/application/configs/conf.php'); require_once('/var/www/airtime/application/configs/conf.php');
//require_once('/var/www/airtime/install/installInit.php');
require_once('/var/www/airtime/application/models/StoredFile.php'); require_once('/var/www/airtime/application/models/StoredFile.php');
require_once('DB.php'); require_once('DB.php');
require_once 'propel/runtime/lib/Propel.php';
Propel::init('/var/www/airtime/application/configs/airtime-conf.php');
function printUsage() {
global $CC_CONFIG; /**
*
echo "Usage:\n"; * Look through all the files in the database and remove the rows
echo " ./airtime-clean-storage [OPTION] \n"; * that have no associated file.
echo "\n"; *
echo "Options:\n"; * @return int
echo " -c, --clean Removes all broken links from the storage server\n"; * The total number of files that were missing.
echo " and empties all missing file information from the database.\n"; */
echo "\n"; function airtime_clean_files() {
echo " -e, --empty Removes all files from the storage server \n"; $count = 0;
echo " and empties all relevant information from the database.\n\n"; $files = StoredFile::GetAll();
echo "Storage server: ". realpath($CC_CONFIG["storageDir"]) ."\n\n\n"; foreach ($files as $file) {
} if (($file["ftype"] == "audioclip") && !@file_exists($file["filepath"])) {
echo " * Removing metadata for id ".$file["id"].":".PHP_EOL;
function airtime_clean_files($p_path) { echo " * File path: ".$file["filepath"].PHP_EOL;
if (!empty($p_path) && (strlen($p_path) > 4)) { echo " * Track title: ".$file["track_title"].PHP_EOL;
list($dirList,$fileList) = File_Find::maptree($p_path); echo " * Artist: ".$file["artist_name"].PHP_EOL;
echo " * Album: ".$file["album_title"].PHP_EOL;
$array_mus; StoredFile::deleteById($file["id"]);
foreach ($fileList as $filepath) { $count++;
if (@substr($filepath, strlen($filepath) - 3) != "xml") {
$array_mus[] = $filepath;
} }
} }
return $count;
foreach ($array_mus as $audio_file) {
if (@is_link($audio_file) && !@stat($audio_file)) {
//filesystem clean up.
@unlink($audio_file);
echo "unlinked $audio_file\n";
@unlink($audio_file . ".xml");
echo "unlinked " . $audio_file . ".xml\n";
@rmdir(@dirname($audio_file));
echo "removed dir " . @dirname($audio_file) . "\n";
//database clean up.
$stored_audio_file = StoredFile::RecallByGunid(@basename($audio_file));
$stored_audio_file->delete();
}
} }
} function airtime_empty_db($db)
} {
global $CC_CONFIG, $CC_DBC;
function airtime_remove_files($p_path) { // NOTE: order matter here.
echo " * Clearing schedule table...".PHP_EOL;
Schedule::deleteAll();
if (!empty($p_path) && (strlen($p_path) > 4)) { // Ugly hack
list($dirList,$fileList) = File_Find::maptree($p_path); echo " * Resetting show instance times to zero...".PHP_EOL;
$sql = "UPDATE cc_show_instances SET time_filled='00:00:00'";
$CC_DBC->query($sql);
foreach ($fileList as $filepath) { echo " * Clearing playlist table...".PHP_EOL;
echo " * Removing $filepath\n"; Playlist::deleteAll();
@unlink($filepath);
echo "done.\n";
}
foreach ($dirList as $dirpath) {
echo " * Removing $dirpath\n";
@rmdir($dirpath);
echo "done.\n";
}
}
}
function airtime_empty_db($db) { echo " * Clearing files table...".PHP_EOL;
global $CC_CONFIG; StoredFile::deleteAll();
if (!PEAR::isError($db)) {
if (AirtimeInstall::DbTableExists($CC_CONFIG['filesTable'])) {
echo " * Deleting from database table ".$CC_CONFIG['filesTable']."\n";
$sql = "DELETE FROM ".$CC_CONFIG['filesTable'];
AirtimeInstall::InstallQuery($sql, false);
}
else {
echo " * Skipping: database table ".$CC_CONFIG['filesTable']."\n";
}
}
} }
global $CC_CONFIG; global $CC_CONFIG;
require_once('Zend/Loader/Autoloader.php');
$autoloader = Zend_Loader_Autoloader::getInstance();
try {
$opts = new Zend_Console_Getopt(
array(
'help|h' => 'Displays usage information.',
'clean|c' => 'Removes all audio file metadata from the database that does not have a matching file in the filesystem.',
'empty|e' => 'Removes all files and playlists from Airtime.'
)
);
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
exit($e->getMessage() .PHP_EOL. $e->getUsageMessage());
}
if (isset($opts->h)) {
echo PHP_EOL;
echo $opts->getUsageMessage();
echo "Storage directory: ". realpath($CC_CONFIG["storageDir"]).PHP_EOL.PHP_EOL;
exit;
}
$CC_DBC = DB::connect($CC_CONFIG['dsn'], TRUE); $CC_DBC = DB::connect($CC_CONFIG['dsn'], TRUE);
$CC_DBC->setFetchMode(DB_FETCHMODE_ASSOC); $CC_DBC->setFetchMode(DB_FETCHMODE_ASSOC);
if ($argc != 2){ if (isset($opts->e)) {
printUsage(); echo PHP_EOL;
exit(1);
}
switch($argv[1]){
case '-e':
case '--empty':
airtime_empty_db($CC_DBC); airtime_empty_db($CC_DBC);
airtime_remove_files($CC_CONFIG['storageDir']); echo "Done.".PHP_EOL.PHP_EOL;
break; } elseif (isset($opts->c)) {
case '-c': $count = airtime_clean_files($CC_CONFIG['storageDir']);
case '--clean': if ($count == 0) {
airtime_clean_files($CC_CONFIG['storageDir']); echo PHP_EOL."All file metadata in the database is linked to a real file. Nothing to be done.".PHP_EOL.PHP_EOL;
break; } else {
default: echo PHP_EOL."Total rows removed: $count".PHP_EOL;
printUsage(); }
} else {
echo PHP_EOL;
echo $opts->getUsageMessage();
echo "Storage directory: ". realpath($CC_CONFIG["storageDir"]).PHP_EOL.PHP_EOL;
} }

View file

@ -21,7 +21,7 @@
# #
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# This script imports audio files to the Airtime storageServer. # This script imports audio files into Airtime.
# #
# To get usage help, try the -h option # To get usage help, try the -h option
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------

View file

@ -10,14 +10,16 @@
ini_set('memory_limit', '128M'); ini_set('memory_limit', '128M');
set_time_limit(0); set_time_limit(0);
error_reporting(E_ALL); error_reporting(E_ALL);
set_error_handler("camp_import_error_handler", E_ALL & !E_NOTICE); set_error_handler("import_error_handler", E_ALL & !E_NOTICE);
set_include_path('/var/www/airtime/library' . PATH_SEPARATOR . get_include_path());
require_once("/var/www/airtime/application/configs/conf.php"); require_once("/var/www/airtime/application/configs/conf.php");
require_once("/var/www/airtime/application/models/StoredFile.php"); require_once("/var/www/airtime/application/models/StoredFile.php");
require_once('DB.php'); require_once('DB.php');
require_once('Console/Getopt.php'); require_once('Console/Getopt.php');
function camp_import_error_handler() function import_error_handler()
{ {
echo var_dump(debug_backtrace()); echo var_dump(debug_backtrace());
exit(); exit();
@ -92,7 +94,7 @@ function import_err($p_pearErrorObj, $txt='')
* *
* @return int * @return int
*/ */
function camp_import_audio_file($p_filepath, $p_importMode = null, $p_testOnly = false) function import_audio_file($p_filepath, $p_importMode = null, $p_testOnly = false)
{ {
global $STORAGE_SERVER_PATH; global $STORAGE_SERVER_PATH;
global $g_fileCount; global $g_fileCount;
@ -126,7 +128,7 @@ function camp_import_audio_file($p_filepath, $p_importMode = null, $p_testOnly =
while (false !== ($file = readdir($d))) { while (false !== ($file = readdir($d))) {
if ($file != "." && $file != "..") { if ($file != "." && $file != "..") {
$path = "$p_filepath/$file"; $path = "$p_filepath/$file";
camp_import_audio_file($path, $p_importMode, $p_testOnly); import_audio_file($path, $p_importMode, $p_testOnly);
} }
} }
closedir($d); closedir($d);
@ -241,7 +243,7 @@ if ($DEBUG_IMPORT) {
$dsn = $CC_CONFIG['dsn']; $dsn = $CC_CONFIG['dsn'];
} }
//PEAR::setErrorHandling(PEAR_ERROR_RETURN); //PEAR::setErrorHandling(PEAR_ERROR_RETURN);
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, "camp_import_error_handler"); PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, "import_error_handler");
$CC_DBC = DB::connect($dsn, TRUE); $CC_DBC = DB::connect($dsn, TRUE);
if (PEAR::isError($CC_DBC)) { if (PEAR::isError($CC_DBC)) {
echo "ERROR: ".$CC_DBC->getMessage()." ".$CC_DBC->getUserInfo()."\n"; echo "ERROR: ".$CC_DBC->getMessage()." ".$CC_DBC->getUserInfo()."\n";
@ -301,13 +303,14 @@ if (is_null($importMode)) {
global $CC_CONFIG; global $CC_CONFIG;
if (!is_writable($CC_CONFIG["storageDir"])) { if ( ($importMode == "copy") && !is_writable($CC_CONFIG["storageDir"])) {
echo "ERROR: You do not have write permissions to the directory you are trying to import to:\n " . $CC_CONFIG["storageDir"] . "\n\n"; echo "ERROR: You do not have write permissions to the directory you are trying to import to:\n " . $CC_CONFIG["storageDir"] . "\n\n";
exit; exit;
} }
global $g_fileCount; global $g_fileCount;
global $g_duplicates; global $g_duplicates;
$g_fileCount = 0;
if (is_array($files)) { if (is_array($files)) {
foreach ($files as $filepath) { foreach ($files as $filepath) {
// absolute path // absolute path
@ -323,7 +326,7 @@ if (is_array($files)) {
echo "ERROR: I cant find the given file: $filepath\n\n"; echo "ERROR: I cant find the given file: $filepath\n\n";
exit; exit;
} }
camp_import_audio_file($fullPath, $importMode, $testonly); import_audio_file($fullPath, $importMode, $testonly);
} }
} }
$end = intval(date('U')); $end = intval(date('U'));
@ -336,7 +339,9 @@ if ($time > 0) {
echo "==========================================================================\n"; echo "==========================================================================\n";
echo " *** Import mode: $importMode\n"; echo " *** Import mode: $importMode\n";
if ($importMode == "copy") {
echo " *** Destination folder: ".$CC_CONFIG['storageDir']."\n"; echo " *** Destination folder: ".$CC_CONFIG['storageDir']."\n";
}
echo " *** Files imported: $g_fileCount\n"; echo " *** Files imported: $g_fileCount\n";
echo " *** Duplicate files (not imported): $g_duplicates\n"; echo " *** Duplicate files (not imported): $g_duplicates\n";
if ($g_errors > 0) { if ($g_errors > 0) {