Commit | Line | Data |
8d9a1cd8 |
1 | #!/usr/bin/env perl |
2 | |
3 | use lib 'lib'; |
4 | use strict; |
5 | use warnings; |
6 | use File::Basename; |
ad1291ee |
7 | use Getopt::Long; |
8d9a1cd8 |
8 | use Text::Tradition; |
12523041 |
9 | use Text::Tradition::Directory; |
8d9a1cd8 |
10 | |
12523041 |
11 | binmode( STDOUT, ':utf8' ); |
12 | binmode( STDERR, ':utf8' ); |
13 | |
ad1291ee |
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 | |
8d9a1cd8 |
25 | # Make a KiokuDB store from the traditions data we have. |
26 | |
12523041 |
27 | my $kdb = Text::Tradition::Directory->new( |
ad1291ee |
28 | 'dsn' => $dsn, |
12523041 |
29 | 'extra_args' => { 'create' => 1 }, |
8d9a1cd8 |
30 | ); |
31 | |
ad1291ee |
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 | } |
8d9a1cd8 |
36 | |
ad1291ee |
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; |
8d9a1cd8 |
53 | } |
ad1291ee |
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; |
8d9a1cd8 |
59 | } |
60 | |
ad1291ee |
61 | if( $delete ) { |
114bba57 |
62 | my $scope = $kdb->new_scope(); |
ad1291ee |
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"; |
8d9a1cd8 |
67 | } |
114bba57 |
68 | } |
ad1291ee |
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 | } |