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

Last change on this file since 17207 was 17207, checked in by kjdon, 16 years ago

BasDownload renamed to BaseDownload, also tidied up the constructors

  • Property svn:keywords set to Author Date Id Revision
File size: 9.4 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 print STDERR "Opening connection to $url\n";
115
116 my $yaz = $self->{'yaz'};
117
118 my $childpid = open2(*YAZOUT, *YAZIN, $yaz)
119 or (print STDERR "<<Finished>>\n" and die "can't open pipe to yaz-client: $!");
120 $self->{'YAZOUT'} = *YAZOUT;
121 $self->{'YAZIN'} = *YAZIN;
122
123 $strOpen = $self->open_connection("open $url");
124
125 if (!$strOpen) {
126 print STDERR "Cannot connect to $url\n";
127 print STDERR "<<Finished>>\n";
128 return 0;
129 }
130
131 print STDERR "Access database: \"$self->{'database'}\"\n";
132 $self->run_command_without_output("base $self->{'database'}");
133 print STDERR "Searching for keyword: \"$self->{'find'}\"\n";
134 $intAmount = $self->findAmount($self->{'find'});
135
136 if($intAmount <= 0)
137 {
138 ($intAmount == -1)?
139 print STDERR "Something wrong with the arguments,downloading can not be performed\n":
140 print STDERR "No Record is found\n";
141 print STDERR "<<Finished>>\n";
142 return 0;
143 }
144 $intMaxRecords = ($self->{'max_records'} > $intAmount)? $intAmount : $self->{'max_records'};
145 print STDERR "<<Total number of record(s):$intMaxRecords>>\n";
146 $strRecords = "Records: $intMaxRecords\n".$self->getRecords($intMaxRecords);
147
148 $self->saveRecords($strRecords,$hashGeneralOptions->{'cache_dir'},$intMaxRecords);
149 print STDERR "Closing connection...\n";
150 print STDERR "<<Finished>>\n";
151
152 close(YAZOUT);
153 close(YAZIN);
154 return 1;
155}
156
157sub open_connection{
158 my ($self,$strCommand) = (@_);
159
160 $self->run_command($strCommand);
161
162 my $out = $self->{'YAZOUT'};
163
164 $_ = <$out>;
165
166 return (/Connecting...OK/i)? 1: 0;
167
168}
169
170sub findAmount
171{
172 my ($self) = shift (@_);
173 my($strFindTarget) = @_;
174 my $strResponse = $self->run_command_with_output("find $strFindTarget","^Number of hits:");
175 return ($strResponse =~ m/^Number of hits: (\d+)/m)? $1:-1;
176}
177
178sub getRecords
179{
180 my ($self) = shift (@_);
181 my ($intMaxRecords) = @_;
182 my ($strShow,$intStartNumber,$numRecords,$strResponse,$strRecords,$intRecordsLeft);
183
184 $intStartNumber = 1;
185 $intRecordsLeft = $intMaxRecords;
186 $numRecords = 0;
187 $strResponse ="";
188
189 while ($intRecordsLeft > 0)
190 {
191 if($intRecordsLeft > 50)
192 {
193
194 print STDERR "Yaz is Gathering records: $intStartNumber - ".($intStartNumber+49)."\n";
195 $numRecords = 50;
196 $strShow = "show $intStartNumber+50";
197 $intStartNumber = $intStartNumber + 50;
198 $intRecordsLeft = $intRecordsLeft - 50;
199
200 }
201 else
202 {
203 $numRecords = $intRecordsLeft;
204 print STDERR "Yaz is Gathering records: $intStartNumber - ".($intStartNumber+$intRecordsLeft-1)."\n";
205 $strShow = "show $intStartNumber+$intRecordsLeft";
206 $intRecordsLeft = 0;
207
208 }
209
210 $strResponse .= $self->get($strShow,$numRecords);
211
212 if ($strResponse eq ""){
213 print STDERR "<<ERROR: failed to get $numRecords records>>\n";
214 }
215 else{
216 print STDERR "<<Done:$numRecords>>\n";
217 }
218 }
219
220 return "$strResponse\n";
221
222}
223
224sub saveRecords
225{
226 my ($self,$strRecords,$strOutputDir,$intMaxRecords) = @_;
227
228 # setup directories
229 # Currently only gather the MARC format
230 my $strFileName = $self->generateFileName($intMaxRecords);
231
232 $strOutputDir =~ s/"//g; #"
233
234 my $strOutputFile = &util::filename_cat($strOutputDir,$self->{'host'},"$strFileName.marc");
235 # prepare subdirectory for record (if needed)
236 my ($strSubDirPath,$unused) = $self->dirFileSplit($strOutputFile);
237
238 &util::mk_all_dir($strSubDirPath);
239
240 print STDERR "Saving records to \"$strOutputFile\"\n";
241
242 # save record
243 open (ZOUT,">$strOutputFile")
244 || die "Unable to save Z3950 record: $!\n";
245 print ZOUT $strRecords;
246 close(ZOUT);
247}
248
249
250sub run_command_with_output
251{
252 my ($self,$strCMD,$strStopRE) =@_;
253
254 $self->run_command($strCMD);
255
256 return $self->get_output($strStopRE);
257
258}
259
260sub get{
261 my ($self,$strShow,$numRecord) = @_;
262
263 $self->run_command($strShow);
264
265 my $strFullOutput="";
266 my $count=0;
267 my $readRecord = 0;
268
269 while (my $strLine = <YAZOUT>)
270 {
271
272 if ($strLine =~ m/Records: ([\d]*)/i ){
273 $readRecord = 1;
274 next;
275 }
276
277 return $strFullOutput if ($strLine =~ m/nextResultSetPosition|Not connected/i);
278
279 next if(!$readRecord);
280
281 $strFullOutput .= $strLine;
282 }
283
284}
285
286sub run_command_without_output
287{
288 my ($self) = shift (@_);
289 my ($strCMD) = @_;
290
291 $self->run_command($strCMD);
292}
293
294sub run_command
295{
296 my ($self,$strCMD) = @_;
297
298 my $input = $self->{'YAZIN'};
299
300 print $input "$strCMD\n";
301}
302
303sub get_output{
304 my ($self,$strStopRE) = @_;
305
306 if (!defined $strStopRE){return "";}
307 else
308 {
309 my $strFullOutput;
310 my $output = $self->{'YAZOUT'};
311 while (my $strLine = <$output>)
312 {
313 $strFullOutput .= $strLine;
314 if($strLine =~ m/^$strStopRE|Not connected/i){return $strFullOutput;}
315 }
316 }
317}
318
319sub generateFileName
320{
321 my ($self,$intMaxRecords) = @_;
322 my $strFileName = ($self->{'database'})."_".($self->{'find'})."_".($intMaxRecords);
323
324}
325
326sub dirFileSplit
327{
328 my ($self,$strFile) = @_;
329
330 my @aryDirs = split(/[\/\\]/,$strFile);
331
332 my $strLocalFile = pop(@aryDirs);
333 my $strSubDirs = join("/",@aryDirs);
334
335 return ($strSubDirs,$strLocalFile);
336}
337
338sub url_information
339{
340 my ($self,$url) = @_;
341
342 $url = $self->{'url'} unless defined $url;
343
344 my $yaz = $self->{'yaz'};
345
346 my $childpid = open2(*YAZOUT, *YAZIN, $yaz)
347 or die "can't open pipe to yaz-client: $!";
348
349 $self->{'YAZOUT'} = *YAZOUT;
350 $self->{'YAZIN'} = *YAZIN;
351
352 my $strOpen = $self->open_connection("open $url");
353
354 if (!$strOpen) {
355 print STDERR "Cannot connect to $url\n";
356 print STDERR "<<Finished>>\n";
357 return 0;
358 }
359
360
361 $strOpen = $self->run_command_with_output("open $url","^Options");
362
363
364 $strOpen =~ s/Z> //g;
365 $strOpen =~ s/Elapsed:.*//g;
366
367 print STDERR $strOpen;
368
369 print STDERR "<<Finished>>\n";
370
371 close(YAZOUT);
372 close(YAZIN);
373
374 return 0;
375
376}
377
378sub error
379{
380 my ($self,$strFunctionName,$strError) = @_;
381 {
382 print STDERR "Error occoured in Z3950Download.pm\n".
383 "In Function:".$strFunctionName."\n".
384 "Error Message:".$strError."\n";
385 exit(-1);
386 }
387}
388
3891;
Note: See TracBrowser for help on using the repository browser.