1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
package GCUpdater;
###################################################
#
# Copyright 2005 Tian
#
# This file is part of GCstar.
#
# GCstar is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# GCstar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GCstar; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#
###################################################
use utf8;
use strict;
use LWP;
my $BASE_URL = 'http://www.gcstar.org/update/';
my $INDEX_FILE = 'list.php';
{
package GCRealUpdater;
use File::Basename;
use File::Path;
sub abort
{
my ($self, $msg) = @_;
print "$msg\n";
exit 1;
}
sub getNextFile
{
my $self = shift;
return $self->{filesList}->[$self->{next}];
}
sub createBrowser
{
my ($self, $proxy) = @_;
$self->{browser} = LWP::UserAgent->new;
$self->{browser}->proxy(['http'], $proxy) if $proxy;
}
sub checkFile
{
my ($self, $file) = @_;
return 1 if $self->{toBeUpdated}->{all};
foreach ('plugins', 'import', 'export', 'lang', 'models', 'extract')
{
return 1 if ($self->{toBeUpdated}->{$_}) && ($file =~ /$_/i);
}
return 0;
}
sub updateNext
{
my $self = shift;
my $file = $self->{filesList}->[$self->{next}];
print "Saving in ",$self->{baseInstallation}.$file,"\n";
mkpath(dirname($self->{baseInstallation}.$file));
my $response = $self->{browser}->get($self->{baseUrl}.$file, ':content_file' => $self->{baseInstallation}.$file);
if (!$response->is_success)
{
print $response->message, "\n";
#print $self->{lang}->{UpdateFileNotFound},"\n";
}
$self->{next}++;
}
sub getIndex
{
my $self = shift;
my $response = $self->{browser}->get($self->{baseUrl}.$INDEX_FILE, ':content_file' => $self->{baseInstallation}.$INDEX_FILE);
$self->abort($self->{lang}->{UpdateNone}) if !$response->is_success;
open INDEX, $self->{baseInstallation}.$INDEX_FILE;
$self->{filesList} = [];
while (<INDEX>)
{
chomp;
push @{$self->{filesList}}, $_
if $self->checkFile($_);
}
close INDEX;
$self->{total} = scalar @{$self->{filesList}};
}
sub total
{
my $self = shift;
$self->getIndex if ( !defined $self->{total});
return $self->{total};
}
sub new
{
my ($proto, $lang, $baseDir, $toBeUpdated, $version) = @_;
my $class = ref($proto) || $proto;
my $self = {
lang => $lang,
baseUrl => $BASE_URL.$version.'/',
baseInstallation => $baseDir.'/',
toBeUpdated => $toBeUpdated
};
bless ($self, $class);
$self->abort($self->{lang}->{UpdateNoPermission}.$baseDir) if (! -w $baseDir);
$self->{next} = 0;
$self->{total} = undef;
return $self;
}
}
{
package GCTextUpdater;
sub new
{
my ($proto, $lang, $baseDir, $toBeUpdated, $noProxy, $version) = @_;
my $class = ref($proto) || $proto;
my $self = {
lang => $lang,
noProxy => $noProxy,
updater => GCRealUpdater->new($lang, $baseDir, $toBeUpdated, $version)
};
bless ($self, $class);
return $self;
}
sub update
{
my $self = shift;
my $proxy;
if (!$self->{noProxy})
{
print $self->{lang}->{UpdateUseProxy};
$proxy = <STDIN>;
chomp $proxy;
}
$self->{updater}->createBrowser($proxy);
my $count = $self->{updater}->total;
print $self->{lang}->{UpdateNone},"\n" if !$count;
for (my $i = 0; $i < $count; $i++)
{
print $i+1," / $count : ",$self->{updater}->getNextFile,"\n";
$self->{updater}->updateNext;
}
}
}
1;
|