load XML::LibXML only when required; handle global relationships more correctly;...
[scpubgit/stemmatology.git] / script / join_readings.pl
CommitLineData
869a1ada 1#!/usr/bin/env perl
2
3use lib 'lib';
4use feature 'say';
5use strict;
6use warnings;
7use Getopt::Long;
869a1ada 8use Text::Tradition::Directory;
869a1ada 9use TryCatch;
10
11binmode STDOUT, ':utf8';
12binmode STDERR, ':utf8';
13eval { no warnings; binmode $DB::OUT, ':utf8'; $DB::deep = 1000 };
14
15my( $dbuser, $dbpass );
16my $dsn = 'dbi:SQLite:dbname=stemmaweb/db/traditions.db';
17my $testrun;
18
19GetOptions(
20 'dsn=s' => \$dsn,
21 'u|user=s' => \$dbuser,
22 'p|pass=s' => \$dbpass,
23 'n|test' => \$testrun,
24 );
25
26my $dbopts = { dsn => $dsn };
27$dbopts->{extra_args}->{user} = $dbuser if $dbuser;
28$dbopts->{extra_args}->{password} = $dbpass if $dbpass;
29
30my $dir = Text::Tradition::Directory->new( $dbopts );
31
32my $scope = $dir->new_scope();
33my $lookfor = $ARGV[0] || '';
34foreach my $tinfo ( $dir->traditionlist() ) {
428bcf0b 35 next if $tinfo->{'name'} eq 'xxxxx';
869a1ada 36 next unless $tinfo->{'name'} =~ /$lookfor/ || $tinfo->{'id'} eq $lookfor;
37 my $tradition = $dir->lookup( $tinfo->{'id'} );
428bcf0b 38 say "Looking at tradition " . $tradition->name;
869a1ada 39 my $c = $tradition->collation;
40
41 # Anywhere in the graph that there is a reading that joins only to a single
42 # successor, and neither of these have any relationships, just join the two
43 # readings.
6771a1b1 44
45 # Save/update the current path texts
869a1ada 46 foreach my $wit ( $tradition->witnesses ) {
6771a1b1 47 my @pathtext = split( /\s+/, $c->path_text( $wit->sigil ) );
48 $wit->text( \@pathtext );
869a1ada 49 if( $wit->is_layered ) {
6771a1b1 50 my @layertext = split( /\s+/, $c->path_text( $wit->sigil.$c->ac_label ) );
51 $wit->layertext( \@layertext );
869a1ada 52 }
53 }
6771a1b1 54
55 # Do the deed
56 $c->compress_readings();
57 # ...and save it.
869a1ada 58 $dir->save( $tradition );
59}