From e4071badd99a52c350715b3f12bc07bb68207cbb Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Thu, 16 Sep 2010 23:05:46 +0100 Subject: [PATCH 1/3] Adds support for lists returned by selectAssoc The column values parameter to RippingCluster_Database::selectAssoc() now accepts an array of columns instead of a single value. If a scalar is provided, the value is returned as a simple associative array; if an array is provided, the full list of columns requested is returned as a nested associative array. --- lib/RippingCluster/Database.class.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/RippingCluster/Database.class.php b/lib/RippingCluster/Database.class.php index 9ff1b14..49f0b13 100644 --- a/lib/RippingCluster/Database.class.php +++ b/lib/RippingCluster/Database.class.php @@ -32,11 +32,20 @@ class RippingCluster_Database { $this->dbh = null; } - public function selectAssoc($sql, $key_col, $value_col) { + public function selectAssoc($sql, $key_col, $value_cols) { $results = array(); foreach ($this->dbh->query($sql) as $row) { - $results[$row[$key_col]] = $row[$value_col]; + if (is_array($value_cols)) { + $values = array(); + foreach ($value_cols as $value_col) { + $values[$value_col] = $row[$value_col]; + } + + $results[$row[$key_col]] = $values; + } else { + $results[$row[$key_col]] = $row[$value_col]; + } } return $results; From 45b97e2d5d36d03f6eeed5319a98b8c871930591 Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Thu, 16 Sep 2010 23:09:27 +0100 Subject: [PATCH 2/3] Adds datatype support to the Config class The config class now supports a type column in the database which specifies how the value should be interpreted. The class defines a number of scalar types, which are returned without transformation. A string list type is also defined, which splits the value into an array based on newlines. Also added phpdoc comments to all class constants, members and methods. --- lib/RippingCluster/Config.class.php | 95 ++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 3 deletions(-) diff --git a/lib/RippingCluster/Config.class.php b/lib/RippingCluster/Config.class.php index 054b86b..88d6064 100644 --- a/lib/RippingCluster/Config.class.php +++ b/lib/RippingCluster/Config.class.php @@ -1,23 +1,86 @@ string) + */ private $databaseConfig = array(); + + /** + * Associative array of settings loaded from the database + * @var array(string=>array(string=>string)) + */ private $settings = array(); + /** + * Constructs a new instance of the Config class + * + * @param string $dbconfig Database configuration file contents + * @return RippingCluster_Config + */ public function __construct($dbconfig) { $this->dbconfig = $dbconfig; $this->parseDatabaseConfig(); } + /** + * Parses the contents of the database configuration file so that individual settings can be retrieved. + * + */ public function parseDatabaseConfig() { $this->databaseConfig = parse_ini_file($this->dbconfig); } + /** + * Returns the value of the named item from the database configuration file + * + * @param string $key Name of the setting to retrieve + */ public function getDatabase($key) { if (!isset($this->databaseConfig[$key])) { throw new RippingCluster_Exception_DatabaseConfigMissing($key); @@ -26,29 +89,55 @@ class RippingCluster_Config { return $this->databaseConfig[$key]; } + /** + * Sets the database instance used by this object + * + * @param RippingCluster_Database $database Database instance + */ public function setDatabase(RippingCluster_Database $database) { $this->database = $database; $this->preload(); } - public function preload() { + /** + * Loads the entire list of settings from the database + * + */ + private function preload() { if (!$this->database) { throw new RippingCluster_Exception_NoDatabaseConnection(); } - $this->settings = $this->database->selectAssoc('SELECT name,value FROM settings', 'name', 'value'); + $this->settings = $this->database->selectAssoc('SELECT name,type,value FROM settings', 'name', array('name', 'value', 'type')); } + /** + * Identifies whether the named setting exists + * + * @param string $key Name of the setting + * @return bool + */ public function exists($key) { return isset($this->settings[$key]); } + /** + * Fetches the value of the named setting + * + * @param string $key Name of the setting + */ public function get($key) { if (!isset($this->settings[$key])) { throw new RippingCluster_Exception_UnknownSetting($key); } - return $this->settings[$key]; + switch ($this->settings[$key]['type']) { + case TYPE_STRING_LIST: + return explode("\n", $this->settings[$key]['value']); + + default: + return $this->settings[$key]['value']; + } } }; From 1e745a37b741e403a1e6d496826ed9440b70d3c2 Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Thu, 16 Sep 2010 23:16:12 +0100 Subject: [PATCH 3/3] Adds support for multiple directories to Source plugins Adds the capability for the HandBrake and Bluray source plugins to look for sources in multiple directories using the new features of the config class to support string lists. --- .../Source/Plugin/Bluray.class.php | 35 +++++++++++-------- .../Source/Plugin/HandBrake.class.php | 35 +++++++++++-------- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/lib/RippingCluster/Source/Plugin/Bluray.class.php b/lib/RippingCluster/Source/Plugin/Bluray.class.php index 5fa94a6..4ecfc27 100644 --- a/lib/RippingCluster/Source/Plugin/Bluray.class.php +++ b/lib/RippingCluster/Source/Plugin/Bluray.class.php @@ -13,17 +13,19 @@ class RippingCluster_Source_Plugin_Bluray extends RippingCluster_PluginBase impl */ public static function enumerate() { $config = RippingCluster_Main::instance()->config(); - $directory = $config->get('source.bluray.dir'); + $directories = $config->get('source.bluray.dir'); - if (!is_dir($directory)) { - throw new RippingCluster_Exception_InvalidSourceDirectory($directory); - } - - $sources = array(); - - $iterator = new RippingCluster_Utility_BlurayDirectoryIterator(new RippingCluster_Utility_VisibleFilesIterator(new DirectoryIterator($directory))); - foreach ($iterator as /** @var SplFileInfo */ $source_vts) { - $sources[] = self::load($source_vts->getPathname(), false); + foreach ($directories as $directory) { + if (!is_dir($directory)) { + throw new RippingCluster_Exception_InvalidSourceDirectory($directory); + } + + $sources = array(); + + $iterator = new RippingCluster_Utility_BlurayDirectoryIterator(new RippingCluster_Utility_VisibleFilesIterator(new DirectoryIterator($directory))); + foreach ($iterator as /** @var SplFileInfo */ $source_vts) { + $sources[] = self::load($source_vts->getPathname(), false); + } } return $sources; @@ -99,11 +101,14 @@ class RippingCluster_Source_Plugin_Bluray extends RippingCluster_PluginBase impl } $real_source_filename = realpath($source_filename); - $source_basedir = $config->get('source.bluray.dir'); - $real_source_basedir = realpath($source_basedir); - - if (substr($real_source_filename, 0, strlen($real_source_basedir)) != $real_source_basedir) { - return false; + // Check all of the source directories specified in the config + $source_directories = $config->get('source.bluray.dir'); + foreach ($source_directories as $source_basedir) { + $real_source_basedir = realpath($source_basedir); + + if (substr($real_source_filename, 0, strlen($real_source_basedir)) != $real_source_basedir) { + return false; + } } return true; diff --git a/lib/RippingCluster/Source/Plugin/HandBrake.class.php b/lib/RippingCluster/Source/Plugin/HandBrake.class.php index 3490ed0..d961023 100644 --- a/lib/RippingCluster/Source/Plugin/HandBrake.class.php +++ b/lib/RippingCluster/Source/Plugin/HandBrake.class.php @@ -23,17 +23,19 @@ class RippingCluster_Source_Plugin_HandBrake extends RippingCluster_PluginBase i */ public static function enumerate() { $config = RippingCluster_Main::instance()->config(); - $directory = $config->get('source.handbrake.dir'); + $directories = $config->get('source.handbrake.dir'); - if (!is_dir($directory)) { - throw new RippingCluster_Exception_InvalidSourceDirectory($directory); - } - - $sources = array(); - - $iterator = new RippingCluster_Utility_DvdDirectoryIterator(new RippingCluster_Utility_VisibleFilesIterator(new DirectoryIterator($directory))); - foreach ($iterator as /** @var SplFileInfo */ $source_vts) { - $sources[] = self::load($source_vts->getPathname(), false); + foreach ($directories as $directory) { + if (!is_dir($directory)) { + throw new RippingCluster_Exception_InvalidSourceDirectory($directory); + } + + $sources = array(); + + $iterator = new RippingCluster_Utility_DvdDirectoryIterator(new RippingCluster_Utility_VisibleFilesIterator(new DirectoryIterator($directory))); + foreach ($iterator as /** @var SplFileInfo */ $source_vts) { + $sources[] = self::load($source_vts->getPathname(), false); + } } return $sources; @@ -203,11 +205,14 @@ class RippingCluster_Source_Plugin_HandBrake extends RippingCluster_PluginBase i } $real_source_filename = realpath($source_filename); - $source_basedir = $config->get('source.handbrake.dir'); - $real_source_basedir = realpath($source_basedir); - - if (substr($real_source_filename, 0, strlen($real_source_basedir)) != $real_source_basedir) { - return false; + // Check all of the source directories specified in the config + $source_directories = $config->get('source.handbrake.dir'); + foreach ($source_directories as $source_basedir) { + $real_source_basedir = realpath($source_basedir); + + if (substr($real_source_filename, 0, strlen($real_source_basedir)) != $real_source_basedir) { + return false; + } } return true;