1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # util.pm -- various useful utilities
|
---|
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 |
|
---|
26 | package util;
|
---|
27 |
|
---|
28 | use strict;
|
---|
29 | use FileUtils;
|
---|
30 |
|
---|
31 | use Encode;
|
---|
32 | use File::Copy;
|
---|
33 | use File::Basename;
|
---|
34 | # Config for getting the perlpath in the recommended way, though it uses paths that are
|
---|
35 | # hard-coded into the Config file that's generated upon configuring and compiling perl.
|
---|
36 | # $^X works better in some cases to return the path to perl used to launch the script,
|
---|
37 | # but if launched with plain "perl" (no full-path), that will be just what it returns.
|
---|
38 | use Config;
|
---|
39 | # New module for file related utility functions - intended as a
|
---|
40 | # placeholder for an extension that allows a variety of different
|
---|
41 | # filesystems (FTP, HTTP, SAMBA, WEBDav, HDFS etc)
|
---|
42 | use FileUtils;
|
---|
43 |
|
---|
44 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
45 | require Win32; # for working out Windows Long Filenames from Win 8.3 short filenames
|
---|
46 | }
|
---|
47 |
|
---|
48 | # removes files (but not directories)
|
---|
49 | sub rm {
|
---|
50 | warnings::warnif("deprecated", "util::rm() is deprecated, using FileUtils::removeFiles() instead");
|
---|
51 | return &FileUtils::removeFiles(@_);
|
---|
52 | }
|
---|
53 |
|
---|
54 | # recursive removal
|
---|
55 | sub filtered_rm_r {
|
---|
56 | warnings::warnif("deprecated", "util::filtered_rm_r() is deprecated, using FileUtils::removeFilesFiltered() instead");
|
---|
57 | return &FileUtils::removeFilesFiltered(@_);
|
---|
58 | }
|
---|
59 |
|
---|
60 | # recursive removal
|
---|
61 | sub rm_r {
|
---|
62 | warnings::warnif("deprecated", "util::rm_r() is deprecated, using FileUtils::removeFilesRecursive() instead");
|
---|
63 | return &FileUtils::removeFilesRecursive(@_);
|
---|
64 | }
|
---|
65 |
|
---|
66 | # moves a file or a group of files
|
---|
67 | sub mv {
|
---|
68 | warnings::warnif("deprecated", "util::mv() is deprecated, using FileUtils::moveFiles() instead");
|
---|
69 | return &FileUtils::moveFiles(@_);
|
---|
70 | }
|
---|
71 |
|
---|
72 | # Move the contents of source directory into target directory
|
---|
73 | # (as opposed to merely replacing target dir with the src dir)
|
---|
74 | # This can overwrite any files with duplicate names in the target
|
---|
75 | # but other files and folders in the target will continue to exist
|
---|
76 | sub mv_dir_contents {
|
---|
77 | warnings::warnif("deprecated", "util::mv_dir_contents() is deprecated, using FileUtils::moveDirectoryContents() instead");
|
---|
78 | return &FileUtils::moveDirectoryContents(@_);
|
---|
79 | }
|
---|
80 |
|
---|
81 | # copies a file or a group of files
|
---|
82 | sub cp {
|
---|
83 | warnings::warnif("deprecated", "util::cp() is deprecated, using FileUtils::copyFiles() instead");
|
---|
84 | return &FileUtils::copyFiles(@_);
|
---|
85 | }
|
---|
86 |
|
---|
87 | # recursively copies a file or group of files
|
---|
88 | # syntax: cp_r (sourcefiles, destination directory)
|
---|
89 | # destination must be a directory - to copy one file to
|
---|
90 | # another use cp instead
|
---|
91 | sub cp_r {
|
---|
92 | warnings::warnif("deprecated", "util::cp_r() is deprecated, using FileUtils::copyFilesrecursive() instead");
|
---|
93 | return &FileUtils::copyFilesRecursive(@_);
|
---|
94 | }
|
---|
95 |
|
---|
96 | # recursively copies a file or group of files
|
---|
97 | # syntax: cp_r (sourcefiles, destination directory)
|
---|
98 | # destination must be a directory - to copy one file to
|
---|
99 | # another use cp instead
|
---|
100 | sub cp_r_nosvn {
|
---|
101 | warnings::warnif("deprecated", "util::cp_r_nosvn() is deprecated, using FileUtils::copyFilesRecursiveNoSVN() instead");
|
---|
102 | return &FileUtils::copyFilesRecursiveNoSVN(@_);
|
---|
103 | }
|
---|
104 |
|
---|
105 | # copies a directory and its contents, excluding subdirectories, into a new directory
|
---|
106 | sub cp_r_toplevel {
|
---|
107 | warnings::warnif("deprecated", "util::cp_r_toplevel() is deprecated, using FileUtils::recursiveCopyTopLevel() instead");
|
---|
108 | return &FileUtils::recursiveCopyTopLevel(@_);
|
---|
109 | }
|
---|
110 |
|
---|
111 | sub mk_dir {
|
---|
112 | warnings::warnif("deprecated", "util::mk_dir() is deprecated, using FileUtils::makeDirectory() instead");
|
---|
113 | return &FileUtils::makeDirectory(@_);
|
---|
114 | }
|
---|
115 |
|
---|
116 | # in case anyone cares - I did some testing (using perls Benchmark module)
|
---|
117 | # on this subroutine against File::Path::mkpath (). mk_all_dir() is apparently
|
---|
118 | # slightly faster (surprisingly) - Stefan.
|
---|
119 | sub mk_all_dir {
|
---|
120 | warnings::warnif("deprecated", "util::mk_all_dir() is deprecated, using FileUtils::makeAllDirectories() instead");
|
---|
121 | return &FileUtils::makeAllDirectories(@_);
|
---|
122 | }
|
---|
123 |
|
---|
124 | # make hard link to file if supported by OS, otherwise copy the file
|
---|
125 | sub hard_link {
|
---|
126 | warnings::warnif("deprecated", "util::hard_link() is deprecated, using FileUtils::hardLink() instead");
|
---|
127 | return &FileUtils::hardLink(@_);
|
---|
128 | }
|
---|
129 |
|
---|
130 | # make soft link to file if supported by OS, otherwise copy file
|
---|
131 | sub soft_link {
|
---|
132 | warnings::warnif("deprecated", "util::soft_link() is deprecated, using FileUtils::softLink() instead");
|
---|
133 | return &FileUtils::softLink(@_);
|
---|
134 | }
|
---|
135 |
|
---|
136 | # Primarily for filenames generated by processing
|
---|
137 | # content of HTML files (which are mapped to UTF-8 internally)
|
---|
138 | #
|
---|
139 | # To turn this into an octet string that really exists on the file
|
---|
140 | # system:
|
---|
141 | # 1. don't need to do anything special for Unix-based systems
|
---|
142 | # (as underlying file system is byte-code)
|
---|
143 | # 2. need to map to short DOS filenames for Windows
|
---|
144 |
|
---|
145 | sub utf8_to_real_filename
|
---|
146 | {
|
---|
147 | my ($utf8_filename) = @_;
|
---|
148 |
|
---|
149 | my $real_filename;
|
---|
150 |
|
---|
151 | if (($ENV{'GSDLOS'} =~ m/^windows$/i) && ($^O ne "cygwin")) {
|
---|
152 | require Win32;
|
---|
153 |
|
---|
154 | print STDERR "***** utf8 filename = $utf8_filename\n\n\n";
|
---|
155 |
|
---|
156 | my $unicode_filename = decode("utf8",$utf8_filename);
|
---|
157 | $real_filename = Win32::GetShortPathName($unicode_filename);
|
---|
158 | }
|
---|
159 | else {
|
---|
160 | $real_filename = $utf8_filename;
|
---|
161 | }
|
---|
162 |
|
---|
163 | return $real_filename;
|
---|
164 | }
|
---|
165 |
|
---|
166 | sub fd_exists {
|
---|
167 | warnings::warnif("deprecated", "util::fd_exists() is deprecated, using FileUtils::fileTest() instead");
|
---|
168 | return &FileUtils::fileTest(@_);
|
---|
169 | }
|
---|
170 |
|
---|
171 | sub file_exists {
|
---|
172 | warnings::warnif("deprecated", "util::file_exists() is deprecated, using FileUtils::fileExists() instead");
|
---|
173 | return &FileUtils::fileExists(@_);
|
---|
174 | }
|
---|
175 |
|
---|
176 | sub dir_exists {
|
---|
177 | warnings::warnif("deprecated", "util::dir_exists() is deprecated, using FileUtils::directoryExists() instead");
|
---|
178 | return &FileUtils::directoryExists(@_);
|
---|
179 | }
|
---|
180 |
|
---|
181 | # updates a copy of a directory in some other part of the filesystem
|
---|
182 | # verbosity settings are: 0=low, 1=normal, 2=high
|
---|
183 | # both $fromdir and $todir should be absolute paths
|
---|
184 | sub cachedir {
|
---|
185 | warnings::warnif("deprecated", "util::cachedir() is deprecated, using FileUtils::synchronizeDirectories() instead");
|
---|
186 | return &FileUtils::synchronizeDirectories(@_);
|
---|
187 | }
|
---|
188 |
|
---|
189 | # this function returns -1 if either file is not found
|
---|
190 | # assumes that $file1 and $file2 are absolute file names or
|
---|
191 | # in the current directory
|
---|
192 | # $file2 is allowed to be newer than $file1
|
---|
193 | sub differentfiles {
|
---|
194 | warnings::warnif("deprecated", "util::differentfiles() is deprecated, using FileUtils::differentFiles() instead");
|
---|
195 | return &FileUtils::differentFiles(@_);
|
---|
196 | }
|
---|
197 |
|
---|
198 |
|
---|
199 | sub get_tmp_filename
|
---|
200 | {
|
---|
201 | my $file_ext = shift(@_) || undef;
|
---|
202 |
|
---|
203 | my $opt_dot_file_ext = "";
|
---|
204 | if (defined $file_ext) {
|
---|
205 | if ($file_ext !~ m/\./) {
|
---|
206 | # no dot, so needs one added in at start
|
---|
207 | $opt_dot_file_ext = ".$file_ext"
|
---|
208 | }
|
---|
209 | else {
|
---|
210 | # allow for "extensions" such as _metadata.txt to be handled
|
---|
211 | # gracefully
|
---|
212 | $opt_dot_file_ext = $file_ext;
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | my $tmpdir = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "tmp");
|
---|
217 | &FileUtils::makeAllDirectories ($tmpdir) unless -e $tmpdir;
|
---|
218 |
|
---|
219 | my $count = 1000;
|
---|
220 | my $rand = int(rand $count);
|
---|
221 | my $full_tmp_filename = &FileUtils::filenameConcatenate($tmpdir, "F$rand$opt_dot_file_ext");
|
---|
222 |
|
---|
223 | while (-e $full_tmp_filename) {
|
---|
224 | $rand = int(rand $count);
|
---|
225 | $full_tmp_filename = &FileUtils::filenameConcatenate($tmpdir, "F$rand$opt_dot_file_ext");
|
---|
226 | $count++;
|
---|
227 | }
|
---|
228 |
|
---|
229 | return $full_tmp_filename;
|
---|
230 | }
|
---|
231 |
|
---|
232 | # These 2 are "static" variables used by the get_timestamped_tmp_folder() subroutine below and
|
---|
233 | # belong with that function. They help ensure the timestamped tmp folders generated are unique.
|
---|
234 | my $previous_timestamp = undef;
|
---|
235 | my $previous_timestamp_f = 0; # frequency
|
---|
236 |
|
---|
237 | sub get_timestamped_tmp_folder
|
---|
238 | {
|
---|
239 |
|
---|
240 | my $tmp_dirname;
|
---|
241 | if(defined $ENV{'GSDLCOLLECTDIR'}) {
|
---|
242 | $tmp_dirname = $ENV{'GSDLCOLLECTDIR'};
|
---|
243 | } elsif(defined $ENV{'GSDLHOME'}) {
|
---|
244 | $tmp_dirname = $ENV{'GSDLHOME'};
|
---|
245 | } else {
|
---|
246 | return undef;
|
---|
247 | }
|
---|
248 |
|
---|
249 | $tmp_dirname = &FileUtils::filenameConcatenate($tmp_dirname, "tmp");
|
---|
250 | &FileUtils::makeDirectory($tmp_dirname) if (!-e $tmp_dirname);
|
---|
251 |
|
---|
252 | # add the timestamp into the path otherwise we can run into problems
|
---|
253 | # if documents have the same name
|
---|
254 | my $timestamp = time;
|
---|
255 |
|
---|
256 | if (!defined $previous_timestamp || ($timestamp > $previous_timestamp)) {
|
---|
257 | $previous_timestamp_f = 0;
|
---|
258 | $previous_timestamp = $timestamp;
|
---|
259 | } else {
|
---|
260 | $previous_timestamp_f++;
|
---|
261 | }
|
---|
262 |
|
---|
263 | my $time_tmp_dirname = &FileUtils::filenameConcatenate($tmp_dirname, $timestamp);
|
---|
264 | $tmp_dirname = $time_tmp_dirname;
|
---|
265 | my $i = $previous_timestamp_f;
|
---|
266 |
|
---|
267 | if($previous_timestamp_f > 0) {
|
---|
268 | $tmp_dirname = $time_tmp_dirname."_".$i;
|
---|
269 | $i++;
|
---|
270 | }
|
---|
271 | while (-e $tmp_dirname) {
|
---|
272 | $tmp_dirname = $time_tmp_dirname."_".$i;
|
---|
273 | $i++;
|
---|
274 | }
|
---|
275 | &FileUtils::makeDirectory($tmp_dirname);
|
---|
276 |
|
---|
277 | return $tmp_dirname;
|
---|
278 | }
|
---|
279 |
|
---|
280 | sub get_timestamped_tmp_filename_in_collection
|
---|
281 | {
|
---|
282 |
|
---|
283 | my ($input_filename, $output_ext) = @_;
|
---|
284 | # derive tmp filename from input filename
|
---|
285 | my ($tailname, $dirname, $suffix)
|
---|
286 | = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
|
---|
287 |
|
---|
288 | # softlink to collection tmp dir
|
---|
289 | my $tmp_dirname = &util::get_timestamped_tmp_folder();
|
---|
290 | $tmp_dirname = $dirname unless defined $tmp_dirname;
|
---|
291 |
|
---|
292 | # following two steps copied from ConvertBinaryFile
|
---|
293 | # do we need them?? can't use them as is, as they use plugin methods.
|
---|
294 |
|
---|
295 | #$tailname = $self->SUPER::filepath_to_utf8($tailname) unless &unicode::check_is_utf8($tailname);
|
---|
296 |
|
---|
297 | # URLEncode this since htmls with images where the html filename is utf8 don't seem
|
---|
298 | # to work on Windows (IE or Firefox), as browsers are looking for filesystem-encoded
|
---|
299 | # files on the filesystem.
|
---|
300 | #$tailname = &util::rename_file($tailname, $self->{'file_rename_method'}, "without_suffix");
|
---|
301 | if (defined $output_ext) {
|
---|
302 | $output_ext = ".$output_ext"; # add the dot
|
---|
303 | } else {
|
---|
304 | $output_ext = $suffix;
|
---|
305 | }
|
---|
306 | $output_ext= lc($output_ext);
|
---|
307 | my $tmp_filename = &FileUtils::filenameConcatenate($tmp_dirname, "$tailname$output_ext");
|
---|
308 |
|
---|
309 | return $tmp_filename;
|
---|
310 | }
|
---|
311 |
|
---|
312 | sub get_toplevel_tmp_dir
|
---|
313 | {
|
---|
314 | return &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "tmp");
|
---|
315 | }
|
---|
316 |
|
---|
317 |
|
---|
318 | sub filename_to_regex {
|
---|
319 | my $filename = shift (@_);
|
---|
320 |
|
---|
321 | # need to make single backslashes double so that regex works
|
---|
322 | $filename =~ s/\\/\\\\/g; # if ($ENV{'GSDLOS'} =~ /^windows$/i);
|
---|
323 |
|
---|
324 | # note that the first part of a substitution is a regex, so RE chars need to be escaped,
|
---|
325 | # the second part of a substitution is not a regex, so for e.g. full-stop can be specified literally
|
---|
326 | $filename =~ s/\./\\./g; # in case there are extensions/other full stops, escape them
|
---|
327 | $filename =~ s@\(@\\(@g; # escape brackets
|
---|
328 | $filename =~ s@\)@\\)@g; # escape brackets
|
---|
329 | $filename =~ s@\[@\\[@g; # escape brackets
|
---|
330 | $filename =~ s@\]@\\]@g; # escape brackets
|
---|
331 |
|
---|
332 | return $filename;
|
---|
333 | }
|
---|
334 |
|
---|
335 | sub unregex_filename {
|
---|
336 | my $filename = shift (@_);
|
---|
337 |
|
---|
338 | # need to put doubled backslashes for regex back to single
|
---|
339 | $filename =~ s/\\\./\./g; # remove RE syntax for .
|
---|
340 | $filename =~ s@\\\(@(@g; # remove RE syntax for ( => "\(" turns into "("
|
---|
341 | $filename =~ s@\\\)@)@g; # remove RE syntax for ) => "\)" turns into ")"
|
---|
342 | $filename =~ s@\\\[@[@g; # remove RE syntax for [ => "\[" turns into "["
|
---|
343 | $filename =~ s@\\\]@]@g; # remove RE syntax for ] => "\]" turns into "]"
|
---|
344 |
|
---|
345 | # \\ goes to \
|
---|
346 | # This is the last step in reverse mirroring the order of steps in filename_to_regex()
|
---|
347 | $filename =~ s/\\\\/\\/g; # remove RE syntax for \
|
---|
348 | return $filename;
|
---|
349 | }
|
---|
350 |
|
---|
351 | sub filename_cat {
|
---|
352 | # I've disabled this warning for now, as every Greenstone perl
|
---|
353 | # script seems to make use of this function and so you drown in a
|
---|
354 | # sea of deprecated warnings [jmt12]
|
---|
355 | # warnings::warnif("deprecated", "util::filename_cat() is deprecated, using FileUtils::filenameConcatenate() instead");
|
---|
356 | return &FileUtils::filenameConcatenate(@_);
|
---|
357 | }
|
---|
358 |
|
---|
359 |
|
---|
360 | sub _pathname_cat {
|
---|
361 | my $join_char = shift(@_);
|
---|
362 | my $first_path = shift(@_);
|
---|
363 | my (@pathnames) = @_;
|
---|
364 |
|
---|
365 | # If first_path is not null or empty, then add it back into the list
|
---|
366 | if (defined $first_path && $first_path =~ /\S/) {
|
---|
367 | unshift(@pathnames, $first_path);
|
---|
368 | }
|
---|
369 |
|
---|
370 | my $pathname = join($join_char, @pathnames);
|
---|
371 |
|
---|
372 | # remove duplicate slashes
|
---|
373 | if ($join_char eq ";") {
|
---|
374 | $pathname =~ s/[\\\/]+/\\/g;
|
---|
375 | } else {
|
---|
376 | $pathname =~ s/[\/]+/\//g;
|
---|
377 | # DB: want a pathname abc\de.html to remain like this
|
---|
378 | }
|
---|
379 |
|
---|
380 | return $pathname;
|
---|
381 | }
|
---|
382 |
|
---|
383 |
|
---|
384 | sub pathname_cat {
|
---|
385 | my (@pathnames) = @_;
|
---|
386 |
|
---|
387 | my $join_char;
|
---|
388 | if (($ENV{'GSDLOS'} =~ /^windows$/i) && ($^O ne "cygwin")) {
|
---|
389 | $join_char = ";";
|
---|
390 | } else {
|
---|
391 | $join_char = ":";
|
---|
392 | }
|
---|
393 | return _pathname_cat($join_char,@pathnames);
|
---|
394 | }
|
---|
395 |
|
---|
396 |
|
---|
397 | sub javapathname_cat {
|
---|
398 | my (@pathnames) = @_;
|
---|
399 |
|
---|
400 | my $join_char;
|
---|
401 |
|
---|
402 | # Unlike pathname_cat() above, not interested if running in a Cygwin environment
|
---|
403 | # This is because the java we run is actually a native Windows executable
|
---|
404 |
|
---|
405 | if (($ENV{'GSDLOS'} =~ /^windows$/i)) {
|
---|
406 | $join_char = ";";
|
---|
407 | } else {
|
---|
408 | $join_char = ":";
|
---|
409 | }
|
---|
410 | return _pathname_cat($join_char,@pathnames);
|
---|
411 | }
|
---|
412 |
|
---|
413 |
|
---|
414 |
|
---|
415 | sub tidy_up_oid {
|
---|
416 | my ($OID) = @_;
|
---|
417 | if ($OID =~ /\./) {
|
---|
418 | print STDERR "Warning, identifier $OID contains periods (.), removing them\n";
|
---|
419 | $OID =~ s/\.//g; #remove any periods
|
---|
420 | }
|
---|
421 | if ($OID =~ /^\s.*\s$/) {
|
---|
422 | print STDERR "Warning, identifier $OID starts or ends with whitespace. Removing it\n";
|
---|
423 | # remove starting and trailing whitespace
|
---|
424 | $OID =~ s/^\s+//;
|
---|
425 | $OID =~ s/\s+$//;
|
---|
426 | }
|
---|
427 | if ($OID =~ /^[\d]*$/) {
|
---|
428 | print STDERR "Warning, identifier $OID contains only digits. Prepending 'D'.\n";
|
---|
429 | $OID = "D" . $OID;
|
---|
430 | }
|
---|
431 |
|
---|
432 | return $OID;
|
---|
433 | }
|
---|
434 |
|
---|
435 | sub envvar_prepend {
|
---|
436 | my ($var,$val) = @_;
|
---|
437 |
|
---|
438 | # 64 bit linux can't handle ";" as path separator, so make sure to set this to the right one for the OS
|
---|
439 | ## my $pathsep = (defined $ENV{'GSDLOS'} && $ENV{'GSDLOS'} !~ m/windows/) ? ":" : ";";
|
---|
440 |
|
---|
441 | # Rewritten above to make ":" the default (Windows is the special
|
---|
442 | # case, anything else 'unusual' such as Solaris etc is Unix)
|
---|
443 | my $pathsep = (defined $ENV{'GSDLOS'} && (($ENV{'GSDLOS'} =~ m/windows/) && ($^O ne "cygwin"))) ? ";" : ":";
|
---|
444 |
|
---|
445 | # do not prepend any value/path that's already in the environment variable
|
---|
446 |
|
---|
447 | my $escaped_val = &filename_to_regex($val); # escape any backslashes and brackets for upcoming regex
|
---|
448 | if (!defined($ENV{$var})) {
|
---|
449 | $ENV{$var} = "$val";
|
---|
450 | }
|
---|
451 | elsif($ENV{$var} !~ m/$escaped_val/) {
|
---|
452 | $ENV{$var} = "$val".$pathsep.$ENV{$var};
|
---|
453 | }
|
---|
454 | }
|
---|
455 |
|
---|
456 | sub envvar_append {
|
---|
457 | my ($var,$val) = @_;
|
---|
458 |
|
---|
459 | # 64 bit linux can't handle ";" as path separator, so make sure to set this to the right one for the OS
|
---|
460 | my $pathsep = (defined $ENV{'GSDLOS'} && $ENV{'GSDLOS'} !~ m/windows/) ? ":" : ";";
|
---|
461 |
|
---|
462 | # do not append any value/path that's already in the environment variable
|
---|
463 |
|
---|
464 | my $escaped_val = &filename_to_regex($val); # escape any backslashes and brackets for upcoming regex
|
---|
465 | if (!defined($ENV{$var})) {
|
---|
466 | $ENV{$var} = "$val";
|
---|
467 | }
|
---|
468 | elsif($ENV{$var} !~ m/$escaped_val/) {
|
---|
469 | $ENV{$var} = $ENV{$var}.$pathsep."$val";
|
---|
470 | }
|
---|
471 | }
|
---|
472 |
|
---|
473 |
|
---|
474 |
|
---|
475 | # splits a filename into a prefix and a tail extension using the tail_re, or
|
---|
476 | # if that fails, splits on the file_extension . (dot)
|
---|
477 | sub get_prefix_and_tail_by_regex {
|
---|
478 |
|
---|
479 | my ($filename,$tail_re) = @_;
|
---|
480 |
|
---|
481 | my ($file_prefix,$file_ext) = ($filename =~ m/^(.*?)($tail_re)$/);
|
---|
482 | if ((!defined $file_prefix) || (!defined $file_ext)) {
|
---|
483 | ($file_prefix,$file_ext) = ($filename =~ m/^(.*)(\..*?)$/);
|
---|
484 | }
|
---|
485 |
|
---|
486 | return ($file_prefix,$file_ext);
|
---|
487 | }
|
---|
488 |
|
---|
489 | # get full path and file only path from a base_dir (which may be empty) and
|
---|
490 | # file (which may contain directories)
|
---|
491 | sub get_full_filenames {
|
---|
492 | my ($base_dir, $file) = @_;
|
---|
493 |
|
---|
494 | # my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(0);
|
---|
495 | # my ($lcfilename) = ($cfilename =~ m/([^\\\/]*)$/);
|
---|
496 | # print STDERR "** Calling method: $lcfilename:$cline $cpackage->$csubr\n";
|
---|
497 |
|
---|
498 |
|
---|
499 | my $filename_full_path = $file;
|
---|
500 | # add on directory if present
|
---|
501 | $filename_full_path = &FileUtils::filenameConcatenate($base_dir, $file) if $base_dir =~ /\S/;
|
---|
502 |
|
---|
503 | my $filename_no_path = $file;
|
---|
504 |
|
---|
505 | # remove directory if present
|
---|
506 | $filename_no_path =~ s/^.*[\/\\]//;
|
---|
507 | return ($filename_full_path, $filename_no_path);
|
---|
508 | }
|
---|
509 |
|
---|
510 | # returns the path of a file without the filename -- ie. the directory the file is in
|
---|
511 | sub filename_head {
|
---|
512 | my $filename = shift(@_);
|
---|
513 |
|
---|
514 | if (($ENV{'GSDLOS'} =~ /^windows$/i) && ($^O ne "cygwin")) {
|
---|
515 | $filename =~ s/[^\\\\]*$//;
|
---|
516 | }
|
---|
517 | else {
|
---|
518 | $filename =~ s/[^\\\/]*$//;
|
---|
519 | }
|
---|
520 |
|
---|
521 | return $filename;
|
---|
522 | }
|
---|
523 |
|
---|
524 |
|
---|
525 |
|
---|
526 | # returns 1 if filename1 and filename2 point to the same
|
---|
527 | # file or directory
|
---|
528 | sub filenames_equal {
|
---|
529 | my ($filename1, $filename2) = @_;
|
---|
530 |
|
---|
531 | # use filename_cat to clean up trailing slashes and
|
---|
532 | # multiple slashes
|
---|
533 | $filename1 = &FileUtils::filenameConcatenate($filename1);
|
---|
534 | $filename2 = &FileUtils::filenameConcatenate($filename2);
|
---|
535 |
|
---|
536 | # filenames not case sensitive on windows
|
---|
537 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
538 | $filename1 =~ tr/[A-Z]/[a-z]/;
|
---|
539 | $filename2 =~ tr/[A-Z]/[a-z]/;
|
---|
540 | }
|
---|
541 | return 1 if $filename1 eq $filename2;
|
---|
542 | return 0;
|
---|
543 | }
|
---|
544 |
|
---|
545 | # If filename is relative to within_dir, returns the relative path of filename to that directory
|
---|
546 | # with slashes in the filename returned as they were in the original (absolute) filename.
|
---|
547 | sub filename_within_directory
|
---|
548 | {
|
---|
549 | my ($filename,$within_dir) = @_;
|
---|
550 |
|
---|
551 | if ($within_dir !~ m/[\/\\]$/) {
|
---|
552 | my $dirsep = &util::get_dirsep();
|
---|
553 | $within_dir .= $dirsep;
|
---|
554 | }
|
---|
555 |
|
---|
556 | $within_dir = &filename_to_regex($within_dir); # escape DOS style file separator and brackets
|
---|
557 | if ($filename =~ m/^$within_dir(.*)$/) {
|
---|
558 | $filename = $1;
|
---|
559 | }
|
---|
560 |
|
---|
561 | return $filename;
|
---|
562 | }
|
---|
563 |
|
---|
564 | # If filename is relative to within_dir, returns the relative path of filename to that directory in URL format.
|
---|
565 | # Filename and within_dir can be any type of slashes, but will be compared as URLs (i.e. unix-style slashes).
|
---|
566 | # The subpath returned will also be a URL type filename.
|
---|
567 | sub filename_within_directory_url_format
|
---|
568 | {
|
---|
569 | my ($filename,$within_dir) = @_;
|
---|
570 |
|
---|
571 | # convert parameters only to / slashes if Windows
|
---|
572 |
|
---|
573 | my $filename_urlformat = &filepath_to_url_format($filename);
|
---|
574 | my $within_dir_urlformat = &filepath_to_url_format($within_dir);
|
---|
575 |
|
---|
576 | #if ($within_dir_urlformat !~ m/\/$/) {
|
---|
577 | # make sure directory ends with a slash
|
---|
578 | #$within_dir_urlformat .= "/";
|
---|
579 | #}
|
---|
580 |
|
---|
581 | my $within_dir_urlformat_re = &filename_to_regex($within_dir_urlformat); # escape any special RE characters, such as brackets
|
---|
582 |
|
---|
583 | #print STDERR "@@@@@ $filename_urlformat =~ $within_dir_urlformat_re\n";
|
---|
584 |
|
---|
585 | # dir prefix may or may not end with a slash (this is discarded when extracting the sub-filepath)
|
---|
586 | if ($filename_urlformat =~ m/^$within_dir_urlformat_re(?:\/)*(.*)$/) {
|
---|
587 | $filename_urlformat = $1;
|
---|
588 | }
|
---|
589 |
|
---|
590 | return $filename_urlformat;
|
---|
591 | }
|
---|
592 |
|
---|
593 | # Convert parameter to use / slashes if Windows (if on Linux leave any \ as is,
|
---|
594 | # since on Linux it doesn't represent a file separator but an escape char).
|
---|
595 | sub filepath_to_url_format
|
---|
596 | {
|
---|
597 | my ($filepath) = @_;
|
---|
598 | if (($ENV{'GSDLOS'} =~ /^windows$/i) && ($^O ne "cygwin")) {
|
---|
599 | # Only need to worry about Windows, as Unix style directories already in url-format
|
---|
600 | # Convert Windows style \ => /
|
---|
601 | $filepath =~ s@\\@/@g;
|
---|
602 | }
|
---|
603 | return $filepath;
|
---|
604 | }
|
---|
605 |
|
---|
606 | # regex filepaths on windows may include \\ as path separator. Convert \\ to /
|
---|
607 | sub filepath_regex_to_url_format
|
---|
608 | {
|
---|
609 | my ($filepath) = @_;
|
---|
610 | if (($ENV{'GSDLOS'} =~ /^windows$/i) && ($^O ne "cygwin")) {
|
---|
611 | # Only need to worry about Windows, as Unix style directories already in url-format
|
---|
612 | # Convert Windows style \\ => /
|
---|
613 | $filepath =~ s@\\\\@/@g;
|
---|
614 | }
|
---|
615 | return $filepath;
|
---|
616 |
|
---|
617 | }
|
---|
618 |
|
---|
619 | # Like File::Basename::fileparse, but expects filepath in url format (ie only / slash for dirsep)
|
---|
620 | # and ignores trailing /
|
---|
621 | # returns (file, dirs) dirs will be empty if no subdirs
|
---|
622 | sub url_fileparse
|
---|
623 | {
|
---|
624 | my ($filepath) = @_;
|
---|
625 | # remove trailing /
|
---|
626 | $filepath =~ s@/$@@;
|
---|
627 | if ($filepath !~ m@/@) {
|
---|
628 | return ($filepath, "");
|
---|
629 | }
|
---|
630 | my ($dirs, $file) = $filepath =~ m@(.+/)([^/]+)@;
|
---|
631 | return ($file, $dirs);
|
---|
632 |
|
---|
633 | }
|
---|
634 |
|
---|
635 |
|
---|
636 | sub filename_within_collection
|
---|
637 | {
|
---|
638 | my ($filename) = @_;
|
---|
639 |
|
---|
640 | my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
|
---|
641 |
|
---|
642 | if (defined $collect_dir) {
|
---|
643 |
|
---|
644 | # if from within GSDLCOLLECTDIR, then remove directory prefix
|
---|
645 | # so source_filename is realative to it. This is done to aid
|
---|
646 | # portability, i.e. the collection can be moved to somewhere
|
---|
647 | # else on the file system and the archives directory will still
|
---|
648 | # work. This is needed, for example in the applet version of
|
---|
649 | # GLI where GSDLHOME/collect on the server will be different to
|
---|
650 | # the collect directory of the remove user. Of course,
|
---|
651 | # GSDLCOLLECTDIR subsequently needs to be put back on to turn
|
---|
652 | # it back into a full pathname.
|
---|
653 |
|
---|
654 | $filename = filename_within_directory($filename,$collect_dir);
|
---|
655 | }
|
---|
656 |
|
---|
657 | return $filename;
|
---|
658 | }
|
---|
659 |
|
---|
660 | sub prettyprint_file
|
---|
661 | {
|
---|
662 | my ($base_dir,$file,$gli) = @_;
|
---|
663 |
|
---|
664 | my $filename_full_path = &FileUtils::filenameConcatenate($base_dir,$file);
|
---|
665 |
|
---|
666 | if (($ENV{'GSDLOS'} =~ m/^windows$/i) && ($^O ne "cygwin")) {
|
---|
667 | require Win32;
|
---|
668 |
|
---|
669 | # For some reason base_dir in the form c:/a/b/c
|
---|
670 | # This leads to confusion later on, so turn it back into
|
---|
671 | # the more usual Windows form
|
---|
672 | $base_dir =~ s/\//\\/g;
|
---|
673 | my $long_base_dir = Win32::GetLongPathName($base_dir);
|
---|
674 | my $long_full_path = Win32::GetLongPathName($filename_full_path);
|
---|
675 |
|
---|
676 | $file = filename_within_directory($long_full_path,$long_base_dir);
|
---|
677 | $file = encode("utf8",$file) if ($gli);
|
---|
678 | }
|
---|
679 |
|
---|
680 | return $file;
|
---|
681 | }
|
---|
682 |
|
---|
683 |
|
---|
684 | sub upgrade_if_dos_filename
|
---|
685 | {
|
---|
686 | my ($filename_full_path,$and_encode) = @_;
|
---|
687 |
|
---|
688 | if (($ENV{'GSDLOS'} =~ m/^windows$/i) && ($^O ne "cygwin")) {
|
---|
689 | # Ensure any DOS-like filename, such as test~1.txt, has been upgraded
|
---|
690 | # to its long (Windows) version
|
---|
691 | my $long_filename = Win32::GetLongPathName($filename_full_path);
|
---|
692 | if (defined $long_filename) {
|
---|
693 | $filename_full_path = $long_filename;
|
---|
694 | }
|
---|
695 | # Make sure initial drive letter is lower-case (to fit in with rest of Greenstone)
|
---|
696 | $filename_full_path =~ s/^(.):/\u$1:/;
|
---|
697 | if ((defined $and_encode) && ($and_encode)) {
|
---|
698 | $filename_full_path = encode("utf8",$filename_full_path);
|
---|
699 | }
|
---|
700 | }
|
---|
701 |
|
---|
702 | return $filename_full_path;
|
---|
703 | }
|
---|
704 |
|
---|
705 |
|
---|
706 | sub downgrade_if_dos_filename
|
---|
707 | {
|
---|
708 | my ($filename_full_path) = @_;
|
---|
709 |
|
---|
710 | if (($ENV{'GSDLOS'} =~ m/^windows$/i) && ($^O ne "cygwin")) {
|
---|
711 | require Win32;
|
---|
712 |
|
---|
713 | # Ensure the given long Windows filename is in a form that can
|
---|
714 | # be opened by Perl => convert it to a short DOS-like filename
|
---|
715 |
|
---|
716 | my $short_filename = Win32::GetShortPathName($filename_full_path);
|
---|
717 | if (defined $short_filename) {
|
---|
718 | $filename_full_path = $short_filename;
|
---|
719 | }
|
---|
720 | # Make sure initial drive letter is lower-case (to fit in
|
---|
721 | # with rest of Greenstone)
|
---|
722 | $filename_full_path =~ s/^(.):/\u$1:/;
|
---|
723 | }
|
---|
724 |
|
---|
725 | return $filename_full_path;
|
---|
726 | }
|
---|
727 |
|
---|
728 | sub block_filename
|
---|
729 | {
|
---|
730 | my ($block_hash,$filename) = @_;
|
---|
731 |
|
---|
732 | if (($ENV{'GSDLOS'} =~ m/^windows$/) && ($^O ne "cygwin")) {
|
---|
733 |
|
---|
734 | # lower case the entire thing, eg for cover.jpg when its actually cover.JPG
|
---|
735 | my $lower_filename = lc($filename);
|
---|
736 | $block_hash->{'file_blocks'}->{$lower_filename} = 1;
|
---|
737 | # my $lower_drive = $filename;
|
---|
738 | # $lower_drive =~ s/^([A-Z]):/\l$1:/i;
|
---|
739 |
|
---|
740 | # my $upper_drive = $filename;
|
---|
741 | # $upper_drive =~ s/^([A-Z]):/\u$1:/i;
|
---|
742 | #
|
---|
743 | # $block_hash->{'file_blocks'}->{$lower_drive} = 1;
|
---|
744 | # $block_hash->{'file_blocks'}->{$upper_drive} = 1;
|
---|
745 | }
|
---|
746 | else {
|
---|
747 | $block_hash->{'file_blocks'}->{$filename} = 1;
|
---|
748 | }
|
---|
749 | }
|
---|
750 |
|
---|
751 |
|
---|
752 | sub filename_is_absolute
|
---|
753 | {
|
---|
754 | warnings::warnif("deprecated", "util::filename_is_absolute() is deprecated, using FileUtils::isFilenameAbsolute() instead");
|
---|
755 | return &FileUtils::isFilenameAbsolute(@_);
|
---|
756 | }
|
---|
757 |
|
---|
758 |
|
---|
759 | ## @method make_absolute()
|
---|
760 | #
|
---|
761 | # Ensure the given file path is absolute in respect to the given base path.
|
---|
762 | #
|
---|
763 | # @param $base_dir A string denoting the base path the given dir must be
|
---|
764 | # absolute to.
|
---|
765 | # @param $dir The directory to be made absolute as a string. Note that the
|
---|
766 | # dir may already be absolute, in which case it will remain
|
---|
767 | # unchanged.
|
---|
768 | # @return The now absolute form of the directory as a string.
|
---|
769 | #
|
---|
770 | # @author John Thompson, DL Consulting Ltd.
|
---|
771 | # @copy 2006 DL Consulting Ltd.
|
---|
772 | #
|
---|
773 | #used in buildcol.pl, doesn't work for all cases --kjdon
|
---|
774 | sub make_absolute {
|
---|
775 |
|
---|
776 | my ($base_dir, $dir) = @_;
|
---|
777 | ### print STDERR "dir = $dir\n";
|
---|
778 | $dir =~ s/[\\\/]+/\//g;
|
---|
779 | $dir = $base_dir . "/$dir" unless ($dir =~ m|^(\w:)?/|);
|
---|
780 | $dir =~ s|^/tmp_mnt||;
|
---|
781 | 1 while($dir =~ s|/[^/]*/\.\./|/|g);
|
---|
782 | $dir =~ s|/[.][.]?/|/|g;
|
---|
783 | $dir =~ tr|/|/|s;
|
---|
784 | ### print STDERR "dir = $dir\n";
|
---|
785 |
|
---|
786 | return $dir;
|
---|
787 | }
|
---|
788 | ## make_absolute() ##
|
---|
789 |
|
---|
790 | sub get_dirsep {
|
---|
791 |
|
---|
792 | if (($ENV{'GSDLOS'} =~ /^windows$/i) && ($^O ne "cygwin")) {
|
---|
793 | return "\\";
|
---|
794 | } else {
|
---|
795 | return "\/";
|
---|
796 | }
|
---|
797 | }
|
---|
798 |
|
---|
799 | sub get_os_dirsep {
|
---|
800 |
|
---|
801 | if (($ENV{'GSDLOS'} =~ /^windows$/i) && ($^O ne "cygwin")) {
|
---|
802 | return "\\\\";
|
---|
803 | } else {
|
---|
804 | return "\\\/";
|
---|
805 | }
|
---|
806 | }
|
---|
807 |
|
---|
808 | sub get_re_dirsep {
|
---|
809 |
|
---|
810 | return "\\\\|\\\/";
|
---|
811 | }
|
---|
812 |
|
---|
813 |
|
---|
814 | sub get_dirsep_tail {
|
---|
815 | my ($filename) = @_;
|
---|
816 |
|
---|
817 | # returns last part of directory or filename
|
---|
818 | # On unix e.g. a/b.d => b.d
|
---|
819 | # a/b/c => c
|
---|
820 |
|
---|
821 | my $dirsep = get_re_dirsep();
|
---|
822 | my @dirs = split (/$dirsep/, $filename);
|
---|
823 | my $tail = pop @dirs;
|
---|
824 |
|
---|
825 | # - caused problems under windows
|
---|
826 | #my ($tail) = ($filename =~ m/^(?:.*?$dirsep)?(.*?)$/);
|
---|
827 |
|
---|
828 | return $tail;
|
---|
829 | }
|
---|
830 |
|
---|
831 |
|
---|
832 | # if this is running on windows we want binaries to end in
|
---|
833 | # .exe, otherwise they don't have to end in any extension
|
---|
834 | sub get_os_exe {
|
---|
835 | return ".exe" if (($ENV{'GSDLOS'} =~ /^windows$/i) && ($^O ne "cygwin"));
|
---|
836 | return "";
|
---|
837 | }
|
---|
838 |
|
---|
839 |
|
---|
840 | # test to see whether this is a big or little endian machine
|
---|
841 | sub is_little_endian
|
---|
842 | {
|
---|
843 | # To determine the name of the operating system, the variable $^O is a cheap alternative to pulling it out of the Config module;
|
---|
844 | # If it is a Macintosh machine (i.e. the Darwin operating system), regardless if it's running on the IBM power-pc cpu or the x86 Intel-based chip with a power-pc emulator running on top of it, it's big-endian
|
---|
845 | # Otherwise, it's little endian
|
---|
846 |
|
---|
847 | #return 0 if $^O =~ /^darwin$/i;
|
---|
848 | #return 0 if $ENV{'GSDLOS'} =~ /^darwin$/i;
|
---|
849 |
|
---|
850 | # Going back to stating exactly whether the machine is little endian
|
---|
851 | # or big endian, without any special case for Macs. Since for rata it comes
|
---|
852 | # back with little endian and for shuttle with bigendian.
|
---|
853 | return (ord(substr(pack("s",1), 0, 1)) == 1);
|
---|
854 | }
|
---|
855 |
|
---|
856 |
|
---|
857 | # will return the collection name if successful, "" otherwise
|
---|
858 | sub use_collection {
|
---|
859 | my ($collection, $collectdir) = @_;
|
---|
860 |
|
---|
861 | if (!defined $collectdir || $collectdir eq "") {
|
---|
862 | $collectdir = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "collect");
|
---|
863 | }
|
---|
864 |
|
---|
865 | if (!defined $ENV{'GREENSTONEHOME'}) { # for GS3, would have been defined in use_site_collection, to GSDL3HOME
|
---|
866 | $ENV{'GREENSTONEHOME'} = $ENV{'GSDLHOME'};
|
---|
867 | }
|
---|
868 |
|
---|
869 | # get and check the collection
|
---|
870 | if (!defined($collection) || $collection eq "") {
|
---|
871 | if (defined $ENV{'GSDLCOLLECTION'}) {
|
---|
872 | $collection = $ENV{'GSDLCOLLECTION'};
|
---|
873 | } else {
|
---|
874 | print STDOUT "No collection specified\n";
|
---|
875 | return "";
|
---|
876 | }
|
---|
877 | }
|
---|
878 |
|
---|
879 | if ($collection eq "modelcol") {
|
---|
880 | print STDOUT "You can't use modelcol.\n";
|
---|
881 | return "";
|
---|
882 | }
|
---|
883 |
|
---|
884 | # make sure the environment variables GSDLCOLLECTION and GSDLCOLLECTDIR
|
---|
885 | # are defined
|
---|
886 | $ENV{'GSDLCOLLECTION'} = $collection;
|
---|
887 | $ENV{'GSDLCOLLECTHOME'} = $collectdir;
|
---|
888 | $ENV{'GSDLCOLLECTDIR'} = &FileUtils::filenameConcatenate($collectdir, $collection);
|
---|
889 |
|
---|
890 | # make sure this collection exists
|
---|
891 | if (!-e $ENV{'GSDLCOLLECTDIR'}) {
|
---|
892 | print STDOUT "Invalid collection ($collection).\n";
|
---|
893 | return "";
|
---|
894 | }
|
---|
895 |
|
---|
896 | # everything is ready to go
|
---|
897 | return $collection;
|
---|
898 | }
|
---|
899 |
|
---|
900 | sub get_current_collection_name {
|
---|
901 | return $ENV{'GSDLCOLLECTION'};
|
---|
902 | }
|
---|
903 |
|
---|
904 |
|
---|
905 | # will return the collection name if successful, "" otherwise.
|
---|
906 | # Like use_collection (above) but for greenstone 3 (taking account of site level)
|
---|
907 |
|
---|
908 | sub use_site_collection {
|
---|
909 | my ($site, $collection, $collectdir) = @_;
|
---|
910 |
|
---|
911 | if (!defined $collectdir || $collectdir eq "") {
|
---|
912 | die "GSDL3HOME not set.\n" unless defined $ENV{'GSDL3HOME'};
|
---|
913 | $collectdir = &FileUtils::filenameConcatenate($ENV{'GSDL3HOME'}, "sites", $site, "collect");
|
---|
914 | }
|
---|
915 |
|
---|
916 | if (defined $ENV{'GSDL3HOME'}) {
|
---|
917 | $ENV{'GREENSTONEHOME'} = $ENV{'GSDL3HOME'};
|
---|
918 | $ENV{'SITEHOME'} = &FileUtils::filenameConcatenate($ENV{'GREENSTONEHOME'}, "sites", $site);
|
---|
919 | } elsif (defined $ENV{'GSDL3SRCHOME'}) {
|
---|
920 | $ENV{'GREENSTONEHOME'} = &FileUtils::filenameConcatenate($ENV{'GSDL3SRCHOME'}, "web");
|
---|
921 | $ENV{'SITEHOME'} = &FileUtils::filenameConcatenate($ENV{'GREENSTONEHOME'}, "sites", $site);
|
---|
922 | } else {
|
---|
923 | print STDERR "*** util::use_site_collection(). Warning: Neither GSDL3HOME nor GSDL3SRCHOME set.\n";
|
---|
924 | }
|
---|
925 |
|
---|
926 | # collectdir explicitly set by this point (using $site variable if required).
|
---|
927 | # Can call "old" gsdl2 use_collection now.
|
---|
928 |
|
---|
929 | return use_collection($collection,$collectdir);
|
---|
930 | }
|
---|
931 |
|
---|
932 |
|
---|
933 |
|
---|
934 | sub locate_config_file
|
---|
935 | {
|
---|
936 | my ($file) = @_;
|
---|
937 |
|
---|
938 | my $locations = locate_config_files($file);
|
---|
939 |
|
---|
940 | return shift @$locations; # returns undef if 'locations' is empty
|
---|
941 | }
|
---|
942 |
|
---|
943 |
|
---|
944 | sub locate_config_files
|
---|
945 | {
|
---|
946 | my ($file) = @_;
|
---|
947 |
|
---|
948 | my @locations = ();
|
---|
949 |
|
---|
950 | if (-e $file) {
|
---|
951 | # Clearly specified (most likely full filename)
|
---|
952 | # No need to hunt in 'etc' directories, return value unchanged
|
---|
953 | push(@locations,$file);
|
---|
954 | }
|
---|
955 | else {
|
---|
956 | # Check for collection specific one before looking in global GSDL 'etc'
|
---|
957 | if (defined $ENV{'GSDLCOLLECTDIR'} && $ENV{'GSDLCOLLECTDIR'} ne "") {
|
---|
958 | my $test_collect_etc_filename
|
---|
959 | = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'},"etc", $file);
|
---|
960 |
|
---|
961 | if (-e $test_collect_etc_filename) {
|
---|
962 | push(@locations,$test_collect_etc_filename);
|
---|
963 | }
|
---|
964 | }
|
---|
965 | my $test_main_etc_filename
|
---|
966 | = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"etc", $file);
|
---|
967 | if (-e $test_main_etc_filename) {
|
---|
968 | push(@locations,$test_main_etc_filename);
|
---|
969 | }
|
---|
970 | }
|
---|
971 |
|
---|
972 | return \@locations;
|
---|
973 | }
|
---|
974 |
|
---|
975 |
|
---|
976 | sub hyperlink_text
|
---|
977 | {
|
---|
978 | my ($text) = @_;
|
---|
979 |
|
---|
980 | $text =~ s/(http:\/\/[^\s]+)/<a href=\"$1\">$1<\/a>/mg;
|
---|
981 | $text =~ s/(^|\s+)(www\.(\w|\.)+)/<a href=\"http:\/\/$2\">$2<\/a>/mg;
|
---|
982 |
|
---|
983 | return $text;
|
---|
984 | }
|
---|
985 |
|
---|
986 |
|
---|
987 | # A method to check if a directory is empty (note that an empty directory still has non-zero size!!!)
|
---|
988 | # Code is from http://episteme.arstechnica.com/eve/forums/a/tpc/f/6330927813/m/436007700831
|
---|
989 | sub is_dir_empty {
|
---|
990 | warnings::warnif("deprecated", "util::is_dir_empty() is deprecated, using FileUtils::isDirectoryEmpty() instead");
|
---|
991 | return &FileUtils::isDirectoryEmpty(@_);
|
---|
992 | }
|
---|
993 |
|
---|
994 | # Returns the given filename converted using either URL encoding or base64
|
---|
995 | # encoding, as specified by $rename_method. If the given filename has no suffix
|
---|
996 | # (if it is just the tailname), then $no_suffix should be some defined value.
|
---|
997 | # rename_method can be url, none, base64
|
---|
998 | sub rename_file {
|
---|
999 | my ($filename, $rename_method, $no_suffix) = @_;
|
---|
1000 |
|
---|
1001 | if(!$filename) { # undefined or empty string
|
---|
1002 | return $filename;
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | if (!$rename_method) {
|
---|
1006 | print STDERR "WARNING: no file renaming method specified. Defaulting to using URL encoding...\n";
|
---|
1007 | # Debugging information
|
---|
1008 | # my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(1);
|
---|
1009 | # print STDERR "Called from method: $cfilename:$cline $cpackage->$csubr\n";
|
---|
1010 | $rename_method = "url";
|
---|
1011 | } elsif($rename_method eq "none") {
|
---|
1012 | return $filename; # would have already been renamed
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | # No longer replace spaces with underscores, since underscores mess with incremental rebuild
|
---|
1016 | ### Replace spaces with underscore. Do this first else it can go wrong below when getting tailname
|
---|
1017 | ###$filename =~ s/ /_/g;
|
---|
1018 |
|
---|
1019 | my ($tailname,$dirname,$suffix);
|
---|
1020 | if($no_suffix) { # given a tailname, no suffix
|
---|
1021 | ($tailname,$dirname) = File::Basename::fileparse($filename);
|
---|
1022 | }
|
---|
1023 | else {
|
---|
1024 | ($tailname,$dirname,$suffix) = File::Basename::fileparse($filename, "\\.(?:[^\\.]+?)\$");
|
---|
1025 | }
|
---|
1026 | if (!$suffix) {
|
---|
1027 | $suffix = "";
|
---|
1028 | }
|
---|
1029 | # This breaks GLI matching extracted metadata to files in Enrich panel, as
|
---|
1030 | # original is eg .JPG while gsdlsourcefilename ends up .jpg
|
---|
1031 | # Not sure why it was done in first place...
|
---|
1032 | #else {
|
---|
1033 | #$suffix = lc($suffix);
|
---|
1034 | #}
|
---|
1035 |
|
---|
1036 | if ($rename_method eq "url") {
|
---|
1037 | $tailname = &unicode::url_encode($tailname);
|
---|
1038 | }
|
---|
1039 | elsif ($rename_method eq "base64") {
|
---|
1040 | $tailname = &unicode::base64_encode($tailname);
|
---|
1041 | $tailname =~ s/\s*//sg; # for some reason it adds spaces not just at end but also in middle
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | $filename = "$tailname$suffix";
|
---|
1045 | $filename = "$dirname$filename" if ($dirname ne "./" && $dirname ne ".\\");
|
---|
1046 |
|
---|
1047 | return $filename;
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 |
|
---|
1051 | # BACKWARDS COMPATIBILITY: Just in case there are old .ldb/.bdb files
|
---|
1052 | sub rename_ldb_or_bdb_file {
|
---|
1053 | my ($filename_no_ext) = @_;
|
---|
1054 |
|
---|
1055 | my $new_filename = "$filename_no_ext.gdb";
|
---|
1056 | return if (-f $new_filename); # if the file has the right extension, don't need to do anything
|
---|
1057 | # try ldb
|
---|
1058 | my $old_filename = "$filename_no_ext.ldb";
|
---|
1059 |
|
---|
1060 | if (-f $old_filename) {
|
---|
1061 | print STDERR "Renaming $old_filename to $new_filename\n";
|
---|
1062 | rename ($old_filename, $new_filename)
|
---|
1063 | || print STDERR "Rename failed: $!\n";
|
---|
1064 | return;
|
---|
1065 | }
|
---|
1066 | # try bdb
|
---|
1067 | $old_filename = "$filename_no_ext.bdb";
|
---|
1068 | if (-f $old_filename) {
|
---|
1069 | print STDERR "Renaming $old_filename to $new_filename\n";
|
---|
1070 | rename ($old_filename, $new_filename)
|
---|
1071 | || print STDERR "Rename failed: $!\n";
|
---|
1072 | return;
|
---|
1073 | }
|
---|
1074 | }
|
---|
1075 |
|
---|
1076 | sub os_dir() {
|
---|
1077 |
|
---|
1078 | my $gsdlarch = "";
|
---|
1079 | if(defined $ENV{'GSDLARCH'}) {
|
---|
1080 | $gsdlarch = $ENV{'GSDLARCH'};
|
---|
1081 | }
|
---|
1082 | return $ENV{'GSDLOS'}.$gsdlarch;
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | # Returns the greenstone URL prefix extracted from the appropriate GS2/GS3 config file.
|
---|
1086 | # By default, /greenstone3 for GS3 or /greenstone for GS2.
|
---|
1087 | sub get_greenstone_url_prefix() {
|
---|
1088 | # if already set on a previous occasion, just return that
|
---|
1089 | # (Don't want to keep repeating this: cost of re-opening and scanning files.)
|
---|
1090 | return $ENV{'GREENSTONE_URL_PREFIX'} if($ENV{'GREENSTONE_URL_PREFIX'});
|
---|
1091 |
|
---|
1092 | my ($configfile, $urlprefix, $defaultUrlprefix);
|
---|
1093 | my @propertynames = ();
|
---|
1094 |
|
---|
1095 | if($ENV{'GSDL3SRCHOME'}) {
|
---|
1096 | $defaultUrlprefix = "/greenstone3";
|
---|
1097 | $configfile = &FileUtils::filenameConcatenate($ENV{'GSDL3SRCHOME'}, "packages", "tomcat", "conf", "Catalina", "localhost", "greenstone3.xml");
|
---|
1098 | push(@propertynames, qw/path\s*\=/);
|
---|
1099 | } else {
|
---|
1100 | $defaultUrlprefix = "/greenstone";
|
---|
1101 | $configfile = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "cgi-bin", &os_dir(), "gsdlsite.cfg");
|
---|
1102 | push(@propertynames, (qw/\nhttpprefix/, qw/\ngwcgi/)); # inspect one property then the other
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | $urlprefix = &extract_propvalue_from_file($configfile, \@propertynames);
|
---|
1106 |
|
---|
1107 | if(!$urlprefix) { # no values found for URL prefix, use default values
|
---|
1108 | $urlprefix = $defaultUrlprefix;
|
---|
1109 | } else {
|
---|
1110 | #gwcgi can contain more than the wanted prefix, we split on / to get the first "directory" level
|
---|
1111 | $urlprefix =~ s/^\///; # remove the starting slash
|
---|
1112 | my @dirs = split(/(\\|\/)/, $urlprefix);
|
---|
1113 | $urlprefix = shift(@dirs);
|
---|
1114 |
|
---|
1115 | if($urlprefix !~ m/^\//) { # in all cases: ensure the required forward slash is at the front
|
---|
1116 | $urlprefix = "/$urlprefix";
|
---|
1117 | }
|
---|
1118 | }
|
---|
1119 |
|
---|
1120 | # set for the future
|
---|
1121 | $ENV{'GREENSTONE_URL_PREFIX'} = $urlprefix;
|
---|
1122 | # print STDERR "*** in get_greenstone_url_prefix(): $urlprefix\n\n";
|
---|
1123 | return $urlprefix;
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 |
|
---|
1127 | # Given a config file (xml or java properties file) and a list/array of regular expressions
|
---|
1128 | # that represent property names to match on, this function will return the value for the 1st
|
---|
1129 | # matching property name. If the return value is undefined, no matching property was found.
|
---|
1130 | sub extract_propvalue_from_file() {
|
---|
1131 | my ($configfile, $propertynames) = @_;
|
---|
1132 |
|
---|
1133 | my $value;
|
---|
1134 | unless(open(FIN, "<$configfile")) {
|
---|
1135 | print STDERR "extract_propvalue_from_file(): Unable to open $configfile. $!\n";
|
---|
1136 | return $value; # not initialised
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | # Read the entire file at once, as one single line, then close it
|
---|
1140 | my $filecontents;
|
---|
1141 | {
|
---|
1142 | local $/ = undef;
|
---|
1143 | $filecontents = <FIN>;
|
---|
1144 | }
|
---|
1145 | close(FIN);
|
---|
1146 |
|
---|
1147 | foreach my $regex (@$propertynames) {
|
---|
1148 | ($value) = $filecontents=~ m/$regex\s*(\S*)/s; # read value of the property given by regex up to the 1st space
|
---|
1149 | if($value) {
|
---|
1150 | $value =~ s/^\"//; # remove any startquotes
|
---|
1151 | $value =~ s/\".*$//; # remove the 1st endquotes (if any) followed by any xml
|
---|
1152 | last; # found value for a matching property, break from loop
|
---|
1153 | }
|
---|
1154 | }
|
---|
1155 |
|
---|
1156 | return $value;
|
---|
1157 | }
|
---|
1158 |
|
---|
1159 | # Subroutine that sources setup.bash, given GSDLHOME and GSDLOS and
|
---|
1160 | # given that perllib is in @INC in order to invoke this subroutine.
|
---|
1161 | # Call as follows -- after setting up INC to include perllib and
|
---|
1162 | # after setting up GSDLHOME and GSDLOS:
|
---|
1163 | #
|
---|
1164 | # require util;
|
---|
1165 | # &util::setup_greenstone_env($ENV{'GSDLHOME'}, $ENV{'GSDLOS'});
|
---|
1166 | #
|
---|
1167 | sub setup_greenstone_env() {
|
---|
1168 | my ($GSDLHOME, $GSDLOS) = @_;
|
---|
1169 |
|
---|
1170 | #my %env_map = ();
|
---|
1171 | # Get the localised ENV settings of running a localised source setup.bash
|
---|
1172 | # and put it into the ENV here. Need to clear GSDLHOME before running setup
|
---|
1173 | #my $perl_command = "(cd $GSDLHOME; export GSDLHOME=; . ./setup.bash > /dev/null; env)";
|
---|
1174 | my $perl_command = "(cd $GSDLHOME; /bin/bash -c \"export GSDLHOME=; source setup.bash > /dev/null; env\")";
|
---|
1175 | if (($GSDLOS =~ m/windows/i) && ($^O ne "cygwin")) {
|
---|
1176 | #$perl_command = "cmd /C \"cd $GSDLHOME&& set GSDLHOME=&& setup.bat > nul&& set\"";
|
---|
1177 | $perl_command = "(cd $GSDLHOME&& set GSDLHOME=&& setup.bat > nul&& set)";
|
---|
1178 | }
|
---|
1179 | if (!open(PIN, "$perl_command |")) {
|
---|
1180 | print STDERR ("Unable to execute command: $perl_command. $!\n");
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | while (defined (my $perl_output_line = <PIN>)) {
|
---|
1184 | my($key,$value) = ($perl_output_line =~ m/^([^=]*)[=](.*)$/);
|
---|
1185 | #$env_map{$key}=$value;
|
---|
1186 | $ENV{$key}=$value;
|
---|
1187 | }
|
---|
1188 | close (PIN);
|
---|
1189 |
|
---|
1190 | # If any keys in $ENV don't occur in Greenstone's localised env
|
---|
1191 | # (stored in $env_map), delete those entries from $ENV
|
---|
1192 | #foreach $key (keys %ENV) {
|
---|
1193 | # if(!defined $env_map{$key}) {
|
---|
1194 | # print STDOUT "**** DELETING ENV KEY: $key\tVALUE: $ENV{'$key'}\n";
|
---|
1195 | # delete $ENV{$key}; # del $ENV(key, value) pair
|
---|
1196 | # }
|
---|
1197 | #}
|
---|
1198 | #undef %env_map;
|
---|
1199 | }
|
---|
1200 |
|
---|
1201 | sub get_perl_exec() {
|
---|
1202 | my $perl_exec = $^X; # may return just "perl"
|
---|
1203 |
|
---|
1204 | if($ENV{'PERLPATH'}) {
|
---|
1205 | # OR: # $perl_exec = &FileUtils::filenameConcatenate($ENV{'PERLPATH'},"perl");
|
---|
1206 | if (($ENV{'GSDLOS'} =~ m/windows/) && ($^O ne "cygwin")) {
|
---|
1207 | $perl_exec = "$ENV{'PERLPATH'}\\Perl.exe";
|
---|
1208 | } else {
|
---|
1209 | $perl_exec = "$ENV{'PERLPATH'}/perl";
|
---|
1210 | }
|
---|
1211 | } else { # no PERLPATH, use Config{perlpath} else $^X: special variables
|
---|
1212 | # containing the full path to the current perl executable we're using
|
---|
1213 | $perl_exec = $Config{perlpath}; # configured path for perl
|
---|
1214 | if (!-e $perl_exec) { # may not point to location on this machine
|
---|
1215 | $perl_exec = $^X; # may return just "perl"
|
---|
1216 | if($perl_exec =~ m/^perl/i) { # warn if just perl or Perl.exe
|
---|
1217 | print STDERR "**** WARNING: Perl exec found contains no path: $perl_exec";
|
---|
1218 | }
|
---|
1219 | }
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 | return $perl_exec;
|
---|
1223 | }
|
---|
1224 |
|
---|
1225 | # returns the path to the java command in the JRE included with GS (if any),
|
---|
1226 | # quoted to safeguard any spaces in this path, otherwise a simple java
|
---|
1227 | # command is returned which assumes and will try for a system java.
|
---|
1228 | sub get_java_command {
|
---|
1229 | my $java = "java";
|
---|
1230 | if(defined $ENV{'GSDLHOME'}) { # should be, as this script would be launched from the cmd line
|
---|
1231 | # after running setup.bat or from GLI which also runs setup.bat
|
---|
1232 | my $java_bin = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"packages","jre","bin");
|
---|
1233 | if(-d $java_bin) {
|
---|
1234 | $java = &FileUtils::filenameConcatenate($java_bin,"java");
|
---|
1235 | $java = "\"".$java."\""; # quoted to preserve spaces in path
|
---|
1236 | }
|
---|
1237 | }
|
---|
1238 | return $java;
|
---|
1239 | }
|
---|
1240 |
|
---|
1241 |
|
---|
1242 | # Given the qualified collection name (colgroup/collection),
|
---|
1243 | # returns the collection and colgroup parts
|
---|
1244 | sub get_collection_parts {
|
---|
1245 | # http://perldoc.perl.org/File/Basename.html
|
---|
1246 | # my($filename, $directories, $suffix) = fileparse($path);
|
---|
1247 | # "$directories contains everything up to and including the last directory separator in the $path
|
---|
1248 | # including the volume (if applicable). The remainder of the $path is the $filename."
|
---|
1249 | #my ($collection, $colgroup) = &File::Basename::fileparse($qualified_collection);
|
---|
1250 |
|
---|
1251 | my $qualified_collection = shift(@_);
|
---|
1252 |
|
---|
1253 | # Since activate.pl can be launched from the command-line, including by a user,
|
---|
1254 | # best not to assume colgroup uses URL-style slashes as would be the case with GLI
|
---|
1255 | # Also allow for the accidental inclusion of multiple slashes
|
---|
1256 | my ($colgroup, $collection) = split(/[\/\\]+/, $qualified_collection); #split('/', $qualified_collection);
|
---|
1257 |
|
---|
1258 | if(!defined $collection) {
|
---|
1259 | $collection = $colgroup;
|
---|
1260 | $colgroup = "";
|
---|
1261 | }
|
---|
1262 | return ($collection, $colgroup);
|
---|
1263 | }
|
---|
1264 |
|
---|
1265 | # work out the "collectdir/collection" location
|
---|
1266 | sub resolve_collection_dir {
|
---|
1267 | my ($collect_dir, $qualified_collection, $site) = @_; #, $gs_mode
|
---|
1268 |
|
---|
1269 | if (defined $ENV{'GSDLCOLLECTDIR'}) { # a predefined collection dir exists
|
---|
1270 | return $ENV{'GSDLCOLLECTDIR'};
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | my ($colgroup, $collection) = &util::get_collection_parts($qualified_collection);
|
---|
1274 |
|
---|
1275 | if (!defined $collect_dir || !$collect_dir) { # if undefined or empty string
|
---|
1276 | $collect_dir = &util::get_working_collect_dir($site);
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | return &FileUtils::filenameConcatenate($collect_dir,$colgroup,$collection);
|
---|
1280 | }
|
---|
1281 |
|
---|
1282 | # work out the full path to "collect" of this greenstone 2/3 installation
|
---|
1283 | sub get_working_collect_dir {
|
---|
1284 | my ($site) = @_;
|
---|
1285 |
|
---|
1286 | if (defined $ENV{'GSDLCOLLECTHOME'}) { # a predefined collect dir exists
|
---|
1287 | return $ENV{'GSDLCOLLECTHOME'};
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | if (defined $site && $site) { # site non-empty, so get default collect dir for GS3
|
---|
1291 |
|
---|
1292 | if (defined $ENV{'GSDL3HOME'}) {
|
---|
1293 | return &FileUtils::filenameConcatenate($ENV{'GSDL3HOME'},"sites",$site,"collect"); # web folder
|
---|
1294 | }
|
---|
1295 | elsif (defined $ENV{'GSDL3SRCHOME'}) {
|
---|
1296 | return &FileUtils::filenameConcatenate($ENV{'GSDL3SRCHOME'},"web","sites",$site,"collect");
|
---|
1297 | }
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 | elsif (defined $ENV{'SITEHOME'}) {
|
---|
1301 | return &FileUtils::filenameConcatenate($ENV{'SITEHOME'},"collect");
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | else { # get default collect dir for GS2
|
---|
1305 | return &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"collect");
|
---|
1306 | }
|
---|
1307 | }
|
---|
1308 |
|
---|
1309 | sub is_abs_path_any_os {
|
---|
1310 | my ($path) = @_;
|
---|
1311 |
|
---|
1312 | # We can have filenames in our DBs that were produced on other OS, so this method exists
|
---|
1313 | # to help identify absolute paths in such cases.
|
---|
1314 |
|
---|
1315 | return 1 if($path =~ m@^/@); # full paths begin with forward slash on linux/mac
|
---|
1316 | return 1 if($path =~ m@^([a-zA-Z]\:|\\)@); # full paths begin with drive letter colon for Win or \ for volume, http://stackoverflow.com/questions/13011013/get-only-volume-name-from-filepath
|
---|
1317 |
|
---|
1318 | return 0;
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 |
|
---|
1322 | # This subroutine is for improving portability of Greenstone collections from one OS to another,
|
---|
1323 | # to be used to convert absolute paths going into db files into paths with placeholders instead.
|
---|
1324 | # This sub works with util::get_common_gs_paths and takes a path to a greenstone file and, if it's
|
---|
1325 | # an absolute path, then it will replace the longest matching greenstone-path prefix of the given
|
---|
1326 | # path with a placeholder to match.
|
---|
1327 | # The Greenstone-path prefixes that can be matched are the following common Greenstone paths:
|
---|
1328 | # the path to the current (specific) collection, the path to the general GS collect directory,
|
---|
1329 | # the path to the site directory if GS3, else the path to the GSDLHOME/GSDL3HOME folder.
|
---|
1330 | # The longest matching prefix will be replaced with the equivalent placeholder:
|
---|
1331 | # @THISCOLLECTPATH@, else @COLLECTHOME@, else @SITEHOME@, else @GSDLHOME@.
|
---|
1332 | sub abspath_to_placeholders {
|
---|
1333 | my $path = shift(@_); # path to convert from absolute to one with placeholders
|
---|
1334 | my $opt_long_or_short_winfilenames = shift(@_) || "short"; # whether we want to force use of long file names even on windows, default uses short
|
---|
1335 |
|
---|
1336 | return $path unless is_abs_path_any_os($path); # path is relative
|
---|
1337 |
|
---|
1338 | if ($opt_long_or_short_winfilenames eq "long") {
|
---|
1339 | $path = &util::upgrade_if_dos_filename($path); # will only do something on windows
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 | # now we know we're dealing with absolute paths and have to replace gs prefixes with placeholders
|
---|
1343 | my @gs_paths = ($ENV{'GSDLCOLLECTDIR'}, $ENV{'GSDLCOLLECTHOME'}, $ENV{'SITEHOME'}, $ENV{'GREENSTONEHOME'}); # list in this order: from longest to shortest path
|
---|
1344 |
|
---|
1345 | my %placeholder_map = ($ENV{'GREENSTONEHOME'} => '@GSDLHOME@', # can't use double-quotes around at-sign, else perl tries to evaluate it as referring to an array
|
---|
1346 | $ENV{'GSDLCOLLECTHOME'} => '@COLLECTHOME@',
|
---|
1347 | $ENV{'GSDLCOLLECTDIR'} => '@THISCOLLECTPATH@'
|
---|
1348 | );
|
---|
1349 | $placeholder_map{$ENV{'SITEHOME'}} = '@SITEHOME@' if defined $ENV{'SITEHOME'};
|
---|
1350 |
|
---|
1351 | $path = &util::_abspath_to_placeholders($path, \@gs_paths, \%placeholder_map);
|
---|
1352 |
|
---|
1353 | if ($ENV{'GSDLOS'} =~ /^windows$/i && $opt_long_or_short_winfilenames eq "short") {
|
---|
1354 | # for windows need to look for matches on short file names too
|
---|
1355 | # matched paths are again to be replaced with the usual placeholders
|
---|
1356 |
|
---|
1357 | my $gsdlcollectdir = &util::downgrade_if_dos_filename($ENV{'GSDLCOLLECTDIR'});
|
---|
1358 | my $gsdlcollecthome = &util::downgrade_if_dos_filename($ENV{'GSDLCOLLECTHOME'});
|
---|
1359 | my $sitehome = (defined $ENV{'SITEHOME'}) ? &util::downgrade_if_dos_filename($ENV{'SITEHOME'}) : undef;
|
---|
1360 | my $greenstonehome = &util::downgrade_if_dos_filename($ENV{'GREENSTONEHOME'});
|
---|
1361 |
|
---|
1362 | @gs_paths = ($gsdlcollectdir, $gsdlcollecthome, $sitehome, $greenstonehome); # order matters
|
---|
1363 |
|
---|
1364 | %placeholder_map = ($greenstonehome => '@GSDLHOME@', # can't use double-quotes around at-sign, else perl tries to evaluate it as referring to an array
|
---|
1365 | $gsdlcollecthome => '@COLLECTHOME@',
|
---|
1366 | $gsdlcollectdir => '@THISCOLLECTPATH@'
|
---|
1367 | );
|
---|
1368 | $placeholder_map{$sitehome} = '@SITEHOME@' if defined $sitehome;
|
---|
1369 |
|
---|
1370 | $path = &util::_abspath_to_placeholders($path, \@gs_paths, \%placeholder_map);
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | return $path;
|
---|
1374 | }
|
---|
1375 |
|
---|
1376 | sub _abspath_to_placeholders {
|
---|
1377 | my ($path, $gs_paths_ref, $placeholder_map_ref) = @_;
|
---|
1378 |
|
---|
1379 | # The sequence of elements in @gs_paths matters
|
---|
1380 | # Need to loop starting from the *longest* matching path (the path to the specific collection)
|
---|
1381 | # to the shortest matching path (the path to gsdlhome/gsdl3home folder):
|
---|
1382 |
|
---|
1383 | foreach my $gs_path (@$gs_paths_ref) {
|
---|
1384 | next if(!defined $gs_path); # site undefined for GS2
|
---|
1385 |
|
---|
1386 | my $re_path = &util::filename_to_regex($gs_path); # escape for regex
|
---|
1387 |
|
---|
1388 | if($path =~ m/^$re_path/i) { # case sensitive or not for OS?
|
---|
1389 |
|
---|
1390 | my $placeholder = $placeholder_map_ref->{$gs_path}; # get the placeholder to replace the matched path with
|
---|
1391 |
|
---|
1392 | $path =~ s/^$re_path/$placeholder/; #case sensitive or not?
|
---|
1393 | #$path =~ s/^[\\\/]//; # remove gs_path's trailing separator left behind at the start of the path
|
---|
1394 | last; # done
|
---|
1395 | }
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 | return $path;
|
---|
1399 | }
|
---|
1400 |
|
---|
1401 | # Function that does the reverse of the util::abspath_to_placeholders subroutine
|
---|
1402 | # Once again, call this with the values returned from util::get_common_gs_paths
|
---|
1403 | sub placeholders_to_abspath {
|
---|
1404 | my $path = shift(@_); # path that can contain placeholders to convert to resolved absolute path
|
---|
1405 | my $opt_long_or_short_winfilenames = shift(@_) || "short"; # whether we want to force use of long file names even on windows, default uses short
|
---|
1406 |
|
---|
1407 | return $path if($path !~ m/@/); # path contains no placeholders
|
---|
1408 |
|
---|
1409 | # replace placeholders with gs prefixes
|
---|
1410 | my @placeholders = ('@THISCOLLECTPATH@', '@COLLECTHOME@', '@SITEHOME@', '@GSDLHOME@'); # order of paths not crucial in this case,
|
---|
1411 | # but listed here from longest to shortest once placeholders are have been resolved
|
---|
1412 |
|
---|
1413 | # can't use double-quotes around at-sign, else perl tries to evaluate it as referring to an array
|
---|
1414 | my %placeholder_to_gspath_map;
|
---|
1415 | if ($ENV{'GSDLOS'} =~ /^windows$/i && $opt_long_or_short_winfilenames eq "short") {
|
---|
1416 | # always replace placeholders with short file names of the absolute paths on windows?
|
---|
1417 | %placeholder_to_gspath_map = ('@GSDLHOME@' => &util::downgrade_if_dos_filename($ENV{'GREENSTONEHOME'}),
|
---|
1418 | '@COLLECTHOME@' => &util::downgrade_if_dos_filename($ENV{'GSDLCOLLECTHOME'}),
|
---|
1419 | '@THISCOLLECTPATH@' => &util::downgrade_if_dos_filename($ENV{'GSDLCOLLECTDIR'})
|
---|
1420 | );
|
---|
1421 | $placeholder_to_gspath_map{'@SITEHOME@'} = &util::downgrade_if_dos_filename($ENV{'SITEHOME'}) if defined $ENV{'SITEHOME'};
|
---|
1422 | } else {
|
---|
1423 | %placeholder_to_gspath_map = ('@GSDLHOME@' => $ENV{'GREENSTONEHOME'},
|
---|
1424 | '@SITEHOME@' => $ENV{'SITEHOME'}, # can be undef
|
---|
1425 | '@COLLECTHOME@' => $ENV{'GSDLCOLLECTHOME'},
|
---|
1426 | '@THISCOLLECTPATH@' => $ENV{'GSDLCOLLECTDIR'}
|
---|
1427 | ); # $placeholder_to_gspath_map{'@SITEHOME@'} = $ENV{'SITEHOME'} if defined $ENV{'SITEHOME'};
|
---|
1428 | }
|
---|
1429 |
|
---|
1430 | foreach my $placeholder (@placeholders) {
|
---|
1431 | my $gs_path = $placeholder_to_gspath_map{$placeholder};
|
---|
1432 |
|
---|
1433 | next if(!defined $gs_path); # sitehome for GS2 is undefined
|
---|
1434 |
|
---|
1435 | if($path =~ m/^$placeholder/) {
|
---|
1436 | $path =~ s/^$placeholder/$gs_path/;
|
---|
1437 | last; # done
|
---|
1438 | }
|
---|
1439 | }
|
---|
1440 |
|
---|
1441 | return $path;
|
---|
1442 | }
|
---|
1443 |
|
---|
1444 | # Used by pdfpstoimg.pl and PDFBoxConverter to create a .item file from
|
---|
1445 | # a directory containing sequentially numbered images.
|
---|
1446 | sub create_itemfile
|
---|
1447 | {
|
---|
1448 | my ($output_dir, $convert_basename, $convert_to) = @_;
|
---|
1449 | my $page_num = "";
|
---|
1450 |
|
---|
1451 | opendir(DIR, $output_dir) || die "can't opendir $output_dir: $!";
|
---|
1452 | my @dir_files = grep {-f "$output_dir/$_"} readdir(DIR);
|
---|
1453 | closedir DIR;
|
---|
1454 |
|
---|
1455 | # Sort files in the directory by page_num
|
---|
1456 | sub page_number {
|
---|
1457 | my ($dir) = @_;
|
---|
1458 | my ($pagenum) =($dir =~ m/^.*?[-\.]?(\d+)(\.(jpg|gif|png))?$/i);
|
---|
1459 | # my ($pagenum) =($dir =~ m/(\d+)(\.(jpg|gif|png))?$/i); # this works but is not as safe/strict about input filepatterns as the above
|
---|
1460 |
|
---|
1461 | $pagenum = 1 unless defined $pagenum;
|
---|
1462 | return $pagenum;
|
---|
1463 | }
|
---|
1464 |
|
---|
1465 | # sort the files in the directory in the order of page_num rather than lexically.
|
---|
1466 | @dir_files = sort { page_number($a) <=> page_number($b) } @dir_files;
|
---|
1467 |
|
---|
1468 | # work out if the numbering of the now sorted image files starts at 0 or not
|
---|
1469 | # by checking the number of the first _image_ file (skipping item files)
|
---|
1470 | my $starts_at_0 = 0;
|
---|
1471 | my $firstfile = ($dir_files[0] !~ /\.item$/i) ? $dir_files[0] : $dir_files[1];
|
---|
1472 | if(page_number($firstfile) == 0) { # 00 will evaluate to 0 too in this condition
|
---|
1473 | $starts_at_0 = 1;
|
---|
1474 | }
|
---|
1475 |
|
---|
1476 | my $item_file = &FileUtils::filenameConcatenate($output_dir, $convert_basename.".item");
|
---|
1477 | my $item_fh;
|
---|
1478 | &FileUtils::openFileHandle($item_file, 'w', \$item_fh);
|
---|
1479 | print $item_fh "<PagedDocument>\n";
|
---|
1480 |
|
---|
1481 | foreach my $file (@dir_files){
|
---|
1482 | if ($file !~ /\.item/i){
|
---|
1483 | $page_num = page_number($file);
|
---|
1484 | $page_num++ if $starts_at_0; # image numbers start at 0, so add 1
|
---|
1485 | print $item_fh " <Page pagenum=\"$page_num\" imgfile=\"$file\" txtfile=\"\"/>\n";
|
---|
1486 | }
|
---|
1487 | }
|
---|
1488 |
|
---|
1489 | print $item_fh "</PagedDocument>\n";
|
---|
1490 | &FileUtils::closeFileHandle($item_file, \$item_fh);
|
---|
1491 | return $item_file;
|
---|
1492 | }
|
---|
1493 |
|
---|
1494 |
|
---|
1495 | ## @function augmentINC()
|
---|
1496 | #
|
---|
1497 | # Prepend a path (if it exists) onto INC but only if it isn't already in INC
|
---|
1498 | # @param $new_path The path to add
|
---|
1499 | # @author jmt12
|
---|
1500 | #
|
---|
1501 | sub augmentINC
|
---|
1502 | {
|
---|
1503 | my ($new_path) = @_;
|
---|
1504 | my $did_add_path = 0;
|
---|
1505 | # might need to be replaced with FileUtils::directoryExists() call eventually
|
---|
1506 | if (-d $new_path)
|
---|
1507 | {
|
---|
1508 | my $did_find_path = 0;
|
---|
1509 | foreach my $existing_path (@INC)
|
---|
1510 | {
|
---|
1511 | if ($existing_path eq $new_path)
|
---|
1512 | {
|
---|
1513 | $did_find_path = 1;
|
---|
1514 | last;
|
---|
1515 | }
|
---|
1516 | }
|
---|
1517 | if (!$did_find_path)
|
---|
1518 | {
|
---|
1519 | unshift(@INC, $new_path);
|
---|
1520 | $did_add_path = 1;
|
---|
1521 | }
|
---|
1522 | }
|
---|
1523 | return $did_add_path;
|
---|
1524 | }
|
---|
1525 | ## augmentINC()
|
---|
1526 |
|
---|
1527 |
|
---|
1528 | 1;
|
---|