test db file
[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
ad1291ee 14my( $tfile, $sfile, $delete, $list, $dsn ) =
15 ( undef, undef, undef, 0, 'dbi:SQLite:dbname=db/traditions.db' );
16
17GetOptions(
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 27my $kdb = Text::Tradition::Directory->new(
ad1291ee 28 'dsn' => $dsn,
12523041 29 'extra_args' => { 'create' => 1 },
8d9a1cd8 30 );
31
ad1291ee 32unless( $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 37if( $tfile && $delete ) {
38 print STDERR "Specify deletion by UUID, not by tradition file\n";
39 exit;
40}
41
42my( $tradition, $stemma );
43if( $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 61if( $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.
71if( $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}