File Coverage

blib/lib/Biblio/Zotero/DB.pm
Criterion Covered Total %
statement 38 65 58.4
branch 2 12 16.6
condition n/a
subroutine 14 22 63.6
pod 1 2 50.0
total 55 101 54.4


line stmt bran cond sub pod time code
1             package Biblio::Zotero::DB;
2             # ABSTRACT: helper module to access the Zotero SQLite database
3             $Biblio::Zotero::DB::VERSION = '0.003';
4 12     12   59359 use strict;
  12         53  
  12         485  
5 12     12   66 use warnings;
  12         27  
  12         392  
6              
7 12     12   180 use v5.14;
  12         44  
  12         461  
8 12     12   12332 use Moo;
  12         277871  
  12         108  
9 12     12   37614 use File::HomeDir;
  12         112949  
  12         1095  
10 12     12   1839 use Path::Class;
  12         172082  
  12         2220  
11 12     12   14249 use Path::Iterator::Rule;
  12         214173  
  12         530  
12 12     12   19557 use List::AllUtils qw(first);
  12         41154  
  12         1124  
13              
14 12     12   8595 use Biblio::Zotero::DB::Schema;
  12         52  
  12         490  
15 12     12   8466 use Biblio::Zotero::DB::Library;
  12         58  
  12         12287  
16              
17              
18             # used for L and L attr
19             my $make_directory_absolute = sub {
20             my $orig = shift;
21             my $self = $_[0];
22             my $dir = $_[1];
23              
24             return $orig->($self, dir($dir)->absolute) if($dir);
25              
26             return $orig->(@_);
27             };
28              
29             has schema => ( is => 'rw', builder => 1, lazy => 1, clearer => 1 );
30              
31             sub _build_schema {
32 11     11   27982 my ($self) = @_;
33 11         61 Biblio::Zotero::DB::Schema->connect('dbi:SQLite:dbname='.$self->db_file,
34             '', '',
35             {
36             (zotero_storage_directory => $self->storage_directory)x!! $self->storage_directory
37             },
38             );
39             }
40              
41             has db_file => ( is => 'rw', builder => 1, lazy => 1 );
42              
43             sub _build_db_file {
44 10     10   4426 my ($self) = @_;
45 10         307 dir($self->profile_directory)->file('zotero.sqlite');
46             }
47              
48             has storage_directory => ( is => 'rw', builder => 1, lazy => 1 );
49              
50             sub _build_storage_directory {
51 11     11   109 my ($self) = @_;
52 11 100       304 dir($self->profile_directory)->subdir('storage')->absolute if $self->profile_directory;
53             }
54              
55             around storage_directory => $make_directory_absolute;
56              
57             has profile_directory => ( is => 'rw' );
58              
59             around profile_directory => $make_directory_absolute;
60              
61             has profile_name => ( is => 'rw', trigger => 1, builder => 1, lazy => 1 );
62              
63             sub _trigger_profile_name {
64 0     0   0 my ($self) = @_;
65             $self->profile_directory(
66 0     0   0 first { dir($_)->components(-2) eq $self->profile_name }
67 0         0 @{$self->find_profile_directories});
  0         0  
68             }
69              
70             sub _build_profile_name {
71 0     0   0 my ($self) = @_;
72 0 0       0 dir($self->profile_directory)->components(-2) if $self->profile_directory;
73             }
74              
75              
76             # From
77             # Zotero for Firefox
78             #
79             # OS X /Users//Library/Application Support/Firefox/Profiles//zotero
80             # Windows 7/Vista C:\Users\\AppData\Roaming\Mozilla\Firefox\Profiles\\zotero
81             # Windows XP/2000 C:\Documents and Settings\\Application Data\Mozilla\Firefox\Profiles\\zotero
82             # Linux (most distributions) ~/.mozilla/firefox/Profiles//zotero
83             #
84             ####
85             #
86             # Zotero Standalone
87             #
88             # OS X /Users//Library/Application Support/Zotero/Profiles//zotero
89             # Windows 7/Vista C:\Users\\AppData\Roaming\Zotero\Profiles\\zotero
90             # Windows XP/2000 C:\Documents and Settings\\Application Data\Zotero\Profiles\\zotero
91             # Linux (most distributions) ~/.zotero/Profiles//zotero
92              
93              
94             sub find_profile_directories {
95 0     0 1 0 my ($self) = @_;
96 0         0 for($^O) {
97 0 0       0 return $self->_find_profile_directories_linux if($_ eq 'linux');
98 0 0       0 return $self->_find_profile_directories_osx if($_ eq 'darwin');
99 0 0       0 return $self->_find_profile_directories_win if($_ eq 'MSWin32');
100 0         0 return [];
101             }
102             }
103              
104             sub _find_profile_directories_osx {
105 0     0   0 my ($self) = @_;
106 0         0 my $my_data = dir(File::HomeDir->my_data)->absolute;
107             # gives /Users//Library/Application Support
108 0         0 my $find = [
109             dir($my_data, 'Firefox'),
110             dir($my_data, 'Zotero') ];
111 0         0 return $self->_find_profile_directories_under($find);
112             }
113              
114             sub _find_profile_directories_win {
115 0     0   0 my ($self) = @_;
116 0         0 my $my_data = dir(File::HomeDir->my_data)->absolute;
117             # gives C:\Users\\AppData\Local
118             # or C:\Documents and Settings\\Local Settings\Application Data
119             # depending on OS version
120 0 0       0 if( $my_data->components(-1) eq "Local" ) {
121             # for Windows 7 / Vista
122             # this returns the \Local directory
123             # we want the \Roaming directory that is on the same level
124 0         0 $my_data = $my_data->parent->subdir('Roaming');
125             }
126 0         0 my $find = [
127             dir( $my_data, 'Mozilla','Firefox'),
128             dir( $my_data, 'Zotero') ];
129 0         0 return $self->_find_profile_directories_under($find);
130             }
131              
132             sub _find_profile_directories_linux {
133 0     0   0 my ($self) = @_;
134 0         0 my $find = [
135             dir(File::HomeDir->my_home , '.mozilla/firefox'),
136             dir(File::HomeDir->my_home , '.zotero/zotero') ];
137 0         0 return $self->_find_profile_directories_under($find);
138             }
139              
140             sub _find_profile_directories_under {
141 0     0   0 my ($self, $dirs) = @_;
142             # finds either
143             # dir('Profiles',,'zotero')
144             # (actually, currently it doesn't check that it is indeed in the Profiles
145             # directory)
146             # or
147             # dir(, 'zotero')
148 0         0 return [ Path::Iterator::Rule->new
149             ->min_depth(1)->max_depth(3)
150             ->dir->name('zotero')->all( @$dirs ) ];
151             }
152              
153             sub library {
154 1     1 0 2295 my $self = shift;
155 1         8 return Biblio::Zotero::DB::Library->new( _db => $self );
156             }
157              
158             1;
159              
160             __END__