1 | #!/usr/bin/perl -w
|
---|
2 |
|
---|
3 | ###########################################################################
|
---|
4 | # submit_translation.pl
|
---|
5 | #
|
---|
6 | # A component of the Greenstone digital library software
|
---|
7 | # from the New Zealand Digital Library Project at the
|
---|
8 | # University of Waikato, New Zealand.
|
---|
9 | #
|
---|
10 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
11 | #
|
---|
12 | # This program is free software; you can redistribute it and/or modify
|
---|
13 | # it under the terms of the GNU General Public License as published by
|
---|
14 | # the Free Software Foundation; either version 2 of the License, or
|
---|
15 | # (at your option) any later version.
|
---|
16 | #
|
---|
17 | # This program is distributed in the hope that it will be useful,
|
---|
18 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | # GNU General Public License for more details.
|
---|
21 | #
|
---|
22 | # You should have received a copy of the GNU General Public License
|
---|
23 | # along with this program; if not, write to the Free Software
|
---|
24 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
25 | #
|
---|
26 | ###########################################################################
|
---|
27 |
|
---|
28 | #SYNOPSIS OF CODE:
|
---|
29 | #Processes translation when user clicks the 'SUBMIT TRANSLATION >>' button on
|
---|
30 | #greenstone site. Takes the cgiargsclass and the language being translated
|
---|
31 | #into, extracts the macronames and macrotext from the cgi data and hashes
|
---|
32 | #them, then links this hash to a database and updates database appropriately
|
---|
33 |
|
---|
34 | BEGIN {
|
---|
35 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
36 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
37 | }
|
---|
38 |
|
---|
39 | use parsargv;
|
---|
40 | use util;
|
---|
41 | use Cwd;
|
---|
42 | use File::Basename;
|
---|
43 | use GDBM_File;
|
---|
44 |
|
---|
45 | sub main
|
---|
46 | {
|
---|
47 | #gets the name of the args file with the cgiargsclass data in it
|
---|
48 | my $filename = shift(@_);
|
---|
49 | #gets the name of the language currently being translated from base language
|
---|
50 | my $language = shift(@_);
|
---|
51 |
|
---|
52 | my $baselanguage = shift(@_);
|
---|
53 |
|
---|
54 | #stores a hash of macronames with numbers still appended and their values
|
---|
55 | my %distinctkeys = ();
|
---|
56 | #stores an hash of macronames without numbers appended and the value
|
---|
57 | #being the full text of the macro
|
---|
58 | my %macronames = ();
|
---|
59 |
|
---|
60 | my $macrotext = "";
|
---|
61 | $number = 0;
|
---|
62 |
|
---|
63 | $langcode = &getlangcode($language);
|
---|
64 |
|
---|
65 | $language = "port" if ($language =~ m/portuguese/);
|
---|
66 | $language = "indo" if ($language =~ m/indonesian/);
|
---|
67 |
|
---|
68 | my ($macronumbered) = &parsebasefile($baselanguage);
|
---|
69 | my ($macronumbered2) = &parsebasefile($baselanguage . "2");
|
---|
70 | ($macronumbered) = &combine_hashes($macronumbered, $macronumbered2);
|
---|
71 |
|
---|
72 | my ($foreign) = &parseforeignfile($language);
|
---|
73 | my ($foreign2) = &parseforeignfile($language . "2");
|
---|
74 | ($foreign) = &combine_hashes($foreign, $foreign2);
|
---|
75 |
|
---|
76 | my ($macronames) = &parseusersubmission($filename);
|
---|
77 |
|
---|
78 | foreach $key (keys %macronames) {
|
---|
79 | if ($key =~ m/(_\w+_)/) {
|
---|
80 | $macronames{$key} = " [l=$langcode] {" . $macronames{$key} . "}\n";
|
---|
81 | $macro = $key;
|
---|
82 | $macro =~ s/.*:://;
|
---|
83 | if ($macronumbered{$macro}) {
|
---|
84 | $number = $macronumbered{$macro};
|
---|
85 | $foreign{$number} = $macro . $macronames{$key};
|
---|
86 | }
|
---|
87 | }
|
---|
88 | else {
|
---|
89 | $macronames{$key} =~ s/\<br\>/\n/g;
|
---|
90 | $macro = $key;
|
---|
91 | $macro =~ s/.*:://;
|
---|
92 | if ($macronumbered{$macro}) {
|
---|
93 | $number = $macronumbered{$macro};
|
---|
94 | $foreign{$number} = $macronames{$key};
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | $language2 = $language . "2";
|
---|
100 |
|
---|
101 | open MACROOUT, ">$ENV{'GSDLHOME'}/macros/$language.dm" or die "MURGH\n";
|
---|
102 |
|
---|
103 | foreach $ky (sort {$a <=> $b} keys %foreign) {
|
---|
104 | if ($foreign{$ky} =~ m/package\s*home/) {
|
---|
105 | close MACROOUT;
|
---|
106 | open MACROOUT, ">$ENV{'GSDLHOME'}/macros/$language2.dm" or die "MURGH\n";
|
---|
107 | }
|
---|
108 | print MACROOUT "$foreign{$ky}\n";
|
---|
109 |
|
---|
110 | }
|
---|
111 |
|
---|
112 | close MACROOUT;
|
---|
113 |
|
---|
114 | return 0;
|
---|
115 |
|
---|
116 | }
|
---|
117 |
|
---|
118 | &main(@ARGV);
|
---|
119 |
|
---|
120 | sub getlangcode {
|
---|
121 |
|
---|
122 | $language = shift (@_);
|
---|
123 |
|
---|
124 | open(LANGFILE, "<$ENV{'GSDLHOME'}/tmp/lang/package_forms/languages.log") or die "MURGH\n";
|
---|
125 |
|
---|
126 | while (<LANGFILE>) {
|
---|
127 | $lang = $_;
|
---|
128 | chomp($lang);
|
---|
129 | $langcode = <LANGFILE>;
|
---|
130 | chomp($langcode);
|
---|
131 | $langHASH{$lang} = $langcode;
|
---|
132 | }
|
---|
133 |
|
---|
134 | close LANGFILE;
|
---|
135 |
|
---|
136 | $langcode = $langHASH{$language};
|
---|
137 |
|
---|
138 | return ($langcode);
|
---|
139 | }
|
---|
140 |
|
---|
141 | sub parsebasefile {
|
---|
142 |
|
---|
143 | $file = shift(@_);
|
---|
144 |
|
---|
145 | open (BASEIN, "<$ENV{'GSDLHOME'}/macros/$file.dm") or die("MURGH\n");
|
---|
146 |
|
---|
147 | while (<BASEIN>) {
|
---|
148 |
|
---|
149 | $line = $_;
|
---|
150 | chomp($line);
|
---|
151 | $number++;
|
---|
152 |
|
---|
153 | if ($line =~ m/\Apackage /) {
|
---|
154 | $currpackage = $line;
|
---|
155 | $currpackage =~ s/package //;
|
---|
156 | $key = $currpackage . "::package";
|
---|
157 | $macronumbered{$key} = $number;
|
---|
158 | }
|
---|
159 | elsif ($line =~ m/\A\#\# .*/) {
|
---|
160 |
|
---|
161 | my @icon = split(/\s*\#\#\s*/, $line);
|
---|
162 |
|
---|
163 | $macro = $icon[(scalar @icon) - 1];
|
---|
164 |
|
---|
165 | $macronumbered{$macro} = $number;
|
---|
166 |
|
---|
167 | while ($line =~ m/\s+/) {
|
---|
168 | $line = <BASEIN>;
|
---|
169 | chomp($line);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | elsif ($line =~ m/\A(_\w+_)/) {
|
---|
173 | $line = $&;
|
---|
174 | $macronumbered{$line} = $number;
|
---|
175 | }
|
---|
176 | elsif ($line =~ m/\A(\#\stext\smacros)/) {
|
---|
177 | $key = $currpackage . "::textmacro";
|
---|
178 | $macronumbered{$key} = $number;
|
---|
179 | }
|
---|
180 | elsif ($line =~ m/\A(\#\sicons)/) {
|
---|
181 | $key = $currpackage . "::icons";
|
---|
182 | $macronumbered{$key} = $number;
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | close BASEIN;
|
---|
187 |
|
---|
188 | return (\%macronumbered);
|
---|
189 |
|
---|
190 | }
|
---|
191 |
|
---|
192 | sub parseforeignfile {
|
---|
193 |
|
---|
194 | $file = shift(@_);
|
---|
195 |
|
---|
196 | open (MACROIN, "<$ENV{'GSDLHOME'}/macros/$file.dm") or die("MURGH\n");
|
---|
197 |
|
---|
198 |
|
---|
199 | while (<MACROIN>) {
|
---|
200 |
|
---|
201 | $number = 0;
|
---|
202 |
|
---|
203 | if ($_ =~ m/\A(_\w+_)/) {
|
---|
204 |
|
---|
205 | $macro = $&;
|
---|
206 | $number = $macronumbered{$macro} if($macronumbered{$macro});
|
---|
207 | $line = $_;
|
---|
208 |
|
---|
209 | # while there is still text of the macro to go...
|
---|
210 | while ($_ !~ /.*\}/) {
|
---|
211 | # ... adds it to the macrotext variable
|
---|
212 | $_ = <MACROIN>;
|
---|
213 | $line .= $_;
|
---|
214 | }
|
---|
215 | if ($number != 0) {
|
---|
216 | $foreign{$number} = $line;
|
---|
217 | }
|
---|
218 | }
|
---|
219 | elsif ($_ =~ m/\A\#\# .*/) {
|
---|
220 | $line = $_;
|
---|
221 |
|
---|
222 | $line .= <MACROIN> unless ($line =~ m/^\#\# .*\#\#/);
|
---|
223 |
|
---|
224 | my @icon = split(/\s*\#\#\s*/, $line);
|
---|
225 |
|
---|
226 | $macro = $icon[(scalar @icon) - 1];
|
---|
227 |
|
---|
228 | $number = $macronumbered{$macro} if($macronumbered{$macro});
|
---|
229 | $currline = $line;
|
---|
230 |
|
---|
231 | while ($currline =~ m/\S+/) {
|
---|
232 | $currline = <MACROIN>;
|
---|
233 | $line .= $currline;
|
---|
234 | }
|
---|
235 | if ($number != 0) {
|
---|
236 | $foreign{$number} = $line;
|
---|
237 | }
|
---|
238 | }
|
---|
239 | else {
|
---|
240 | if ($_ =~ m/\S+/) {
|
---|
241 | $line = $_;
|
---|
242 | if ($line =~ m/\Apackage /) {
|
---|
243 | $currpackage = $line;
|
---|
244 | chomp($currpackage);
|
---|
245 | $currpackage =~ s/package //;
|
---|
246 | $key = $currpackage . "::package";
|
---|
247 | $number = $macronumbered{$key} if($macronumbered{$key});
|
---|
248 | $line = "###################################################################\n" . $line;
|
---|
249 | $line .= "###################################################################\n";
|
---|
250 | $foreign{$number} = $line;
|
---|
251 | }
|
---|
252 | elsif ($line =~ m/\A(\#\stext\smacros)/) {
|
---|
253 | $key = $currpackage . "::textmacro";
|
---|
254 | $number = $macronumbered{$key} if($macronumbered{$key});
|
---|
255 | $line = "#-------------------------------------------------------------------\n" . $line;
|
---|
256 | $line .= "#-------------------------------------------------------------------\n";
|
---|
257 | $foreign{$number} = $line;
|
---|
258 | }
|
---|
259 | elsif ($line =~ m/\A(\#\sicons)/) {
|
---|
260 | $key = $currpackage . "::icons";
|
---|
261 | $number = $macronumbered{$key} if($macronumbered{$key});
|
---|
262 | $line = "#-------------------------------------------------------------------\n" . $line;
|
---|
263 | $line .= "#-------------------------------------------------------------------\n";
|
---|
264 | $foreign{$number} = $line;
|
---|
265 | }
|
---|
266 | }
|
---|
267 | }
|
---|
268 | }
|
---|
269 | close MACROIN;
|
---|
270 |
|
---|
271 | return (\%foreign);
|
---|
272 |
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 | sub parseusersubmission {
|
---|
277 |
|
---|
278 | $filename = shift(@_);
|
---|
279 |
|
---|
280 | #opens the args file or kills program
|
---|
281 | open(ARGUMENTS, "<$filename") or die("Did not open $filename\n");
|
---|
282 | #while there are still lines left to read
|
---|
283 | while (<ARGUMENTS>) {
|
---|
284 | #gets the line
|
---|
285 | $line = $_;
|
---|
286 | #chomps off the final newline character
|
---|
287 | chomp($line);
|
---|
288 |
|
---|
289 | #if the line contains :: (browser converts to %3A%3A) then it has a macroname
|
---|
290 | if ($line =~ m/%3A%3A/) {
|
---|
291 | $macro = $line;
|
---|
292 | #changes the codes back to ::
|
---|
293 | $macro =~ s/%3A%3A/::/;
|
---|
294 | #removes value of macro leaving just the name
|
---|
295 | $macro =~ s/=.*//;
|
---|
296 | #saves the value
|
---|
297 | $value = $&;
|
---|
298 | #removes all " and = from value
|
---|
299 | $value =~ s/=//g;
|
---|
300 | $value =~ s/\A\"//;
|
---|
301 | $value =~ s/\"\Z//;
|
---|
302 | $value =~ s/<br>/\n/g;
|
---|
303 | #removes all " from macro
|
---|
304 | $macro =~ s/\"//g;
|
---|
305 | #removes any whitespace from macro
|
---|
306 | $macro =~ s/\s+//g;
|
---|
307 | #hashes the macro and it's value
|
---|
308 | if ($value =~ m/\S+/) {
|
---|
309 | $distinctkeys{$macro} = $value;
|
---|
310 | }
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | foreach $distinctkey (sort keys %distinctkeys) {
|
---|
315 |
|
---|
316 | $key = $distinctkey;
|
---|
317 | $key =~ s/\d+//;
|
---|
318 | $number = $&;
|
---|
319 |
|
---|
320 | $orderingHASH{$key}{$number} = $distinctkeys{$distinctkey};
|
---|
321 | }
|
---|
322 |
|
---|
323 | for $key (sort keys %orderingHASH) {
|
---|
324 |
|
---|
325 | for $number (sort {$a <=> $b} keys %{$orderingHASH{$key}}) {
|
---|
326 | $macrotext .= $orderingHASH{$key}{$number};
|
---|
327 |
|
---|
328 | }
|
---|
329 |
|
---|
330 | $macronames{$key} = $macrotext;
|
---|
331 | $macrotext = "";
|
---|
332 | }
|
---|
333 |
|
---|
334 | close ARGUMENTS;
|
---|
335 |
|
---|
336 | return (\%macronames);
|
---|
337 | }
|
---|
338 |
|
---|
339 | sub combine_hashes {
|
---|
340 |
|
---|
341 | my ($hash1, $hash2) = @_;
|
---|
342 | my %combined = ();
|
---|
343 |
|
---|
344 | foreach $key (keys %$hash1) {
|
---|
345 | $combined{$key} = $hash1->{$key};
|
---|
346 | }
|
---|
347 | foreach $key (keys %$hash2) {
|
---|
348 | $combined{$key} = $hash2->{$key};
|
---|
349 | }
|
---|
350 |
|
---|
351 | return (\%combined);
|
---|
352 |
|
---|
353 | }
|
---|
354 |
|
---|
355 |
|
---|
356 |
|
---|