source: trunk/gsdl/perllib/downloaders/WgetDownload.pm@ 11783

Last change on this file since 11783 was 11783, checked in by kjdon, 18 years ago

Jefferey's new download modules

  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1###########################################################################
2#
3# WgetDownload.pm -- Download base module that handles calling Wget
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) 2006 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 WgetDownload;
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 BasDownload;
35use strict;
36
37sub BEGIN {
38 @WgetDownload::ISA = ('BasDownload');
39}
40
41my $arguments =
42# [ { 'name' => "url",
43# 'desc' => "{WgetDownload.url}",
44# 'type' => "string",
45# 'deft' => "",
46# 'reqd' => "yes"},
47 [ { 'name' => "proxy_on",
48 'desc' => "{WgetDownload.proxy_on}",
49 'type' => "flag",
50 'reqd' => "no",
51 'hiddengli' => "yes"},
52 { 'name' => "proxy_host",
53 'desc' => "{WgetDownload.proxy_host}",
54 'type' => "string",
55 'reqd' => "no",
56 'hiddengli' => "yes"},
57 { 'name' => "proxy_port",
58 'desc' => "{WgetDownload.proxy_port}",
59 'type' => "string",
60 'reqd' => "no",
61 'hiddengli' => "yes"},
62 { 'name' => "user_name",
63 'desc' => "{WgetDownload.user_name}",
64 'type' => "string",
65 'reqd' => "no",
66 'hiddengli' => "yes"},
67 { 'name' => "user_password",
68 'desc' => "{WgetDownload.user_password}",
69 'type' => "string",
70 'reqd' => "no",
71 'hiddengli' => "yes"}];
72
73my $options = { 'name' => "WgetDownload",
74 'desc' => "{WgetDownload.desc}",
75 'abstract' => "yes",
76 'inherits' => "yes",
77 'args' => $arguments };
78
79
80sub new {
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 return bless $self, $class;
91}
92
93sub checkWgetSetup
94{
95 my ($self,$blnGliCall) = @_;
96 #TODO: proxy detection??
97
98 if((!$blnGliCall) && $self->{'proxy_on'})
99 {
100 &checkProxySetup($self);
101 }
102 &checkURL($self);
103}
104
105sub getWgetOptions
106{
107 my ($self) = @_;
108 my $strOptions = "";
109
110 if($self->{'proxy_on'} && $self->{'proxy_host'} && $self->{'proxy_port'}
111 && $self->{'user_name'} && $self->{'user_password'}
112 )
113 {
114 $strOptions = " -e httpproxy=$self->{'proxy_host'}:$self->{'proxy_port'} --proxy-user=$self->{'user_name'}"." --proxy-passwd=$self->{'user_password'} -Y on ";
115 }
116
117 return $strOptions;
118}
119
120# Checking for proxy setup: proxy server, proxy port, proxy username and password.
121sub checkProxySetup
122{
123 my ($self) = @_;
124 ($self->{'proxy_on'}) || &error("checkProxySetup","The proxy is not on? How could that be happening?");
125 # Setup .wgetrc by using $self->{'proxy_host'} and $self->{'proxy_port'}
126 # Test if the connection is succeful. If the connection wasn't succeful then ask user to supply username and password.
127
128
129 # TODO: How to test run if the proxy setup is working correctly??
130 # Use -spider to test whether the connection is working correctly.
131 # TODO: Ask user to supply username and password.
132 # Try to use the .wgetrc to setup the user name and password
133
134}
135
136sub useWget
137{
138 my ($cmdWget,$blnShow) = @_;
139
140 my $strReadIn = "";
141 my $strLine;
142
143 open (WIN,"$ENV{'GSDLHOME'}/packages/wget/wget-1.9/src/wget $cmdWget|") || die "wget request failed: $!\n";
144 while (defined($strLine=<WIN>))
145 {
146 if($blnShow)
147 {
148 print "$strReadIn\n";
149 }
150 $strReadIn .= $strLine;
151 }
152 close(WIN);
153
154 return $strReadIn;
155}
156
157# TODO: Check if the URL is valid?? Not sure what should be in this function yet!!
158sub checkURL
159{
160 my ($self) = @_;
161 if ($self->{'url'} eq "")
162 {
163 &error("checkURL","no URL is specified!! Please specifies the URL for downloading.");
164 }
165}
166
167sub error
168{
169 my ($strFunctionName,$strError) = @_;
170 {
171 print "Error occoured in WgetDownload.pm\n".
172 "In Function:".$strFunctionName."\n".
173 "Error Message:".$strError."\n";
174 exit(-1);
175 }
176}
177
1781;
Note: See TracBrowser for help on using the repository browser.