summaryrefslogtreecommitdiff
path: root/lib/kolab_api_service.php
diff options
context:
space:
mode:
authorAleksander Machniak <alec@alec.pl>2013-04-16 13:51:05 (GMT)
committerAleksander Machniak <alec@alec.pl>2013-04-16 13:51:05 (GMT)
commitdba00f1f7c20cf3130d32070fd06603485b4916a (patch)
treeaa571c7f17ffb7a180429707c0442f9132502fe6 /lib/kolab_api_service.php
parent93830d49acca3bd5eb2de5acbe8d7fb293698ddd (diff)
downloadkolab-wap-dba00f1f7c20cf3130d32070fd06603485b4916a.tar.gz
Improved object type detection - score static values
Diffstat (limited to 'lib/kolab_api_service.php')
-rw-r--r--lib/kolab_api_service.php74
1 files changed, 59 insertions, 15 deletions
diff --git a/lib/kolab_api_service.php b/lib/kolab_api_service.php
index ca93c8a..0c047c4 100644
--- a/lib/kolab_api_service.php
+++ b/lib/kolab_api_service.php
@@ -134,8 +134,7 @@ abstract class kolab_api_service
$object_class = array_map('strtolower', $object_class);
$object_keys = array_keys($attributes);
$keys_count = count($object_keys);
- $type_score = -1;
- $keys_score = 0;
+ $type_score = null;
$type_id = null;
Log::trace("kolab_api_service::object_type_id objectClasses: " . implode(", ", $object_class));
@@ -149,6 +148,9 @@ abstract class kolab_api_service
Log::trace("Reference objectclasses for " . $elem['key'] . ": " . implode(", ", $ref_class));
+ $elem_keys_score = 0;
+ $elem_values_score = 0;
+
// Eliminate the duplicates between the $data_ocs and $ref_ocs
$_object_class = array_diff($object_class, $ref_class);
$_ref_class = array_diff($ref_class, $object_class);
@@ -169,6 +171,19 @@ abstract class kolab_api_service
$elem_keys_score = $keys_count - count(array_diff($object_keys, $ref_keys));
}
+ // Static attributes score
+ $elem_values_score = 0;
+ foreach ((array) $elem['attributes']['fields'] as $attr => $value) {
+ $v = $attributes[$attr];
+ if (is_array($value)) {
+ $value = implode('', $value);
+ }
+ if (is_array($v)) {
+ $v = implode('', $v);
+ }
+ $elem_values_score += intval($v == $value);
+ }
+
// Position in tree score
if (!empty($elem['attributes']['fields']['ou'])) {
if (!empty($attributes['ou'])) {
@@ -180,27 +195,27 @@ abstract class kolab_api_service
}
}
- Log::trace("\$object_class not in \$ref_class (" . $elem['key'] . "): " . implode(", ", $_object_class));
- Log::trace("\$ref_class not in \$object_class (" . $elem['key'] . "): " . implode(", ", $_ref_class));
- Log::trace("Score for $object_name type " . $elem['name'] . ": " . $elem_score . " (" . $commonalities . "/" . $differences . ") " . $elem_keys_score);
-
- // Compare last and current element score
- if ($elem_score > $type_score || ($elem_score == $type_score && $elem_keys_score > $keys_score)) {
- $type_id = $idx;
- $type_score = $elem_score;
- $keys_score = $elem_keys_score;
- }
-
// On the likely chance that the object is a resource (types of which likely have the same
// set of objectclass attribute values), consider the other attributes. (#853)
if ($object_name == 'resource') {
//console("From database", $elem);
//console("Element key is " . $elem['key'] . " and \$attributes['mail'] is " . $attributes['mail']);
if (strpos($attributes['mail'], 'resource-' . $elem['key'] . '-') === 0) {
- $type_id = $idx;
- $type_score = 10;
+ $elem_score += 10;
}
}
+
+ $elem_score .= ':' . $elem_keys_score . ':' . $elem_values_score;
+
+// Log::trace("\$object_class not in \$ref_class (" . $elem['key'] . "): " . implode(", ", $_object_class));
+// Log::trace("\$ref_class not in \$object_class (" . $elem['key'] . "): " . implode(", ", $_ref_class));
+ Log::trace("Score for $object_name type " . $elem['name'] . ": " . $elem_score . " (" . $commonalities . "/" . $differences . ")");
+
+ // Compare last and current element (object type) score
+ if ($this->score_compare($elem_score, $type_score)) {
+ $type_id = $idx;
+ $type_score = $elem_score;
+ }
}
return $type_id;
@@ -485,6 +500,35 @@ abstract class kolab_api_service
}
/**
+ * Compare two score values
+ *
+ * @param string $s1 Score
+ * @param string $s2 Score
+ *
+ * @return bool True when $s1 is greater than $s2
+ */
+ protected function score_compare($s1, $s2)
+ {
+ if (empty($s2) && !empty($s1)) {
+ return true;
+ }
+
+ $s1 = explode(':', $s1);
+ $s2 = explode(':', $s2);
+
+ foreach ($s1 as $key => $val) {
+ if ($val > $s2[$key]) {
+ return true;
+ }
+ if ($val < $s2[$key]) {
+ return false;
+ }
+ }
+
+ return false;
+ }
+
+ /**
* Returns name of unique attribute
*
* @return string Unique attribute name