Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Resolved ambiguity with different formats

  • Loading branch information...
commit 50457ea5332a74e054079b1b59fc25a402d56be2 1 parent b40a88f
@shakty authored
View
4 CHANGELOG
@@ -1,3 +1,7 @@
+7026:
+
+ - Patterns are correctly identified by the file name now. Now ambiguity with extensions
+
7025:
- Fixed inconsistencies in the removal process in the GUI. Removed patterns are still not
View
42 includes/db.inc
@@ -118,34 +118,16 @@ function patterns_db_remove_pattern($pid) {
* The new path where the file has just been saved or read from.
* @param mixed $name
* The name of the pattern file without the extension.
- * @TODO Check for success.
+ * @param mixed $format the format of the pattern (YAML, XML, etc.)
*/
-function patterns_db_save_pattern($pattern, $path, $name) {
-
- // TODO: title, description, etc. are actually mandatory fields
- // in the database, so handle them accordingly.
- // Try to parse the array in two different formats.
- if (isset($pattern['info'])) {
- $title = $pattern['info']['title'];
- $description = $pattern['info']['description'];
- $author = $pattern['info']['author'];
- }
- else {
-
- if (isset($pattern['title'])) {
- $title = $pattern['title'];
- }
-
- if (isset($pattern['description'])) {
- $description = $pattern['description'];
- }
-
- if (isset($pattern['author'])) {
- $author = $pattern['author'];
- }
- }
+function patterns_db_save_pattern($pattern, $path, $name, $format = NULL) {
+ $title = patterns_utils_init_from_pattern('title', $pattern);
+ $description = patterns_utils_init_from_pattern('description', $pattern);
+ $format = patterns_utils_init_from_pattern('format', $pattern, $format, PATTERNS_FORMAT_YAML);
+
// Check if it is valid pattern
+ // TODO: validate different formats
if (patterns_validate_pattern($pattern)) {
$status = PATTERNS_STATUS_OK;
}
@@ -156,6 +138,7 @@ function patterns_db_save_pattern($pattern, $path, $name) {
$pattern_data = db_select('patterns', 'p')
->fields('p', array('pid', 'file', 'updated', 'status'))
->condition('p.name', $name)
+ ->condition('p.format', $format)
->execute()
->fetchAssoc();
@@ -209,13 +192,14 @@ function patterns_db_save_pattern($pattern, $path, $name) {
// Note: time() != REQUEST_TIME since it's a long process.
$query_params = array( 'name' => $name,
'pattern' => serialize($pattern),
+ 'format' => $format,
'title' => $title,
'file' => $path,
'descr' => $description,
'time' => time(),
'status' => $status,
);
- db_query("INSERT INTO {patterns} (name, status, file, updated, enabled, title, description, pattern) VALUES ( :name, :status, :file, :time, 0, :title, :descr, :pattern)", $query_params);
+ db_query("INSERT INTO {patterns} (name, format, status, file, updated, enabled, title, description, pattern) VALUES ( :name, :format, :status, :file, 0, :time, :title, :descr, :pattern)", $query_params);
}
return TRUE;
@@ -309,10 +293,10 @@ function patterns_db_get_patterns($fields = array()) {
}
/**
- * Returns the array of patterns array that are currently enabled.
+ * Returns the array of patterns loaded from the database.
*
- * It is possible to specify which fields to return with the fields
- * parameter, that can be either a string or an array.
+ * It is possible to specify which fields to return with the $fields
+ * parameter, that can be either a string or an array of strings.
*
* If found, pattern code inside each pattern object is
* automatically unserialized.
View
36 includes/io.inc
@@ -57,7 +57,7 @@ function patterns_load_xml($path, $local = TRUE) {
}
/**
- * @TODO
+ * @TODO Probably Validation should be removed from inside here
*/
function patterns_load_string_xml($source) {
$pattern = patterns_from_source($source);
@@ -320,20 +320,21 @@ function patterns_io_scan_directories($verbose = FALSE) {
foreach ($patterns_paths as $path) {
//debug($path);
foreach (file_scan_directory($path, $mask, $no_mask) as $file) {
-
- if (in_array($file->name, $existing)) {
+ $format = substr($file->filename, strlen($file->name) + 1);
+
+ if (in_array($file->filename, $existing)) {
if (!variable_get('patterns_update_db_from_fs', TRUE)) {
continue;
}
- if (!patterns_db_is_pattern_updated($file->name)) {
+ if (!patterns_db_is_pattern_updated($file->filename)) {
// the database is already updated to the most recent version
continue;
}
if (!empty($enabled)) {
- if (in_array($file->name, $enabled)) {
- $messages['skipped'][] = $file->name;
+ if (in_array($file->filename, $enabled)) {
+ $messages['skipped'][] = $file->filename;
continue; // Skip updating enabled patterns.
}
}
@@ -345,7 +346,7 @@ function patterns_io_scan_directories($verbose = FALSE) {
}
// Choose appropriate function based on the file extension: YAML or XML.
- $func = 'patterns_load_' . substr($file->filename, strlen($file->name) + 1);
+ $func = 'patterns_load_' . $format;
// Load and save pattern.
if (!($pattern = $func($file->uri))) {
@@ -359,25 +360,25 @@ function patterns_io_scan_directories($verbose = FALSE) {
}
// If everything was fine save it to the database
- $result = patterns_db_save_pattern($pattern, $file->uri, $file->name);
+ $result = patterns_db_save_pattern($pattern, $file->uri, $file->filename, $format);
- if ($result && !in_array($file->name, $existing)) {
- $messages['found'][] = $file->name;
+ if ($result && !in_array($file->filename, $existing)) {
+ $messages['found'][] = $file->filename;
continue;
}
- if (!$result && !in_array($file->name, $existing)) {
- $messages['error_found'][] = $file->name;
+ if (!$result && !in_array($file->filename, $existing)) {
+ $messages['error_found'][] = $file->filename;
continue;
}
- if ($result && in_array($file->name, $existing)) {
- $messages['updated'][] = $file->name;
+ if ($result && in_array($file->filename, $existing)) {
+ $messages['updated'][] = $file->filename;
continue;
}
- if (!$result && in_array($file->name, $existing)) {
- $messages['error_updated'][] = $file->name;
+ if (!$result && in_array($file->filename, $existing)) {
+ $messages['error_updated'][] = $file->filename;
continue;
}
@@ -408,6 +409,9 @@ function _patterns_io_analyze_scandir_messages($messages = NULL) {
if (!empty($messages['updated'])) {
drupal_set_message(t('The following patterns were updated to the newest version found in the file system: ') . '<br/>' . implode('<br/>', $messages['updated']) . '<br/><br/>');
}
+ if (!empty($messages['errors'])) {
+ drupal_set_message(t('A generic error occurred while trying to load the following files: ') . '<br/>' . implode('<br/>', $messages['errors']) . '<br/><br/>', 'warning');
+ }
if (!empty($messages['invalid'])) {
drupal_set_message(t('Some invalid patterns were found: ') . '<br/>' . implode('<br/>', $messages['invalid']) . '<br/><br/>', 'warning');
}
View
32 includes/utils.inc
@@ -601,3 +601,35 @@ function patterns_utils_if_invalid_go_back($pattern = NULL,
function patterns_utils_is_fopenurl_enabled() {
return ini_get('allow_url_fopen') ? TRUE : FALSE;
}
+
+/**
+ * Take as input an array representing the pattern code or the pattern as
+ * extracted from the database and looks for the specified key.
+ *
+ * In the following order returns:
+ *
+ * - the $value parameter, if not NULL
+ * - the value of the key if found in the INFO section
+ * - the value of the key if found in the first level of the array
+ * - NULL or $fallback if the key is not found
+ *
+ *
+ * @param mixed $key the key to look for
+ * @param array $patterm the array to inspect
+ * @param mixed $value a default value for the key
+ * @param mixes $fallback to return instead of NULL, if no key is found
+ *
+ */
+function patterns_utils_init_from_pattern($key, $pattern, $value = NULL, $fallback = NULL) {
+ if (!is_null($value)) return $value;
+ if (isset($pattern['info'])) {
+ if (isset($pattern['info'][$key])) {
+ return $pattern['info'][$key];
+ }
+ }
+ if (isset($pattern[$key])) {
+ return $pattern[$key];
+ }
+ return $fallback;
+}
+
View
4 includes/variables.inc
@@ -3,6 +3,10 @@
* @file
* A file containing the set of constant variables.
*/
+
+define('PATTERNS_FORMAT_YAML', 'yaml');
+define('PATTERNS_FORMAT_XML', 'xml');
+
define('PATTERNS_SUCCESS', 1);
define('PATTERNS_WARN', 0);
define('PATTERNS_ERR', -1);
View
2  patterns.info
@@ -5,7 +5,7 @@ dependencies[] = libraries
core = 7.x
version = 7.x-1.x-dev
-build = 7025
+build = 7026
package = Site Setup
php = 5.2
View
8 patterns.install
@@ -24,6 +24,7 @@ function patterns_uninstall() {
* Implements hook_schema().
*/
function patterns_schema() {
+
$schema['patterns'] = array(
'description' => 'Stores patterns information.',
'fields' => array(
@@ -39,7 +40,12 @@ function patterns_schema() {
'default' => '',
'description' => 'Machine readable name of this pattern.',
),
-
+ 'format' => array(
+ 'type' => 'varchar',
+ 'length' => 10,
+ 'default' => '',
+ 'description' => 'Format of the patterns (XML, YAML, etc.).',
+ ),
/*
* STATUSES:
*
Please sign in to comment.
Something went wrong with that request. Please try again.