Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
executable file 237 lines (191 sloc) 5.87 KB
#!/usr/bin/perl -w
# This code is a part of Slash, and is released under the GPL.
# Copyright 1997-2005 by Open Source Technology Group. See README
# and COPYING for more information, or see http://slashcode.com/.
# $Id$
use strict;
use File::Basename;
use Getopt::Std;
use Slash;
use Slash::Utility;
use Benchmark;
use Data::Dumper;
use vars qw( $slashdb $constants $moddb $metamod_db );
(my $VERSION) = ' $Revision$ ' =~ /\$Revision:\s+([^\s]+)/;
my $PROGNAME = basename($0);
my (%opts);
# Remember to doublecheck these match usage()!
usage('Options used incorrectly') unless getopts('hFD:u:v', \%opts);
usage() if $opts{'h'};
version() if $opts{'v'};
$opts{'u'} ||= 'slash';
$opts{num_closed} = $ARGV[0] || 10;
usage('Invalid number of meta-moderations')
if $opts{num_closed} !~ /^\d+$/ || ($opts{num_mods} && $opts{num_mods} < 0);
createEnvironment($opts{u});
setCurrentSkin(determineCurrentSkin());
$slashdb = getCurrentDB();
$constants = getCurrentStatic();
$metamod_db = getObject('Slash::Metamod');
if (!$constants->{m2}) {
usage('Metamoderation has been disabled with the "m2" var');
}
$moddb = getObject("Slash::$constants->{m1_pluginname}");
my $reasons = $moddb->getReasons();
# Insure we don't exceed the maximum number of moderations that can be made.
{
my $max_mods = $slashdb->sqlCount(
'moderatorlog',
"m2count < $constants->{m2_consensus}"
);
if ($opts{num_closed} > $max_mods) {
$opts{num_closed} = $max_mods ;
print "- Count set to maximum number of mods, $max_mods\n";
}
}
# main program logic (in braces to offset nicely)
{
print "Generating meta-moderations...\n";
my $multicount = $constants->{m2_multicount} || 0;
my $t0 = new Benchmark;
my $startcount = getM2ClosedCount();
my($curcount, $m2count) = (0, 0);
my(@cids);
my $users_without_m2 = 0;
LOOP: { do {{
my $m2uid = getRandomUID();
my($m2user, $comments);
if ($m2uid) {
$m2user = $slashdb->getUser($m2uid);
$comments = $metamod_db->getMetamodsForUser(
$m2user, $constants->{m2_comments}
);
if (!$comments || !@$comments) {
# No more M2 for this user.
last LOOP if ++$users_without_m2 > 10;
getRandomUID('exclude', $m2user->{uid});
next LOOP;
}
}
last LOOP if !$m2uid;
$users_without_m2 = 0;
@cids = @{$comments};
{
local $" = ', ';
$_ = $_->{id} for @cids;
print "M2: UID #$m2user->{uid} metamoderates"
. " " . ($#cids + 1)
. " - (@cids)"
. " (+more? multi=$multicount)\n";
}
my %m2s = ( );
for (@{$comments}) {
# Our M2 vote starts off with a bias towards "fair".
my $unfair_frac = 0.10;
$unfair_frac = 0.30 if $reasons->{$_->{reason}}{val} == -1;
# Some mods are more likely to be unfair than others.
# They are just bad mods. Don't hold it against them.
my $old_rand = rand(2**30);
srand($_->{id});
my $mod_bias = rand(3)*rand(3);
$unfair_frac *= $mod_bias;
# And some users are more likely to vote fair or unfair
# than others: about 10 percentage points (up or down)
# is the average variance.
srand($m2user->{uid});
my $user_bias = rand(0.3)+rand(0.3)-0.3;
$unfair_frac += $user_bias;
# Oh, and try to keep the random flow going outside
# this little hiccup of nonrandomness.
srand($old_rand);
# OK, decide whether the vote is for fair or unfair.
my $is_fair = (rand(1) < $unfair_frac) ? 0 : 1;
$m2s{$_->{id}}{is_fair} = $is_fair;
}
# Do the metamod.
$metamod_db->setM2ModsSaved($m2user, [( keys %m2s )]);
$m2user = $slashdb->getUser($m2user->{uid});
$metamod_db->createMetaMod($m2user, \%m2s, $multicount);
$m2count++;
$curcount = getM2ClosedCount() if ! $opts{F};
}} while (!$opts{F} && $curcount-$startcount < $opts{num_closed}) ||
$opts{F};
} # LOOP
my $now_closed = getM2ClosedCount();
my $closed = $now_closed - $startcount;
printf "Exhausted M2 on %d users\n", getRandomUID('size');
my $t1 = new Benchmark;
printf <<EOT, $closed || 0, timestr(timediff($t1, $t0), 'noc');
$m2count meta-moderations (closing %d mods) created in:
%s
EOT
}
# subroutines
sub getM2ClosedCount {
return $slashdb->sqlCount('moderatorlog', 'm2status=1');
}
# CLOSURE!
{
my($list_size);
my($uid_list);
sub getRandomUID {
my($op, $data) = @_;
if ($op) {
if ($op eq 'exclude') {
print "Excluding user #$data\n";
@{$uid_list} = grep { $_ != $data} @{$uid_list};
}
if ($op eq 'size') {
return $list_size;
}
}
if (!$uid_list) {
my $max_uid = int(
$slashdb->countUsers({ max => 1}) *
$constants->{m2_userpercentage}
);
$uid_list = $slashdb->sqlSelectAll(
'users.uid',
'users, users_prefs, users_info',
"karma >= 0 AND willing > 0 AND users.uid < $max_uid AND
users.uid=users_prefs.uid AND users.uid=users_info.uid
AND users.uid!=$constants->{anonymous_coward_uid}"
);
$_ = $_->[0] for @{$uid_list};
$list_size = scalar @{$uid_list};
}
return $uid_list->[int rand($#{$uid_list} + 1)] if @{$uid_list};
return;
} }
sub usage {
print "*** $_[0]\n" if $_[0];
# Remember to doublecheck these match getopts()!
print <<EOT;
Usage: $PROGNAME [OPTIONS] [# of moderations to close]
This utility creates test moderations for a given Slash site. This program is for
testing purposes, only, particularly for those ambitious Slash users out there
who want to try their hand at modifying the comment or moderation systems.
This program is *not* designed to work for large sites, just small test sites
that have been recently installed.
DO NOT USE THIS ON A PRODUCTION SITE.
You have been warned. :)
Main options:
-h Help (this message)
-D <x> Set debug level to <x>
-F Finish - Create M2s until there are no more valid moderations
left
-v Version
-u Virtual user (default is "slash")
EOT
exit;
}
sub version {
print <<EOT;
$PROGNAME $VERSION
This code is a part of Slash, and is released under the GPL.
Copyright 1997-2005 by Open Source Technology Group. See README
and COPYING for more information, or see http://slashcode.com/.
EOT
exit;
}
__END__
Something went wrong with that request. Please try again.