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