From 6b2a7074aee52128919afc159f1df3ba8f1a5515 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Wed, 13 Feb 2013 02:01:54 +0000 Subject: [PATCH] Rewrote search criteria matching: - Allows now 'exclude' rules (if found, result will be FALSE) and choice (if one is found, result will be TRUE) - Rewrote LocalFileDatabase class to above added method - Expanded interface LocalSearchCriteria will all public methods --- .../interfaces/criteria/class_Criteria.php | 25 ++++++++++ .../extended/class_LocalSearchCriteria.php | 49 +++++++++++++++++++ .../main/criteria/class_BaseCriteria.php | 41 +++++++++++++++- .../criteria/search/class_SearchCriteria.php | 31 ++++++++++++ .../databases/class_LocalFileDatabase.php | 34 +++++-------- 5 files changed, 157 insertions(+), 23 deletions(-) diff --git a/inc/classes/interfaces/criteria/class_Criteria.php b/inc/classes/interfaces/criteria/class_Criteria.php index 737d5bc7..0a1264cc 100644 --- a/inc/classes/interfaces/criteria/class_Criteria.php +++ b/inc/classes/interfaces/criteria/class_Criteria.php @@ -37,6 +37,31 @@ interface Criteria extends FrameworkInterface { */ function getWrapperConfigEntry (); + /** + * Checks whether given key is set + * + * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude' + * @param $criteriaKey Criteria key + * @return $isSet Whether key is set + */ + function isKeySet ($criteriaType, $criteriaKey); + + /** + * Checks whether given key is set for 'choice' type + * + * @param $criteriaKey Criteria key + * @return $isSet Whether key is set + */ + function isChoiceKeySet ($criteriaKey); + + /** + * Checks whether given key is set for 'exclude' type + * + * @param $criteriaKey Criteria key + * @return $isSet Whether key is set + */ + function isExcludeKeySet ($criteriaKey); + /** * Getter for criteria array * diff --git a/inc/classes/interfaces/criteria/extended/class_LocalSearchCriteria.php b/inc/classes/interfaces/criteria/extended/class_LocalSearchCriteria.php index 7a768a1a..32ccdcf7 100644 --- a/inc/classes/interfaces/criteria/extended/class_LocalSearchCriteria.php +++ b/inc/classes/interfaces/criteria/extended/class_LocalSearchCriteria.php @@ -22,6 +22,55 @@ * along with this program. If not, see . */ interface LocalSearchCriteria extends Criteria { + /** + * Setter for limit + * + * @param $limit Search limit + * @return void + * @todo Find a nice casting here. (int) allows until and including 32766. + */ + function setLimit ($limit); + + /** + * "Setter" for limit from a configuration entry + * + * @param $configEntry The configuration entry which hold a number as limit + * @return void + */ + function setConfiguredLimit ($configEntry); + + /** + * Getter for limit + * + * @return $limit Search limit + */ + function getLimit (); + + /** + * Setter for skip + * + * @param $skip Search skip + * @return void + * @todo Find a nice casting here. (int) allows until and including 32766. + */ + function setSkip ($skip); + + /** + * Getter for skip + * + * @return $skip Search skip + */ + function getSkip (); + + /** + * Checks whether the given key/value pair is matching with 'default' and one of 'choice' and + * never with in 'exclude'. + * + * @param $key Key element to check + * @param $value Value to check + * @return $isMatching Whether the key/value is matching or excluded + */ + function isCriteriaMatching ($key, $value); } // [EOF] diff --git a/inc/classes/main/criteria/class_BaseCriteria.php b/inc/classes/main/criteria/class_BaseCriteria.php index 3b97b8b5..b53ce648 100644 --- a/inc/classes/main/criteria/class_BaseCriteria.php +++ b/inc/classes/main/criteria/class_BaseCriteria.php @@ -50,6 +50,43 @@ class BaseCriteria extends BaseFrameworkSystem implements Criteria { parent::__construct($className); } + /** + * Checks whether given key is set + * + * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude' + * @param $criteriaKey Criteria key + * @return $isSet Whether key is set + */ + public function isKeySet ($criteriaType, $criteriaKey) { + // Determine it + $isSet = isset($this->criteria[$criteriaType][$criteriaKey]); + + // Return it + return $isSet; + } + + /** + * Checks whether given key is set for 'choice' type + * + * @param $criteriaKey Criteria key + * @return $isSet Whether key is set + */ + public function isChoiceKeySet ($criteriaKey) { + // Call inner method + return $this->isKeySet('choice', $criteriaKey); + } + + /** + * Checks whether given key is set for 'exclude' type + * + * @param $criteriaKey Criteria key + * @return $isSet Whether key is set + */ + public function isExcludeKeySet ($criteriaKey) { + // Call inner method + return $this->isKeySet('exclude', $criteriaKey); + } + /** * Setter for wrapper class name * @@ -115,7 +152,7 @@ class BaseCriteria extends BaseFrameworkSystem implements Criteria { $criteriaKey = $this->convertDashesToUnderscores($criteriaKey); // Is it already there? - if (isset($this->criteria[$criteriaType][$criteriaKey])) { + if ($this->isKeySet($criteriaType, $criteriaKey)) { // Append it $this->criteria[$criteriaType][$criteriaKey] .= ',' . $criteriaValue; } else { @@ -186,7 +223,7 @@ class BaseCriteria extends BaseFrameworkSystem implements Criteria { $value = NULL; // Is the criteria there? - if (isset($this->criteria[$criteriaType][$criteriaKey])) { + if ($this->isKeySet($criteriaType, $criteriaKey)) { // Then use it $value = $this->criteria[$criteriaType][$criteriaKey]; } // END - if diff --git a/inc/classes/main/criteria/search/class_SearchCriteria.php b/inc/classes/main/criteria/search/class_SearchCriteria.php index 419427a2..8d770f3e 100644 --- a/inc/classes/main/criteria/search/class_SearchCriteria.php +++ b/inc/classes/main/criteria/search/class_SearchCriteria.php @@ -113,6 +113,37 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria { public final function getSkip () { return $this->skip; } + + /** + * Checks whether the given key/value pair is matching with 'default' and one of 'choice' and + * never with in 'exclude'. + * + * @param $key Key element to check + * @param $value Value to check + * @return $isMatching Whether the key/value is matching or excluded + */ + public function isCriteriaMatching ($key, $value) { + // Get 'default' search value + $search = $this->getCriteriaElemnent($key); + + // 'default' check + $isMatching = ((!is_null($search)) && ($search == $value)); + + // Get 'choice' search value (can be NULL or comma-separated string) + $search = $this->getCriteriaChoiceElemnent($key); + + // 'choice' check + $isMatching = (($isMatching === TRUE) && ((is_null($search)) || (in_array($value, explode(',', $search))))); + + // Get 'exclude' search value + $search = $this->getCriteriaExcludeElemnent($key); + + // 'exclude' check + $isMatching = (($isMatching === TRUE) && ((is_null($search)) || ($search != $value))); + + // Return result + return $isMatching; + } } // [EOF] diff --git a/inc/classes/main/database/databases/class_LocalFileDatabase.php b/inc/classes/main/database/databases/class_LocalFileDatabase.php index 4d14624b..f4d855ff 100644 --- a/inc/classes/main/database/databases/class_LocalFileDatabase.php +++ b/inc/classes/main/database/databases/class_LocalFileDatabase.php @@ -308,13 +308,13 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn * Starts a SELECT query on the database by given return type, table name * and search criteria * - * @param $tableName Name of the database table - * @param $criteria Local search criteria class - * @return $resultData Result data of the query + * @param $tableName Name of the database table + * @param $searchInstance Local search criteria class + * @return $resultData Result data of the query * @throws UnsupportedCriteriaException If the criteria is unsupported * @throws SqlException If an 'SQL error' occurs */ - public function querySelect ($tableName, LocalSearchCriteria $criteriaInstance) { + public function querySelect ($tableName, LocalSearchCriteria $searchInstance) { // The result is null by any errors $resultData = NULL; @@ -339,7 +339,7 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn $idx = 1; // Read the directory with some exceptions - while (($dataFile = $directoryInstance->readDirectoryExcept(array('.', '..', '.htaccess', '.svn', 'info.' . $this->getFileExtension()))) && (($limitFound < $criteriaInstance->getLimit()) || ($criteriaInstance->getLimit() == 0))) { + while (($dataFile = $directoryInstance->readDirectoryExcept(array('.', '..', '.htaccess', '.svn', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) { // Does the extension match? if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) { // Skip this file! @@ -354,16 +354,12 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn if (is_array($dataArray)) { // Search in the criteria with FMFW (First Matches, First Wins) foreach ($dataArray as $key => $value) { - // Get criteria element - $criteria = $criteriaInstance->getCriteriaElemnent($key); - // Is the criteria met or none set? - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: criteria[' . gettype($criteria) . ']=' . $criteria . ';()=' . strlen($criteria) . ',criteriaInstance()=' . $criteriaInstance->count() . ',value(' . strlen($value) . ')=' . $value); - if (((!is_null($criteria)) && ($criteria == $value)) || ($criteriaInstance->count() == 0)) { + if ($searchInstance->isCriteriaMatching($key, $value)) { // Shall we skip this entry? - if ($criteriaInstance->getSkip() > 0) { + if ($searchInstance->getSkip() > 0) { // We shall skip some entries - if ($skipFound < $criteriaInstance->getSkip()) { + if ($skipFound < $searchInstance->getSkip()) { // Skip this entry $skipFound++; break; @@ -463,7 +459,7 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn $skipFound = 0; // Get the criteria array from the dataset - $criteriaArray = $dataSetInstance->getCriteriaArray(); + $searchArray = $dataSetInstance->getCriteriaArray(); // Get search criteria $searchInstance = $dataSetInstance->getSearchInstance(); @@ -486,12 +482,8 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn if (is_array($dataArray)) { // Search in the criteria with FMFW (First Matches, First Wins) foreach ($dataArray as $key => $value) { - // Get criteria element - $criteria = $searchInstance->getCriteriaElemnent($key); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',key=' . $key . ',criteria=' . $criteria); - // Is the criteria met? - if (((!is_null($criteria)) && ($criteria == $value)) || ($searchInstance->count() == 0)) { + if ($searchInstance->isCriteriaMatching($key, $value)) { // Shall we skip this entry? if ($searchInstance->getSkip() > 0) { // We shall skip some entries @@ -503,9 +495,9 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn } // END - if // Entry found, so update it - foreach ($criteriaArray as $criteriaKey => $criteriaValue) { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue); - $dataArray[$criteriaKey] = $criteriaValue; + foreach ($searchArray as $searchKey => $searchValue) { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue); + $dataArray[$searchKey] = $searchValue; } // END - foreach // Write the data to a local file -- 2.39.5