-
Notifications
You must be signed in to change notification settings - Fork 8
/
createTestMetamods
executable file
·230 lines (185 loc) · 5.6 KB
/
createTestMetamods
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/perl -w
# This code is a part of Slash, and is released under the GPL.
# Copyright 1997-2004 by Open Source Development Network. 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 );
(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} < 0;
createEnvironment($opts{u});
$slashdb = getCurrentDB();
$constants = getCurrentStatic();
my $reasons = $slashdb->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 = $slashdb->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.
$slashdb->setModsSaved($m2user, [( keys %m2s )]);
$m2user = $slashdb->getUser($m2user->{uid});
$slashdb->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-2004 by Open Source Development Network. See README
and COPYING for more information, or see http://slashcode.com/.
EOT
exit;
}
__END__