source: gsdl/trunk/perllib/downloaders/Z3950Download.pm@ 17229

Last change on this file since 17229 was 17229, checked in by ak19, 16 years ago

Moved code for starting up (including opening connections) and quitting yaz-client into subroutines since the same code was repeated numerous times

  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 KB
Line 
1###########################################################################
2#
3# WebDownload.pm -- base class for all the import plugins
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;
38
39sub BEGIN {
40 @Z3950Download::ISA = ('BaseDownload');
41}
42
43my $arguments =
44 [ { 'name' => "host",
45 'disp' => "{Z3950Download.host_disp}",
46 'desc' => "{Z3950Download.host}",
47 'type' => "string",
48 'reqd' => "yes"},
49 { 'name' => "port",
50 'disp' => "{Z3950Download.port_disp}",
51 'desc' => "{Z3950Download.port}",
52 'type' => "string",
53 'reqd' => "yes"},
54 { 'name' => "database",
55 'disp' => "{Z3950Download.database_disp}",
56 'desc' => "{Z3950Download.database}",
57 'type' => "string",
58 'reqd' => "yes"},
59 { 'name' => "find",
60 'disp' => "{Z3950Download.find_disp}",
61 'desc' => "{Z3950Download.find}",
62 'type' => "string",
63 'deft' => "",
64 'reqd' => "yes"},
65 { 'name' => "max_records",
66 'disp' => "{Z3950Download.max_records_disp}",
67 'desc' => "{Z3950Download.max_records}",
68 'type' => "int",
69 'deft' => "500",
70 'reqd' => "no"}];
71
72my $options = { 'name' => "Z3950Download",
73 'desc' => "{Z3950Download.desc}",
74 'abstract' => "no",
75 'inherits' => "yes",
76 'args' => $arguments };
77
78
79sub new
80{
81 my ($class) = shift (@_);
82 my ($getlist,$inputargs,$hashArgOptLists) = @_;
83 push(@$getlist, $class);
84
85 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
86 push(@{$hashArgOptLists->{"OptList"}},$options);
87
88 my $self = new BaseDownload($getlist,$inputargs,$hashArgOptLists);
89
90 if ($self->{'info_only'}) {
91 # don't worry about any options etc
92 return bless $self, $class;
93 }
94
95 # Must set $self->{'url'}, since GLI use $self->{'url'} to calculate the log file name!
96 $self->{'url'} = $self->{'host'}.":".$self->{'port'};
97
98 $self->{'yaz'} = &util::filename_cat($ENV{'GSDLHOME'}, "bin", $ENV{'GSDLOS'}, "yaz-client");
99
100 return bless $self, $class;
101
102}
103
104sub download
105{
106 my ($self) = shift (@_);
107 my ($hashGeneralOptions) = @_;
108 my ($strOpen,$strBase,$strFind,$strResponse,$intAmount,$intMaxRecords,$strRecords);
109
110 my $url = $self->{'url'};
111
112 print STDERR "<<Defined Maximum>>\n";
113
114 $strOpen = $self->start_yaz($url);
115
116 print STDERR "Access database: \"$self->{'database'}\"\n";
117 $self->run_command_without_output("base $self->{'database'}");
118 print STDERR "Searching for keyword: \"$self->{'find'}\"\n";
119 $intAmount = $self->findAmount($self->{'find'});
120
121 if($intAmount <= 0)
122 {
123 ($intAmount == -1)?
124 print STDERR "Something wrong with the arguments,downloading can not be performed\n":
125 print STDERR "No Record is found\n";
126 print STDERR "<<Finished>>\n";
127 return 0;
128 }
129 $intMaxRecords = ($self->{'max_records'} > $intAmount)? $intAmount : $self->{'max_records'};
130 print STDERR "<<Total number of record(s):$intMaxRecords>>\n";
131 $strRecords = "Records: $intMaxRecords\n".$self->getRecords($intMaxRecords);
132
133 $self->saveRecords($strRecords,$hashGeneralOptions->{'cache_dir'},$intMaxRecords);
134 print STDERR "Closing connection...\n";
135
136 $self->quit_yaz();
137 return 1;
138}
139
140
141sub start_yaz
142{
143 my ($self, $url) = @_;
144
145 print STDERR "Opening connection to $url\n";
146
147 my $yaz = $self->{'yaz'};
148
149 my $childpid = open2(*YAZOUT, *YAZIN, $yaz)
150 or (print STDERR "<<Finished>>\n" and die "can't open pipe to yaz-client: $!");
151 $self->{'YAZOUT'} = *YAZOUT;
152 $self->{'YAZIN'} = *YAZIN;
153
154 my $strOpen = $self->open_connection("open $url");
155
156 if (!$strOpen) {
157 print STDERR "Cannot connect to $url\n";
158 print STDERR "<<Finished>>\n";
159 return 0;
160 }
161 return $strOpen;
162}
163
164sub quit_yaz
165{
166 my ($self) = shift (@_);
167
168 print STDERR "<<Finished>>\n";
169
170 # need to send the quit command, else yaz-client is still running in the background
171 $self->run_command_without_output("quit");
172 close($self->{'YAZIN'}); # close the input to yaz. It also flushes quit command to yaz.
173
174 # make sure nothing is being output by yaz
175 my $output = $self->{'YAZOUT'};
176 my $line;
177 while (defined ($line = <$output>)) {
178 print STDERR "***### $line";
179 }
180
181 close($self->{'YAZOUT'});
182}
183
184sub open_connection{
185 my ($self,$strCommand) = (@_);
186
187 $self->run_command($strCommand);
188
189 my $out = $self->{'YAZOUT'};
190
191 my $opening_line = <$out>;
192
193 return ($opening_line =~ m/Connecting...OK/i)? 1: 0;
194
195}
196
197sub findAmount
198{
199 my ($self) = shift (@_);
200 my($strFindTarget) = @_;
201 my $strResponse = $self->run_command_with_output("find $strFindTarget","^Number of hits:");
202 return ($strResponse =~ m/^Number of hits: (\d+)/m)? $1:-1;
203}
204
205sub getRecords
206{
207 my ($self) = shift (@_);
208 my ($intMaxRecords) = @_;
209 my ($strShow,$intStartNumber,$numRecords,$strResponse,$strRecords,$intRecordsLeft);
210
211 $intStartNumber = 1;
212 $intRecordsLeft = $intMaxRecords;
213 $numRecords = 0;
214 $strResponse ="";
215
216 while ($intRecordsLeft > 0)
217 {
218 if($intRecordsLeft > 50)
219 {
220
221 print STDERR "Yaz is Gathering records: $intStartNumber - ".($intStartNumber+49)."\n";
222 $numRecords = 50;
223 $strShow = "show $intStartNumber+50";
224 $intStartNumber = $intStartNumber + 50;
225 $intRecordsLeft = $intRecordsLeft - 50;
226
227 }
228 else
229 {
230 $numRecords = $intRecordsLeft;
231 print STDERR "Yaz is Gathering records: $intStartNumber - ".($intStartNumber+$intRecordsLeft-1)."\n";
232 $strShow = "show $intStartNumber+$intRecordsLeft";
233 $intRecordsLeft = 0;
234
235 }
236
237 $strResponse .= $self->get($strShow,$numRecords);
238
239 if ($strResponse eq ""){
240 print STDERR "<<ERROR: failed to get $numRecords records>>\n";
241 }
242 else{
243 print STDERR "<<Done:$numRecords>>\n";
244 }
245 }
246
247 return "$strResponse\n";
248
249}
250
251sub saveRecords
252{
253 my ($self,$strRecords,$strOutputDir,$intMaxRecords) = @_;
254
255 # setup directories
256 # Currently only gather the MARC format
257 my $strFileName = $self->generateFileName($intMaxRecords);
258
259 $strOutputDir =~ s/"//g; #"
260
261 my $strOutputFile = &util::filename_cat($strOutputDir,$self->{'host'},"$strFileName.marc");
262 # prepare subdirectory for record (if needed)
263 my ($strSubDirPath,$unused) = $self->dirFileSplit($strOutputFile);
264
265 &util::mk_all_dir($strSubDirPath);
266
267 print STDERR "Saving records to \"$strOutputFile\"\n";
268
269 # save record
270 open (ZOUT,">$strOutputFile")
271 || die "Unable to save Z3950 record: $!\n";
272 print ZOUT $strRecords;
273 close(ZOUT);
274}
275
276
277sub run_command_with_output
278{
279 my ($self,$strCMD,$strStopRE) =@_;
280
281 $self->run_command($strCMD);
282
283 return $self->get_output($strStopRE);
284
285}
286
287sub get{
288 my ($self,$strShow,$numRecord) = @_;
289
290 $self->run_command($strShow);
291
292 my $strFullOutput="";
293 my $count=0;
294 my $readRecord = 0;
295
296 my $output = $self->{'YAZOUT'};
297 while (my $strLine = <$output>)
298 {
299
300 if ($strLine =~ m/Records: ([\d]*)/i ){
301 $readRecord = 1;
302 next;
303 }
304
305 return $strFullOutput if ($strLine =~ m/nextResultSetPosition|Not connected/i);
306
307 next if(!$readRecord);
308
309 $strFullOutput .= $strLine;
310 }
311
312}
313
314sub run_command_without_output
315{
316 my ($self) = shift (@_);
317 my ($strCMD) = @_;
318
319 $self->run_command($strCMD);
320}
321
322sub run_command
323{
324 my ($self,$strCMD) = @_;
325
326 my $input = $self->{'YAZIN'};
327
328 print $input "$strCMD\n";
329}
330
331sub get_output{
332 my ($self,$strStopRE) = @_;
333
334 if (!defined $strStopRE){return "";}
335 else
336 {
337 my $strFullOutput;
338 my $output = $self->{'YAZOUT'};
339 while (my $strLine = <$output>)
340 {
341 $strFullOutput .= $strLine;
342 if($strLine =~ m/^$strStopRE|Not connected/i){return $strFullOutput;}
343 }
344 }
345}
346
347sub generateFileName
348{
349 my ($self,$intMaxRecords) = @_;
350 my $strFileName = ($self->{'database'})."_".($self->{'find'})."_".($intMaxRecords);
351
352}
353
354sub dirFileSplit
355{
356 my ($self,$strFile) = @_;
357
358 my @aryDirs = split(/[\/\\]/,$strFile);
359
360 my $strLocalFile = pop(@aryDirs);
361 my $strSubDirs = join("/",@aryDirs);
362
363 return ($strSubDirs,$strLocalFile);
364}
365
366sub url_information
367{
368 my ($self,$url) = @_;
369
370 $url = $self->{'url'} unless defined $url;
371
372 my $strOpen = $self->start_yaz();
373
374 $strOpen = $self->run_command_with_output("open $url","^Options");
375
376 $strOpen =~ s/Z> //g;
377 $strOpen =~ s/Elapsed:.*//g;
378
379 print STDERR $strOpen;
380
381 $self->quit_yaz();
382
383 return 0;
384
385}
386
387sub error
388{
389 my ($self,$strFunctionName,$strError) = @_;
390 {
391 print STDERR "Error occoured in Z3950Download.pm\n".
392 "In Function:".$strFunctionName."\n".
393 "Error Message:".$strError."\n";
394 exit(-1);
395 }
396}
397
3981;
Note: See TracBrowser for help on using the repository browser.