2 package OpenILS::Application::Search::Z3950;
3 use strict; use warnings;
4 use base qw/OpenSRF::Application/;
10 use OpenSRF::Utils::SettingsClient;
11 use Unicode::Normalize;
13 use OpenILS::Utils::FlatXML;
14 use OpenILS::Application::Cat::Utils;
15 use OpenILS::Application::AppUtils;
18 use OpenSRF::Utils::Logger qw/$logger/;
20 use OpenSRF::EX qw(:try);
22 my $utils = "OpenILS::Application::Cat::Utils";
23 my $apputils = "OpenILS::Application::AppUtils";
26 use OpenILS::Utils::ModsParser;
29 my $output = "USMARC"; # only support output for now
51 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
56 $settings_client = OpenSRF::Utils::SettingsClient->new();
58 $defserv = $settings_client->config_value("z3950", "default" );
60 ( $host, $port, $database, $username, $password ) = _load_settings($defserv);
61 $tcnattr = $settings_client->config_value("z3950", $defserv, "tcnattr");
62 $isbnattr = $settings_client->config_value("z3950", $defserv, "isbnattr");
64 $logger->info("z3950: Loading Defaults: service=$defserv, host=$host, port=$port, ".
65 "db=$database, tcnattr=$tcnattr, isbnattr=$isbnattr, username=$username, password=$password" );
71 if( $service eq $defserv and $host ) {
72 return ( $host, $port, $database, $username, $password );
76 $settings_client->config_value("z3950", $service, "host"),
77 $settings_client->config_value("z3950", $service, "port"),
78 $settings_client->config_value("z3950", $service, "db"),
79 $settings_client->config_value("z3950", $service, "username"),
80 $settings_client->config_value("z3950", $service, "password"),
85 __PACKAGE__->register_method(
86 method => "marcxml_to_brn",
87 api_name => "open-ils.search.z3950.marcxml_to_brn",
91 my( $self, $client, $marcxml ) = @_;
96 # Strip the namespace info from the <collection> node and shove it into
97 # the <record> node, if the collection node exists
98 my ($ns) = ( $marcxml =~ /<collection(.*)?>/og );
99 $logger->info("marcxml_to_brn extracted namespace info: $ns") if $ns;
100 $marcxml =~ s/<collection(.*)?>//og;
101 $marcxml =~ s/<\/collection>//og;
102 $marcxml =~ s/<record>/<record $ns>/og if $ns;
104 my $flat = OpenILS::Utils::FlatXML->new( xml => $marcxml );
105 my $doc = $flat->xml_to_doc();
107 $logger->debug("z3950: Turning doc into a nodeset...");
110 my $nodes = OpenILS::Utils::FlatXML->new->xmldoc_to_nodeset($doc);
111 $logger->debug("z3950: turning nodeset into tree");
112 $tree = $utils->nodeset2tree( $nodes->nodeset );
118 $logger->error("z3950: Error turning doc into nodeset/node tree: $err");
125 __PACKAGE__->register_method(
126 method => "z39_search_by_string",
127 api_name => "open-ils.search.z3950.raw_string",
130 sub z39_search_by_string {
132 my( $self, $connection, $authtoken, $params ) = @_;
133 my( $hst, $prt, $db, $usr, $pw );
136 my( $requestor, $evt ) = $U->checksesperm($authtoken, 'REMOTE_Z3950_QUERY');
138 my $service = $$params{service};
139 my $search = $$params{search};
142 ($hst, $prt, $db, $usr, $pw ) = _load_settings($$params{service});
144 $hst = $$params{host};
145 $prt = $$params{prt};
147 $usr = $$params{username};
148 $pw = $$params{password};
149 $service = "(custom)";
153 $logger->info("z3950: Search App connecting: service=$service, ".
154 "host=$hst, port=$prt, db=$db, username=$usr, password=$pw, search=$search" );
156 return OpenILS::Event->new('BAD_PARAMS') unless ($hst and $prt and $db);
158 $usr ||= ""; $pw ||= "";
160 my $conn = new Net::Z3950::Connection(
165 preferredRecordSyntax => $output,
169 $logger->error("Unable to create Z3950 connection: $hst, $prt, $db, $usr, $pw, $output");
170 return OpenILS::Event->new('UNKNOWN'); # XXX needs to be a real event
173 my $rs = $conn->search( $search );
174 return OpenILS::Event->new('Z3950_SEARCH_FAILED') unless $rs;
176 # We want nice full records
177 $rs->option(elementSetName => "f");
182 $hash->{count} = $rs->size();
183 $logger->info("z3950: Search recovered " . $hash->{count} . " records");
185 # until there is a more graceful way to handle this
186 if($hash->{count} > 20) { return $hash; }
188 for( my $x = 0; $x != $hash->{count}; $x++ ) {
189 $logger->debug("z3950: Churning on z39 record count $x");
191 my $rec = $rs->record($x+1);
192 my $marc = MARC::Record->new_from_usmarc($rec->rawdata());
194 my $marcxml = entityize( $marc->as_xml_record() );
197 my $u = OpenILS::Utils::ModsParser->new();
198 $u->start_mods_batch( $marcxml );
199 $mods = $u->finish_mods_batch();
201 push @$records, { 'mvr' => $mods, 'marcxml' => $marcxml };
204 $logger->debug("z3950: got here near the end with " . scalar(@$records) . " records." );
206 $hash->{records} = $records;
212 __PACKAGE__->register_method(
213 method => "tcn_search",
214 api_name => "open-ils.search.z3950.tcn",
218 my($self, $connection, $authtoken, $tcn, $service) = @_;
220 my( $requestor, $evt ) = $U->checksesperm($authtoken, 'REMOTE_Z3950_QUERY');
222 $service ||= $defserv;
224 my $attr = $settings_client->config_value("z3950", $service, "tcnattr");
226 $logger->info("z3950: Searching for TCN $tcn");
228 return $self->z39_search_by_string(
229 $connection, $authtoken, {
230 search => "\@attr 1=$attr \"$tcn\"",
231 service => $service });
235 __PACKAGE__->register_method(
236 method => "isbn_search",
237 api_name => "open-ils.search.z3950.isbn",
241 my( $self, $connection, $authtoken, $isbn, $service ) = @_;
243 my( $requestor, $evt ) = $U->checksesperm($authtoken, 'REMOTE_Z3950_QUERY');
245 $service ||= $defserv;
247 my $attr = $settings_client->config_value("z3950", $service, "isbnattr");
249 $logger->info("z3950: Performing ISBN search : $isbn");
251 return $self->z39_search_by_string(
252 $connection, $authtoken, {
253 search => "\@attr 1=$attr \"$isbn\"",
254 service => $service });
258 __PACKAGE__->register_method(
259 method => "query_interfaces",
260 api_name => "open-ils.search.z3950.services.retrieve",
263 sub query_interfaces {
264 my( $self, $client, $authtoken ) = @_;
265 my( $requestor, $evt ) = $U->checksesperm($authtoken, 'REMOTE_Z3950_QUERY');
267 my $services = $settings_client->config_value("z3950");
268 $services = { $services } unless ref($services);
270 return [ grep { $_ ne 'default' } keys %$services ];