]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/authority.pm
adding initial "full tag" authority validation
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher / authority.pm
1 package OpenILS::Application::Storage::Publisher::authority;
2 use base qw/OpenILS::Application::Storage::Publisher/;
3 use vars qw/$VERSION/;
4 use OpenSRF::EX qw/:try/;
5 use OpenILS::Application::Storage::FTS;
6 use OpenILS::Utils::Fieldmapper;
7 use OpenSRF::Utils::Logger qw/:level/;
8 use OpenSRF::Utils::Cache;
9 use Data::Dumper;
10 use Digest::MD5 qw/md5_hex/;
11 use XML::LibXML;
12 use Time::HiRes qw/time sleep/;
13 use Unicode::Normalize;
14
15 my $log = 'OpenSRF::Utils::Logger';
16
17 $VERSION = 1;
18
19 my $parser = XML::LibXML->new;
20
21 sub validate_tag {
22         my $self = shift;
23         my $client = shift;
24         my %args = @_;
25         
26         my @tags = @{$args{tags}};
27         my @searches = @{$args{searches}};
28
29         my $search_table = authority::full_rec->table;
30
31         my @values;
32         my @selects;
33         for my $t ( @tags ) {
34                 for my $search ( @searches ) {
35                         my $sf = $$search{subfield};
36                         my $term = NFD(lc($$search{term}));
37
38                         $term =~ s/\pM+//sgo;
39                         $term =~ s/\pC+//sgo;
40                         $term =~ s/\W+$//o;
41
42                         $tag = [$tag] if (!ref($tag));
43
44                         push @values, $t, $sf, $term;
45
46                         push @selects,
47                                 "SELECT record FROM $search_table ".
48                                 "WHERE tag = ? AND subfield = ? AND value = ?";
49                 }
50
51                 my $sql = 'SELECT COUNT(DISTINCT record) FROM (';
52                 $sql .= 'SELECT record FROM (('.join(') INTERSECT (', @selects).')) AS x ';
53                 $sql .= "JOIN $search_table recheck USING (record) WHERE recheck.tag = ? ";
54                 $sql .= "GROUP BY 1 HAVING (COUNT(recheck.id) - ?) = 0) AS foo;";
55
56                 my $count = authority::full_rec->db_Main->selectcol_arrayref( $sql, {}, @values, $t, scalar(@searches) )->[0];
57                 return $count if ($count > 0);
58         }
59
60         return 0;
61 }
62 __PACKAGE__->register_method(
63         api_name        => "open-ils.storage.authority.validate.tag",
64         method          => 'validate_tag',
65         api_level       => 1,
66 );
67
68 sub find_authority_marc {
69         my $self = shift;
70         my $client = shift;
71         my %args = @_;
72         
73         my $term = NFD(lc($args{term}));
74         my $tag = $args{tag};
75         my $subfield = $args{subfield};
76         my $limit = $args{limit} || 100;
77         my $offset = $args{offset} || 0;
78
79         if ($limit) {
80                 $limit = "LIMIT $limit";
81         } else {
82                 $limit = '';
83         }
84
85         if ($offset) {
86                 $offset = "OFFSET $offset";
87         } else {
88                 $offset = '';
89         }
90
91         my $tag_where = "AND f.tag LIKE '$tag'";
92         if (ref $tag) {
93                 $tag_where = "AND f.tag IN ('".join("','",@$tag)."')";
94         }
95
96         my $sf_where = "AND f.subfield = '$subfield'";
97         if (ref $subfield) {
98                 $sf_where = "AND f.subfield IN ('".join("','",@$subfield)."')";
99         }
100
101         my $search_table = authority::full_rec->table;
102         my $marc_table = authority::record_entry->table;
103
104         my ($index_col) = authority::full_rec->columns('FTS');
105         $index_col ||= 'value';
106
107         my $fts = OpenILS::Application::Storage::FTS->compile($term, 'f.value', "f.$index_col");
108
109         $term =~ s/\W+$//gso;
110         $term =~ s/'/''/gso;
111         $term =~ s/\pM//gso;
112
113         my $fts_where = $fts->sql_where_clause;
114         my $fts_words = join '%', $fts->words;
115         my $fts_words_where = "f.value LIKE '$fts_words\%'";
116         my $fts_start_where = "f.value LIKE '$term\%'";
117         my $fts_eq_where = "f.value = '$term'";
118
119         my $fts_rank = join '+', $fts->fts_rank;
120
121         my $select = <<"        SQL";
122                 SELECT  a.marc, sum($fts_rank), count(f.record), first(f.value)
123                 FROM    $search_table f,
124                         $marc_table a
125                 WHERE   $fts_start_where
126                         $tag_where
127                         $sf_where
128                         AND a.id = f.record
129                         GROUP BY 1
130                         ORDER BY 2 desc, 3 desc, 4
131                         $limit
132                         $offset
133                         
134         SQL
135
136         $log->debug("Authority Search SQL :: [$select]",DEBUG);
137
138         my $recs = authority::full_rec->db_Main->selectcol_arrayref( $select );
139         
140         $log->debug("Search yielded ".scalar(@$recs)." results.",DEBUG);
141
142         $client->respond($_) for (@$recs);
143         return undef;
144 }
145 __PACKAGE__->register_method(
146         api_name        => "open-ils.storage.authority.search.marc",
147         method          => 'find_authority_marc',
148         api_level       => 1,
149         stream          => 1,
150         cachable        => 1,
151 );
152
153 sub _empty_check {
154         my $term = shift;
155         my $class = shift || 'metabib::full_rec';
156
157         my $table = $class->table;
158
159         my ($index_col) = $class->columns('FTS');
160         $index_col ||= 'value';
161
162         my $fts = OpenILS::Application::Storage::FTS->compile($term, 'm.value', "m.$index_col");
163         my $fts_where = $fts->sql_where_clause;
164
165         my $sql = <<"   SQL";
166                 SELECT  TRUE
167                 FROM    $table m
168                 WHERE   $fts_where
169                 LIMIT 1
170         SQL
171
172         return $class->db_Main->selectcol_arrayref($sql)->[0];
173 }
174
175 my $prevtime;
176
177 sub find_see_from_controlled {
178         my $self = shift;
179         my $client = shift;
180         my $term = shift;
181         my $limit = shift;
182         my $offset = shift;
183
184         $prevtime = time;
185
186         (my $class = $self->api_name) =~ s/^.+authority.([^\.]+)\.see.+$/$1/o;
187         my $sf = 'a';
188         $sf = 't' if ($class eq 'title');
189
190         my @marc = $self->method_lookup('open-ils.storage.authority.search.marc')
191                         ->run( term => $term, tag => [400,410,411,430,450,455], subfield => $sf, limit => $limit, offset => $offset );
192
193         
194         for my $m ( @marc ) {
195                 my $doc = $parser->parse_string($m);
196                 my @nodes = $doc->documentElement->findnodes('//*[substring(@tag,1,1)="1"]/*[@code="a" or @code="d" or @code="x"]');
197                 my $list = [ map { $_->textContent } @nodes ];
198                 $client->respond( $list ) if (_empty_check(join(' ',@$list), "metabib::${class}_field_entry"));
199         }
200         return undef;
201 }
202 for my $class ( qw/title author subject keyword series/ ) {
203         __PACKAGE__->register_method(
204                 api_name        => "open-ils.storage.authority.$class.see_from.controlled",
205                 method          => 'find_see_from_controlled',
206                 api_level       => 1,
207                 stream          => 1,
208                 cachable        => 1,
209         );
210 }
211
212 sub find_see_also_from_controlled {
213         my $self = shift;
214         my $client = shift;
215         my $term = shift;
216         my $limit = shift;
217         my $offset = shift;
218
219         (my $class = $self->api_name) =~ s/^.+authority.([^\.]+)\.see.+$/$1/o;
220         my $sf = 'a';
221         $sf = 't' if ($class eq 'title');
222
223         my @marc = $self->method_lookup('open-ils.storage.authority.search.marc')
224                         ->run( term => $term, tag => [500,510,511,530,550,555], subfield => $sf, limit => $limit, offset => $offset );
225         for my $m ( @marc ) {
226                 my $doc = $parser->parse_string($m);
227                 my @nodes = $doc->documentElement->findnodes('//*[substring(@tag,1,1)="1"]/*[@code="a" or @code="d" or @code="x"]');
228                 my $list = [ map { $_->textContent } @nodes ];
229                 $client->respond( $list ) if (_empty_check(join(' ',@$list), "metabib::${class}_field_entry"));
230         }
231         return undef;
232 }
233 for my $class ( qw/title author subject keyword series/ ) {
234         __PACKAGE__->register_method(
235                 api_name        => "open-ils.storage.authority.$class.see_also_from.controlled",
236                 method          => 'find_see_also_from_controlled',
237                 api_level       => 1,
238                 stream          => 1,
239                 cachable        => 1,
240         );
241 }
242
243
244 1;