<?php
-// $Id: SpamTest.php,v 1.8 2007/05/04 14:54:21 henoheno Exp $
+// $Id: SpamTest.php,v 1.9 2007/05/05 04:59:31 henoheno Exp $
// Copyright (C) 2007 heno
//
// Design test case for spam.php (called from runner.php)
$this->assertEquals(19, array_count_leaves($array, TRUE));
}
+ function testPhpFunc_array_unique()
+ {
+ $this->assertEquals(array(1), array_unique(array(1, 1)));
+
+ // Keys are preserved, array()s inside are preserved
+ $this->assertEquals(
+ array(0, 2 => array(1, 1), 3 => 2),
+ array_unique(
+ array(0, 0, array(1, 1), 2, 2)
+ )
+ );
+
+ // Keys are preserved
+ $this->assertEquals(
+ array(0, 2 => array(1, 1), 3 => 2),
+ array_unique(array(0, 0, array(1, 1), 2, 2))
+ );
+
+ // ONLY the first array() is preserved
+ $this->assertEquals(
+ array(0 => array(1, 1)),
+ array_unique(array_unique(array(0 => array(1, 1), 'a' => array(2,2), 'b' => array(3, 3))))
+ );
+ }
+
function testFunc_array_merge_leaves()
{
+ $array1 = array(1);
+ $array2 = array(1);
+ $result = array(1);
+ $this->assertEquals($result, array_merge_leaves($array1, $array2));
+
$array1 = array(2);
$array2 = array(1);
$result = array(2, 1);
);
$this->assertEquals($result, array_merge_leaves($array1, $array2));
- // ----
-
- // Values will not be unique now
+ // Values will not be unique?
$array1 = array(5, 4);
$array2 = array(4, 5);
- $result = array(5, 4, 4, 5);
+ $result = array(5, 4);
$this->assertEquals($result, array_merge_leaves($array1, $array2));
// One more thing ...
$array1 = array('b' => array('k3'));
$array2 = array('b' => 'k3');
- $result = array('b' => array('k3', 'k3'));
+ $result = array('b' => array('k3'));
$this->assertEquals($result, array_merge_leaves($array1, $array2));
}
<?php
-// $Id: spam.php,v 1.147 2007/05/04 14:55:36 henoheno Exp $
+// $Id: spam.php,v 1.148 2007/05/05 04:59:31 henoheno Exp $
// Copyright (C) 2006-2007 PukiWiki Developers Team
// License: GPL v2 or (at your option) any later version
//
}
}
+// Remove redundant values from array()
+function array_unique_recursive($array = array())
+{
+ if (! is_array($array)) return $array;
+
+ $tmp = array();
+ foreach($array as $key => $value){
+ if (is_array($value)) {
+ $array[$key] = array_unique_recursive($value);
+ } else {
+ if (isset($tmp[$value])) {
+ unset($array[$key]);
+ } else {
+ $tmp[$value] = TRUE;
+ }
+ }
+ }
+
+ return $array;
+}
+
// Roughly strings(1) using PCRE
// This function is useful to:
// * Reduce the size of data, from removing unprintable binary data
if ($asap && $is_spam) break;
// Merge $blocked
- // TODO: unique the hosts
$blocked = array_merge_leaves($blocked, $_progress['blocked']);
// Merge $hosts
- // TODO: unique the hosts
$hosts = array_merge_leaves($hosts, $_progress['hosts']);
}
}
// Merge two leaves' value
-function array_merge_leaves(& $array1, & $array2)
+function array_merge_leaves(& $array1, & $array2, $unique_values = TRUE)
{
// All NUMERIC keys are always renumbered from 0
- return array_merge_recursive($array1, $array2);
+ $array = array_merge_recursive($array1, $array2);
+
+ // Redundant values (and keys) are vanished
+ if ($unique_values) $array = array_unique_recursive($array);
+
+ return $array;
}
+
// ---------------------
// Reporting