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

Last change on this file since 12465 was 12465, checked in by shaoqun, 18 years ago

fixed th bugs on windows

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