| 1 | package Biblio::Zotero::DB; | ||||||||||
| 2 | # ABSTRACT: helper module to access the Zotero SQLite database | ||||||||||
| 3 | 12 | $Biblio::Zotero::DB::VERSION = '0.004'; | |||||||||
| 4 | 12 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 12 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 12 | ▶ | use v5.14; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 12 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 12 | ▶ | use File::HomeDir; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 12 | ▶ | use Path::Class; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 12 | ▶ | use Path::Iterator::Rule; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 12 | ▶ | use List::AllUtils qw(first); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 12 | ▶ | use Biblio::Zotero::DB::Schema; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 12 | ▶ | use Biblio::Zotero::DB::Library; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | |||||||||||
| 18 | # used for L</storage_directory> and L</profile_directory> attr | ||||||||||
| 19 | 54 | ▶ | my $make_directory_absolute = sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 20 | 54 | my $orig = shift; | |||||||||
| 21 | 54 | my $self = $_[0]; | |||||||||
| 22 | 54 | my $dir = $_[1]; | |||||||||
| 23 | |||||||||||
| 24 | 54 | ▶ | return $orig->($self, dir($dir)->absolute) if($dir); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | |||||||||||
| 26 | 54 | return $orig->(@_); | |||||||||
| 27 | 12 | }; | |||||||||
| 28 | |||||||||||
| 29 | 12 | has schema => ( is => 'rw', builder => 1, lazy => 1, clearer => 1 ); | |||||||||
| 30 | |||||||||||
| 31 | 11 | ▶ | sub _build_schema { | ||||||||
|
Subroutine
_build_schema: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 11 | my ($self) = @_; | |||||||||
| 33 | 11 | 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 | 12 | has db_file => ( is => 'rw', builder => 1, lazy => 1 ); | |||||||||
| 42 | |||||||||||
| 43 | 10 | ▶ | sub _build_db_file { | ||||||||
|
Subroutine
_build_db_file: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 10 | my ($self) = @_; | |||||||||
| 45 | 10 | dir($self->profile_directory)->file('zotero.sqlite'); | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 12 | has storage_directory => ( is => 'rw', builder => 1, lazy => 1 ); | |||||||||
| 49 | |||||||||||
| 50 | 11 | ▶ | sub _build_storage_directory { | ||||||||
|
Subroutine
_build_storage_directory: called CC=2 SCAR=6.9
| |||||||||||
| 51 | 11 | my ($self) = @_; | |||||||||
| 52 | 11 | ▶ | dir($self->profile_directory)->subdir('storage')->absolute if $self->profile_directory; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 12 | around storage_directory => $make_directory_absolute; | |||||||||
| 56 | |||||||||||
| 57 | 12 | has profile_directory => ( is => 'rw' ); | |||||||||
| 58 | |||||||||||
| 59 | 12 | around profile_directory => $make_directory_absolute; | |||||||||
| 60 | |||||||||||
| 61 | 12 | has profile_name => ( is => 'rw', trigger => 1, builder => 1, lazy => 1 ); | |||||||||
| 62 | |||||||||||
| 63 | 0 | ▶ | sub _trigger_profile_name { | ||||||||
|
Subroutine
_trigger_profile_name: not called CC=1 SCAR=6.9
| |||||||||||
| 64 | 0 | my ($self) = @_; | |||||||||
| 65 | $self->profile_directory( | ||||||||||
| 66 | 0 | ▶ | first { dir($_)->components(-2) eq $self->profile_name } | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 67 | 0 | @{$self->find_profile_directories}); | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 0 | ▶ | sub _build_profile_name { | ||||||||
|
Subroutine
_build_profile_name: not called CC=2 SCAR=17.9
| |||||||||||
| 71 | 0 | my ($self) = @_; | |||||||||
| 72 | 0 | ▶ | dir($self->profile_directory)->components(-2) if $self->profile_directory; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | |||||||||||
| 76 | # From <http://www.zotero.org/support/zotero_data> | ||||||||||
| 77 | # Zotero for Firefox | ||||||||||
| 78 | # | ||||||||||
| 79 | # OS X /Users/<username>/Library/Application Support/Firefox/Profiles/<randomstring>/zotero | ||||||||||
| 80 | # Windows 7/Vista C:\Users\<User Name>\AppData\Roaming\Mozilla\Firefox\Profiles\<randomstring>\zotero | ||||||||||
| 81 | # Windows XP/2000 C:\Documents and Settings\<username>\Application Data\Mozilla\Firefox\Profiles\<randomstring>\zotero | ||||||||||
| 82 | # Linux (most distributions) ~/.mozilla/firefox/Profiles/<randomstring>/zotero | ||||||||||
| 83 | # | ||||||||||
| 84 | #### | ||||||||||
| 85 | # | ||||||||||
| 86 | # Zotero Standalone | ||||||||||
| 87 | # | ||||||||||
| 88 | # OS X /Users/<username>/Library/Application Support/Zotero/Profiles/<randomstring>/zotero | ||||||||||
| 89 | # Windows 7/Vista C:\Users\<User Name>\AppData\Roaming\Zotero\Profiles\<randomstring>\zotero | ||||||||||
| 90 | # Windows XP/2000 C:\Documents and Settings\<username>\Application Data\Zotero\Profiles\<randomstring>\zotero | ||||||||||
| 91 | # Linux (most distributions) ~/.zotero/Profiles/<randomstring>/zotero | ||||||||||
| 92 | |||||||||||
| 93 | |||||||||||
| 94 | 0 | ▶ | sub find_profile_directories { | ||||||||
|
Subroutine
find_profile_directories: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 95 | 0 | my ($self) = @_; | |||||||||
| 96 | 0 | for($^O) { | |||||||||
| 97 | 0 | ▶ | return $self->_find_profile_directories_linux if($_ eq 'linux'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 0 | ▶ | return $self->_find_profile_directories_osx if($_ eq 'darwin'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 0 | ▶ | return $self->_find_profile_directories_win if($_ eq 'MSWin32'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 0 | return []; | |||||||||
| 101 | } | ||||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 0 | ▶ | sub _find_profile_directories_osx { | ||||||||
|
Subroutine
_find_profile_directories_osx: not called CC=1 SCAR=6.9
| |||||||||||
| 105 | 0 | my ($self) = @_; | |||||||||
| 106 | 0 | my $my_data = dir(File::HomeDir->my_data)->absolute; | |||||||||
| 107 | # gives /Users/<username>/Library/Application Support | ||||||||||
| 108 | 0 | my $find = [ | |||||||||
| 109 | dir($my_data, 'Firefox'), | ||||||||||
| 110 | dir($my_data, 'Zotero') ]; | ||||||||||
| 111 | 0 | return $self->_find_profile_directories_under($find); | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 0 | ▶ | sub _find_profile_directories_win { | ||||||||
|
Subroutine
_find_profile_directories_win: not called CC=2 SCAR=17.9
| |||||||||||
| 115 | 0 | my ($self) = @_; | |||||||||
| 116 | 0 | my $my_data = dir(File::HomeDir->my_data)->absolute; | |||||||||
| 117 | # gives C:\Users\<User Name>\AppData\Local | ||||||||||
| 118 | # or C:\Documents and Settings\<User Name>\Local Settings\Application Data | ||||||||||
| 119 | # depending on OS version | ||||||||||
| 120 | 0 | ▶ | if( $my_data->components(-1) eq "Local" ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | $my_data = $my_data->parent->subdir('Roaming'); | |||||||||
| 125 | } | ||||||||||
| 126 | 0 | my $find = [ | |||||||||
| 127 | dir( $my_data, 'Mozilla','Firefox'), | ||||||||||
| 128 | dir( $my_data, 'Zotero') ]; | ||||||||||
| 129 | 0 | return $self->_find_profile_directories_under($find); | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 0 | ▶ | sub _find_profile_directories_linux { | ||||||||
|
Subroutine
_find_profile_directories_linux: not called CC=1 SCAR=6.9
| |||||||||||
| 133 | 0 | my ($self) = @_; | |||||||||
| 134 | 0 | my $find = [ | |||||||||
| 135 | dir(File::HomeDir->my_home , '.mozilla/firefox'), | ||||||||||
| 136 | dir(File::HomeDir->my_home , '.zotero/zotero') ]; | ||||||||||
| 137 | 0 | return $self->_find_profile_directories_under($find); | |||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | 0 | ▶ | sub _find_profile_directories_under { | ||||||||
|
Subroutine
_find_profile_directories_under: not called CC=1 SCAR=6.9
| |||||||||||
| 141 | 0 | my ($self, $dirs) = @_; | |||||||||
| 142 | # finds either | ||||||||||
| 143 | # dir('Profiles',<randomstring>,'zotero') | ||||||||||
| 144 | # (actually, currently it doesn't check that it is indeed in the Profiles | ||||||||||
| 145 | # directory) | ||||||||||
| 146 | # or | ||||||||||
| 147 | # dir(<randomstring>, 'zotero') | ||||||||||
| 148 | 0 | return [ Path::Iterator::Rule->new | |||||||||
| 149 | ->min_depth(1)->max_depth(3) | ||||||||||
| 150 | ->dir->name('zotero')->all( @$dirs ) ]; | ||||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | 1 | ▶ | sub library { | ||||||||
|
Subroutine
library: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 154 | 1 | my $self = shift; | |||||||||
| 155 | 1 | return Biblio::Zotero::DB::Library->new( _db => $self ); | |||||||||
| 156 | } | ||||||||||
| 157 | |||||||||||
| 158 | 12 | 1; | |||||||||
| 159 | |||||||||||
| 160 | __END__ | ||||||||||