-
Notifications
You must be signed in to change notification settings - Fork 8
/
createTestComments
executable file
·505 lines (463 loc) · 15.8 KB
/
createTestComments
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#!/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$
BEGIN {
{
require Silly::Werder;
Silly::Werder->import;
}
die "Installation of Silly::Werder is required for this util, sorry!\n"
if $@;
}
use strict;
use File::Basename;
use Getopt::Std;
use Slash;
use Slash::Utility;
use Digest::MD5 'md5_hex';
use Benchmark;
use vars qw( $slashdb $werder $constants $junk );
(my $VERSION) = ' $Revision$ ' =~ /\$Revision:\s+([^\s]+)/;
my $PROGNAME = basename($0);
my (%opts, %family_tree, %deep_tree);
# Remember to doublecheck these match usage()!
usage('Options used incorrectly') unless getopts('hI:RTJu:U:tv', \%opts);
usage() if ($opts{'h'} || !keys %opts);
version() if $opts{'v'};
if ($opts{R}) {
usage("Can't specify -U when using -R") if exists $opts{U};
} else {
usage("Must specify either -R, or both -U and -I")
if ! exists $opts{U};
usage('Invalid UID specified') unless $opts{U} =~ /^\d+$/;
}
if ($opts{T} && $opts{t}) {
usage("Can't specify both -t and -T, pick one");
}
$junk = $opts{'J'} ? 1 : 0;
$opts{'u'} ||= 'slash';
$opts{num_comments} = $ARGV[0] || 10;
usage('Invalid number of comments')
if $opts{num_comments} !~ /^\d+$/ || $opts{num_comments} < 0;
createEnvironment($opts{u});
setCurrentSkin(determineCurrentSkin());
$slashdb = getCurrentDB();
$constants = getCurrentStatic();
$werder = new Silly::Werder;
# main program logic (in braces to offset nicely)
{
# Determine user list size (for -R)
my $max_uid = $slashdb->countUsers({ max => 1});
my $min_uid = $slashdb->sqlSelect("MIN(uid)", "users");
my $story_id = $opts{I} || "";
my $t0 = new Benchmark;
my $count = $opts{num_comments};
while ($count) {
my($stoid, $discussion_id) = ('', '');
if ($story_id =~ /^\d+$/) {
$discussion_id = $story_id;
} else {
if ($story_id =~ m{/}) {
$stoid = $story_id;
} else {
$stoid = getRandomStoid();
}
$discussion_id = $slashdb->getStory($stoid, 'discussion');
}
my $discussion = $slashdb->getDiscussion($discussion_id);
if (!$discussion_id || !$discussion) {
print "Cannot create comments, no stories found.\n";
exit 0;
}
%family_tree = %deep_tree = ( );
# Initialize the user.
my $uid;
my $user = undef;
while (!$user) {
if ($opts{R}) {
if (rand(1) < 0.5) {
$uid = getRandomUserID($min_uid, $max_uid);
} else {
$uid = $constants->{anonymous_coward_uid};
}
} elsif ($opts{U}) {
$uid = $opts{U};
} else {
$uid = $min_uid;
}
$user = $slashdb->getUser($uid);
}
setRandomIPID($user);
# Before we start creating comments, we should take a look at
# the thread structure (if we care about threading...)
my (@werds);
if (!$opts{T}) {
my $comments = $slashdb->sqlSelectAllHashref(
'cid',
'cid, pid',
'comments',
"sid=$discussion_id",
);
print "R: ", ref $comments;
map {
$family_tree{$comments->{$_}{cid}}
= $comments->{$_}{pid}
} keys %{$comments};
}
if ($opts{t}) {
for my $cid (keys %family_tree) {
my $count = 0;
my $cid_back = $cid;
while (exists($family_tree{$cid_back}) && ++$count < 1000) {
$deep_tree{$cid}++;
$cid_back = $family_tree{$cid_back};
}
}
}
my $num_werds = rand(20)*rand(20)+rand(10) + 1;
$num_werds += 200 if rand(1) < 0.1;
$num_werds += 300 if rand(1) < 0.1;
my $comment = make_werds($num_werds, 0.1);
my $mode_rand = rand(1);
if ($mode_rand < 0.25) { $comment = strip_plaintext($comment) }
elsif ($mode_rand < 0.50) { $comment = strip_html ($comment) }
elsif ($mode_rand < 0.75) { $comment = strip_extrans ($comment) }
else { $comment = strip_code ($comment) }
$comment = balanceTags($comment, { deep_nesting => 1 });
$comment = addDomainTags($comment);
my $score = 1;
if ($uid == $constants->{anonymous_coward_uid}
|| $user->{karma} < $constants->{badkarma}) {
$score = 0;
} elsif ($user->{karma} > $constants->{goodkarma}) {
$score = 2;
}
my $score_rand = rand(1);
if ($score_rand < 0.08 ) { $score = -1 }
elsif ($score_rand < 0.15 ) { $score += -1 }
elsif ($score_rand < 0.75 ) { }
elsif ($score_rand < 0.90 ) { $score += 1 }
elsif ($score_rand < 0.94 ) { $score += 2 }
elsif ($score_rand < 0.98 ) { $score += 3 }
else { $score = 5 }
my $subj = make_werds(1 + rand 5);
$subj =~ s/\W+$// if rand(1) < 0.1;
$subj =~ s/<[^>]+>//g;
$subj =~ s/\b(\w)/\U$1/g if rand(1) < 0.2;
if (rand(1) < 0.2) {
$subj =~ s/\W*$/ +((qw( . ! ? ))[rand 3]) x (2+rand 5) /e;
}
my $pid = getRandomParent();
my $clean_comment = {
comment => $comment,
sid => $discussion_id,
pid => $pid,
ipid => $user->{ipid},
subnetid => $user->{subnetid},
subject => $subj,
uid => $user->{uid},
points => $score,
};
my $cid = $slashdb->createComment($clean_comment);
my $tc = $slashdb->getVar('totalComments', 'value', 1);
$slashdb->setVar('totalComments', ++$tc);
$slashdb->setStory($stoid, { is_dirty => 1 }) if $stoid;
$family_tree{$cid} = $pid if ! $opts{T};
$deep_tree{$cid} = $deep_tree{$pid}+1 if $opts{t};
print <<EOT;
($count) Comment created (user=$uid discussion=$discussion_id cid=$cid pid=$pid)
EOT
$count--;
}
my $t1 = new Benchmark;
print "$opts{num_comments} comments created in: ",
timestr(timediff($t1, $t0), 'noc'),"\n";
}
# subroutines
sub getRandomStoid {
# A minority of comments are posted to sectional stories.
my $sectional = rand(1) < 0.3 ? 1 : 0;
my $nexus_ar;
if ($sectional) {
$nexus_ar = $slashdb->getNexusChildrenTids($constants->{mainpage_nexus_tid});
} else {
$nexus_ar = [ $constants->{mainpage_nexus_tid} ];
}
my $nexus_list = join(", ", @$nexus_ar);
my $stoids = $slashdb->sqlSelectColArrayref(
"stories.stoid",
"stories, story_topics_rendered",
"stories.stoid=story_topics_rendered.stoid
AND discussion > 0
AND story_topics_rendered.tid IN ($nexus_list)
AND is_archived = 'no'
AND in_trash = 'no'
AND time < NOW()",
"ORDER BY time DESC");
return undef unless $stoids && @$stoids;
# Try to pick a random stoid to post to that weights toward the
# most recent sids.
my $r;
if (rand(1) < 0.05 || scalar(@$stoids) < 15) {
$r = rand(@$stoids);
} elsif (rand(1) < 0.8) {
$r = rand(1) * rand(1) * 15;
} elsif (rand(1) < 0.5 || scalar(@$stoids) < 30) {
$r = rand(1) * rand(1) * 10;
} else {
$r = rand(1) * rand(1) * 30;
}
return $stoids->[$r];
}
sub getRandomParent {
return 0 if $opts{T};
my @a = keys %family_tree;
if ($opts{t}) {
# Weight toward nesting deeply.
my @deepest_cids =
sort { $deep_tree{$b} <=> $deep_tree{$a} }
keys %deep_tree;
if (scalar(@deepest_cids) >= 8) {
@a = @deepest_cids[0, 0, 0, 0, 0, 1, 1, 2..5];
} elsif (scalar(@deepest_cids) >= 5) {
@a = @deepest_cids;
} else {
# leave @a alone
}
}
if (!$opts{t}) {
# If not very many posts, return 0 for the root.
return 0 if rand(@a) < 5;
# Always a chance of returning 0.
return 0 if rand(1) > 0.75;
}
# Otherwise pick one.
my $pid = $a[ rand @a ];
return $pid || 0;
}
sub getRandomUserID {
my($min_uid, $max_uid) = @_;
my $rand_uid = $slashdb->sqlSelect("uid", "users",
"uid BETWEEN $min_uid AND $max_uid", "ORDER BY RAND() LIMIT 1");
return $rand_uid;
}
# 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;
$uid10 = int(rand(7)) if $user->{uid} == $constants->{anonymous_coward_uid};
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 make_werds {
my($werds, $link_prob) = @_;
$werds = int($werds);
$link_prob ||= 0;
if ($werds > 40) {
$werder->set_werds_num(5,20);
} elsif ($werds > 20) {
$werder->set_werds_num(3,10);
} else {
$werder->set_werds_num($werds, $werds);
}
my $min_syl = int(rand(2)+1);
$werder->set_syllables_num($min_syl, $min_syl + int(rand(5)+1));
my @links = qw(
http://slashdot.org/ http://www.brunching.com/
http://www.newsforge.com http://ostg.com
http://whitehouse.gov http://www.whitehouse.gov/
http://us.imdb.com/ http://www.kernel.org
http://ic.ac.uk http://cr.yp.to/
http://www.verizonreallyreallyreallysucks.com/
ftp://ftp.kernel.org/ ftp://ftp.gnu.org/gnu/Licenses/COPYING-2.0
news:news.admin.net-abuse.email gopher://ccat.sas.upenn.edu:3333/11/Fiction/
http://List-Etiquette.com/about/
http://slashdot.org/features/99/03/31/0137221.shtml
http://www.nap.edu/readingroom/books/newpath/chap2.html
http://www.forthnet.gr/forthnet/isoc/short.history.of.internet
http://www.freenix.fr/unix/linux/HOWTO/French-HOWTO.html
http://www.linux.org.mx/ http://www.linux.net.mx/linux.php
http://www.linux.org.uk/diary/ http://hepwww.ph.qmw.ac.uk/HEPpc/
http://www.w3.org http://peipa.essex.ac.uk/tp-linux/
http://www.crosswinds.net/~tvquotes/futurama/2.html
http://www.google.com/search?q=cache:h58W7o4qU-w:www.space.com/sciencefiction/tv/futurama_gore_000516_wg.html+Futurama+Fry&hl=en
http://dmoz.org/Regional/Europe/United_Kingdom/Wales/Isle_of_Anglesey/Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch/
www.google.com bad_link b.a.d l.i.n.k . .. ... ....
mailto:jamie@slashdot.org mailto:pater@slashdot.org
);
push @links, 'http://directory.google.com/Top/Arts/Literature/Authors/C/Carroll,_Lewis/Works/Hunting_of_the_Snark,_The/';
my $comment;
my $cur_werds = 0;
my $tag = "";
my $quote = q{};
my $last_p_werd_num = 0;
while ($cur_werds < $werds) {
my $new_werds = '';
if (int(rand(6))==0 || $werds < $cur_werds * 1.1) {
if (int(rand(2))) {
$new_werds = $werder->question;
} else {
$new_werds = $werder->exclaimation; # Dave, you spelled this word wrong
}
} else {
$new_werds = $werder->sentence;
}
if ($link_prob && rand(1) < $link_prob) {
my @new_werds = split / /, $new_werds;
if ($#new_werds > 2) {
my $a_href = q{<a href="} . $links[rand @links] . q{">} . (rand(1)<0.1?" ":"");
my $close_a = (rand(1)<0.1?" ":"") . "</a>";
my $insert_start = int(rand($#new_werds-2));
my $insert_end = $insert_start + int(rand(4))+1;
$insert_end = $#new_werds - 1 if $insert_end > $#new_werds - 1;
$new_werds = join(" ",
@new_werds[0..$insert_start-1],
$a_href . $new_werds[$insert_start],
@new_werds[$insert_start+1..$insert_end-1],
$new_werds[$insert_end] . $close_a,
@new_werds[$insert_end+1..$#new_werds]
);
}
}
if ($cur_werds && !$tag && rand > 0.9) {
if (rand(1) < 0.95) { $tag = "i" }
else { $tag = "b" }
if (rand(1) < 0.50) { $quote = int(rand(3)) ? q{'} : q{"} };
$new_werds = "<$tag>$quote$new_werds";
}
if ($tag && rand(1) < 0.5) {
$new_werds = "$new_werds$quote</$tag>";
$tag = ""; $quote = q{};
}
$comment .= " $new_werds";
$comment =~ s{\s+}{ }g;
$cur_werds = $comment =~ tr/ / /;
if (rand($werds) > 30
and rand($cur_werds - $last_p_werd_num) > 20) {
$comment .= "<P>";
$last_p_werd_num = $cur_werds;
}
}
$comment .= "</$tag>" if $tag;
$comment =~ s{</(\w+)>\s+<\1>}{ }g;
$comment =~ s/^\s*(.+)\s*$/$1/;
$comment =~ s/^\s+//; $comment =~ s/\s+$//;
if ($link_prob && $junk) {
my @fragments = (
"<a", "href=", 'href="', '"', "'", ">", '>">', " > >",
"<", " < ", " <<", ">>", " < < <", "< < ", "<? ", "<<",
".", "..", "../", "/..", "/../../..", " ", " ", " \r ",
":::", "::::::", "::::::::::", "///", "////////", ":/:/:/",
" \n ", "\n ", "\n\n", "\r\n\r", "\n\r\n", "\t \t", "\t",
"<i>", "</ i >", "<i >", "< /i >", "< i>", "< / i>",
"<i", "</i", "i>", "/i>", "/i/", "i/i", " // ", " / ",
"%2e", "%2f", "%3c", "%3e", "<", ">", "&", "&",
"&", "& ", "&&", "> ;", "�", "", "&#amp;",
"x", "(", "0", "&12345;", "ऩ", "&999 ",
"<<%3c", ">>%3e", "%3e< ", "><", "<>", "<><>",
"%25%", "%25", ".", "", "& &", "<&> <", ">&<",
'<a hReF="/', '<A href = "//', '<A HREF=http://',
'<a href=""http://', '<a HREF="news:', '<a href="ftp:',
"<a\thref=\"/", '<a href="///', "<a\rhref=\"http:",
"<A\nHREF=\"http:", "<A\nHREF=http:", "<A\nHREF=\"http",
'<a href="ftp://ab:xz@', '<a href="ww~w\@foo.com/',
'<a href="@bar.com/', '<a href="http://@bar.com/',
'<a href="news:a:b@', '<a href="news:a:b:c:d@foo.com',
'<a href="ftp://aaa@foo.com', '<a href="http://bbb@bar',
'<a href="http://ab:xz@', '<a href="http://www@',
'<a href="ftp://www@www/', '<a href="news:new@new/',
'<a href="http://@', '<a href="http://:bar@', '%5c',
'<a href=http://foo:bar@', '<a href=http://@', '%7f',
'<a href="http://foo.com%20%20%20%20%20%20%20@',
"<A\nhref=\"HTTP://", "<!-- script ", "<script ",
'< a href="', '" <script="', "<script>", "<script",
"script>", "/script>", "href=", ' href="', ' HREF="',
'<img src="', "<img ", "</img>", "</hr>", "<!-- ",
"<!- - ", "<!--", "<! ", "<!", "!>", "-->", " -->",
"<a href=\"\r\n", "foo.com", "bar.com", "test.cx",
"&#".int(rand(256)).";", "&#".int(rand(256)).";",
"&#".int(rand(256)).";", "&#".int(rand(256)).";",
"&#".int(rand(256)).";", "&#".int(rand(256)).";",
"&#".int(rand(65536)).";", "&#".int(rand(65536)).";",
"&#".int(rand(65536)).";", "&#".int(rand(65536)).";",
"&#".int(rand(65536)).";", "&#".int(rand(65536)).";",
"&#".int(rand(65536)).";", "&#".int(rand(65536)).";",
"&#".int(rand(65536)).";", "&#".int(rand(65536)).";",
"&#".int(rand(65536)).";", "&#".int(rand(65536)).";",
);
for (1..$werds) {
my $offset = int(rand(length($comment))+1);
my $done = 0;
while (!$done) {
my $fragment = $fragments[rand @fragments];
substr($comment, $offset, 0) = $fragment;
$done = 1 if rand(1) < 0.8;
}
}
}
$comment;
}
sub usage {
print "*** $_[0]\n" if $_[0];
# Remember to doublecheck these match getopts()!
print <<EOT;
Usage: $PROGNAME [OPTIONS] [#comments]
This utility creates test comments 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.
Main options:
-h Help (this message)
-v Version
-u Virtual user (default is "slash")
-I Story ID to post into.
Identity options (specify one):
-U User to post as.
-R Post as random users.
Posting options:
-t Weight towards nesting deeply.
-T Do NOT post with threads.
-J Post junk links (random text to stress canonicalization)
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__