Commit 2df4aeb3 authored by Martyn Smith's avatar Martyn Smith Committed by Martyn Smith
Browse files

add random groups now :)

parent 6e72807f
......@@ -2,16 +2,20 @@
use strict;
use warnings;
use Carp;
use FindBin;
use Data::Dumper;
use DBI;
use Data::RandomPerson;
use Data::Random::WordList;
use Getopt::Declare;
use Perl6::Slurp;
#use Smart::Comments;
my $args = Getopt::Declare->new(q(
[strict]
-t <type> Select type of data (currently only 'user') [required]
-t <type> Select type of data (user or group) [required]
-u <user> User to create data for (required for type 'group')
-c <configfile> What config.php to use (defaults to ../htdocs/config.php)
-n <count> How many to generate (default 1)
{ reject $count !~ /^\d+$/; }
......@@ -35,11 +39,11 @@ $config->{dbprefix} = '' unless defined $config->{dbprefix};
if ( $args->{-t} eq 'user' ) {
# get a list of existing usernames
my $existing_users = $dbh->selectall_hashref('SELECT ' . $config->{dbprefix} . 'username FROM usr', 'username');
my $existing_users = $dbh->selectall_hashref('SELECT id, username FROM ' . $config->{dbprefix} . 'usr', 'username');
$dbh->begin_work();
foreach ( 1 .. $args->{-n} ) {
foreach ( 1 .. $args->{-n} ) { ### [... ] (%)
my $userinfo = Data::RandomPerson->new()->create();
$userinfo->{username} = lc $userinfo->{firstname};
$userinfo->{email} = lc $userinfo->{firstname} . '.' . lc $userinfo->{lastname} . '@dollyfish.net.nz';
......@@ -67,6 +71,63 @@ if ( $args->{-t} eq 'user' ) {
$dbh->commit();
}
if ( $args->{-t} eq 'group' ) {
unless ( defined $args->{-u} ) {
croak 'Need to specify a user with -u';
}
my $existing_users = $dbh->selectall_hashref('SELECT id, username FROM ' . $config->{dbprefix} . 'usr', 'username');
unless ( exists $existing_users->{$args->{-u}} ) {
croak qq{User '$args->{-u}' doesn't exist\n};
}
my $user_id = $existing_users->{$args->{-u}}{id};
print qq{Adding groups for '$args->{-u}' ($user_id)\n};
my $wl = new Data::Random::WordList( wordlist => '/usr/share/dict/words' );
$dbh->begin_work();
foreach ( 1 .. $args->{-n} ) { ### [... ] (%)
my $groupname = join(' ', $wl->get_words(int(rand(5)) + 1));
my $groupdescription = join(' ', $wl->get_words(int(rand(50)) + 10));
if ( $args->{-p} or $args->{-v} ) {
print "Creating group '$groupname'\n";
print "INSERT INTO usr_group (name,owner,description,ctime,mtime) VALUES (?,?,?,?,?)\n";
my $members = {};
foreach (1..(int(rand(20)) + 5)) {
$members->{((keys %$existing_users)[int(rand(keys %$existing_users))])} = 1;
}
print "Members ... " . join(', ', keys %$members) . "\n";;
}
unless ( $args->{-p} ) {
$dbh->do(
'INSERT INTO usr_group (name,owner,description,ctime,mtime) VALUES (?,?,?,current_timestamp,current_timestamp)',
undef,
$groupname,
$user_id,
$groupdescription,
);
my $members = {};
foreach (1..(int(rand(20)) + 5)) {
$members->{$existing_users->{((keys %$existing_users)[int(rand(keys %$existing_users))])}{id}} = 1;
}
foreach my $id (keys %$members) {
$dbh->do(
q{INSERT INTO usr_group_member (grp,member,ctime) VALUES (currval('usr_group_id_seq'),?,current_timestamp)},
undef,
$id,
);
}
}
}
$dbh->commit();
}
sub read_config {
my ($file) = @_;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment