cellog Sun Apr 8 02:32:24 2007 UTC Modified files: /CVSROOT/pear addkarma.php Log: feature completion :) - smart sensing of existing karma - smart sensing of super-karma (won't add pear/Foo if the user has pear/ karma) - smart appending to existing karma group for new user - smart detection of karma groups - smart removal of unnecessary sub-karma (removes pear/Foo if user is upgraded to pear/ karma) - add in-file docs
http://cvs.php.net/viewvc.cgi/CVSROOT/pear/addkarma.php?r1=1.2&r2=1.3&diff_format=u Index: CVSROOT/pear/addkarma.php diff -u CVSROOT/pear/addkarma.php:1.2 CVSROOT/pear/addkarma.php:1.3 --- CVSROOT/pear/addkarma.php:1.2 Sat Apr 7 20:40:29 2007 +++ CVSROOT/pear/addkarma.php Sun Apr 8 02:32:24 2007 @@ -1,4 +1,41 @@ <?php +/** + * Karma manager for PEAR + * + * This file is used to add or upgrade karma to a CVS user for any pear-related + * repository managed by the avail file (not CVSROOT/pear - that is managed in + * CVSROOT/pear/group by hand). + * + * To use, simply type: + * + * php addkarma.php handle dir1[,dir2[,dir3...]] + * + * To add karma for a top-level directory like "pear", append / like: + * + * php addkarma.php handle pear/ + * + * The script is smart enough to detect an attempt to give unnecessary karma. + * If a user has karma for pear/, and karma is requested for pear/Foo, the script + * will exit with a message saying this is unnecessary. + * + * The script is also smart enough to detect an existing line (for instance, if you + * wish to add karma for "pear,peardoc") and will detect the correct line regardless + * of input order, so that this works: + * + * php addkarma.php handle peardoc/,pear/ + * + * as well as: + * + * php addkarma.php handle pear/,peardoc/ + * + * Both statements will add a handle to the long pear,peardoc avail statement. + * + * If a user is upgraded (given karma for pear/ when they before had only karma + * for pear/subdir), the script will remove the karma for pear/subdir. + * + * Always do cvs diff -u avail prior to committing to ensure changes are as + * expected! + */ if (!isset($_SERVER['argv'])) { die("Must use CLI\n"); } @@ -8,6 +45,7 @@ if (!strpos($_SERVER['argv'][2], '/')) { die("usage: addkarma.php handle directory,directory (use directory/ for top-level)\n"); } +$handle = $_SERVER['argv'][1]; $newinfo = array(); $karma = array(); $new = new SplFileObject(dirname(__FILE__) . '/avail'); @@ -35,7 +73,7 @@ $peoples = explode(',', $line[0]); foreach ($peoples as $person) { foreach ($modules as $module) { - if ($person == $_SERVER['argv'][1]) { + if ($person == $handle) { $karma[] = $module; } $newinfo[$person][$module] = 1; @@ -56,7 +94,7 @@ } $modules = explode(',', $_SERVER['argv'][2]); array_walk($modules, create_function('&$a,$b', '$a = trim($a,"/");')); -$need = array(); +$need = $remove = array(); foreach ($modules as $module) { $test = explode('/', $module); // test karma for directory and for parent directories @@ -79,6 +117,82 @@ } $need[] = $module; } +if (count($need)) { + foreach ($karma as $module) { + $test = explode('/', $module); + array_pop($test); + // test to see if karma grants more than existing + // i.e. give pear/ karma, and the user already has pear/Foo karma + while (is_array($test) && count($test)) { + $mod = implode('/', $test); + if (in_array($mod, $need)) { + $remove[] = $module; + echo 'removing ', $module, + ' karma (adding ', $mod, " karma)\n"; + continue 2; + } + array_pop($test); + } + } + if (count($remove)) { + $flipkarma = array_flip($karma); + } + foreach ($remove as $module) { + unset($karma[$flipkarma[$module]]); + foreach ($modulelines[$module] as $linenum) { + if (!$lines[$linenum]['processed']) continue; + if (in_array($handle, $lines[$linenum]['processed']['people'])) { + if (count($lines[$linenum]['processed']['people']) == 1) { + // only 1 handle for this line (our dude) + if (count($lines[$linenum]['processed']['modules']) == 1) { + // this line is avail|dude|module and will become avail||module + // so erase it + $lines[$linenum]['line'] = ''; + $lines[$linenum]['processed'] = false; + continue; + } else { + // this line contains other karma information i.e. + // avail|dude|module,module2 + // remove the module only, leaving avail|dude|module2 + foreach ($lines[$linenum]['processed']['modules'] as $i => $b) { + if ($b == $module) { + unset($lines[$linenum]['processed']['modules'][$i]); + $lines[$linenum]['processed']['rawmodules'] = + implode(',', $lines[$linenum]['processed']['modules']); + $lines[$linenum]['line'] = 'avail|' . + $handle . '|' . + $lines[$linenum]['processed']['rawmodules'] . + "\n"; + continue 2; + } + } + } + } + // if we reach here, there is more than 1 handle on this line i.e. + // avail|dude,somebodyelse|module + foreach ($lines[$linenum]['processed']['people'] as $i => $b) { + // remove dude + if ($b == $handle) { + unset($lines[$linenum]['processed']['people'][$i]); + break; + } + } + if (!count($lines[$linenum]['processed']['people'])) { + // delete this line, no handles left on it i.e. + // avail||module + $lines[$linenum]['line'] = ''; + $lines[$linenum]['processed'] = false; + continue; + } + // if we reach here, we've deleted dude and left: + // avail|somebodyelse|module + $lines[$linenum]['line'] = 'avail|' . + implode(',', $lines[$linenum]['processed']['people']) . + '|' . $lines[$linenum]['processed']['rawmodules'] . "\n"; + } + } + } +} function combinations($need, $top = true) { if (count($need) == 1) { @@ -104,13 +218,16 @@ $possibilities = combinations($need); // check for combos like pear,peardoc foreach ($possibilities as $possible) { + if (is_array($possible)) { + $possible = $possible[0]; + } 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 ", + echo 'adding ', implode(',', $modules), ' karma for ' . $handle . "\n"; + echo $handle, " now has karma for: \n ", implode(',', $modules), "\n ", implode("\n ", $karma) . "\n"; foreach ($need as $mod) { if (isset($modulelines[$mod])) { @@ -120,14 +237,14 @@ } $lines[$newline]['line'] = 'avail|' . implode(',', $lines[$newline]['processed']['people']) . ',' . - $_SERVER['argv'][1] . + $handle . '|' . $lines[$newline]['processed']['rawmodules'] . "\n"; } else { - $lines[] = array('line' => "avail|" . $_SERVER['argv'][1] . '|' . $mod . "\n", + $lines[] = array('line' => "avail|" . $handle . '|' . $mod . "\n", 'processed' => false); } } - $lines[] = array('line' => "\n# vim:set ft=conf sw=2 ts=2 et:\n", 'processed' => false); + $lines[] = array('line' => "# 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