blib/lib/ORDB/Unihan.pm

filter: Statementstmt 30.1% 41 / 136 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 81.2% 13 / 16 Podpod 50.0% 1 / 2 totaltotal 22.3% 55 / 246 CC 36 SCAR 67.6
CC
36
Cov
19%
import
1260.0
dir
6.0
sqlite_path
2.0
CRAP 865.2
↑ Summary
1 package ORDB::Unihan;
2 {
3 2 $ORDB::Unihan::VERSION = '0.03';
4 }
5  
6 # ABSTRACT: An ORM for the published Unihan database
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::Spec 0.80 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Path 2.04 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use File::Remove 1.42 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use File::HomeDir 0.69 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use LWP::Online ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Params::Util 0.33 qw{ _STRING _NONNEGINT _HASH };
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use ORLite 1.22 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use vars qw{@ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 @ISA = 'ORLite';
23 }
24  
25 2 my $url = 'http://www.unicode.org/Public/UNIDATA/Unihan.zip';
26  
27 0 ▶sub dir {
Subroutine dir: not called CC=2 SCAR=17.9
Pod undocumented
28 0 ▶ File::Spec->catdir( File::HomeDir->my_data,
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
29 ( $^O eq 'MSWin32' ? 'Perl' : '.perl' ),
30 'ORDB-Unihan', );
31 }
32 0 ▶sub sqlite_path { File::Spec->catfile( dir(), 'Unihan.sqlite' ) }
Subroutine sqlite_path: not called CC=1 SCAR=6.9
Pod documented
33  
34 0 ▶sub import {
Subroutine import: not called CC=35 SCAR=71.4
35 0 my $self = shift;
36 0 ▶ my $class = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
37  
38 # Check for debug mode
39 0 my $DEBUG = 0;
40 0 ▶ if ( scalar @_ and defined _STRING( $_[-1] ) and $_[-1] eq '-DEBUG' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ and defined &_STRING($_[-1]) and $_[-1] eq "-DEBUG") 0 0 0
Condition0%0/6
scalar @_ and defined &_STRING($_[-1]) and $_[-1] eq "-DEBUG"
!ll&&!rl&&r
000
scalar @_ and defined &_STRING($_[-1])
!ll&&!rl&&r
000
MC/DC0%0/3
scalar @_ and defined &_STRING($_[-1]) and $_[-1] eq "-DEBUG"
scalar @_ defined &_STRING($_[-1]) $_[-1] eq "-DEBUG"
Truth table0%0/4
A and B and C
A= scalar @_B= defined &_STRING($_[-1])C= $_[-1] eq "-DEBUG"
ABCresult
0XX0
10X0
1100
1111
41 0 $DEBUG = 1;
42 0 pop @_;
43 }
44 0 my %params;
45 0 ▶ if ( _HASH( $_[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_HASH($_[0])) { } 0 0 0
46 0 %params = %{ $_[0] };
47 }
48 else {
49 0 %params = @_;
50 }
51  
52 # where we save .sqlite to?
53 # Determine the database directory
54 0 my $dir = dir();
55  
56 # Create it if needed
57 0 ▶ unless ( -e $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $dir) 0 0 0
58 0 File::Path::mkpath( $dir, { verbose => 0 } );
59 }
60  
61 # Determine the mirror database file
62 0 my $db = sqlite_path();
63 0 my $zip_path = File::Spec->catfile( $dir, 'Unihan.zip' );
64  
65 # Create the default useragent
66 0 my $show_progress = $DEBUG;
67 0 my $useragent = delete $params{useragent};
68 0 ▶ unless ($useragent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($useragent) 0 0 0
69 0 $useragent = LWP::UserAgent->new(
70 timeout => 30,
71 show_progress => $show_progress,
72 );
73 }
74  
75 # Do we need refecth?
76 0 my $need_refetch = 1;
77 {
78 0 my $last_mod_file = File::Spec->catfile( $dir, 'last_mod.txt' );
79 0 my $last_mod_local = 'N/A';
80 0 ▶ if ( open( my $fh, '<', $last_mod_file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", $last_mod_file) 0 0 0
81 0 flock( $fh, 1 );
82 0 $last_mod_local = <$fh>;
83 0 chomp($last_mod_local);
84 0 ▶ $last_mod_local ||= 0;
Condition0%0/2
$last_mod_local ||= 0
l!l
00
MC/DC0%0/1
$last_mod_local ||= 0
$last_mod_local
Truth table0%0/2
A
A= $last_mod_local
Aresult
11
00
85 0 close($fh);
86 }
87  
88 0 my $res = $useragent->head($url);
89 0 my $last_mod = $res->header('last-modified');
90 0 ▶ if ( $last_mod_local eq $last_mod ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_mod_local eq $last_mod) { } 0 0 0
91 0 $need_refetch = 0;
92 }
93 else {
94 0 ▶ print STDERR
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
95 "Unihan.zip last-modified $last_mod, we have $last_mod_local\n"
96 if $DEBUG;
97 0 open( my $fh, '>', $last_mod_file );
98 0 flock( $fh, 2 );
99 0 print $fh $last_mod;
100 0 close($fh);
101 }
102 }
103  
104 0 my $online = LWP::Online::online();
105 0 ▶ unless ( $online or -f $db ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($online or -f $db) 0 0 0
Condition0%0/3
$online or -f $db
l!l&&r!l&&!r
000
MC/DC0%0/2
$online or -f $db
$online -f $db
Truth table0%0/3
A or B
A= $onlineB= -f $db
ABresult
1X1
011
000
106  
107 # Don't have the file and can't get it
108 0 Carp::croak("Cannot fetch database without an internet connection");
109 }
110  
111 # refetch the .zip
112 0 my $regenerated_sqlite = 0;
113 0 ▶ if ( $need_refetch or !-e $zip_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_refetch or not -e $zip_path) 0 0 0
Condition0%0/3
$need_refetch or not -e $zip_path
l!l&&r!l&&!r
000
MC/DC0%0/2
$need_refetch or not -e $zip_path
$need_refetch not -e $zip_path
Truth table0%0/3
A or not(B)
A= $need_refetchB= not -e $zip_path
ABresult
1X1
011
000
114 0 ▶ print STDERR "Mirror $url to $zip_path\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
115  
116 # Fetch the archive
117 0 my $response = $useragent->mirror( $url => $zip_path );
118 0 ▶ unless ( $response->is_success or $response->code == 304 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success or $response->code == 304) 0 0 0
Condition0%0/3
$response->is_success or $response->code == 304
l!l&&r!l&&!r
000
MC/DC0%0/2
$response->is_success or $response->code == 304
$response->is_success $response->code == 304
Truth table0%0/3
A or B
A= $response->is_successB= $response->code == 304
ABresult
1X1
011
000
119 0 Carp::croak("Error: Failed to fetch $url");
120 }
121 0 $regenerated_sqlite = 1;
122 }
123  
124 # Extract .txt file
125 0 my $old_txt_file = File::Spec->catfile( $dir, 'Unihan.txt' );
126 0 ▶ unlink($old_txt_file) if -e $old_txt_file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $old_txt_file 0 0 0
127 0 my $txt_path = File::Spec->catfile( $dir, 'Unihan_Readings.txt' );
128 0 ▶ if ( $regenerated_sqlite or !-e $txt_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regenerated_sqlite or not -e $txt_path) 0 0 0
Condition0%0/3
$regenerated_sqlite or not -e $txt_path
l!l&&r!l&&!r
000
MC/DC0%0/2
$regenerated_sqlite or not -e $txt_path
$regenerated_sqlite not -e $txt_path
Truth table0%0/3
A or not(B)
A= $regenerated_sqliteB= not -e $txt_path
ABresult
1X1
011
000
129 0 ▶ print STDERR "Extract $zip_path to $dir\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
130 0 require Archive::Extract;
131 0 my $ae = Archive::Extract->new( archive => $zip_path );
132 0 my $ok = $ae->extract( to => $dir );
133 0 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
134 0 Carp::croak("Error: Failed to read .zip");
135 }
136 0 ▶ unless ( -e $txt_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $txt_path) 0 0 0
137 0 Carp::croak("Error: Failed to extract .zip");
138 }
139 }
140  
141 # regenerate the .sqlite
142 0 ▶ if ( $regenerated_sqlite or !-e $db ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regenerated_sqlite or not -e $db) 0 0 0
Condition0%0/3
$regenerated_sqlite or not -e $db
l!l&&r!l&&!r
000
MC/DC0%0/2
$regenerated_sqlite or not -e $db
$regenerated_sqlite not -e $db
Truth table0%0/3
A or not(B)
A= $regenerated_sqliteB= not -e $db
ABresult
1X1
011
000
143 0 unlink($db);
144 0 my $dbh = DBI->connect(
145 "DBI:SQLite:$db",
146 undef, undef,
147 {
148 RaiseError => 1,
149 PrintError => 1,
150 }
151 );
152 0 $dbh->do('PRAGMA synchronous=OFF');
153 0 $dbh->do('PRAGMA count_changes=OFF');
154 0 $dbh->do('PRAGMA journal_mode=MEMORY');
155 0 $dbh->do('PRAGMA temp_store=MEMORY');
156 0 $dbh->do(<<'SQL');
157 CREATE TABLE unihan (
158 "hex" CHAR(5) NOT NULL,
159 "type" VARCHAR(18) NOT NULL,
160 "val" VARCHAR(255),
161 PRIMARY KEY ("hex", "type")
162 )
163 SQL
164 0 my $sql =
165 'INSERT INTO "unihan" ("hex", "type", "val") VALUES (?, ?, ?)';
166 0 my $sth = $dbh->prepare($sql);
167  
168 0 opendir( my $fdir, $dir );
169 0 my @files = grep { /.txt$/ } readdir($fdir);
170 0 closedir($fdir);
171 0 foreach my $file (@files) {
172 0 ▶ next if $file eq 'last_mod.txt';
Branch: 1 branch
BranchTrueFalseTotal
if $file eq "last_mod.txt" 0 0 0
173 0 ▶ print STDERR "Populate $dir/$file\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
174 0 open( my $fh, '<:utf8', "$dir/$file" );
175 0 flock( $fh, 1 );
176 0 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
177 0 ▶ next if ( $line =~ /^\#/ ); # comment line
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\#/ 0 0 0
178 0 ▶ next if ( $line =~ /^\s+$/ ); # blank line
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s+$/ 0 0 0
179 0 chomp($line);
180 0 my ( $hex, $type, $val ) = split( /\t/, $line, 3 );
181 0 $hex =~ s/^U\+//;
182 0 $type =~ s/^k//;
183 0 $val =~ s/(^\s|\s+)//g;
184 0 ▶ $sth->execute( $hex, $type, $val )
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($hex, $type, $val) or die "${dbh}:errstr $type, $hex, $val" 0 0 0
185 or die "$dbh:errstr $type, $hex, $val";
186 }
187 0 close($fh);
188 }
189 }
190  
191 0 $params{file} = $db;
192 0 $params{readonly} = 1;
193  
194 # Hand off to the main ORLite class.
195 0 ▶ $class->SUPER::import( \%params, $DEBUG ? '-DEBUG' : () );
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG ? : 0 0 0
196  
197 }
198  
199 2 1;
200  
201 __END__
↑ Summary