[system] / trunk / webwork-modperl / bin / wwdb Repository:
ViewVC logotype

View of /trunk/webwork-modperl/bin/wwdb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5319 - (download) (annotate)
Mon Aug 13 22:59:59 2007 UTC (5 years, 9 months ago) by sh002i
File size: 3094 byte(s)
updated copyright dates

    1 #!/usr/bin/env perl
    2 ################################################################################
    3 # WeBWorK Online Homework Delivery System
    4 # Copyright © 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    5 # $CVSHeader: webwork2/bin/wwdb,v 1.13 2006/01/25 23:13:45 sh002i Exp $
    6 # 
    7 # This program is free software; you can redistribute it and/or modify it under
    8 # the terms of either: (a) the GNU General Public License as published by the
    9 # Free Software Foundation; either version 2, or (at your option) any later
   10 # version, or (b) the "Artistic License" which comes with this package.
   11 # 
   12 # This program is distributed in the hope that it will be useful, but WITHOUT
   13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   14 # FOR A PARTICULAR PURPOSE.  See either the GNU General Public License or the
   15 # Artistic License for more details.
   16 ################################################################################
   17 
   18 =head1 NAME
   19 
   20 wwdb - export and import webwork databases.
   21 
   22 =head1 SYNOPSIS
   23 
   24  wwdb [-f] course { import | export } file [table ...]
   25 
   26 =head1 DESCRIPTION
   27 
   28 Exports data from a course database to an XML file, or imports data from an XML
   29 file to a course database. Optionally restrict which tables are imported or
   30 exported and specify a duplicate policy.
   31 
   32 =head1 OPTIONS
   33 
   34 =over
   35 
   36 =item -f
   37 
   38 Overwite duplicate records.
   39 
   40 =item course
   41 
   42 Course to use for import or export.
   43 
   44 =item { import | export }
   45 
   46 Specify action -- export or import data.
   47 
   48 =item file
   49 
   50 XML file to write to (in the case of export) or read from (in the case of
   51 import).
   52 
   53 =item [table ...]
   54 
   55 If specified, only the listed tables will be imported or exported.
   56 
   57 =back
   58 
   59 =cut
   60 
   61 use strict;
   62 use warnings;
   63 use Getopt::Std;
   64 
   65 BEGIN {
   66 	die "WEBWORK_ROOT not found in environment.\n"
   67 		unless exists $ENV{WEBWORK_ROOT};
   68 }
   69 
   70 use lib "$ENV{WEBWORK_ROOT}/lib";
   71 use WeBWorK::CourseEnvironment;
   72 use WeBWorK::DB;
   73 use WeBWorK::Utils::DBImportExport qw/listTables dbExport dbImport/;
   74 
   75 sub usage {
   76 	print STDERR "usage: $0 [-f] course { import | export } file [table ...]\n";
   77 	print STDERR "tables: ", join(" ", listTables()), "\n";
   78 	exit 1;
   79 }
   80 
   81 our $opt_f;
   82 getopts("f");
   83 
   84 my ($course, $command, $file, @tables) = @ARGV;
   85 
   86 usage() unless $course and $command and $file;
   87 
   88 my $ce = WeBWorK::CourseEnvironment->new({
   89 	webwork_dir => $ENV{WEBWORK_ROOT},
   90 	courseName => $course,
   91 });
   92 
   93 my $db = WeBWorK::DB->new($ce->{dbLayout});
   94 
   95 my @errors;
   96 
   97 if ($command eq "export") {
   98 	my $fh;
   99 	if ($file eq "-") {
  100 		$fh = *STDOUT;
  101 	} else {
  102 		open $fh, ">", $file or die "failed to open file '$file' for writing: $!\n";
  103 	}
  104 	@errors = dbExport(
  105 		db => $db,
  106 		xml => $fh,
  107 		tables => \@tables,
  108 	);
  109 	close $fh;
  110 } elsif ($command eq "import") {
  111 	my $conflict = ($opt_f ? "replace" : "skip");
  112 	open my $fh, "<", $file or die "failed to open file '$file' for writing: $!\n";
  113 	@errors = dbImport(
  114 		db => $db,
  115 		xml => $fh,
  116 		tables => \@tables,
  117 		conflict => $conflict,
  118 	);
  119 	close $fh;
  120 } else {
  121 	die "$command: unrecognized command.\n";
  122 }
  123 
  124 if (@errors) {
  125 	warn "The following errors occured:\n", map { "* $_\n" } @errors;
  126 	exit 1;
  127 }

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9