OSDN Git Service

Simplify. Added array_leaf()
authorhenoheno <henoheno>
Fri, 11 May 2007 15:25:36 +0000 (00:25 +0900)
committerhenoheno <henoheno>
Fri, 11 May 2007 15:25:36 +0000 (00:25 +0900)
spam/SpamTest.php
spam/spam.php

index dea1ca2..e73721c 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: SpamTest.php,v 1.11 2007/05/05 13:58:39 henoheno Exp $
+// $Id: SpamTest.php,v 1.12 2007/05/11 15:25:36 henoheno Exp $
 // Copyright (C) 2007 heno
 //
 // Design test case for spam.php (called from runner.php)
@@ -101,41 +101,44 @@ class SpamTest extends PHPUnit_TestCase
                );
        }
 
-       function testFunc_array_merge_leaves()
+       // And array_unique_recursive()
+       function testPhPFunc_array_merge_recursive()
        {
                $array1 = array(1);
                $array2 = array(1);
-               $result = array(1);
-               $this->assertEquals($result, array_merge_leaves($array1, $array2));
+               $result = array_merge_recursive($array1, $array2);
+               $this->assertEquals(array(1, 1), $result);
+               $result = array_unique_recursive($result);
+               $this->assertEquals(array(1),    $result);
 
                $array1 = array(2);
                $array2 = array(1);
                $result = array(2, 1);
-               $this->assertEquals($result, array_merge_leaves($array1, $array2));
+               $this->assertEquals($result, array_merge_recursive($array1, $array2));
 
                // All NUMERIC keys are always renumbered from 0
                $array1 = array('10' => 'f3');
                $array2 = array('10' => 'f4');
                $result = array('f3', 'f4');
-               $this->assertEquals($result, array_merge_leaves($array1, $array2));
+               $this->assertEquals($result, array_merge_recursive($array1, $array2));
 
                // One more thing ...
                $array1 = array('20' => 'f5');
                $array2 = array();
                $result = array('f5');
-               $this->assertEquals($result, array_merge_leaves($array1, $array2));
+               $this->assertEquals($result, array_merge_recursive($array1, $array2));
 
                // Non-numeric keys and values will be marged as you think
                $array1 = array('a' => 'f1');
                $array2 = array('a' => 'f2');
                $result = array('a' => array('f1', 'f2'));
-               $this->assertEquals($result, array_merge_leaves($array1, $array2));
+               $this->assertEquals($result, array_merge_recursive($array1, $array2));
 
                // Non-numeric keys: An array and a value will be marged
                $array1 = array('b' => array('k1'));
                $array2 = array('b' => 'k2');
                $result = array('b' => array('k1', 'k2'));
-               $this->assertEquals($result, array_merge_leaves($array1, $array2));
+               $this->assertEquals($result, array_merge_recursive($array1, $array2));
 
                // Combination
                $array1 = array(
@@ -166,19 +169,23 @@ class SpamTest extends PHPUnit_TestCase
                        1,
                        'f4',
                );
-               $this->assertEquals($result, array_merge_leaves($array1, $array2));
+               $this->assertEquals($result, array_merge_recursive($array1, $array2));
 
-               // Values will not be unique?
+               // Values will not be unique
                $array1 = array(5, 4);
                $array2 = array(4, 5);
-               $result = array(5, 4);
-               $this->assertEquals($result, array_merge_leaves($array1, $array2));
+               $result = array_merge_recursive($array1, $array2);
+               $this->assertEquals(array(5, 4, 4, 5), $result);
+               $result = array_unique_recursive($result);
+               $this->assertEquals(array(5, 4),       $result);
 
                // One more thing ...
                $array1 = array('b' => array('k3'));
                $array2 = array('b' => 'k3');
-               $result = array('b' => array('k3'));
-               $this->assertEquals($result, array_merge_leaves($array1, $array2));
+               $result = array_merge_recursive($array1, $array2);
+               $this->assertEquals(array('b' => array('k3', 'k3')), $result);
+               $result = array_unique_recursive($result);
+               $this->assertEquals(array('b' => array('k3')),       $result);
        }
 
        function testFunc_uri_pickup()
@@ -523,26 +530,6 @@ EOF;
                $array = get_blocklist('goodhost');
                $this->assertTrue(isset($array['IANA-examples']));
        }
