Add Email::Template for post-registratin email sending
[scpubgit/stemmatology.git] / script / save_to_db.pl
CommitLineData
8d9a1cd8 1#!/usr/bin/env perl
2
3use lib 'lib';
4use strict;
5use warnings;
6use File::Basename;
ad1291ee 7use Getopt::Long;
8d9a1cd8 8use Text::Tradition;
12523041 9use Text::Tradition::Directory;
8d9a1cd8 10
12523041 11binmode( STDOUT, ':utf8' );
12binmode( STDERR, ':utf8' );
13
683f22b1 14my( $tfile, $format, $sfile, $delete, $list, $dsn ) =
15 ( undef, 'Self', undef, undef, 0, 'dbi:SQLite:dbname=db/traditions.db' );
ad1291ee 16
17GetOptions(
18 't|tradition=s' => \$tfile,
683f22b1 19 'f|format=s' => \$format,
ad1291ee 20 's|stemma=s' => \$sfile,
21 'l|list' => \$list,
22 'd|delete=s' => \$delete,
23 'dsn=s' => \$dsn,
24 );
25
8d9a1cd8 26# Make a KiokuDB store from the traditions data we have.
27
12523041 28my $kdb = Text::Tradition::Directory->new(
ad1291ee 29 'dsn' => $dsn,
12523041 30 'extra_args' => { 'create' => 1 },
8d9a1cd8 31 );
32
ad1291ee 33unless( $tfile || $delete || $list ) {
34 print STDERR "Please specify a tradition file, an ID to delete, or the --list option\n";
35 exit;
36}
8d9a1cd8 37
ad1291ee 38if( $tfile && $delete ) {
39 print STDERR "Specify deletion by UUID, not by tradition file\n";
40 exit;
41}
42
43my( $tradition, $stemma );
44if( $tfile ) {
45 print STDERR "Reading tradition from $tfile\n";
46 $tradition = Text::Tradition->new(
683f22b1 47 'input' => $format,
ad1291ee 48 'file' => $tfile,
49 'linear' => 1,
50 );
51 if( $tradition && $sfile ) {
173ecc07 52 $stemma = $tradition->add_stemma( dotfile => $sfile );
ad1291ee 53 warn "Did not get stemma from $sfile\n" unless $stemma;
8d9a1cd8 54 }
ad1291ee 55
56 my $scope = $kdb->new_scope();
57 my $tid = $kdb->save( $tradition );
58 print STDERR "Stored tradition for " . $tradition->name . " at $tid\n";
59 print STDERR "...and associated stemma from $sfile\n" if $stemma;
8d9a1cd8 60}
61
ad1291ee 62if( $delete ) {
114bba57 63 my $scope = $kdb->new_scope();
ad1291ee 64 if( $kdb->exists( $delete ) ) {
65 $kdb->delete( $delete );
66 } else {
67 print STDERR "Object $delete does not appear to be a Text::Tradition in the DB\n";
8d9a1cd8 68 }
114bba57 69}
ad1291ee 70
71# Now try reading the objects from the DB.
72if( $list ) {
d75416c0 73 foreach my $tref ( $kdb->traditionlist ) {
74 my $tid = $tref->{'id'};
ad1291ee 75 my $scope = $kdb->new_scope();
76 my $t = $kdb->tradition( $tid );
77 print STDERR "$tid: Tradition '" . $t->name . "'\n";
78 my @wits = map { $_->sigil } $t->witnesses;
79 print STDERR "...with witnesses @wits\n";
80 my $c = $t->collation;
81 print STDERR "...collation has " . scalar( $c->readings ) . " readings\n";
82 print STDERR "...collation has " . scalar( $c->paths ) . " paths\n";
83 print STDERR "...collation has " . scalar( $c->relationships ) . " relationship links\n";
7f52eac8 84 foreach my $s ( $t->stemmata ) {
ad1291ee 85 print STDERR "...associated stemma has graph " . $s->graph . "\n";
86 }
87 }
88}