source: main/trunk/greenstone2/perllib/downloaders/Z3950Download.pm

Last change on this file was 34498, checked in by kjdon, 4 years ago

small mods to prevent warnings when calling start_yaz without a url (as happens in url_information)

  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1###########################################################################
2#
3# Z3950Download.pm -- downloading class for z3950 protocol
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package Z3950Download;
27
28eval {require bytes};
29
30# suppress the annoying "subroutine redefined" warning that various
31# plugins cause under perl 5.6
32$SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
33
34use strict;
35
36use BaseDownload;
37use IPC::Open2;
38use POSIX ":sys_wait_h"; # for waitpid, http://perldoc.perl.org/functions/waitpid.html
39
40sub BEGIN {
41 @Z3950Download::ISA = ('BaseDownload');
42}
43
44my $arguments =
45 [ { 'name' => "host",
46 'disp' => "{Z3950Download.host_disp}",
47 'desc' => "{Z3950Download.host}",
48 'type' => "string",
49 'reqd' => "yes"},
50 { 'name' => "port",
51 'disp' => "{Z3950Download.port_disp}",
52 'desc' => "{Z3950Download.port}",
53 'type' => "string",
54 'reqd' => "yes"},
55 { 'name' => "database",
56 'disp' => "{Z3950Download.database_disp}",
57 'desc' => "{Z3950Download.database}",
58 'type' => "string",
59 'reqd' => "yes"},
60 { 'name' => "find",
61 'disp' => "{Z3950Download.find_disp}",
62 'desc' => "{Z3950Download.find}",
63 'type' => "string",
64 'deft' => "",
65 'reqd' => "yes"},
66 { 'name' => "max_records",
67 'disp' => "{Z3950Download.max_records_disp}",
68 'desc' => "{Z3950Download.max_records}",
69 'type' => "int",
70 'deft' => "500",
71 'reqd' => "no"}];
72
73my $options = { 'name' => "Z3950Download",
74 'desc' => "{Z3950Download.desc}",
75 'abstract' => "no",
76 'inherits' => "yes",
77 'args' => $arguments };
78
79
80sub new
81{
82 my ($class) = shift (@_);
83 my ($getlist,$inputargs,$hashArgOptLists) = @_;
84 push(@$getlist, $class);
85
86 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
87 push(@{$hashArgOptLists->{"OptList"}},$options);
88
89 my $self = new BaseDownload($getlist,$inputargs,$hashArgOptLists);
90
91 if ($self->{'info_only'}) {
92 # don't worry about any options etc
93 return bless $self, $class;
94 }
95
96 # Must set $self->{'url'}, since GLI use $self->{'url'} to calculate the log file name!
97 $self->{'url'} = $self->{'host'}.":".$self->{'port'};
98
99 $self->{'yaz'} = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "bin", $ENV{'GSDLOS'}, "yaz-client");
100
101 return bless $self, $class;
102
103}
104
105sub download
106{
107 my ($self) = shift (@_);
108 my ($hashGeneralOptions) = @_;
109 my ($strOpen,$strBase,$strFind,$strResponse,$intAmount,$intMaxRecords,$strRecords);
110
111 my $url = $self->{'url'};
112
113 print STDERR "<<Defined Maximum>>\n";
114
115 $strOpen = $self->start_yaz($url);
116
117 print STDERR "Access database: \"$self->{'database'}\"\n";
118 $self->run_command_without_output("base $self->{'database'}");
119 print STDERR "Searching for keyword: \"$self->{'find'}\"\n";
120 $intAmount = $self->findAmount($self->{'find'});
121
122 if($intAmount <= 0)
123 {
124 ($intAmount == -1)?
125 print STDERR "Something wrong with the arguments,downloading can not be performed\n":
126 print STDERR "No Record is found\n";
127 print STDERR "<<Finished>>\n";
128 return 0;
129 }
130 $intMaxRecords = ($self->{'max_records'} > $intAmount)? $intAmount : $self->{'max_records'};
131 print STDERR "<<Total number of record(s):$intMaxRecords>>\n";
132 $strRecords = "Records: $intMaxRecords\n".$self->getRecords($intMaxRecords);
133
134 $self->saveRecords($strRecords,$hashGeneralOptions->{'cache_dir'},$intMaxRecords);
135 print STDERR "Closing connection...\n";
136
137 $self->quit_yaz();
138 return 1;
139}
140
141
142sub start_yaz
143{
144 my ($self, $url) = @_;
145
146 my $yaz = $self->{'yaz'};
147
148 my $childpid = open2(*YAZOUT, *YAZIN, $yaz)
149 or (print STDERR "<<Finished>>\n" and die "can't open pipe to yaz-client: $!");
150 $self->{'pid'} = $childpid;
151 $self->{'YAZOUT'} = *YAZOUT;
152 $self->{'YAZIN'} = *YAZIN;
153
154 if (defined $url) {
155 print STDERR "Opening connection to $url\n";
156 my $strOpen = $self->open_connection("open $url");
157
158 if (!$strOpen) {
159 print STDERR "Cannot connect to $url\n";
160 print STDERR "<<Finished>>\n";
161 return 0;
162 }
163
164 return $strOpen;
165 }
166 return 1;
167}
168
169sub quit_yaz
170{
171 my ($self) = shift (@_);
172
173 # can't send a "close" cmd to close the database here, since the close command is only
174 # recognised by Z3950, not by SRW. (This method is also used by the subclass for SRW.)
175
176 print STDERR "<<Finished>>\n";
177
178 # need to send the quit command, else yaz-client is still running in the background
179 $self->run_command_without_output("quit");
180 close($self->{'YAZIN'}); # close the input to yaz. It also flushes quit command to yaz.
181
182 # make sure nothing is being output by yaz
183 # flush the yaz-client process' outputstream, else we'll be stuck in an infinite
184 # loop waiting for the process to quit.
185 my $output = $self->{'YAZOUT'};
186 my $line;
187 while (defined ($line = <$output>)) {
188 if($line !~ m/\w/s) { # print anything other than plain whitespace in case it is important
189 print STDERR "***### $line";
190 }
191 }
192
193 close($self->{'YAZOUT'});
194
195 # Is the following necessary? The PerlDoc on open2 (http://perldoc.perl.org/IPC/Open2.html)
196 # says that waitpid must be called to "reap the child process", or otherwise it will hang
197 # around like a zombie process in the background. Adding it here makes the code work as
198 # before, but it is certainly necessary to call waitpid on wget (see WgetDownload.pm).
199 # http://perldoc.perl.org/functions/waitpid.html
200 my $kidpid;
201 do {
202 $kidpid = waitpid($self->{'pid'}, WNOHANG);
203 } while $kidpid > 0; # waiting for pid to become -1
204}
205
206sub open_connection{
207 my ($self,$strCommand) = (@_);
208
209 $self->run_command($strCommand);
210
211 my $out = $self->{'YAZOUT'};
212
213 my $opening_line = <$out>;
214
215 return ($opening_line =~ m/Connecting...OK/i)? 1: 0;
216
217}
218
219sub findAmount
220{
221 my ($self) = shift (@_);
222 my($strFindTarget) = @_;
223 my $strResponse = $self->run_command_with_output("find $strFindTarget","^Number of hits:");
224 return ($strResponse =~ m/^Number of hits: (\d+)/m)? $1:-1;
225}
226
227sub getRecords
228{
229 my ($self) = shift (@_);
230 my ($intMaxRecords) = @_;
231 my ($strShow,$intStartNumber,$numRecords,$strResponse,$strRecords,$intRecordsLeft);
232
233 $intStartNumber = 1;
234 $intRecordsLeft = $intMaxRecords;
235 $numRecords = 0;
236 $strResponse ="";
237
238 while ($intRecordsLeft > 0)
239 {
240 if($intRecordsLeft > 50)
241 {
242
243 print STDERR "Yaz is Gathering records: $intStartNumber - ".($intStartNumber+49)."\n";
244 $numRecords = 50;
245 $strShow = "show $intStartNumber+50";
246 $intStartNumber = $intStartNumber + 50;
247 $intRecordsLeft = $intRecordsLeft - 50;
248
249 }
250 else
251 {
252 $numRecords = $intRecordsLeft;
253 print STDERR "Yaz is Gathering records: $intStartNumber - ".($intStartNumber+$intRecordsLeft-1)."\n";
254 $strShow = "show $intStartNumber+$intRecordsLeft";
255 $intRecordsLeft = 0;
256
257 }
258
259 $strResponse .= $self->get($strShow,$numRecords);
260
261 if ($strResponse eq ""){
262 print STDERR "<<ERROR: failed to get $numRecords records>>\n";
263 }
264 else{
265 print STDERR "<<Done:$numRecords>>\n";
266 }
267 }
268
269 return "$strResponse\n";
270
271}
272
273sub saveRecords
274{
275 my ($self,$strRecords,$strOutputDir,$intMaxRecords) = @_;
276
277 # setup directories
278 # Currently only gather the MARC format
279 my $strFileName = $self->generateFileName($intMaxRecords);
280
281 $strOutputDir =~ s/"//g; #"
282
283 my $strOutputFile = &FileUtils::filenameConcatenate($strOutputDir,$self->{'host'},"$strFileName.marc");
284 # prepare subdirectory for record (if needed)
285 my ($strSubDirPath,$unused) = $self->dirFileSplit($strOutputFile);
286
287 &FileUtils::makeAllDirectories($strSubDirPath);
288
289 print STDERR "Saving records to \"$strOutputFile\"\n";
290
291 # save record
292 open (ZOUT,">$strOutputFile")
293 || die "Unable to save Z3950 record: $!\n";
294 print ZOUT $strRecords;
295 close(ZOUT);
296}
297
298
299sub run_command_with_output
300{
301 my ($self,$strCMD,$strStopRE) =@_;
302
303 $self->run_command($strCMD);
304
305 return $self->get_output($strStopRE);
306
307}
308
309sub get{
310 my ($self,$strShow,$numRecord) = @_;
311
312 $self->run_command($strShow);
313
314 my $strFullOutput="";
315 my $count=0;
316 my $readRecord = 0;
317
318 my $output = $self->{'YAZOUT'};
319 while (my $strLine = <$output>)
320 {
321
322 if ($strLine =~ m/Records: ([\d]*)/i ){
323 $readRecord = 1;
324 next;
325 }
326
327 return $strFullOutput if ($strLine =~ m/nextResultSetPosition|Not connected/i);
328
329 next if(!$readRecord);
330
331 $strFullOutput .= $strLine;
332 }
333
334}
335
336sub run_command_without_output
337{
338 my ($self) = shift (@_);
339 my ($strCMD) = @_;
340
341 $self->run_command($strCMD);
342}
343
344sub run_command
345{
346 my ($self,$strCMD) = @_;
347
348 my $input = $self->{'YAZIN'};
349
350 print $input "$strCMD\n";
351}
352
353sub get_output{
354 my ($self,$strStopRE) = @_;
355
356 if (!defined $strStopRE){return "";}
357 else
358 {
359 my $strFullOutput;
360 my $output = $self->{'YAZOUT'};
361 while (my $strLine = <$output>)
362 {
363 $strFullOutput .= $strLine;
364 if($strLine =~ m/^$strStopRE|Not connected/i){return $strFullOutput;}
365 }
366 }
367}
368
369sub generateFileName
370{
371 my ($self,$intMaxRecords) = @_;
372 my $strFileName = ($self->{'database'})."_".($self->{'find'})."_".($intMaxRecords);
373
374}
375
376sub dirFileSplit
377{
378 my ($self,$strFile) = @_;
379
380 my @aryDirs = split(/[\/\\]/,$strFile);
381
382 my $strLocalFile = pop(@aryDirs);
383 my $strSubDirs = join("/",@aryDirs);
384
385 return ($strSubDirs,$strLocalFile);
386}
387
388sub url_information
389{
390 my ($self,$url) = @_;
391
392 $url = $self->{'url'} unless defined $url;
393
394 #my $strOpen = $self->start_yaz();
395 $self->start_yaz();
396
397 my $strOpen = $self->run_command_with_output("open $url","^Options");
398
399 $strOpen =~ s/Z> //g;
400 $strOpen =~ s/Elapsed:.*//g;
401
402 print STDERR $strOpen;
403
404 $self->quit_yaz();
405
406 return 0;
407
408}
409
410sub error
411{
412 my ($self,$strFunctionName,$strError) = @_;
413 {
414 print STDERR "Error occoured in Z3950Download.pm\n".
415 "In Function:".$strFunctionName."\n".
416 "Error Message:".$strError."\n";
417 exit(-1);
418 }
419}
420
4211;
Note: See TracBrowser for help on using the repository browser.