-
Notifications
You must be signed in to change notification settings - Fork 8
/
createTestMods
executable file
·306 lines (263 loc) · 7.73 KB
/
createTestMods
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/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 Digest::MD5 'md5_hex';
use Benchmark;
use Slash;
use Slash::Utility;
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('hI:RrTu:U:v', \%opts);
usage() if ($opts{'h'} || !keys %opts);
version() if $opts{'v'};
# Parallel syntax checking for R/U and r/I.
for ([qw(R U)], [qw(r I)]) {
if (exists $opts{$_->[0]}) {
usage("Can't specify -$_->[1] when using -$_->[0]")
if exists $opts{$_->[1]};
} else {
usage("Must specify either -$_->[0], or -$_->[1]")
if ! exists $opts{$_->[1]};
usage('Invalid UID specified')
unless $_->[1] ne 'U' || $opts{$_->[1]} =~ /^\d+$/;
}
}
$opts{'u'} ||= 'slash';
$opts{num_mods} = $ARGV[0] || 10;
usage('Invalid number of moderations')
if $opts{num_mods} !~ /^\d+$/ || $opts{num_mods} < 0;
createEnvironment($opts{u});
$slashdb = getCurrentDB();
$constants = getCurrentStatic();
# Story->Discussion lookup hash.
print "Loading stories...\n";
my $stories = $slashdb->sqlSelectAllHashref(
'sid',
'sid, discussion',
'stories'
);
# CID->UID lookup hash.
print "Loading comment authors...\n";
# For chosing random discussions.
print "Loading discussions...\n";
my $discussions = [
map { $_ = $_->[0][0] }
$slashdb->sqlSelectAll(
'sid, count(*) as c',
'comments',
'',
'group by sid having c > 0'
)
];
my(%comments, %comment_data);
# main program logic (in braces to offset nicely)
{
print "Generating moderations...\n";
my $story_id = $opts{I} || '';
my $discussion_id;
if ($story_id =~ /^\d+$/) {
$discussion_id = $story_id;
} else {
$discussion_id = $stories->{$discussion_id}{discussion}
}
my $t0 = new Benchmark;
my $count = $opts{num_mods};
while ($count) {
my $did = (exists $opts{r}) ?
$discussions->[rand $#{$discussions} + 1] :
$discussion_id;
if (!$did) {
print "Cannot create comments, no discussions found.\n";
exit 0;
}
if (!exists $comments{$did}) {
$comments{$did} = [
map { $_ = $_->[0] }
@{$slashdb->sqlSelectAll(
'cid', 'comments', "sid=$did"
)}
];
$comment_data{$did} = $slashdb->sqlSelectAllHashref(
'cid',
'sid, cid, uid, points',
'comments',
"sid=$did"
);
}
# Determine the moderator.
my $uid = ($opts{R}) ?
getRandomUserID($comment_data{$did}) :
$opts{U};
# Moderators will usually moderate in batches. For now, we
# will use random batches up to a tenth of the specified
# value.
#
# Here's the catch. Any random moderator picked, cannot be
# a comment creator, and vice versa!
my $batchsize = ($opts{num_mods} / 10) || 1;
my $batch = int(rand $batchsize) + 1;
$batch = $count if $batch > $count;
my @cidbatch = ();
while ($batch--) {
my @val = (-1, +1);
# CAREFUL!
$count--;
LOOP: while (@cidbatch < @{$comments{$did}}) {
my $rcid = $comments{$did}->[
rand $#{$comments{$did}} + 1
];
for (@cidbatch) {
next LOOP if $_ == $rcid;
}
push @cidbatch, $rcid;
#{local $"='|';print STDERR "C: [@cidbatch]\n"}
last;
}
my $cid = $cidbatch[-1];
my $comment = $slashdb->getComment($cid);
my($modval, $reason, $active);
my $reasons = $slashdb->getReasons();
$reason = (keys %$reasons)[rand(scalar(keys %$reasons)-1) + 1];
$modval = $reasons->{$reason}{val};
my $pointsneeded = $slashdb->getModPointsNeeded(
$comment_data{$did}{$cid}{points},
$comment_data{$did}{$cid}{points} + $reasons->{$reason}{val},
$reason);
my $user = { uid => $uid };
setRandomIPID($user);
$slashdb->createModeratorLog(
$comment,
$user,
$modval,
$reason,
($active = (rand(100) > 95) ? 0 : 1),
$pointsneeded,
);
die if $DBI::errstr;
printf <<EOT, $comment->{cid}, $comment->{uid}, $modval;
M($count): Mod=$uid, CID=%s, CUid=%s, R=$reason, V=%s, A=$active
EOT
}
}
my $t1 = new Benchmark;
print "$opts{num_mods} moderations created in: ",
timestr(timediff($t1, $t0), 'noc'),"\n";
}
# subroutines
# CLOSURE!
# Yes. I am getting a little silly with these. But it's nice not having.
# them clutter scope elsewhere, when they are only needed here.
{
my $depth = 0;
my($min_uid, $max_uid);
my(%exception_list);
sub getRandomUserID {
my ($key,$val) = each %{$_[0]};
++$depth;
die "sorry, not enough activity on site to do a random mod" if $depth > 10;
if (!exists $exception_list{$val->{sid}}) {
LOOP:
for my $cid (keys %{$_[0]}) {
for (@{$exception_list{$val->{sid}}}) {
next LOOP if $_ == $_[0]->{$cid}{uid};
}
push @{$exception_list{$val->{sid}}},
$_[0]->{$cid}{uid};
}
@{$exception_list{$val->{sid}}} =
sort {$a <=> $b} @{$exception_list{$val->{sid}}};
# {
# local $"=',';
# printf STDERR <<EOT;
#E ($val->{sid}): @{$exception_list{$val->{sid}}}
#EOT
#
# }
}
$max_uid ||= $slashdb->countUsers({ max => 1});
$min_uid ||= $slashdb->sqlSelect('MIN(uid)', 'users');
my $ret = $min_uid + int(rand($max_uid - $min_uid + 1));
for (@{$exception_list{$val->{sid}}}) {
$ret = getRandomUserID(@_), last if $_ == $ret;
}
--$depth;
return $ret;
} }
# CLOSURE!
{
my(@subnets) = ();
sub setRandomIPID {
my $user = shift;
my $seed = int(rand(65536));
if (!@subnets) {
# We now generate 10 random subnets which we will use for
# the rest of this execution.
srand(12345);
while (@subnets < 10) {
my $subnet = sprintf "%d.%d.%d",
1+rand 254, 1+rand 254, 1+rand 254;
push @subnets, $subnet;
}
}
srand($user->{uid} + (time % 5));
my $uid10 = $user->{uid} % 10;
my $quad3;
my $fourth;
if ($uid10 < rand(10)) {
$quad3 = $subnets[$uid10];
if ($uid10 < rand(10)) {
$fourth = $uid10 + 1 + (int($user->{uid}/50) % 254);
} else {
$fourth = 1+int(rand(4))*int(rand(4))*int(rand(12));
}
} else {
$quad3 = $subnets[rand($#subnets + 1)];
$fourth = 1+int(rand(16))*int(rand(16));
}
$user->{subnetid} = md5_hex("$quad3.0");
$user->{ipid} = md5_hex("$quad3.$fourth");
srand($$ + time + $seed);
}
}
sub usage {
print "*** $_[0]\n" if $_[0];
# Remember to doublecheck these match getopts()!
print <<EOT;
Usage: $PROGNAME [OPTIONS] [#moderations]
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)
-v Version
-u Virtual user (default is "slash")
Identity options (specify one):
-U User to moderate as. Not for use with -R
-R Moderate as random users. Not for use with -U
-r Moderate over random stories. Not for use with -I
-I Story ID to be moderated. Not for use with -r
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__