1 package OpenILS::Application::Storage::Publisher::authority;
2 use base qw/OpenILS::Application::Storage::Publisher/;
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;
10 use Digest::MD5 qw/md5_hex/;
13 my $log = 'OpenSRF::Utils::Logger';
17 my $parser = XML::LibXML->new;
19 sub find_authority_marc {
24 my $term = $args{term};
26 my $subfield = $args{subfield};
27 my $limit = $args{limit} || 100;
28 my $offset = $args{offset} || 0;
31 $limit = "LIMIT $limit";
37 $offset = "OFFSET $offset";
42 my $tag_where = "AND f.tag LIKE '$tag'";
44 $tag_where = "AND f.tag IN ('".join("','",@$tag)."')";
47 my $sf_where = "AND f.subfield = '$subfield'";
49 $sf_where = "AND f.subfield IN ('".join("','",@$subfield)."')";
52 my $search_table = authority::full_rec->table;
53 my $marc_table = authority::record_entry->table;
55 my ($index_col) = authority::full_rec->columns('FTS');
56 $index_col ||= 'value';
58 my $fts = OpenILS::Application::Storage::FTS->compile($term, 'f.value', "f.$index_col");
61 my $fts_where = $fts->sql_where_clause;
62 my $fts_words = join '%', $fts->words;
63 my $fts_words_where = "f.value LIKE '$fts_words\%'";
65 my $fts_rank = join '+', $fts->fts_rank;
67 my $select = <<" SQL";
68 SELECT a.marc, sum($fts_rank)
72 -- AND $fts_words_where
83 $log->debug("Authority Search SQL :: [$select]",DEBUG);
85 my $recs = authority::full_rec->db_Main->selectcol_arrayref( $select );
87 $log->debug("Search yielded ".scalar(@$recs)." results.",DEBUG);
89 $client->respond($_) for (@$recs);
92 __PACKAGE__->register_method(
93 api_name => "open-ils.storage.authority.search.marc",
94 method => 'find_authority_marc',
102 my $class = shift || 'metabib::full_rec';
104 my $table = $class->table;
106 my ($index_col) = $class->columns('FTS');
107 $index_col ||= 'value';
109 my $fts = OpenILS::Application::Storage::FTS->compile($term, 'm.value', "m.$index_col");
110 my $fts_where = $fts->sql_where_clause;
119 return $class->db_Main->selectcol_arrayref($sql)->[0];
122 sub find_see_from_controlled {
129 (my $class = $self->api_name) =~ s/^.+authority.([^\.]+)\.see.+$/$1/o;
131 $sf = 't' if ($class eq 'title');
133 my @marc = $self->method_lookup('open-ils.storage.authority.search.marc')
134 ->run( term => $term, tag => '4%', subfield => $sf, limit => $limit, offset => $offset );
135 for my $m ( @marc ) {
136 my $doc = $parser->parse_string($m);
137 my @nodes = $doc->documentElement->findnodes('//*[substring(@tag,1,1)="1"]/*[@code="a" or @code="d" or @code="x"]');
138 my $list = [ map { $_->textContent } @nodes ];
139 $client->respond( $list ) if (_empty_check(join(' ',@$list), "metabib::${class}_field_entry"));
143 for my $class ( qw/title author subject keyword series/ ) {
144 __PACKAGE__->register_method(
145 api_name => "open-ils.storage.authority.$class.see_from.controlled",
146 method => 'find_see_from_controlled',
153 sub find_see_also_from_controlled {
160 (my $class = $self->api_name) =~ s/^.+authority.([^\.]+)\.see.+$/$1/o;
162 $sf = 't' if ($class eq 'title');
164 my @marc = $self->method_lookup('open-ils.storage.authority.search.marc')
165 ->run( term => $term, tag => '5%', subfield => $sf, limit => $limit, offset => $offset );
166 for my $m ( @marc ) {
167 my $doc = $parser->parse_string($m);
168 my @nodes = $doc->documentElement->findnodes('//*[substring(@tag,1,1)="1"]/*[@code="a" or @code="d" or @code="x"]');
169 my $list = [ map { $_->textContent } @nodes ];
170 $client->respond( $list ) if (_empty_check(join(' ',@$list), "metabib::${class}_field_entry"));
174 for my $class ( qw/title author subject keyword series/ ) {
175 __PACKAGE__->register_method(
176 api_name => "open-ils.storage.authority.$class.see_also_from.controlled",
177 method => 'find_see_also_from_controlled',