-
-       function testFunc_is_badhost()
-       {
-               // FALSE (Nothing)
-               $this->assertEquals(FALSE,   is_badhost(array(), FALSE, TRUE));
-               $this->assertEquals(array(), is_badhost(array(), FALSE, FALSE));
-
-               // TRUE
-               $this->assertEquals(TRUE,    is_badhost('=.blogspot.com', FALSE, TRUE));
-               $this->assertEquals(
-                       array(
-                               'B-1' => array(
-                                       '*.blogspot.com' => array(
-                                               '=.blogspot.com'
-                                       )
-                               )
-                       ),
-                       is_badhost('=.blogspot.com', FALSE, FALSE)
-               );
-       }
 }
 
 ?>
index 7ce706e..1ff264e 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: spam.php,v 1.158 2007/05/05 16:02:53 henoheno Exp $
+// $Id: spam.php,v 1.159 2007/05/11 15:25:36 henoheno Exp $
 // Copyright (C) 2006-2007 PukiWiki Developers Team
 // License: GPL v2 or (at your option) any later version
 //
@@ -1108,18 +1108,6 @@ function blocklist_distiller(& $hosts, $keys = array('goodhost', 'badhost'), $as
        return $blocked;
 }
 
-// Simple example for badhost (not used now)
-function is_badhost($hosts = array(), $asap = TRUE, $bool = TRUE)
-{
-       $list = get_blocklist('list');
-       $blocked = blocklist_distiller($hosts, array_keys($list), $asap);
-       foreach($list as $key=>$type){
-               if (! $type) unset($blocked[$key]); // Ignore goodhost etc
-       }
-
-       return $bool ? ! empty($blocked) : $blocked;
-}
-
 // Default (enabled) methods and thresholds (for content insertion)
 function check_uri_spam_method($times = 1, $t_area = 0, $rule = TRUE)
 {
@@ -1234,18 +1222,14 @@ function check_uri_spam($target = '', $method = array())
                        if ($asap && $is_spam) break;
 
                        // Merge only
-                       $blocked = array_merge_leaves($blocked, $_progress['blocked'], FALSE, FALSE);
-                       $hosts   = array_merge_leaves($hosts,   $_progress['hosts'],   FALSE, FALSE);
+                       $blocked = array_merge_recursive($blocked, $_progress['blocked']);
+                       $hosts   = array_merge_recursive($hosts,   $_progress['hosts']);
                }
 
                // Unique values
                $blocked = array_unique_recursive($blocked);
                $hosts   = array_unique_recursive($hosts);
 
-               // Renumber numeric keys
-               array_renumber_numeric_keys($blocked);
-               array_renumber_numeric_keys($hosts);
-
                // Recount $sum['badhost']
                $sum['badhost'] = array_count_leaves($blocked);
 
@@ -1396,20 +1380,6 @@ function array_count_leaves($array = array(), $count_empty = FALSE)
        return $count;
 }
 
-// Merge two leaves' value
-function array_merge_leaves(& $array1, & $array2, $unique_values = TRUE, $renumber_numeric = TRUE)
-{
-       $array = array_merge_recursive($array1, $array2);
-
-       // Redundant values (and keys) are vanished
-       if ($unique_values) $array = array_unique_recursive($array);
-
-       // All NUMERIC keys are always renumbered from 0
-       if ($renumber_numeric) array_renumber_numeric_keys($array);
-
-       return $array;
-}
-
 // An array-leaves to a flat array
 function array_flat_leaves($array, $unique = TRUE)
 {
@@ -1430,10 +1400,27 @@ function array_flat_leaves($array, $unique = TRUE)
        return $unique ? array_values(array_unique($tmp)) : $tmp;
 }
 
+// An array() to an array leaf
+function array_leaf($array = array('A', 'B', 'C.D'), $array_all = FALSE)
+{
+       $leaf = array();
+       $tmp  = & $leaf;
+       foreach($array as $arg) {
+               if (! is_string($arg) && ! is_int($arg)) continue;
+               $tmp[$arg] = array();
+               $parent    = & $tmp;
+               $tmp       = & $tmp[$arg];
+       }
+
+       if (! $array_all) $parent = key($parent);
+
+       return $leaf;   // array('A' => array('B' => 'C.D'))
+}
+
+
 // ---------------------
 // Reporting
 
-// TODO: Don't show unused $method!
 // Summarize $progress (blocked only)
 function summarize_spam_progress($progress = array(), $blockedonly = FALSE)
 {