source: main/tags/2.40/gsdl/bin/script/fromsearch.pl@ 21085

Last change on this file since 21085 was 4068, checked in by mdewsnip, 21 years ago

Files specific to a translation process are now stored in a separate subdirectory of tmp/lang.

  • Property svn:keywords set to Author Date Id Revision
File size: 8.4 KB
Line 
1#!/usr/bin/perl
2
3###########################################################################
4#
5# fromsearch.pl
6#
7# A component of the Greenstone digital library software
8# from the New Zealand Digital Library Project at the
9# University of Waikato, New Zealand.
10#
11# Copyright (C) 1999 New Zealand Digital Library Project
12#
13# This program is free software; you can redistribute it and/or modify
14# it under the terms of the GNU General Public License as published by
15# the Free Software Foundation; either version 2 of the License, or
16# (at your option) any later version.
17#
18# This program is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21# GNU General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License
24# along with this program; if not, write to the Free Software
25# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26#
27###########################################################################
28
29# This code generates a single page version of the Greenstone Interface
30# Translation Agency when accessed from a search of the Macro Collection.
31# This allows users to correct mistakes made in translations.
32
33BEGIN {
34 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
35 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
36}
37
38use parsargv;
39use util;
40use Cwd;
41use File::Basename;
42use GDBM_File;
43use CGI;
44#require utf8;
45#use unicode;
46
47&main(@ARGV);
48
49sub main {
50
51 $macroname = shift(@_);
52 $language = shift(@_);
53 $baselanguage = shift(@_);
54
55 $dir = util::filename_cat($ENV{'GSDLHOME'},"tmp","lang","gdbmfiles");
56
57 #finds the macroname in the dbfiles
58 my $db1_blang = util::filename_cat($dir,$baselanguage.".db");
59 my $db1_lang = util::filename_cat($dir,language.".db");
60 my $db2_blang = util::filename_cat($dir,$baselanguage."2.db");
61 my $db2_lang = util::filename_cat($dir,language."2.db");
62
63 my ($formhash) = &db_db_diffs ($db1_blang, $db1_lang, $macroname);
64 my ($formhash2) = &db_db_diffs($db2_blang, $db2_lang, $macroname);
65
66 #puts results of above calls into one hash
67 $formhash = &combine_hashes($formhash,$formhash2);
68
69 #generates form using the one macro and macrotext from above
70 &generate_form($formhash, $language, $baselanguage);
71
72}
73#loops through each hash which is passed to it, appending each value
74#to a new third hash. Returns the third hash as result.
75sub combine_hashes {
76
77 my ($hash1, $hash2) = @_;
78 my %combined = ();
79
80 foreach $key (sort (keys %$hash1)) {
81 $combined{$key} = $hash1->{$key};
82 }
83 foreach $key (sort (keys %$hash2)) {
84 $combined{$key} = $hash2->{$key};
85 }
86
87 return (\%combined);
88
89}
90
91sub db_db_diffs
92{
93 my ($db_one, $db_two, $macroname) = @_;
94
95 my %db1hash = ();
96 my %db2hash = ();
97 my %months = ("Jan",1,"Feb",2,"Mar",3,"Apr",4,"May",5,"Jun",6,
98 "Jul",7,"Aug",8,"Sep",9,"Oct",10,"Nov",11,"Dec",12);
99
100 # this hash holds the database entries that are in
101 # english.db but not in $filename.db
102 my %diffhash = ();
103
104 #ties the hash to database so when you read from hash it fetches from external
105 #database and when you set hash it writes to external database
106 tie(%db1hash, "GDBM_File", $db_one, GDBM_READER, 0640);
107 tie(%db2hash, "GDBM_File", $db_two, GDBM_READER, 0640);
108
109 #the key is the name of the macro
110 foreach $key (sort keys(%db1hash)) {
111
112 #the macro has to be in one of the dbfiles so just want to keep scrolling
113 #through until we hit it...
114 if ($key eq $macroname) {
115 $foreigntext = $db2hash{$key};
116 if ($foreigntext =~ m/\#\# /) {
117 $foreigntext = $& if ($foreigntext =~ m/\"(.|^)*\"/);
118 }
119 else {
120 $foreigntext =~ s/^.*\n//;
121 $foreigntext =~ s/.*\{//;
122 $foreigntext =~ s/\}\Z//;
123 $foreigntext =~ s/\n\Z//;
124 }
125 #puts the macroname as a key with the array containing the macro text in
126 #the base language and in the translation language
127 $diffhash{$key} = [$db1hash{$key}, $foreigntext];
128 }
129 }
130 untie %db1hash;
131 untie %db2hash;
132
133 return \%diffhash;
134}
135
136sub generate_form
137{
138 my ($formhash) = shift(@_);
139 my $language = shift(@_);
140 my $baselanguage = shift(@_);
141 my $fs_fname = util::filename_cat($ENV{'GSDLHOME'},"tmp","lang",
142 "package_forms", "$lang", "fromsearch.lang");
143
144 open (FILE, ">$fs_fname") or die "Unable to open $fs_fname: $!\n";
145 # common gateway interface for writing the stuff to the web
146 my $query = new CGI;
147
148 #used to indicate whether or not content of table has been generated
149 my $processed = "false";
150
151 #prints table headings and passes hidden data for page
152 print FILE ($query->hidden(-name=>"hiddenlanguage",-default=>"$language"),
153 $query->hidden(-name=>"baselanguage",-default=>"$baselanguage"),
154 "</center><table cellpadding=\"10\">\n",
155 "<tr><td><strong><center>".uc ($baselanguage)."</strong></center></td>\n",
156 "<td><strong><center>". uc ($language)."</strong></center></td></tr>\n");
157
158
159 foreach $key (keys(%$formhash)) {
160
161 #changes to indicate hash did contain data and table content will be created
162 $processed = "true";
163
164 #gets the base language text from the hash
165 my $text = $formhash->{$key}[0];
166 my $keyname = $key;
167 $keyname =~ s/_/\\_/g;
168 # strip off the date
169 $text =~ s/.*//;
170 $text =~ s/(\[l=.*\])//;
171
172 # must take care of image macros single and multiple line ones
173 $text =~ s/(\<br\>\#\#.*\#\#\s*\<br\>).*/$1/g;
174
175 # format the text for displaying in the browser
176 $text =~ s/[\{\}]//g unless($text =~ m/\#\# /);
177 $text =~ s/(\A\n)//;
178 $text =~ s/(\n\Z)//;
179
180 #gets the translation language text from the hash
181 my $default = $formhash->{$key}[1];
182
183 # so that when it is an image only get the text to translate not coding stuff aswell
184 if ($text =~ m/\#\# (^|.)*/) {
185 if ($& =~ m/".*"/) {
186 #prints to file two text fields, the left contains the base language text
187 #and cannot be modified, the right contains the translation language text
188 #to be modified and submitted
189 print FILE ("<tr><td>",
190
191 $query->textfield(-name=>"whocares",
192 -default=>"$&",
193 -size=>50,
194 -readonly=>1),
195 "</td><td>",
196 $query->hidden(-name=>"$keyname",-default=>"$`"));
197 print FILE ($query->textfield(-name=>"$keyname",
198 -default=>"$default",
199 -size=>50));
200 print FILE ($query->hidden(-name=>"$keyname",-default=>"$'"),
201 "</td></tr>\n");
202 }
203 }
204 #else have a normal macro of form _this_
205 elsif ($text =~ m/\S+/) {
206 $text =~ s/\s+/ /g;
207
208 $text =~ s/<p>/<p>\n/g;
209 $text =~ s/<br>/<br>\n/g;
210 $text =~ s/<\/td>/<\/td>\n/g;
211
212 #see how long the macro text is to determine whether to use a text
213 #field or a text area
214 my @words = split(/ /, $text);
215 my $words = scalar(@words);
216 my $rows = sprintf("%.0f", $words/5);
217 print FILE ("<tr><td>\n");
218 #prints to file two text regions, the left contains the base language text
219 #and cannot be modified, the right contains the translation language text
220 #to be modified and submitted
221 if ($rows > 1) {
222 print FILE ($query->textarea(-name=>"whocares",
223 -rows=>$rows * 2,
224 -default=>"$text",
225 -columns=>50,
226 -readonly=>1),
227 "</td><td>\n",
228 $query->textarea(-name=>"$keyname",
229 -rows=>$rows * 2,
230 -default=>"$default",
231 -columns=>50),
232 "<br></td></tr>\n");
233 }
234 else {
235 print FILE ($query->textfield(-name=>"whocares",
236 -default=>"$text",
237 -size=>50,
238 -readonly=>1),
239 "</td><td>\n",
240 $query->textfield(-name=>"$keyname",
241 -default=>"$default",
242 -size=>50),
243 "<br></td></tr>\n");
244 }
245 }
246 }
247
248 #catch where hash is empty and print appropriate error message for user
249 if ($processed eq "false") {
250 print FILE ("<tr><td colspan=2 align=center>",
251 "An error occurred while processing your request\n<br>",
252 "</td></tr>")
253 }
254
255 #finishes table and adds a SUBMIT CHANGES option on the end
256 print FILE ("</table><br><center>\n",
257 "<table><tr><td align=left width=\"50%\">",
258 $query->reset("RESET FORM"),
259 "</td><td align=right width=\"50%\">",
260 $query->submit($pageno, "_textsubmit_"),
261 "</td></tr><tr><td width=\"50%\"></td><td align=right width=\"50%\">",
262 "<br> _textsubmittext_ \n",
263 "</td></tr></table>");
264
265 # adds the CGI area just written to the array of CGI areas
266 push (@queries, $query);
267
268 return $query;
269}
270
Note: See TracBrowser for help on using the repository browser.