cellog Sat Apr 7 20:40:29 2007 UTC Modified files: /CVSROOT/pear addkarma.php Log: improve addkarma dramatically http://cvs.php.net/viewvc.cgi/CVSROOT/pear/addkarma.php?r1=1.1&r2=1.2&diff_format=u Index: CVSROOT/pear/addkarma.php diff -u CVSROOT/pear/addkarma.php:1.1 CVSROOT/pear/addkarma.php:1.2 --- CVSROOT/pear/addkarma.php:1.1 Sat Apr 7 04:46:22 2007 +++ CVSROOT/pear/addkarma.php Sat Apr 7 20:40:29 2007 @@ -11,7 +11,13 @@ $newinfo = array(); $karma = array(); $new = new SplFileObject(dirname(__FILE__) . '/avail'); -foreach ($new as $line) { +$lines = array(); +$modulelines = array(); +foreach ($new as $i => $line) { + if (trim($line) == '# vim:set ft=conf sw=2 ts=2 et:') { + continue; + } + $lines[$i] = array('line' => $line, 'processed' => false); $line = trim($line); if (!strlen($line) || $line[0] == '#' || $line == 'unavail') { continue; @@ -35,6 +41,18 @@ $newinfo[$person][$module] = 1; } } + if (!isset($modulelines[$line[1]])) { + $modulelines[$line[1]] = array(); + } + $modulelines[$line[1]][] = $i; + foreach ($modules as $module) { + if (!isset($modulelines[$module])) { + $modulelines[$module] = array(); + } + $modulelines[$module][] = $i; + } + $lines[$i]['processed'] = array('rawmodules' => $line[1], + 'modules' => $modules, 'people' => $peoples); } $modules = explode(',', $_SERVER['argv'][2]); array_walk($modules, create_function('&$a,$b', '$a = trim($a,"/");')); @@ -61,14 +79,55 @@ } $need[] = $module; } +function combinations($need, $top = true) +{ + if (count($need) == 1) { + return array($need); + } + $ret = array(); + $max = count($need) - 1; + for ($i = 0; $i <= $max; $i++) { + $newarr = $need; + $first = array($need[$i]); + unset($newarr[$i]); + $newarr = array_values($newarr); + $combos = combinations($newarr, false); + foreach ($combos as $combo) { + $x = array_merge($first, $combo); + if ($top) $x = implode(',', $x); + $ret[] = $x; + } + } + return $ret; +} if (count($need)) { - echo 'adding karma for ' . implode(',', $modules) . "\n"; - clearstatcache(); - $fp = fopen(dirname(__FILE__) . '/avail', 'r+'); - // strlen("\n# vim:set ft=conf sw=2 ts=2 et:\n") - fseek($fp, -33, SEEK_END); - ftruncate($fp, ftell($fp)); - fwrite($fp, "avail|" . $_SERVER['argv'][1] . '|' . implode(',', $need) . - "\n# vim:set ft=conf sw=2 ts=2 et:\n"); - fclose($fp); + $possibilities = combinations($need); + // check for combos like pear,peardoc + foreach ($possibilities as $possible) { + if ($possible && isset($modulelines[$possible])) { + $need = $modules = array($possible); + break; + } + } + echo 'adding ' . $_SERVER['argv'][1] . ' karma for ' . implode(',', $modules) . "\n"; + echo "user now has karma for: \n ", implode(',', $modules), "\n ", + implode("\n ", $karma) . "\n"; + foreach ($need as $mod) { + if (isset($modulelines[$mod])) { + $newline = -1; + while ($newline == -1 || !$lines[$newline]['processed']) { + $newline = array_pop($modulelines[$mod]); + } + $lines[$newline]['line'] = 'avail|' . + implode(',', $lines[$newline]['processed']['people']) . ',' . + $_SERVER['argv'][1] . + '|' . $lines[$newline]['processed']['rawmodules'] . "\n"; + } else { + $lines[] = array('line' => "avail|" . $_SERVER['argv'][1] . '|' . $mod . "\n", + 'processed' => false); + } + } + $lines[] = array('line' => "\n# vim:set ft=conf sw=2 ts=2 et:\n", 'processed' => false); + array_walk($lines, create_function('&$a,&$b','$a = $a[\'line\'];')); + file_put_contents(dirname(__FILE__) . '/avail', implode('', $lines)); } \ No newline at end of file
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php