{$var}', '/\(:votecounter\s(.*?):\)/ie', 'VoteCounter(\$pagename, \$pagename, "$1")'); function VoteCounter($pagename, $pn, $args) { global $vcDetailFmt, $vcDetailSep, $vcMaxWeight; SDV($vcMaxWeight, 4); // don't allow more than ++++, ----, +4, or -4 SDV($vcDetailFmt, '{RATETEXT}({COUNT})'); SDV($vcDetailSep, ', '); $opt = ParseArgs($args); if ($opt['page']) $pn = $opt['page']; $maxweight = (@$opt['maxweight'] ? $opt['maxweight'] : $vcMaxWeight); if (!$pn) $pn = $pagename; if (!$opt['']) $opt[''][] = 'votes'; if ($opt['detailfmt']) $DetailFmt = $opt['detailfmt']; else $DetailFmt = $vcDetailFmt; if ($opt['detailsep']) $DetailSep = $opt['detailsep']; else $DetailSep = $vcDetailSep; if (defined('toolbox')) { $page = tbRetrieveAuthPage($pagename, $pn, 'read'); $page['text'] = $page['ntext']; } else { $pn = MakePageName($pagename, $pn); $page = RetrieveAuthPage($pn, 'read'); } if (preg_match_all('/^\s*[*#]+\s*\(([^)]*)\)/m', $page['text'], $matches, PREG_SET_ORDER)) { foreach ($matches as $m) $votes[strtolower($m[1])]++; # Calculate $detail $detail = ''; foreach ($votes as $k => $v) { $detail .= ($detail?$DetailSep:''). str_replace(array('{RATETEXT}', '{COUNT}'), array($k, $v), $DetailFmt); } # Calculate Sum $sum = 0; foreach ($votes as $k => $v) { if ($k{0} == '+') $sum += $v; elseif ($k{0} == '-') $sum -= $v; } # Calculate weighted sum $wsum = 0; foreach ($votes as $k => $v) { if (preg_match('/([+-])((\1+)|(\d*))/', $k, $m)) { if (!$m[2]) $i = 1; // (+) or (-) elseif ($m[3]) $i = strlen($m[3])+1; // (++) or (--) else $i = $m[4]; // (+2) or (-2) if ($i > $maxweight) $i = $maxweight; if ($m[1] == '+') $wsum += $v*$i; else $wsum -= $v*$i; } } # Calculate votecount $count = count($matches); } else { $count = $sum = $wsum = 0; $detail = '(none)'; } $rtn = ''; foreach ($opt[''] as $mode) { $mode = strtolower($mode); if ($rtn && !preg_match('/[:,]\s*$/', $rtn)) $rtn .= ', '; switch ($mode) { case 'votes': $rtn .= "$count: $detail"; break; case 'votecount': $rtn .= $count; break; case 'votedetail': case 'votedetails': $rtn .= $detail; break; case 'votesum': $rtn .= $sum; break; case 'voteweightedsum': $rtn .= $wsum; break; default: $rtn .= $mode; break; } } return($rtn); }