2 package OpenILS::Application::Search::Z3950;
3 use strict; use warnings;
4 use base qw/OpenSRF::Application/;
10 use OpenSRF::Utils::SettingsClient;
12 use OpenILS::Utils::FlatXML;
13 use OpenILS::Application::Cat::Utils;
14 use OpenILS::Application::AppUtils;
17 use OpenSRF::Utils::Logger qw/$logger/;
19 use OpenSRF::EX qw(:try);
21 my $utils = "OpenILS::Application::Cat::Utils";
22 my $apputils = "OpenILS::Application::AppUtils";
25 use OpenILS::Utils::ModsParser;
28 my $output = "USMARC"; # only support output for now
41 $settings_client = OpenSRF::Utils::SettingsClient->new();
43 $defserv = $settings_client->config_value("z3950", "default" );
45 ( $host, $port, $database, $username, $password ) = _load_settings($defserv);
46 $tcnattr = $settings_client->config_value("z3950", $defserv, "tcnattr");
47 $isbnattr = $settings_client->config_value("z3950", $defserv, "isbnattr");
49 $logger->info("z3950: Loading Defaults: service=$defserv, host=$host, port=$port, ".
50 "db=$database, tcnattr=$tcnattr, isbnattr=$isbnattr, username=$username, password=$password" );
56 if( $service eq $defserv and $host ) {
57 return ( $host, $port, $database, $username, $password );
61 $settings_client->config_value("z3950", $service, "host"),
62 $settings_client->config_value("z3950", $service, "port"),
63 $settings_client->config_value("z3950", $service, "db"),
64 $settings_client->config_value("z3950", $service, "username"),
65 $settings_client->config_value("z3950", $service, "password"),
70 __PACKAGE__->register_method(
71 method => "marcxml_to_brn",
72 api_name => "open-ils.search.z3950.marcxml_to_brn",
76 my( $self, $client, $marcxml ) = @_;
81 # Strip the namespace info from the <collection> node and shove it into
82 # the <record> node, if the collection node exists
83 my ($ns) = ( $marcxml =~ /<collection(.*)?>/og );
84 $logger->info("marcxml_to_brn extracted namespace info: $ns") if $ns;
85 $marcxml =~ s/<collection(.*)?>//og;
86 $marcxml =~ s/<\/collection>//og;
87 $marcxml =~ s/<record>/<record $ns>/og if $ns;
89 my $flat = OpenILS::Utils::FlatXML->new( xml => $marcxml );
90 my $doc = $flat->xml_to_doc();
92 $logger->debug("z3950: Turning doc into a nodeset...");
95 my $nodes = OpenILS::Utils::FlatXML->new->xmldoc_to_nodeset($doc);
96 $logger->debug("z3950: turning nodeset into tree");
97 $tree = $utils->nodeset2tree( $nodes->nodeset );
103 $logger->error("z3950: Error turning doc into nodeset/node tree: $err");
110 __PACKAGE__->register_method(
111 method => "z39_search_by_string",
112 api_name => "open-ils.search.z3950.raw_string",
115 sub z39_search_by_string {
117 my( $self, $connection, $authtoken, $params ) = @_;
118 my( $hst, $prt, $db, $usr, $pw );
121 my( $requestor, $evt ) = $U->checksesperm($authtoken, 'REMOTE_Z3950_QUERY');
123 my $service = $$params{service};
124 my $search = $$params{search};
127 ($hst, $prt, $db, $usr, $pw ) = _load_settings($$params{service});
129 $hst = $$params{host};
130 $prt = $$params{prt};
132 $usr = $$params{username};
133 $pw = $$params{password};
134 $service = "(custom)";
138 $logger->info("z3950: Search App connecting: service=$service, ".
139 "host=$hst, port=$prt, db=$db, username=$usr, password=$pw, search=$search" );
141 return OpenILS::Event->new('BAD_PARAMS') unless ($hst and $prt and $db);
143 $usr ||= ""; $pw ||= "";
145 my $conn = new Net::Z3950::Connection(
150 preferredRecordSyntax => $output,
154 $logger->error("Unable to create Z3950 connection: $hst, $prt, $db, $usr, $pw, $output");
155 return OpenILS::Event->new('UNKNOWN'); # XXX needs to be a real event
158 my $rs = $conn->search( $search );
159 return OpenILS::Event->new('Z3950_SEARCH_FAILED') unless $rs;
161 # We want nice full records
162 $rs->option(elementSetName => "f");
167 $hash->{count} = $rs->size();
168 $logger->info("z3950: Search recovered " . $hash->{count} . " records");
170 # until there is a more graceful way to handle this
171 if($hash->{count} > 20) { return $hash; }
173 for( my $x = 0; $x != $hash->{count}; $x++ ) {
174 $logger->debug("z3950: Churning on z39 record count $x");
176 my $rec = $rs->record($x+1);
177 my $marc = MARC::Record->new_from_usmarc($rec->rawdata());
179 my $marcxml = $marc->as_xml();
182 my $u = OpenILS::Utils::ModsParser->new();
183 $u->start_mods_batch( $marcxml );
184 $mods = $u->finish_mods_batch();
186 push @$records, { 'mvr' => $mods, 'marcxml' => $marcxml };
189 $logger->debug("z3950: got here near the end with " . scalar(@$records) . " records." );
191 $hash->{records} = $records;
197 __PACKAGE__->register_method(
198 method => "tcn_search",
199 api_name => "open-ils.search.z3950.tcn",
203 my($self, $connection, $authtoken, $tcn, $service) = @_;
205 my( $requestor, $evt ) = $U->checksesperm($authtoken, 'REMOTE_Z3950_QUERY');
207 $service ||= $defserv;
209 my $attr = $settings_client->config_value("z3950", $service, "tcnattr");
211 $logger->info("z3950: Searching for TCN $tcn");
213 return $self->z39_search_by_string(
214 $connection, $authtoken, {
215 search => "\@attr 1=$attr \"$tcn\"",
216 service => $service });
220 __PACKAGE__->register_method(
221 method => "isbn_search",
222 api_name => "open-ils.search.z3950.isbn",
226 my( $self, $connection, $authtoken, $isbn, $service ) = @_;
228 my( $requestor, $evt ) = $U->checksesperm($authtoken, 'REMOTE_Z3950_QUERY');
230 $service ||= $defserv;
232 my $attr = $settings_client->config_value("z3950", $service, "isbnattr");
234 $logger->info("z3950: Performing ISBN search : $isbn");
236 return $self->z39_search_by_string(
237 $connection, $authtoken, {
238 search => "\@attr 1=$attr \"$isbn\"",
239 service => $service });
243 __PACKAGE__->register_method(
244 method => "query_interfaces",
245 api_name => "open-ils.search.z3950.services.retrieve",
248 sub query_interfaces {
249 my( $self, $client, $authtoken ) = @_;
250 my( $requestor, $evt ) = $U->checksesperm($authtoken, 'REMOTE_Z3950_QUERY');
252 my $services = $settings_client->config_value("z3950");
253 $services = { $services } unless ref($services);
255 return [ grep { $_ ne 'default' } keys %$services ];