randomdata.pl 4.64 KB
Newer Older
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
#!/usr/bin/perl

use strict;
use warnings;

use FindBin;
use lib qq{$FindBin::Bin/lib/};

use Carp;
$Carp::Verbose = 1;
use Data::Dumper;
use Getopt::Declare;
use Mahara::Config;
use Mahara::RandomData;

my $types = [qw(user activity community artefact view watchlist template file folder image blog blogpost)];

my $args = Getopt::Declare->new(q(
    [strict]
    -t <type>        	Select type of data ) . '(' . join(', ', @{$types}) . ')' . q(  [required]
    -u <user>        	User to create data for (required for type 'activity')
    -ua             	Create data for all users (can be used instead of -u)
    -c <configfile>  	What config.php to use (defaults to ../htdocs/config.php)	
    -n <count>       	How many to generate (default 1)	
        { reject $count !~ /^\d+$/; }
    -p                 	Pretend (just print what _would_ happen)
    -v                 	Verbose (like pretend but print _and_ insert)
                        
));

exit unless defined $args;

$args->{-n} ||= 1;
$args->{-c} ||= qq{$FindBin::Bin/../htdocs/config.php};
my $config = Mahara::Config->new($args->{-c});
my $randomdata = Mahara::RandomData->new($config);
$randomdata->verbose($args->{-v});
$randomdata->pretend($args->{-p});

unless ( grep { $args->{-t} eq $_ } @{$types} ) {
    croak q{Type must be one of } . join(', ', map { qq{'$_'} } @{$types} );
}

if ( $args->{-t} eq 'user' ) {
    $randomdata->insert_random_users($args->{-n});
}

if ( $args->{-t} eq 'activity' ) {
    unless ( defined $args->{-u} or defined $args->{-ua} ) {
        croak 'Need to specify a user with -u or -ua';
    }
    if ( defined $args->{-u} ) {
        $randomdata->insert_random_activity($args->{-u}, $args->{-n});
    }
    else {
        $randomdata->insert_random_activity_all_users($args->{-n});
    }
}

if ( $args->{-t} eq 'community' ) {
    unless ( defined $args->{-u} or defined $args->{-ua} ) {
        croak 'Need to specify a user with -u or -ua';
    }
    if ( defined $args->{-u} ) {
        $randomdata->insert_random_communities($args->{-u}, $args->{-n});
    }
    else {
        $randomdata->insert_random_communities_all_users($args->{-n});
    }
}

if ( $args->{-t} eq 'artefact' ) {
    unless ( defined $args->{-u} or defined $args->{-ua} ) {
        croak 'Need to specify a user with -u or -ua';
    }
    if ( defined $args->{-u} ) {
        $randomdata->insert_random_artefacts($args->{-u}, $args->{-n});
    }
    else {
        $randomdata->insert_random_artefacts_all_users($args->{-n});
    }
}

if ( $args->{-t} eq 'view' ) {
    unless ( defined $args->{-u} or defined $args->{-ua} ) {
        croak 'Need to specify a user with -u or -ua';
    }
    if ( defined $args->{-u} ) {
        $randomdata->insert_random_views($args->{-u}, $args->{-n});
    }
    else {
        $randomdata->insert_random_views_all_users($args->{-n});
    }
}

if ( $args->{-t} eq 'watchlist' ) {
    unless ( defined $args->{-u} or defined $args->{-ua} ) {
        croak 'Need to specify a user with -u or -ua';
    }
    if ( defined $args->{-u} ) {
        $randomdata->insert_random_watchlist($args->{-u}, $args->{-n});
    }
    else {
        $randomdata->insert_random_watchlist_all_users($args->{-n});
    }
}

if ( $args->{-t} eq 'template' ) {
    unless ( defined $args->{-u} or defined $args->{-ua} ) {
        croak 'Need to specify a user with -u or -ua';
    }
    if ( defined $args->{-u} ) {
        $randomdata->insert_random_template($args->{-u}, $args->{-n});
    }
    else {
        $randomdata->insert_random_template_all_users($args->{-n});
    }
}

if ( $args->{-t} eq 'file' or $args->{-t} eq 'folder' or $args->{-t} eq 'image' ) {
    unless ( defined $args->{-u} or defined $args->{-ua} ) {
        croak 'Need to specify a user with -u or -ua';
    }
    if ( defined $args->{-u} ) {
        $randomdata->insert_random_filethings($args->{-u}, $args->{-n}, $args->{-t});
    }
    else {
        $randomdata->insert_random_filethings_all_users($args->{-n}, $args->{-t});
    }
}

if ( $args->{-t} eq 'blog' ) {
    unless ( defined $args->{-u} or defined $args->{-ua} ) {
        croak 'Need to specify a user with -u or -ua';
    }
    if ( defined $args->{-u} ) {
        $randomdata->insert_random_blogs($args->{-u}, $args->{-n}, $args->{-t});
    }
    else {
        $randomdata->insert_random_blogs_all_users($args->{-n}, $args->{-t});
    }
}

if ( $args->{-t} eq 'blogpost' ) {
    unless ( defined $args->{-u} or defined $args->{-ua} ) {
        croak 'Need to specify a user with -u or -ua';
    }
    if ( defined $args->{-u} ) {
        $randomdata->insert_random_blogposts($args->{-u}, $args->{-n}, $args->{-t});
    }
    else {
        $randomdata->insert_random_blogposts_all_users($args->{-n}, $args->{-t});
    }
}