#!/usr/bin/env perl ################################################################################ # WeBWorK mod_perl (c) 1995-2002 WeBWorK Team, Univeristy of Rochester # $Id: wwdb,v 1.2 2003-04-17 21:01:13 sh002i Exp $ ################################################################################ =head1 NAME wwdb - command-line interface to the WeBWorK databases (WWDBv2). =cut use strict; use warnings; use FindBin; use lib "$FindBin::Bin/../lib"; use WeBWorK::ContentGenerator; # for cook_args use WeBWorK::CourseEnvironment; use WeBWorK::DB; # Command line syntax # # wwdb course command [ argument ... ] # # Commands correspond to the functions of WeBWorK::DB. Arguments are either # strings or records. To represent records, use the following syntax: # # {id="sh002i" first_name="Sam" last_name="Hathaway"} # # You'll have to protect arguments like this from your shell by enclosing them # in single quotes. # # The special string \undef represents an undefined value. You'll have to # protect this from your shell as well. Use \\undef or '\undef'. sub main(@) { my ($course, $command, @arguments) = @_; unless ($course and $command) { die "usage: $0 course command [arguments ...]\n"; } my $ce = WeBWorK::CourseEnvironment->new($ENV{WEBWORK_ROOT}, "", $course); my $db = WeBWorK::DB->new($ce); if ($command eq "dumpDB") { print $db->$command("set_user"); exit; } die "$command: unsupported command.\n" unless $db->can($command); my ($verb, $noun) = $command =~ m/^(list|add|get|put|delete)(.*?)s?$/; my $type = "WeBWorK::DB::Record::"; if ($noun =~ m/^Global(User)?(Set|Problem)$/) { $type .= "$1$2"; } else { $type .= $noun; } foreach (@arguments) { if (m/^\\undef$/) { $_ = undef; } elsif (m/^{(.*)}$/) { $_ = string2record($type, $1); } } my @result = $db->$command(@arguments); if ($verb eq "list") { print join("\n", @result), "\n"; } elsif ($verb eq "add") { print "result: $result[0]\n"; } elsif ($verb eq "get") { if (defined $result[0]) { print "{", record2string($result[0]), "}\n"; } else { print join("/", @arguments), ": record not found\n"; } } elsif ($verb eq "put") { print "result: $result[0]\n"; } elsif ($verb eq "delete") { print "result: $result[0]\n"; } } sub string2record($$) { my ($type, $string) = @_; my %hash = WeBWorK::ContentGenerator::cook_args($string); my $record = $type->new(%hash); return $record; } sub record2string($) { my ($record) = @_; my $string; foreach my $key ($record->FIELDS()) { my $value = $record->$key(); $value = "" unless defined $value; $value =~ s/"/\\"/g; $string .= "$key=\"$value\" "; } chop $string; return $string; } main(@ARGV);