source: main/trunk/greenstone2/perllib/util.pm@ 31876

Last change on this file since 31876 was 31876, checked in by ak19, 7 years ago
  1. Getting the recently added util::print_env() subroutine, which has now already helped in debugging a 2nd issue, to work on Windows. Keeping the same changes for linux too. 2. Dr Bainbridge said that single quoting the string https in dictionary props files would be better.
  • Property svn:keywords set to Author Date Id Revision
File size: 62.7 KB
RevLine 
[537]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###########################################################################
[4]25
26package util;
27
[23362]28use strict;
[31863]29no strict 'refs'; # make an exception so we can use variables as filehandles
[27509]30use FileUtils;
[23362]31
32use Encode;
[29807]33use Unicode::Normalize 'normalize';
34
[4]35use File::Copy;
[619]36use File::Basename;
[24362]37# Config for getting the perlpath in the recommended way, though it uses paths that are
38# hard-coded into the Config file that's generated upon configuring and compiling perl.
39# $^X works better in some cases to return the path to perl used to launch the script,
40# but if launched with plain "perl" (no full-path), that will be just what it returns.
[27303]41use Config;
42# New module for file related utility functions - intended as a
43# placeholder for an extension that allows a variety of different
44# filesystems (FTP, HTTP, SAMBA, WEBDav, HDFS etc)
45use FileUtils;
[4]46
[28226]47if ($ENV{'GSDLOS'} =~ /^windows$/i) {
[28225]48 require Win32; # for working out Windows Long Filenames from Win 8.3 short filenames
49}
50
[4]51# removes files (but not directories)
52sub rm {
[27303]53 warnings::warnif("deprecated", "util::rm() is deprecated, using FileUtils::removeFiles() instead");
54 return &FileUtils::removeFiles(@_);
[4]55}
56
57# recursive removal
[10211]58sub filtered_rm_r {
[27303]59 warnings::warnif("deprecated", "util::filtered_rm_r() is deprecated, using FileUtils::removeFilesFiltered() instead");
60 return &FileUtils::removeFilesFiltered(@_);
[4]61}
62
[10211]63# recursive removal
64sub rm_r {
[27326]65 warnings::warnif("deprecated", "util::rm_r() is deprecated, using FileUtils::removeFilesRecursive() instead");
[27303]66 return &FileUtils::removeFilesRecursive(@_);
[10211]67}
68
[721]69# moves a file or a group of files
70sub mv {
[27303]71 warnings::warnif("deprecated", "util::mv() is deprecated, using FileUtils::moveFiles() instead");
72 return &FileUtils::moveFiles(@_);
[721]73}
74
[25554]75# Move the contents of source directory into target directory
76# (as opposed to merely replacing target dir with the src dir)
77# This can overwrite any files with duplicate names in the target
78# but other files and folders in the target will continue to exist
79sub mv_dir_contents {
[27303]80 warnings::warnif("deprecated", "util::mv_dir_contents() is deprecated, using FileUtils::moveDirectoryContents() instead");
81 return &FileUtils::moveDirectoryContents(@_);
[25554]82}
[721]83
[4]84# copies a file or a group of files
85sub cp {
[27303]86 warnings::warnif("deprecated", "util::cp() is deprecated, using FileUtils::copyFiles() instead");
87 return &FileUtils::copyFiles(@_);
[4]88}
89
90# recursively copies a file or group of files
[1454]91# syntax: cp_r (sourcefiles, destination directory)
92# destination must be a directory - to copy one file to
93# another use cp instead
[4]94sub cp_r {
[27303]95 warnings::warnif("deprecated", "util::cp_r() is deprecated, using FileUtils::copyFilesrecursive() instead");
96 return &FileUtils::copyFilesRecursive(@_);
97}
[4]98
[21762]99# recursively copies a file or group of files
100# syntax: cp_r (sourcefiles, destination directory)
101# destination must be a directory - to copy one file to
102# another use cp instead
103sub cp_r_nosvn {
[27303]104 warnings::warnif("deprecated", "util::cp_r_nosvn() is deprecated, using FileUtils::copyFilesRecursiveNoSVN() instead");
105 return &FileUtils::copyFilesRecursiveNoSVN(@_);
[21762]106}
107
[11179]108# copies a directory and its contents, excluding subdirectories, into a new directory
109sub cp_r_toplevel {
[27303]110 warnings::warnif("deprecated", "util::cp_r_toplevel() is deprecated, using FileUtils::recursiveCopyTopLevel() instead");
111 return &FileUtils::recursiveCopyTopLevel(@_);
[11179]112}
113
[721]114sub mk_dir {
[27303]115 warnings::warnif("deprecated", "util::mk_dir() is deprecated, using FileUtils::makeDirectory() instead");
116 return &FileUtils::makeDirectory(@_);
[721]117}
118
[1046]119# in case anyone cares - I did some testing (using perls Benchmark module)
120# on this subroutine against File::Path::mkpath (). mk_all_dir() is apparently
121# slightly faster (surprisingly) - Stefan.
[4]122sub mk_all_dir {
[27303]123 warnings::warnif("deprecated", "util::mk_all_dir() is deprecated, using FileUtils::makeAllDirectories() instead");
124 return &FileUtils::makeAllDirectories(@_);
[4]125}
126
[619]127# make hard link to file if supported by OS, otherwise copy the file
128sub hard_link {
[27303]129 warnings::warnif("deprecated", "util::hard_link() is deprecated, using FileUtils::hardLink() instead");
130 return &FileUtils::hardLink(@_);
[619]131}
132
[2193]133# make soft link to file if supported by OS, otherwise copy file
[721]134sub soft_link {
[27303]135 warnings::warnif("deprecated", "util::soft_link() is deprecated, using FileUtils::softLink() instead");
136 return &FileUtils::softLink(@_);
[721]137}
138
[23362]139# Primarily for filenames generated by processing
140# content of HTML files (which are mapped to UTF-8 internally)
141#
142# To turn this into an octet string that really exists on the file
143# system:
144# 1. don't need to do anything special for Unix-based systems
145# (as underlying file system is byte-code)
146# 2. need to map to short DOS filenames for Windows
[721]147
[23362]148sub utf8_to_real_filename
149{
150 my ($utf8_filename) = @_;
[721]151
[23362]152 my $real_filename;
[721]153
[28375]154 if (($ENV{'GSDLOS'} =~ m/^windows$/i) && ($^O ne "cygwin")) {
[23362]155 require Win32;
[23388]156
[23362]157 my $unicode_filename = decode("utf8",$utf8_filename);
158 $real_filename = Win32::GetShortPathName($unicode_filename);
159 }
160 else {
161 $real_filename = $utf8_filename;
162 }
163
164 return $real_filename;
165}
166
[29794]167sub raw_filename_to_unicode
168{
[29810]169 my ($directory, $raw_file, $filename_encoding ) = @_;
[29794]170
[29810]171 my $unicode_filename = $raw_file;
172 if (($ENV{'GSDLOS'} =~ m/^windows$/i) && ($^O ne "cygwin")) {
[29816]173 # Try turning a short version to the long version
174 # If there are "funny" characters in the file name, that can't be represented in the ANSI code, then we will have a short weird version, eg E74~1.txt
175 $unicode_filename = &util::get_dirsep_tail(&util::upgrade_if_dos_filename(&FileUtils::filenameConcatenate($directory, $raw_file), 0));
176
177
178 if ($unicode_filename eq $raw_file) {
179 # This means the original filename *was* able to be encoded in the local ANSI file encoding (eg windows_1252), so now we turn it back to perl's unicode
[29810]180
[29816]181 $unicode_filename = &Encode::decode(locale_fs => $unicode_filename);
182 }
183 # else This means we did have one of the funny filenames. the getLongPathName (used in upgrade_if_dos_filename) will return unicode, so we don't need to do anything more.
184
[29810]185
186 } else {
[29816]187 # we had a utf-8 string, turn it into perl internal unicode
188 $unicode_filename = &Encode::decode("utf-8", $unicode_filename);
[29810]189
190
[29816]191 }
192 #Does the filename have url encoded chars in it?
193 if (&unicode::is_url_encoded($unicode_filename)) {
194 $unicode_filename = &unicode::url_decode($unicode_filename);
195 }
196
[29794]197 # Normalise the filename to canonical composition - on mac, filenames use decopmposed form for accented chars
198 if ($ENV{'GSDLOS'} =~ m/^darwin$/i) {
[29816]199 $unicode_filename = normalize('C', $unicode_filename); # Composed form 'C'
200 }
[29794]201 return $unicode_filename;
202
203}
[27303]204sub fd_exists {
205 warnings::warnif("deprecated", "util::fd_exists() is deprecated, using FileUtils::fileTest() instead");
206 return &FileUtils::fileTest(@_);
[23362]207}
208
[27303]209sub file_exists {
210 warnings::warnif("deprecated", "util::file_exists() is deprecated, using FileUtils::fileExists() instead");
211 return &FileUtils::fileExists(@_);
[23362]212}
213
[27303]214sub dir_exists {
215 warnings::warnif("deprecated", "util::dir_exists() is deprecated, using FileUtils::directoryExists() instead");
216 return &FileUtils::directoryExists(@_);
[23362]217}
218
[4]219# updates a copy of a directory in some other part of the filesystem
220# verbosity settings are: 0=low, 1=normal, 2=high
221# both $fromdir and $todir should be absolute paths
222sub cachedir {
[27303]223 warnings::warnif("deprecated", "util::cachedir() is deprecated, using FileUtils::synchronizeDirectories() instead");
224 return &FileUtils::synchronizeDirectories(@_);
[4]225}
226
227# this function returns -1 if either file is not found
228# assumes that $file1 and $file2 are absolute file names or
229# in the current directory
230# $file2 is allowed to be newer than $file1
231sub differentfiles {
[27303]232 warnings::warnif("deprecated", "util::differentfiles() is deprecated, using FileUtils::differentFiles() instead");
233 return &FileUtils::differentFiles(@_);
[4]234}
235
236
[30574]237# works out the temporary directory, including in the case where Greenstone is not writable
238# In that case, gs3-setup.bat would already have set the GS_TMP_OUTPUT_DIR temp variable
239sub determine_tmp_dir
240{
241 my $try_collect_dir = shift(@_) || 0;
242
243 my $tmp_dirname;
244 if(defined $ENV{'GS_TMP_OUTPUT_DIR'}) {
245 $tmp_dirname = $ENV{'GS_TMP_OUTPUT_DIR'};
246 } elsif($try_collect_dir && defined $ENV{'GSDLCOLLECTDIR'}) {
247 $tmp_dirname = $ENV{'GSDLCOLLECTDIR'};
248 } elsif(defined $ENV{'GSDLHOME'}) {
249 $tmp_dirname = $ENV{'GSDLHOME'};
250 } else {
251 return undef;
252 }
253
254 if(!defined $ENV{'GS_TMP_OUTPUT_DIR'}) {
255 # test the tmp_dirname folder is writable, by trying to write out a file
256 # Unfortunately, cound not get if(-w $dirname) to work on directories on Windows
257 ## http://alvinalexander.com/blog/post/perl/perl-file-test-operators-reference-cheat-sheet (test file/dir writable)
258 ## http://www.makelinux.net/alp/083 (real and effective user IDs)
259
260 my $tmp_test_file = &FileUtils::filenameConcatenate($tmp_dirname, "writability_test.tmp");
[30575]261 if (open (FOUT, ">$tmp_test_file")) {
[30574]262 close(FOUT);
263 &FileUtils::removeFiles($tmp_test_file);
264 } else { # location not writable, use TMP location
[31694]265 if (defined $ENV{'TMP'}) {
266 $tmp_dirname = $ENV{'TMP'};
267 } else {
268 $tmp_dirname = "/tmp";
269 }
270 $tmp_dirname = &FileUtils::filenameConcatenate($tmp_dirname, "greenstone");
[30574]271 $ENV{'GS_TMP_OUTPUT_DIR'} = $tmp_dirname; # store for next time
272 }
273 }
274
275 $tmp_dirname = &FileUtils::filenameConcatenate($tmp_dirname, "tmp");
276 &FileUtils::makeAllDirectories ($tmp_dirname) unless -e $tmp_dirname;
277
278 return $tmp_dirname;
279}
280
[16266]281sub get_tmp_filename
282{
283 my $file_ext = shift(@_) || undef;
284
[22438]285 my $opt_dot_file_ext = "";
286 if (defined $file_ext) {
287 if ($file_ext !~ m/\./) {
288 # no dot, so needs one added in at start
289 $opt_dot_file_ext = ".$file_ext"
290 }
291 else {
292 # allow for "extensions" such as _metadata.txt to be handled
293 # gracefully
294 $opt_dot_file_ext = $file_ext;
295 }
296 }
[16266]297
[31514]298 my $tmpdir = &util::determine_tmp_dir(0);
[4]299
300 my $count = 1000;
301 my $rand = int(rand $count);
[27303]302 my $full_tmp_filename = &FileUtils::filenameConcatenate($tmpdir, "F$rand$opt_dot_file_ext");
[16266]303
304 while (-e $full_tmp_filename) {
[4]305 $rand = int(rand $count);
[27303]306 $full_tmp_filename = &FileUtils::filenameConcatenate($tmpdir, "F$rand$opt_dot_file_ext");
[4]307 $count++;
308 }
[16266]309
310 return $full_tmp_filename;
[4]311}
312
[28066]313# These 2 are "static" variables used by the get_timestamped_tmp_folder() subroutine below and
314# belong with that function. They help ensure the timestamped tmp folders generated are unique.
315my $previous_timestamp = undef;
316my $previous_timestamp_f = 0; # frequency
317
[22886]318sub get_timestamped_tmp_folder
[22873]319{
[30574]320 my $tmp_dirname = &util::determine_tmp_dir(1);
[28066]321
[22873]322 # add the timestamp into the path otherwise we can run into problems
323 # if documents have the same name
[28066]324 my $timestamp = time;
325
326 if (!defined $previous_timestamp || ($timestamp > $previous_timestamp)) {
327 $previous_timestamp_f = 0;
328 $previous_timestamp = $timestamp;
329 } else {
330 $previous_timestamp_f++;
331 }
332
[27303]333 my $time_tmp_dirname = &FileUtils::filenameConcatenate($tmp_dirname, $timestamp);
[28066]334 $tmp_dirname = $time_tmp_dirname;
335 my $i = $previous_timestamp_f;
336
337 if($previous_timestamp_f > 0) {
338 $tmp_dirname = $time_tmp_dirname."_".$i;
339 $i++;
340 }
[22873]341 while (-e $tmp_dirname) {
[28066]342 $tmp_dirname = $time_tmp_dirname."_".$i;
[22873]343 $i++;
344 }
[28066]345 &FileUtils::makeDirectory($tmp_dirname);
346
[22886]347 return $tmp_dirname;
348}
[22873]349
[22886]350sub get_timestamped_tmp_filename_in_collection
351{
352
353 my ($input_filename, $output_ext) = @_;
354 # derive tmp filename from input filename
355 my ($tailname, $dirname, $suffix)
356 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
357
358 # softlink to collection tmp dir
359 my $tmp_dirname = &util::get_timestamped_tmp_folder();
360 $tmp_dirname = $dirname unless defined $tmp_dirname;
361
[22873]362 # following two steps copied from ConvertBinaryFile
[22886]363 # do we need them?? can't use them as is, as they use plugin methods.
364
[22873]365 #$tailname = $self->SUPER::filepath_to_utf8($tailname) unless &unicode::check_is_utf8($tailname);
366
367 # URLEncode this since htmls with images where the html filename is utf8 don't seem
368 # to work on Windows (IE or Firefox), as browsers are looking for filesystem-encoded
369 # files on the filesystem.
370 #$tailname = &util::rename_file($tailname, $self->{'file_rename_method'}, "without_suffix");
371 if (defined $output_ext) {
372 $output_ext = ".$output_ext"; # add the dot
373 } else {
374 $output_ext = $suffix;
375 }
376 $output_ext= lc($output_ext);
[27303]377 my $tmp_filename = &FileUtils::filenameConcatenate($tmp_dirname, "$tailname$output_ext");
[22873]378
379 return $tmp_filename;
380}
381
[21218]382sub get_toplevel_tmp_dir
383{
[27303]384 return &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "tmp");
[21218]385}
386
387
[28460]388sub get_collectlevel_tmp_dir
389{
390 my $tmp_dirname = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, "tmp");
391 &FileUtils::makeDirectory($tmp_dirname) if (!-e $tmp_dirname);
392
393 return $tmp_dirname;
394}
395
[31513]396sub get_parent_folder
397{
398 my ($path) = @_;
399 my ($tailname, $dirname, $suffix)
400 = &File::Basename::fileparse($path, "\\.[^\\.]+\$");
401
402 return &FileUtils::sanitizePath($dirname);
403}
404
[17512]405sub filename_to_regex {
406 my $filename = shift (@_);
[4]407
[24971]408 # need to make single backslashes double so that regex works
[24832]409 $filename =~ s/\\/\\\\/g; # if ($ENV{'GSDLOS'} =~ /^windows$/i);
[24829]410
[24832]411 # note that the first part of a substitution is a regex, so RE chars need to be escaped,
412 # the second part of a substitution is not a regex, so for e.g. full-stop can be specified literally
[24829]413 $filename =~ s/\./\\./g; # in case there are extensions/other full stops, escape them
414 $filename =~ s@\(@\\(@g; # escape brackets
415 $filename =~ s@\)@\\)@g; # escape brackets
[24932]416 $filename =~ s@\[@\\[@g; # escape brackets
417 $filename =~ s@\]@\\]@g; # escape brackets
[24829]418
[17512]419 return $filename;
420}
421
[24829]422sub unregex_filename {
423 my $filename = shift (@_);
424
425 # need to put doubled backslashes for regex back to single
426 $filename =~ s/\\\./\./g; # remove RE syntax for .
427 $filename =~ s@\\\(@(@g; # remove RE syntax for ( => "\(" turns into "("
428 $filename =~ s@\\\)@)@g; # remove RE syntax for ) => "\)" turns into ")"
[24932]429 $filename =~ s@\\\[@[@g; # remove RE syntax for [ => "\[" turns into "["
430 $filename =~ s@\\\]@]@g; # remove RE syntax for ] => "\]" turns into "]"
[24940]431
432 # \\ goes to \
433 # This is the last step in reverse mirroring the order of steps in filename_to_regex()
434 $filename =~ s/\\\\/\\/g; # remove RE syntax for \
[24829]435 return $filename;
436}
437
[4]438sub filename_cat {
[27303]439 # I've disabled this warning for now, as every Greenstone perl
440 # script seems to make use of this function and so you drown in a
441 # sea of deprecated warnings [jmt12]
442# warnings::warnif("deprecated", "util::filename_cat() is deprecated, using FileUtils::filenameConcatenate() instead");
443 return &FileUtils::filenameConcatenate(@_);
[4]444}
445
[21413]446
[28394]447sub _pathname_cat {
448 my $join_char = shift(@_);
449 my $first_path = shift(@_);
[21413]450 my (@pathnames) = @_;
451
452 # If first_path is not null or empty, then add it back into the list
453 if (defined $first_path && $first_path =~ /\S/) {
454 unshift(@pathnames, $first_path);
455 }
456
457 my $pathname = join($join_char, @pathnames);
458
459 # remove duplicate slashes
[28394]460 if ($join_char eq ";") {
[21413]461 $pathname =~ s/[\\\/]+/\\/g;
[28460]462 if ($^O eq "cygwin") {
463 # Once we've collapsed muliple (potentialy > 2) slashes
464 # For cygwin, actually want things double-backslahed
465 $pathname =~ s/\\/\\\\/g;
466 }
467
[21413]468 } else {
469 $pathname =~ s/[\/]+/\//g;
470 # DB: want a pathname abc\de.html to remain like this
471 }
472
473 return $pathname;
474}
475
476
[28394]477sub pathname_cat {
478 my (@pathnames) = @_;
479
480 my $join_char;
481 if (($ENV{'GSDLOS'} =~ /^windows$/i) && ($^O ne "cygwin")) {
482 $join_char = ";";
483 } else {
484 $join_char = ":";
485 }
486 return _pathname_cat($join_char,@pathnames);
487}
488
489
490sub javapathname_cat {
491 my (@pathnames) = @_;
492
493 my $join_char;
494
495 # Unlike pathname_cat() above, not interested if running in a Cygwin environment
496 # This is because the java we run is actually a native Windows executable
497
498 if (($ENV{'GSDLOS'} =~ /^windows$/i)) {
499 $join_char = ";";
500 } else {
501 $join_char = ":";
502 }
503 return _pathname_cat($join_char,@pathnames);
504}
505
506
[28460]507sub makeFilenameJavaCygwinCompatible
508{
509 my ($java_filename) = @_;
[28394]510
[28460]511 if ($^O eq "cygwin") {
512 # To be used with a Java program, but under Cygwin
513 # Because the java binary that is native to Windows, need to
514 # convert the Cygwin paths (i.e. Unix style) to be Windows
515 # compatible
516
517 $java_filename = `cygpath -wp "$java_filename"`;
518 chomp($java_filename);
519 $java_filename =~ s%\\%\\\\%g;
520 }
521
522 return $java_filename;
523}
524
[19616]525sub tidy_up_oid {
526 my ($OID) = @_;
[29112]527 if ($OID =~ /[\.\/\\]/) {
528 print STDERR "Warning, identifier $OID contains periods or slashes(.\\\/), replacing them with _\n";
529 $OID =~ s/[\.\\\/]/_/g; #remove any periods
[19616]530 }
531 if ($OID =~ /^\s.*\s$/) {
532 print STDERR "Warning, identifier $OID starts or ends with whitespace. Removing it\n";
533 # remove starting and trailing whitespace
534 $OID =~ s/^\s+//;
535 $OID =~ s/\s+$//;
536 }
537 if ($OID =~ /^[\d]*$/) {
538 print STDERR "Warning, identifier $OID contains only digits. Prepending 'D'.\n";
539 $OID = "D" . $OID;
540 }
541
542 return $OID;
543}
[26206]544
[10212]545sub envvar_prepend {
546 my ($var,$val) = @_;
547
[26206]548 # 64 bit linux can't handle ";" as path separator, so make sure to set this to the right one for the OS
[28375]549## my $pathsep = (defined $ENV{'GSDLOS'} && $ENV{'GSDLOS'} !~ m/windows/) ? ":" : ";";
[26206]550
[28375]551 # Rewritten above to make ":" the default (Windows is the special
552 # case, anything else 'unusual' such as Solaris etc is Unix)
553 my $pathsep = (defined $ENV{'GSDLOS'} && (($ENV{'GSDLOS'} =~ m/windows/) && ($^O ne "cygwin"))) ? ";" : ":";
554
[16404]555 # do not prepend any value/path that's already in the environment variable
[24832]556
557 my $escaped_val = &filename_to_regex($val); # escape any backslashes and brackets for upcoming regex
558 if (!defined($ENV{$var})) {
559 $ENV{$var} = "$val";
[16442]560 }
[24832]561 elsif($ENV{$var} !~ m/$escaped_val/) {
[26206]562 $ENV{$var} = "$val".$pathsep.$ENV{$var};
[10212]563 }
564}
565
566sub envvar_append {
567 my ($var,$val) = @_;
[26206]568
569 # 64 bit linux can't handle ";" as path separator, so make sure to set this to the right one for the OS
570 my $pathsep = (defined $ENV{'GSDLOS'} && $ENV{'GSDLOS'} !~ m/windows/) ? ":" : ";";
[24832]571
[16404]572 # do not append any value/path that's already in the environment variable
[26206]573
[24832]574 my $escaped_val = &filename_to_regex($val); # escape any backslashes and brackets for upcoming regex
575 if (!defined($ENV{$var})) {
576 $ENV{$var} = "$val";
[16442]577 }
[24832]578 elsif($ENV{$var} !~ m/$escaped_val/) {
[26206]579 $ENV{$var} = $ENV{$var}.$pathsep."$val";
[24832]580 }
[10212]581}
582
[31862]583# debug aid
584sub print_env {
[31863]585 my ($handle, @envvars) = @_; # print to $handle, which can be STDERR/STDOUT/file, etc.
586
[31862]587 if (scalar(@envvars) == 0) {
[31876]588 #print $handle "@@@ All env vars requested\n";
589
590 my $output = "";
591
592 print $handle "@@@ Environment was:\n********\n";
593 foreach my $envvar (sort keys(%ENV)) {
594 if(defined $ENV{$envvar}) {
595 print $handle "\t$envvar = $ENV{$envvar}\n";
596 } else {
597 print $handle "\t$envvar = \n";
598 }
599 }
600 print $handle "********\n";
[31862]601 } else {
[31863]602 print $handle "@@@ Environment was:\n********\n";
[31862]603 foreach my $envvar (@envvars) {
604 if(defined $ENV{$envvar}) {
[31863]605 print $handle "\t$envvar = ".$ENV{$envvar}."\n";
[31876]606 } else {
607 print $handle "Env var '$envvar' was not set\n";
[31862]608 }
609 }
[31863]610 print $handle "********\n";
[31862]611 }
612}
[28394]613
[31862]614
[16380]615# splits a filename into a prefix and a tail extension using the tail_re, or
616# if that fails, splits on the file_extension . (dot)
617sub get_prefix_and_tail_by_regex {
[10212]618
[16380]619 my ($filename,$tail_re) = @_;
620
621 my ($file_prefix,$file_ext) = ($filename =~ m/^(.*?)($tail_re)$/);
622 if ((!defined $file_prefix) || (!defined $file_ext)) {
623 ($file_prefix,$file_ext) = ($filename =~ m/^(.*)(\..*?)$/);
624 }
625
626 return ($file_prefix,$file_ext);
627}
628
629# get full path and file only path from a base_dir (which may be empty) and
630# file (which may contain directories)
631sub get_full_filenames {
632 my ($base_dir, $file) = @_;
633
[28375]634# my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(0);
635# my ($lcfilename) = ($cfilename =~ m/([^\\\/]*)$/);
636# print STDERR "** Calling method: $lcfilename:$cline $cpackage->$csubr\n";
637
638
[16380]639 my $filename_full_path = $file;
640 # add on directory if present
[27303]641 $filename_full_path = &FileUtils::filenameConcatenate($base_dir, $file) if $base_dir =~ /\S/;
[16380]642
643 my $filename_no_path = $file;
644
645 # remove directory if present
646 $filename_no_path =~ s/^.*[\/\\]//;
647 return ($filename_full_path, $filename_no_path);
648}
649
[8682]650# returns the path of a file without the filename -- ie. the directory the file is in
651sub filename_head {
652 my $filename = shift(@_);
653
[28375]654 if (($ENV{'GSDLOS'} =~ /^windows$/i) && ($^O ne "cygwin")) {
[8682]655 $filename =~ s/[^\\\\]*$//;
656 }
657 else {
658 $filename =~ s/[^\\\/]*$//;
659 }
660
661 return $filename;
662}
663
664
[23362]665
[1454]666# returns 1 if filename1 and filename2 point to the same
667# file or directory
668sub filenames_equal {
669 my ($filename1, $filename2) = @_;
670
671 # use filename_cat to clean up trailing slashes and
672 # multiple slashes
[27303]673 $filename1 = &FileUtils::filenameConcatenate($filename1);
674 $filename2 = &FileUtils::filenameConcatenate($filename2);
[1454]675
676 # filenames not case sensitive on windows
677 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
678 $filename1 =~ tr/[A-Z]/[a-z]/;
679 $filename2 =~ tr/[A-Z]/[a-z]/;
680 }
681 return 1 if $filename1 eq $filename2;
682 return 0;
683}
684
[24932]685# If filename is relative to within_dir, returns the relative path of filename to that directory
686# with slashes in the filename returned as they were in the original (absolute) filename.
[23362]687sub filename_within_directory
688{
689 my ($filename,$within_dir) = @_;
690
[23371]691 if ($within_dir !~ m/[\/\\]$/) {
692 my $dirsep = &util::get_dirsep();
[23362]693 $within_dir .= $dirsep;
694 }
695
[24829]696 $within_dir = &filename_to_regex($within_dir); # escape DOS style file separator and brackets
[23362]697 if ($filename =~ m/^$within_dir(.*)$/) {
698 $filename = $1;
699 }
700
701 return $filename;
702}
703
[24932]704# If filename is relative to within_dir, returns the relative path of filename to that directory in URL format.
705# Filename and within_dir can be any type of slashes, but will be compared as URLs (i.e. unix-style slashes).
706# The subpath returned will also be a URL type filename.
707sub filename_within_directory_url_format
708{
709 my ($filename,$within_dir) = @_;
710
711 # convert parameters only to / slashes if Windows
712
[24971]713 my $filename_urlformat = &filepath_to_url_format($filename);
714 my $within_dir_urlformat = &filepath_to_url_format($within_dir);
715
[24932]716 #if ($within_dir_urlformat !~ m/\/$/) {
717 # make sure directory ends with a slash
718 #$within_dir_urlformat .= "/";
719 #}
720
721 my $within_dir_urlformat_re = &filename_to_regex($within_dir_urlformat); # escape any special RE characters, such as brackets
722
723 #print STDERR "@@@@@ $filename_urlformat =~ $within_dir_urlformat_re\n";
724
725 # dir prefix may or may not end with a slash (this is discarded when extracting the sub-filepath)
726 if ($filename_urlformat =~ m/^$within_dir_urlformat_re(?:\/)*(.*)$/) {
727 $filename_urlformat = $1;
728 }
729
730 return $filename_urlformat;
731}
732
[24971]733# Convert parameter to use / slashes if Windows (if on Linux leave any \ as is,
734# since on Linux it doesn't represent a file separator but an escape char).
735sub filepath_to_url_format
736{
737 my ($filepath) = @_;
[28375]738 if (($ENV{'GSDLOS'} =~ /^windows$/i) && ($^O ne "cygwin")) {
[24971]739 # Only need to worry about Windows, as Unix style directories already in url-format
740 # Convert Windows style \ => /
741 $filepath =~ s@\\@/@g;
742 }
743 return $filepath;
744}
[24932]745
[25093]746# regex filepaths on windows may include \\ as path separator. Convert \\ to /
747sub filepath_regex_to_url_format
748{
749 my ($filepath) = @_;
[28375]750 if (($ENV{'GSDLOS'} =~ /^windows$/i) && ($^O ne "cygwin")) {
[25093]751 # Only need to worry about Windows, as Unix style directories already in url-format
752 # Convert Windows style \\ => /
753 $filepath =~ s@\\\\@/@g;
754 }
755 return $filepath;
756
757}
[24971]758
[25093]759# Like File::Basename::fileparse, but expects filepath in url format (ie only / slash for dirsep)
760# and ignores trailing /
761# returns (file, dirs) dirs will be empty if no subdirs
762sub url_fileparse
763{
764 my ($filepath) = @_;
765 # remove trailing /
766 $filepath =~ s@/$@@;
767 if ($filepath !~ m@/@) {
768 return ($filepath, "");
769 }
770 my ($dirs, $file) = $filepath =~ m@(.+/)([^/]+)@;
771 return ($file, $dirs);
772
773}
774
775
[10281]776sub filename_within_collection
777{
778 my ($filename) = @_;
779
780 my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
781
782 if (defined $collect_dir) {
[23362]783
[15875]784 # if from within GSDLCOLLECTDIR, then remove directory prefix
785 # so source_filename is realative to it. This is done to aid
786 # portability, i.e. the collection can be moved to somewhere
787 # else on the file system and the archives directory will still
788 # work. This is needed, for example in the applet version of
789 # GLI where GSDLHOME/collect on the server will be different to
790 # the collect directory of the remove user. Of course,
791 # GSDLCOLLECTDIR subsequently needs to be put back on to turn
792 # it back into a full pathname.
[23362]793
794 $filename = filename_within_directory($filename,$collect_dir);
[10281]795 }
796
797 return $filename;
798}
799
[23362]800sub prettyprint_file
801{
[23484]802 my ($base_dir,$file,$gli) = @_;
[23362]803
[27303]804 my $filename_full_path = &FileUtils::filenameConcatenate($base_dir,$file);
[23362]805
[28375]806 if (($ENV{'GSDLOS'} =~ m/^windows$/i) && ($^O ne "cygwin")) {
[23362]807 require Win32;
808
809 # For some reason base_dir in the form c:/a/b/c
810 # This leads to confusion later on, so turn it back into
811 # the more usual Windows form
812 $base_dir =~ s/\//\\/g;
813 my $long_base_dir = Win32::GetLongPathName($base_dir);
814 my $long_full_path = Win32::GetLongPathName($filename_full_path);
815
816 $file = filename_within_directory($long_full_path,$long_base_dir);
[23484]817 $file = encode("utf8",$file) if ($gli);
[23362]818 }
819
820 return $file;
821}
822
823
824sub upgrade_if_dos_filename
825{
[23371]826 my ($filename_full_path,$and_encode) = @_;
[23362]827
[28375]828 if (($ENV{'GSDLOS'} =~ m/^windows$/i) && ($^O ne "cygwin")) {
[23362]829 # Ensure any DOS-like filename, such as test~1.txt, has been upgraded
830 # to its long (Windows) version
[23416]831 my $long_filename = Win32::GetLongPathName($filename_full_path);
832 if (defined $long_filename) {
[29810]833
[23416]834 $filename_full_path = $long_filename;
835 }
[23362]836 # Make sure initial drive letter is lower-case (to fit in with rest of Greenstone)
[23483]837 $filename_full_path =~ s/^(.):/\u$1:/;
[29810]838
[23371]839 if ((defined $and_encode) && ($and_encode)) {
840 $filename_full_path = encode("utf8",$filename_full_path);
841 }
[23362]842 }
843
844 return $filename_full_path;
845}
846
847
[23388]848sub downgrade_if_dos_filename
849{
850 my ($filename_full_path) = @_;
851
[28375]852 if (($ENV{'GSDLOS'} =~ m/^windows$/i) && ($^O ne "cygwin")) {
[23388]853 require Win32;
854
855 # Ensure the given long Windows filename is in a form that can
856 # be opened by Perl => convert it to a short DOS-like filename
857
[23414]858 my $short_filename = Win32::GetShortPathName($filename_full_path);
859 if (defined $short_filename) {
860 $filename_full_path = $short_filename;
861 }
[23416]862 # Make sure initial drive letter is lower-case (to fit in
863 # with rest of Greenstone)
[23483]864 $filename_full_path =~ s/^(.):/\u$1:/;
[23388]865 }
866
867 return $filename_full_path;
868}
869
870
[18441]871sub filename_is_absolute
872{
[27303]873 warnings::warnif("deprecated", "util::filename_is_absolute() is deprecated, using FileUtils::isFilenameAbsolute() instead");
874 return &FileUtils::isFilenameAbsolute(@_);
[18441]875}
876
877
[17572]878## @method make_absolute()
879#
880# Ensure the given file path is absolute in respect to the given base path.
881#
882# @param $base_dir A string denoting the base path the given dir must be
883# absolute to.
884# @param $dir The directory to be made absolute as a string. Note that the
885# dir may already be absolute, in which case it will remain
886# unchanged.
887# @return The now absolute form of the directory as a string.
888#
889# @author John Thompson, DL Consulting Ltd.
890# @copy 2006 DL Consulting Ltd.
891#
892#used in buildcol.pl, doesn't work for all cases --kjdon
893sub make_absolute {
894
895 my ($base_dir, $dir) = @_;
[18441]896### print STDERR "dir = $dir\n";
[17572]897 $dir =~ s/[\\\/]+/\//g;
898 $dir = $base_dir . "/$dir" unless ($dir =~ m|^(\w:)?/|);
899 $dir =~ s|^/tmp_mnt||;
900 1 while($dir =~ s|/[^/]*/\.\./|/|g);
901 $dir =~ s|/[.][.]?/|/|g;
902 $dir =~ tr|/|/|s;
[18441]903### print STDERR "dir = $dir\n";
[17572]904
905 return $dir;
906}
907## make_absolute() ##
[10281]908
[7929]909sub get_dirsep {
910
[28375]911 if (($ENV{'GSDLOS'} =~ /^windows$/i) && ($^O ne "cygwin")) {
[7929]912 return "\\";
913 } else {
914 return "\/";
915 }
916}
917
[619]918sub get_os_dirsep {
[4]919
[28375]920 if (($ENV{'GSDLOS'} =~ /^windows$/i) && ($^O ne "cygwin")) {
[619]921 return "\\\\";
922 } else {
923 return "\\\/";
924 }
925}
926
927sub get_re_dirsep {
928
929 return "\\\\|\\\/";
930}
931
932
[15003]933sub get_dirsep_tail {
934 my ($filename) = @_;
935
936 # returns last part of directory or filename
937 # On unix e.g. a/b.d => b.d
938 # a/b/c => c
939
[15088]940 my $dirsep = get_re_dirsep();
941 my @dirs = split (/$dirsep/, $filename);
942 my $tail = pop @dirs;
[15003]943
[15088]944 # - caused problems under windows
945 #my ($tail) = ($filename =~ m/^(?:.*?$dirsep)?(.*?)$/);
946
[15003]947 return $tail;
948}
949
950
[4]951# if this is running on windows we want binaries to end in
952# .exe, otherwise they don't have to end in any extension
953sub get_os_exe {
[28375]954 return ".exe" if (($ENV{'GSDLOS'} =~ /^windows$/i) && ($^O ne "cygwin"));
[4]955 return "";
956}
957
958
[86]959# test to see whether this is a big or little endian machine
[15713]960sub is_little_endian
961{
962 # To determine the name of the operating system, the variable $^O is a cheap alternative to pulling it out of the Config module;
963 # 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
964 # Otherwise, it's little endian
965
966 #return 0 if $^O =~ /^darwin$/i;
[17714]967 #return 0 if $ENV{'GSDLOS'} =~ /^darwin$/i;
968
969 # Going back to stating exactly whether the machine is little endian
970 # or big endian, without any special case for Macs. Since for rata it comes
971 # back with little endian and for shuttle with bigendian.
[15713]972 return (ord(substr(pack("s",1), 0, 1)) == 1);
[86]973}
[4]974
[86]975
[135]976# will return the collection name if successful, "" otherwise
977sub use_collection {
[1454]978 my ($collection, $collectdir) = @_;
[135]979
[1454]980 if (!defined $collectdir || $collectdir eq "") {
[27303]981 $collectdir = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "collect");
[1454]982 }
983
[28211]984 if (!defined $ENV{'GREENSTONEHOME'}) { # for GS3, would have been defined in use_site_collection, to GSDL3HOME
985 $ENV{'GREENSTONEHOME'} = $ENV{'GSDLHOME'};
986 }
987
[135]988 # get and check the collection
989 if (!defined($collection) || $collection eq "") {
990 if (defined $ENV{'GSDLCOLLECTION'}) {
991 $collection = $ENV{'GSDLCOLLECTION'};
992 } else {
[2359]993 print STDOUT "No collection specified\n";
[135]994 return "";
995 }
996 }
997
998 if ($collection eq "modelcol") {
[2359]999 print STDOUT "You can't use modelcol.\n";
[135]1000 return "";
1001 }
1002
1003 # make sure the environment variables GSDLCOLLECTION and GSDLCOLLECTDIR
1004 # are defined
[17204]1005 $ENV{'GSDLCOLLECTION'} = $collection;
[28211]1006 $ENV{'GSDLCOLLECTHOME'} = $collectdir;
[27303]1007 $ENV{'GSDLCOLLECTDIR'} = &FileUtils::filenameConcatenate($collectdir, $collection);
[135]1008
1009 # make sure this collection exists
1010 if (!-e $ENV{'GSDLCOLLECTDIR'}) {
[2359]1011 print STDOUT "Invalid collection ($collection).\n";
[135]1012 return "";
1013 }
1014
1015 # everything is ready to go
1016 return $collection;
1017}
1018
[21207]1019sub get_current_collection_name {
1020 return $ENV{'GSDLCOLLECTION'};
1021}
[14926]1022
1023
1024# will return the collection name if successful, "" otherwise.
1025# Like use_collection (above) but for greenstone 3 (taking account of site level)
1026
1027sub use_site_collection {
1028 my ($site, $collection, $collectdir) = @_;
1029
1030 if (!defined $collectdir || $collectdir eq "") {
1031 die "GSDL3HOME not set.\n" unless defined $ENV{'GSDL3HOME'};
[27303]1032 $collectdir = &FileUtils::filenameConcatenate($ENV{'GSDL3HOME'}, "sites", $site, "collect");
[14926]1033 }
1034
[28211]1035 if (defined $ENV{'GSDL3HOME'}) {
1036 $ENV{'GREENSTONEHOME'} = $ENV{'GSDL3HOME'};
1037 $ENV{'SITEHOME'} = &FileUtils::filenameConcatenate($ENV{'GREENSTONEHOME'}, "sites", $site);
1038 } elsif (defined $ENV{'GSDL3SRCHOME'}) {
1039 $ENV{'GREENSTONEHOME'} = &FileUtils::filenameConcatenate($ENV{'GSDL3SRCHOME'}, "web");
1040 $ENV{'SITEHOME'} = &FileUtils::filenameConcatenate($ENV{'GREENSTONEHOME'}, "sites", $site);
1041 } else {
1042 print STDERR "*** util::use_site_collection(). Warning: Neither GSDL3HOME nor GSDL3SRCHOME set.\n";
1043 }
1044
[14926]1045 # collectdir explicitly set by this point (using $site variable if required).
1046 # Can call "old" gsdl2 use_collection now.
1047
1048 return use_collection($collection,$collectdir);
1049}
1050
1051
1052
[15018]1053sub locate_config_file
1054{
1055 my ($file) = @_;
1056
1057 my $locations = locate_config_files($file);
1058
1059 return shift @$locations; # returns undef if 'locations' is empty
1060}
1061
1062
1063sub locate_config_files
1064{
1065 my ($file) = @_;
1066
1067 my @locations = ();
1068
1069 if (-e $file) {
1070 # Clearly specified (most likely full filename)
1071 # No need to hunt in 'etc' directories, return value unchanged
1072 push(@locations,$file);
1073 }
1074 else {
1075 # Check for collection specific one before looking in global GSDL 'etc'
[16969]1076 if (defined $ENV{'GSDLCOLLECTDIR'} && $ENV{'GSDLCOLLECTDIR'} ne "") {
1077 my $test_collect_etc_filename
[27303]1078 = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'},"etc", $file);
[16969]1079
1080 if (-e $test_collect_etc_filename) {
1081 push(@locations,$test_collect_etc_filename);
1082 }
[15018]1083 }
1084 my $test_main_etc_filename
[27303]1085 = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"etc", $file);
[15018]1086 if (-e $test_main_etc_filename) {
1087 push(@locations,$test_main_etc_filename);
1088 }
1089 }
1090
1091 return \@locations;
1092}
1093
1094
[9955]1095sub hyperlink_text
1096{
1097 my ($text) = @_;
1098
1099 $text =~ s/(http:\/\/[^\s]+)/<a href=\"$1\">$1<\/a>/mg;
1100 $text =~ s/(^|\s+)(www\.(\w|\.)+)/<a href=\"http:\/\/$2\">$2<\/a>/mg;
1101
1102 return $text;
1103}
1104
1105
[16436]1106# A method to check if a directory is empty (note that an empty directory still has non-zero size!!!)
1107# Code is from http://episteme.arstechnica.com/eve/forums/a/tpc/f/6330927813/m/436007700831
[27303]1108sub is_dir_empty {
1109 warnings::warnif("deprecated", "util::is_dir_empty() is deprecated, using FileUtils::isDirectoryEmpty() instead");
1110 return &FileUtils::isDirectoryEmpty(@_);
[16436]1111}
1112
[18337]1113# Returns the given filename converted using either URL encoding or base64
1114# encoding, as specified by $rename_method. If the given filename has no suffix
[20413]1115# (if it is just the tailname), then $no_suffix should be some defined value.
1116# rename_method can be url, none, base64
[18319]1117sub rename_file {
[18337]1118 my ($filename, $rename_method, $no_suffix) = @_;
[18329]1119
[18337]1120 if(!$filename) { # undefined or empty string
[18329]1121 return $filename;
1122 }
[18319]1123
[20413]1124 if (!$rename_method) {
1125 print STDERR "WARNING: no file renaming method specified. Defaulting to using URL encoding...\n";
1126 # Debugging information
[22856]1127 # my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(1);
1128 # print STDERR "Called from method: $cfilename:$cline $cpackage->$csubr\n";
[20413]1129 $rename_method = "url";
1130 } elsif($rename_method eq "none") {
1131 return $filename; # would have already been renamed
1132 }
1133
[19762]1134 # No longer replace spaces with underscores, since underscores mess with incremental rebuild
1135 ### Replace spaces with underscore. Do this first else it can go wrong below when getting tailname
1136 ###$filename =~ s/ /_/g;
[18337]1137
1138 my ($tailname,$dirname,$suffix);
1139 if($no_suffix) { # given a tailname, no suffix
1140 ($tailname,$dirname) = File::Basename::fileparse($filename);
1141 }
1142 else {
1143 ($tailname,$dirname,$suffix) = File::Basename::fileparse($filename, "\\.(?:[^\\.]+?)\$");
1144 }
[23388]1145 if (!$suffix) {
1146 $suffix = "";
1147 }
[26973]1148 # This breaks GLI matching extracted metadata to files in Enrich panel, as
1149 # original is eg .JPG while gsdlsourcefilename ends up .jpg
1150 # Not sure why it was done in first place...
1151 #else {
1152 #$suffix = lc($suffix);
1153 #}
[18337]1154
[20413]1155 if ($rename_method eq "url") {
[18319]1156 $tailname = &unicode::url_encode($tailname);
1157 }
1158 elsif ($rename_method eq "base64") {
[18341]1159 $tailname = &unicode::base64_encode($tailname);
[18319]1160 $tailname =~ s/\s*//sg; # for some reason it adds spaces not just at end but also in middle
1161 }
[18326]1162
[18319]1163 $filename = "$tailname$suffix";
[18326]1164 $filename = "$dirname$filename" if ($dirname ne "./" && $dirname ne ".\\");
[18319]1165
1166 return $filename;
1167}
1168
[21616]1169
1170# BACKWARDS COMPATIBILITY: Just in case there are old .ldb/.bdb files
[21664]1171sub rename_ldb_or_bdb_file {
[18657]1172 my ($filename_no_ext) = @_;
1173
1174 my $new_filename = "$filename_no_ext.gdb";
[21615]1175 return if (-f $new_filename); # if the file has the right extension, don't need to do anything
[18657]1176 # try ldb
1177 my $old_filename = "$filename_no_ext.ldb";
1178
1179 if (-f $old_filename) {
[19056]1180 print STDERR "Renaming $old_filename to $new_filename\n";
1181 rename ($old_filename, $new_filename)
1182 || print STDERR "Rename failed: $!\n";
[18657]1183 return;
1184 }
1185 # try bdb
1186 $old_filename = "$filename_no_ext.bdb";
1187 if (-f $old_filename) {
[19056]1188 print STDERR "Renaming $old_filename to $new_filename\n";
1189 rename ($old_filename, $new_filename)
1190 || print STDERR "Rename failed: $!\n";
[18657]1191 return;
1192 }
1193}
1194
[24874]1195sub os_dir() {
1196
1197 my $gsdlarch = "";
1198 if(defined $ENV{'GSDLARCH'}) {
1199 $gsdlarch = $ENV{'GSDLARCH'};
1200 }
1201 return $ENV{'GSDLOS'}.$gsdlarch;
1202}
[18657]1203
[28997]1204# returns 1 if this (GS server) is a GS3 installation, returns 0 if it's GS2.
1205sub is_gs3() {
1206 if($ENV{'GSDL3SRCHOME'}) {
1207 return 1;
1208 } else {
1209 return 0;
1210 }
1211}
1212
[21719]1213# Returns the greenstone URL prefix extracted from the appropriate GS2/GS3 config file.
1214# By default, /greenstone3 for GS3 or /greenstone for GS2.
1215sub get_greenstone_url_prefix() {
1216 # if already set on a previous occasion, just return that
1217 # (Don't want to keep repeating this: cost of re-opening and scanning files.)
1218 return $ENV{'GREENSTONE_URL_PREFIX'} if($ENV{'GREENSTONE_URL_PREFIX'});
[18657]1219
[21719]1220 my ($configfile, $urlprefix, $defaultUrlprefix);
1221 my @propertynames = ();
1222
1223 if($ENV{'GSDL3SRCHOME'}) {
1224 $defaultUrlprefix = "/greenstone3";
[27303]1225 $configfile = &FileUtils::filenameConcatenate($ENV{'GSDL3SRCHOME'}, "packages", "tomcat", "conf", "Catalina", "localhost", "greenstone3.xml");
[21719]1226 push(@propertynames, qw/path\s*\=/);
1227 } else {
1228 $defaultUrlprefix = "/greenstone";
[27303]1229 $configfile = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "cgi-bin", &os_dir(), "gsdlsite.cfg");
[21719]1230 push(@propertynames, (qw/\nhttpprefix/, qw/\ngwcgi/)); # inspect one property then the other
1231 }
1232
1233 $urlprefix = &extract_propvalue_from_file($configfile, \@propertynames);
1234
1235 if(!$urlprefix) { # no values found for URL prefix, use default values
1236 $urlprefix = $defaultUrlprefix;
1237 } else {
1238 #gwcgi can contain more than the wanted prefix, we split on / to get the first "directory" level
1239 $urlprefix =~ s/^\///; # remove the starting slash
1240 my @dirs = split(/(\\|\/)/, $urlprefix);
1241 $urlprefix = shift(@dirs);
1242
1243 if($urlprefix !~ m/^\//) { # in all cases: ensure the required forward slash is at the front
1244 $urlprefix = "/$urlprefix";
1245 }
1246 }
1247
1248 # set for the future
1249 $ENV{'GREENSTONE_URL_PREFIX'} = $urlprefix;
1250# print STDERR "*** in get_greenstone_url_prefix(): $urlprefix\n\n";
1251 return $urlprefix;
1252}
1253
1254
[28460]1255
1256#
[30561]1257# The following comes from activate.pl
[28460]1258#
1259# Designed to work with a server included with GS.
1260# - For GS3, we ask ant for the library URL.
1261# - For GS2, we derive the URL from the llssite.cfg file.
1262
1263sub get_full_greenstone_url_prefix
1264{
[31518]1265 my ($gs_mode, $lib_name) = @_;
1266
[28460]1267 # if already set on a previous occasion, just return that
1268 # (Don't want to keep repeating this: cost of re-opening and scanning files.)
[31517]1269 return $ENV{'FULL_GREENSTONE_URL_PREFIX'} if($ENV{'FULL_GREENSTONE_URL_PREFIX'});
[28460]1270
[31518]1271 # set gs_mode if it was not passed in (servercontrol.pm would pass it in, any other callers won't)
1272 $gs_mode = ($ENV{'GSDL3SRCHOME'}) ? "gs3" : "gs2" unless defined $gs_mode;
[28460]1273
1274 my $url = undef;
1275
1276 if($gs_mode eq "gs2") {
1277 my $llssite_cfg = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "llssite.cfg");
1278
1279 if(-f $llssite_cfg) {
1280 # check llssite.cfg for line with url property
1281 # for server.exe also need to use portnumber and enterlib properties
1282
1283 # Read in the entire contents of the file in one hit
1284 if (!open (FIN, $llssite_cfg)) {
[31515]1285 print STDERR "util::get_full_greenstone_url_prefix() failed to open $llssite_cfg ($!)\n";
[28460]1286 return undef;
1287 }
1288
1289 my $contents;
1290 sysread(FIN, $contents, -s FIN);
1291 close(FIN);
1292
1293 my @lines = split(/[\n\r]+/, $contents); # split on carriage-returns and/or linefeeds
1294 my $enterlib = "";
[30561]1295 my $portnumber = "8282"; # will remain empty (implicit port 80) unless it's specifically been assigned
[28460]1296
1297 foreach my $line (@lines) {
1298 if($line =~ m/^url=(.*)$/) {
1299 $url = $1;
1300 } elsif($line =~ m/^enterlib=(.*)$/) {
1301 $enterlib = $1;
1302 } elsif($line =~ m/^portnumber=(.*)$/) {
1303 $portnumber = $1;
1304 }
1305 }
1306
1307 if(!$url) {
1308 return undef;
1309 }
1310 elsif($url eq "URL_pending") { # library is not running
1311 # do not process url=URL_pending in the file, since for server.exe
1312 # this just means the Enter Library button hasn't been pressed yet
1313 $url = undef;
1314 }
1315 else {
1316 # In the case of server.exe, need to do extra work to get the proper URL
1317 # But first, need to know whether we're indeed dealing with server.exe:
1318
1319 # compare the URL's domain to the full URL
1320 # E.g. for http://localhost:8383/greenstone3/cgi-bin, the domain is localhost:8383
1321 my $uri = URI->new( $url );
1322 my $host = $uri->host;
1323 #print STDERR "@@@@@ host: $host\n";
[31514]1324 if($url =~ m/https?:\/\/$host(\/)?$/) {
[31515]1325 #if($url !~ m/https?:\/\/$host:$portnumber(\/)?/ || $url =~ m/https?:\/\/$host(\/)?$/) {
[28460]1326 # (if the URL does not contain the portnumber, OR if the port is implicitly 80 and)
1327 # If the domain with http:// prefix is completely the same as the URL, assume server.exe
1328 # then the actual URL is the result of suffixing the port and enterlib properties in llssite.cfg
1329 $url = $url.":".$portnumber.$enterlib;
1330 } # else, apache web server
1331
1332 }
1333 }
1334 } elsif($gs_mode eq "gs3") {
1335 # Either check build.properties for tomcat.server, tomcat.port and app.name (and default servlet name).
1336 # app.name is stored in app.path by build.xml. Need to move app.name in build.properties from build.xml
1337
1338 # Or, run the new target get-default-servlet-url
1339 # the output can look like:
1340 #
1341 # Buildfile: build.xml
1342 # [echo] os.name: Windows Vista
1343 #
1344 # get-default-servlet-url:
1345 # [echo] http://localhost:8383/greenstone3/library
1346 # BUILD SUCCESSFUL
1347 # Total time: 0 seconds
1348
1349 #my $output = qx/ant get-default-servlet-url/; # backtick operator, to get STDOUT (else 2>&1)
1350 # - see http://stackoverflow.com/questions/799968/whats-the-difference-between-perls-backticks-system-and-exec
1351
1352 # The get-default-servlet-url ant target can be run from anywhere by specifying the
1353 # location of GS3's ant build.xml buildfile. Activate.pl can be run from anywhere for GS3
1354 # GSDL3SRCHOME will be set for GS3 by gs3-setup.sh, a step that would have been necessary
1355 # to run the activate.pl script in the first place
1356
1357 my $full_build_xml = &FileUtils::javaFilenameConcatenate($ENV{'GSDL3SRCHOME'},"build.xml");
1358
1359 my $perl_command = "ant -buildfile \"$full_build_xml\" get-default-servlet-url";
1360
1361 if (open(PIN, "$perl_command |")) {
1362 while (defined (my $perl_output_line = <PIN>)) {
1363
[31514]1364 if($perl_output_line =~ m@(https?):\/\/(\S*)@) { # grab all the non-whitespace chars
1365 $url="$1://".$2; # preserve the http protocol #$url="http://".$1;
[28460]1366 }
1367 }
1368 close(PIN);
[31518]1369
1370 if (defined $lib_name) { # url won't be undef now
1371 # replace the servlet_name portion of the url found, with the given library_name
1372 $url =~ s@/[^/]*$@/$lib_name@;
1373 }
[28460]1374 } else {
[31515]1375 print STDERR "util::get_full_greenstone_url_prefix() failed to run $perl_command to work out library URL for $gs_mode\n";
[31514]1376 }
[28460]1377 }
1378
1379 # either the url is still undef or it is now set
1380 #print STDERR "\n@@@@@ final URL:|$url|\n" if $url;
1381 #print STDERR "\n@@@@@ URL still undef\n" if !$url;
1382
1383 $ENV{'FULL_GREENSTONE_URL_PREFIX'} = $url;
1384
1385 return $url;
1386}
1387
1388
[21719]1389# Given a config file (xml or java properties file) and a list/array of regular expressions
1390# that represent property names to match on, this function will return the value for the 1st
1391# matching property name. If the return value is undefined, no matching property was found.
1392sub extract_propvalue_from_file() {
1393 my ($configfile, $propertynames) = @_;
1394
1395 my $value;
1396 unless(open(FIN, "<$configfile")) {
1397 print STDERR "extract_propvalue_from_file(): Unable to open $configfile. $!\n";
1398 return $value; # not initialised
1399 }
1400
1401 # Read the entire file at once, as one single line, then close it
1402 my $filecontents;
1403 {
1404 local $/ = undef;
1405 $filecontents = <FIN>;
1406 }
1407 close(FIN);
1408
1409 foreach my $regex (@$propertynames) {
1410 ($value) = $filecontents=~ m/$regex\s*(\S*)/s; # read value of the property given by regex up to the 1st space
1411 if($value) {
1412 $value =~ s/^\"//; # remove any startquotes
1413 $value =~ s/\".*$//; # remove the 1st endquotes (if any) followed by any xml
1414 last; # found value for a matching property, break from loop
1415 }
1416 }
1417
1418 return $value;
1419}
1420
[23306]1421# Subroutine that sources setup.bash, given GSDLHOME and GSDLOS and
1422# given that perllib is in @INC in order to invoke this subroutine.
1423# Call as follows -- after setting up INC to include perllib and
1424# after setting up GSDLHOME and GSDLOS:
1425#
1426# require util;
1427# &util::setup_greenstone_env($ENV{'GSDLHOME'}, $ENV{'GSDLOS'});
1428#
1429sub setup_greenstone_env() {
1430 my ($GSDLHOME, $GSDLOS) = @_;
1431
1432 #my %env_map = ();
1433 # Get the localised ENV settings of running a localised source setup.bash
[23314]1434 # and put it into the ENV here. Need to clear GSDLHOME before running setup
1435 #my $perl_command = "(cd $GSDLHOME; export GSDLHOME=; . ./setup.bash > /dev/null; env)";
1436 my $perl_command = "(cd $GSDLHOME; /bin/bash -c \"export GSDLHOME=; source setup.bash > /dev/null; env\")";
[28375]1437 if (($GSDLOS =~ m/windows/i) && ($^O ne "cygwin")) {
[23314]1438 #$perl_command = "cmd /C \"cd $GSDLHOME&& set GSDLHOME=&& setup.bat > nul&& set\"";
1439 $perl_command = "(cd $GSDLHOME&& set GSDLHOME=&& setup.bat > nul&& set)";
[23306]1440 }
1441 if (!open(PIN, "$perl_command |")) {
1442 print STDERR ("Unable to execute command: $perl_command. $!\n");
[24563]1443 }
[23306]1444
[29582]1445 my $lastkey;
[23306]1446 while (defined (my $perl_output_line = <PIN>)) {
1447 my($key,$value) = ($perl_output_line =~ m/^([^=]*)[=](.*)$/);
[29582]1448 if(defined $key) {
1449 #$env_map{$key}=$value;
1450 $ENV{$key}=$value;
1451 $lastkey = $key;
1452 } elsif($lastkey && $perl_output_line !~ m/^\s*$/) {
1453 # there was no equals sign in $perl_output_line, so this
1454 # $perl_output_line may be a spillover from the previous
1455 $ENV{$lastkey} = $ENV{$lastkey}."\n".$perl_output_line;
1456 }
[23306]1457 }
[24563]1458 close (PIN);
1459
[23306]1460 # If any keys in $ENV don't occur in Greenstone's localised env
1461 # (stored in $env_map), delete those entries from $ENV
1462 #foreach $key (keys %ENV) {
1463 # if(!defined $env_map{$key}) {
[29708]1464 # print STDOUT "**** DELETING ENV KEY: $key\tVALUE: $ENV{$key}\n";
[23306]1465 # delete $ENV{$key}; # del $ENV(key, value) pair
1466 # }
1467 #}
1468 #undef %env_map;
1469}
1470
[24362]1471sub get_perl_exec() {
1472 my $perl_exec = $^X; # may return just "perl"
1473
1474 if($ENV{'PERLPATH'}) {
[27303]1475 # OR: # $perl_exec = &FileUtils::filenameConcatenate($ENV{'PERLPATH'},"perl");
[28375]1476 if (($ENV{'GSDLOS'} =~ m/windows/) && ($^O ne "cygwin")) {
[24362]1477 $perl_exec = "$ENV{'PERLPATH'}\\Perl.exe";
1478 } else {
1479 $perl_exec = "$ENV{'PERLPATH'}/perl";
1480 }
1481 } else { # no PERLPATH, use Config{perlpath} else $^X: special variables
1482 # containing the full path to the current perl executable we're using
1483 $perl_exec = $Config{perlpath}; # configured path for perl
1484 if (!-e $perl_exec) { # may not point to location on this machine
1485 $perl_exec = $^X; # may return just "perl"
1486 if($perl_exec =~ m/^perl/i) { # warn if just perl or Perl.exe
1487 print STDERR "**** WARNING: Perl exec found contains no path: $perl_exec";
1488 }
1489 }
1490 }
1491
1492 return $perl_exec;
1493}
1494
[25533]1495# returns the path to the java command in the JRE included with GS (if any),
1496# quoted to safeguard any spaces in this path, otherwise a simple java
1497# command is returned which assumes and will try for a system java.
[25512]1498sub get_java_command {
1499 my $java = "java";
1500 if(defined $ENV{'GSDLHOME'}) { # should be, as this script would be launched from the cmd line
1501 # after running setup.bat or from GLI which also runs setup.bat
[27303]1502 my $java_bin = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"packages","jre","bin");
[25512]1503 if(-d $java_bin) {
[27303]1504 $java = &FileUtils::filenameConcatenate($java_bin,"java");
[25533]1505 $java = "\"".$java."\""; # quoted to preserve spaces in path
[25512]1506 }
1507 }
1508 return $java;
1509}
[24362]1510
[25512]1511
[25577]1512# Given the qualified collection name (colgroup/collection),
1513# returns the collection and colgroup parts
1514sub get_collection_parts {
1515 # http://perldoc.perl.org/File/Basename.html
1516 # my($filename, $directories, $suffix) = fileparse($path);
1517 # "$directories contains everything up to and including the last directory separator in the $path
1518 # including the volume (if applicable). The remainder of the $path is the $filename."
1519 #my ($collection, $colgroup) = &File::Basename::fileparse($qualified_collection);
1520
1521 my $qualified_collection = shift(@_);
1522
1523 # Since activate.pl can be launched from the command-line, including by a user,
1524 # best not to assume colgroup uses URL-style slashes as would be the case with GLI
1525 # Also allow for the accidental inclusion of multiple slashes
1526 my ($colgroup, $collection) = split(/[\/\\]+/, $qualified_collection); #split('/', $qualified_collection);
1527
1528 if(!defined $collection) {
1529 $collection = $colgroup;
1530 $colgroup = "";
1531 }
1532 return ($collection, $colgroup);
1533}
1534
1535# work out the "collectdir/collection" location
1536sub resolve_collection_dir {
1537 my ($collect_dir, $qualified_collection, $site) = @_; #, $gs_mode
1538
[28211]1539 if (defined $ENV{'GSDLCOLLECTDIR'}) { # a predefined collection dir exists
1540 return $ENV{'GSDLCOLLECTDIR'};
1541 }
1542
[25577]1543 my ($colgroup, $collection) = &util::get_collection_parts($qualified_collection);
1544
[28175]1545 if (!defined $collect_dir || !$collect_dir) { # if undefined or empty string
1546 $collect_dir = &util::get_working_collect_dir($site);
[25577]1547 }
[28175]1548
1549 return &FileUtils::filenameConcatenate($collect_dir,$colgroup,$collection);
1550}
1551
1552# work out the full path to "collect" of this greenstone 2/3 installation
1553sub get_working_collect_dir {
[28211]1554 my ($site) = @_;
1555
1556 if (defined $ENV{'GSDLCOLLECTHOME'}) { # a predefined collect dir exists
1557 return $ENV{'GSDLCOLLECTHOME'};
[28175]1558 }
[28211]1559
1560 if (defined $site && $site) { # site non-empty, so get default collect dir for GS3
1561
1562 if (defined $ENV{'GSDL3HOME'}) {
1563 return &FileUtils::filenameConcatenate($ENV{'GSDL3HOME'},"sites",$site,"collect"); # web folder
1564 }
1565 elsif (defined $ENV{'GSDL3SRCHOME'}) {
1566 return &FileUtils::filenameConcatenate($ENV{'GSDL3SRCHOME'},"web","sites",$site,"collect");
1567 }
1568 }
1569
[28213]1570 elsif (defined $ENV{'SITEHOME'}) {
[28211]1571 return &FileUtils::filenameConcatenate($ENV{'SITEHOME'},"collect");
[28177]1572 }
1573
1574 else { # get default collect dir for GS2
1575 return &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"collect");
1576 }
1577}
1578
[28211]1579sub is_abs_path_any_os {
[28177]1580 my ($path) = @_;
1581
[28211]1582 # We can have filenames in our DBs that were produced on other OS, so this method exists
1583 # to help identify absolute paths in such cases.
1584
[28177]1585 return 1 if($path =~ m@^/@); # full paths begin with forward slash on linux/mac
1586 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
1587
1588 return 0;
1589}
1590
1591
1592# This subroutine is for improving portability of Greenstone collections from one OS to another,
1593# to be used to convert absolute paths going into db files into paths with placeholders instead.
1594# This sub works with util::get_common_gs_paths and takes a path to a greenstone file and, if it's
1595# an absolute path, then it will replace the longest matching greenstone-path prefix of the given
1596# path with a placeholder to match.
1597# The Greenstone-path prefixes that can be matched are the following common Greenstone paths:
1598# the path to the current (specific) collection, the path to the general GS collect directory,
1599# the path to the site directory if GS3, else the path to the GSDLHOME/GSDL3HOME folder.
1600# The longest matching prefix will be replaced with the equivalent placeholder:
1601# @THISCOLLECTPATH@, else @COLLECTHOME@, else @SITEHOME@, else @GSDLHOME@.
1602sub abspath_to_placeholders {
1603 my $path = shift(@_); # path to convert from absolute to one with placeholders
[28236]1604 my $opt_long_or_short_winfilenames = shift(@_) || "short"; # whether we want to force use of long file names even on windows, default uses short
[28177]1605
[28211]1606 return $path unless is_abs_path_any_os($path); # path is relative
[28236]1607
1608 if ($opt_long_or_short_winfilenames eq "long") {
1609 $path = &util::upgrade_if_dos_filename($path); # will only do something on windows
1610 }
[28225]1611
[28177]1612 # now we know we're dealing with absolute paths and have to replace gs prefixes with placeholders
[28213]1613 my @gs_paths = ($ENV{'GSDLCOLLECTDIR'}, $ENV{'GSDLCOLLECTHOME'}, $ENV{'SITEHOME'}, $ENV{'GREENSTONEHOME'}); # list in this order: from longest to shortest path
[28177]1614
[28211]1615 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
1616 $ENV{'GSDLCOLLECTHOME'} => '@COLLECTHOME@',
1617 $ENV{'GSDLCOLLECTDIR'} => '@THISCOLLECTPATH@'
[28177]1618 );
[28213]1619 $placeholder_map{$ENV{'SITEHOME'}} = '@SITEHOME@' if defined $ENV{'SITEHOME'};
[28177]1620
[28228]1621 $path = &util::_abspath_to_placeholders($path, \@gs_paths, \%placeholder_map);
[28177]1622
[28236]1623 if ($ENV{'GSDLOS'} =~ /^windows$/i && $opt_long_or_short_winfilenames eq "short") {
[28228]1624 # for windows need to look for matches on short file names too
1625 # matched paths are again to be replaced with the usual placeholders
1626
1627 my $gsdlcollectdir = &util::downgrade_if_dos_filename($ENV{'GSDLCOLLECTDIR'});
1628 my $gsdlcollecthome = &util::downgrade_if_dos_filename($ENV{'GSDLCOLLECTHOME'});
1629 my $sitehome = (defined $ENV{'SITEHOME'}) ? &util::downgrade_if_dos_filename($ENV{'SITEHOME'}) : undef;
1630 my $greenstonehome = &util::downgrade_if_dos_filename($ENV{'GREENSTONEHOME'});
1631
1632 @gs_paths = ($gsdlcollectdir, $gsdlcollecthome, $sitehome, $greenstonehome); # order matters
1633
1634 %placeholder_map = ($greenstonehome => '@GSDLHOME@', # can't use double-quotes around at-sign, else perl tries to evaluate it as referring to an array
1635 $gsdlcollecthome => '@COLLECTHOME@',
1636 $gsdlcollectdir => '@THISCOLLECTPATH@'
1637 );
1638 $placeholder_map{$sitehome} = '@SITEHOME@' if defined $sitehome;
1639
1640 $path = &util::_abspath_to_placeholders($path, \@gs_paths, \%placeholder_map);
1641 }
1642
1643 return $path;
1644}
1645
1646sub _abspath_to_placeholders {
1647 my ($path, $gs_paths_ref, $placeholder_map_ref) = @_;
1648
[28177]1649 # The sequence of elements in @gs_paths matters
1650 # Need to loop starting from the *longest* matching path (the path to the specific collection)
1651 # to the shortest matching path (the path to gsdlhome/gsdl3home folder):
1652
[28228]1653 foreach my $gs_path (@$gs_paths_ref) {
[28213]1654 next if(!defined $gs_path); # site undefined for GS2
[28211]1655
[28177]1656 my $re_path = &util::filename_to_regex($gs_path); # escape for regex
1657
[28211]1658 if($path =~ m/^$re_path/i) { # case sensitive or not for OS?
[28177]1659
[28228]1660 my $placeholder = $placeholder_map_ref->{$gs_path}; # get the placeholder to replace the matched path with
[28177]1661
1662 $path =~ s/^$re_path/$placeholder/; #case sensitive or not?
1663 #$path =~ s/^[\\\/]//; # remove gs_path's trailing separator left behind at the start of the path
[28604]1664 # lowercase file extension, This is needed when shortfilenames are used, as case affects alphetical ordering, which affects diffcol
1665 $path =~ s/\.([A-Z]+)$/".".lc($1)/e;
[28177]1666 last; # done
1667 }
1668 }
1669
1670 return $path;
1671}
1672
1673# Function that does the reverse of the util::abspath_to_placeholders subroutine
1674# Once again, call this with the values returned from util::get_common_gs_paths
1675sub placeholders_to_abspath {
1676 my $path = shift(@_); # path that can contain placeholders to convert to resolved absolute path
[28236]1677 my $opt_long_or_short_winfilenames = shift(@_) || "short"; # whether we want to force use of long file names even on windows, default uses short
[28177]1678
1679 return $path if($path !~ m/@/); # path contains no placeholders
1680
[28213]1681 # replace placeholders with gs prefixes
[28177]1682 my @placeholders = ('@THISCOLLECTPATH@', '@COLLECTHOME@', '@SITEHOME@', '@GSDLHOME@'); # order of paths not crucial in this case,
1683 # but listed here from longest to shortest once placeholders are have been resolved
1684
[28213]1685 # can't use double-quotes around at-sign, else perl tries to evaluate it as referring to an array
[28228]1686 my %placeholder_to_gspath_map;
[28236]1687 if ($ENV{'GSDLOS'} =~ /^windows$/i && $opt_long_or_short_winfilenames eq "short") {
[28228]1688 # always replace placeholders with short file names of the absolute paths on windows?
1689 %placeholder_to_gspath_map = ('@GSDLHOME@' => &util::downgrade_if_dos_filename($ENV{'GREENSTONEHOME'}),
1690 '@COLLECTHOME@' => &util::downgrade_if_dos_filename($ENV{'GSDLCOLLECTHOME'}),
1691 '@THISCOLLECTPATH@' => &util::downgrade_if_dos_filename($ENV{'GSDLCOLLECTDIR'})
1692 );
1693 $placeholder_to_gspath_map{'@SITEHOME@'} = &util::downgrade_if_dos_filename($ENV{'SITEHOME'}) if defined $ENV{'SITEHOME'};
1694 } else {
1695 %placeholder_to_gspath_map = ('@GSDLHOME@' => $ENV{'GREENSTONEHOME'},
1696 '@SITEHOME@' => $ENV{'SITEHOME'}, # can be undef
1697 '@COLLECTHOME@' => $ENV{'GSDLCOLLECTHOME'},
1698 '@THISCOLLECTPATH@' => $ENV{'GSDLCOLLECTDIR'}
1699 ); # $placeholder_to_gspath_map{'@SITEHOME@'} = $ENV{'SITEHOME'} if defined $ENV{'SITEHOME'};
1700 }
[28177]1701
1702 foreach my $placeholder (@placeholders) {
1703 my $gs_path = $placeholder_to_gspath_map{$placeholder};
1704
[28213]1705 next if(!defined $gs_path); # sitehome for GS2 is undefined
[28177]1706
1707 if($path =~ m/^$placeholder/) {
1708 $path =~ s/^$placeholder/$gs_path/;
1709 last; # done
1710 }
1711 }
1712
1713 return $path;
1714}
1715
[25994]1716# Used by pdfpstoimg.pl and PDFBoxConverter to create a .item file from
1717# a directory containing sequentially numbered images.
1718sub create_itemfile
1719{
1720 my ($output_dir, $convert_basename, $convert_to) = @_;
[27303]1721 my $page_num = "";
[25994]1722
[27303]1723 opendir(DIR, $output_dir) || die "can't opendir $output_dir: $!";
[25994]1724 my @dir_files = grep {-f "$output_dir/$_"} readdir(DIR);
[27303]1725 closedir DIR;
[25994]1726
[27303]1727 # Sort files in the directory by page_num
[25994]1728 sub page_number {
1729 my ($dir) = @_;
[27970]1730 my ($pagenum) =($dir =~ m/^.*?[-\.]?(\d+)(\.(jpg|gif|png))?$/i);
1731# my ($pagenum) =($dir =~ m/(\d+)(\.(jpg|gif|png))?$/i); # this works but is not as safe/strict about input filepatterns as the above
[25994]1732
1733 $pagenum = 1 unless defined $pagenum;
1734 return $pagenum;
1735 }
1736
[27303]1737 # sort the files in the directory in the order of page_num rather than lexically.
[25994]1738 @dir_files = sort { page_number($a) <=> page_number($b) } @dir_files;
1739
1740 # work out if the numbering of the now sorted image files starts at 0 or not
1741 # by checking the number of the first _image_ file (skipping item files)
1742 my $starts_at_0 = 0;
1743 my $firstfile = ($dir_files[0] !~ /\.item$/i) ? $dir_files[0] : $dir_files[1];
1744 if(page_number($firstfile) == 0) { # 00 will evaluate to 0 too in this condition
1745 $starts_at_0 = 1;
1746 }
1747
[27303]1748 my $item_file = &FileUtils::filenameConcatenate($output_dir, $convert_basename.".item");
1749 my $item_fh;
1750 &FileUtils::openFileHandle($item_file, 'w', \$item_fh);
1751 print $item_fh "<PagedDocument>\n";
[25994]1752
1753 foreach my $file (@dir_files){
1754 if ($file !~ /\.item/i){
1755 $page_num = page_number($file);
1756 $page_num++ if $starts_at_0; # image numbers start at 0, so add 1
[27303]1757 print $item_fh " <Page pagenum=\"$page_num\" imgfile=\"$file\" txtfile=\"\"/>\n";
1758 }
[25994]1759 }
1760
[27303]1761 print $item_fh "</PagedDocument>\n";
1762 &FileUtils::closeFileHandle($item_file, \$item_fh);
[25994]1763 return $item_file;
1764}
1765
[28560]1766# Sets the gnomelib_env. Based on the logic in wvware.pl which can perhaps be replaced with a call to this function in future
1767sub set_gnomelib_env
1768{
1769 ## SET THE ENVIRONMENT AS DONE IN SETUP.BASH/BAT OF GNOME-LIB
1770 # Though this is only needed for darwin Lion at this point (and android, though that is untested)
[27303]1771
[28560]1772 my $libext = "so";
1773 if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
1774 return;
1775 } elsif ($ENV{'GSDLOS'} =~ m/^darwin$/i) {
1776 $libext = "dylib";
1777 }
1778
1779 if (!defined $ENV{'GEXTGNOME'}) {
1780 ##print STDERR "@@@ Setting GEXTGNOME env\n";
1781
1782 my $gnome_dir = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"ext","gnome-lib-minimal");
1783
1784 if(! -d $gnome_dir) {
1785 $gnome_dir = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"ext","gnome-lib");
1786
1787 if(! -d $gnome_dir) {
1788 $gnome_dir = "";
1789 }
1790 }
1791
1792 # now set other the related env vars,
1793 # IF we've found the gnome-lib dir installed in the ext folder
1794
1795 if ($gnome_dir ne "" && -f &FileUtils::filenameConcatenate($gnome_dir, $ENV{'GSDLOS'}, "lib", "libiconv.$libext")) {
1796 $ENV{'GEXTGNOME'} = $gnome_dir;
1797 $ENV{'GEXTGNOME_INSTALLED'}=&FileUtils::filenameConcatenate($ENV{'GEXTGNOME'}, $ENV{'GSDLOS'});
1798
1799 my $gnomelib_bin = &FileUtils::filenameConcatenate($ENV{'GEXTGNOME_INSTALLED'}, "bin");
1800 if(-d $gnomelib_bin) { # no bin subfolder in GS binary's cutdown gnome-lib-minimal folder
1801 &util::envvar_prepend("PATH", $gnomelib_bin);
1802 }
1803
1804 # util's prepend will create LD/DYLD_LIB_PATH if it doesn't exist yet
1805 my $gextlib = &FileUtils::filenameConcatenate($ENV{'GEXTGNOME_INSTALLED'}, "lib");
1806
1807 if($ENV{'GSDLOS'} eq "linux") {
1808 &util::envvar_prepend("LD_LIBRARY_PATH", $gextlib);
1809 }
1810 elsif ($ENV{'GSDLOS'} eq "darwin") {
1811 #&util::envvar_prepend("DYLD_LIBRARY_PATH", $gextlib);
1812 &util::envvar_prepend("DYLD_FALLBACK_LIBRARY_PATH", $gextlib);
1813 }
1814 }
1815
1816 # Above largely mimics the setup.bash of the gnome-lib-minimal.
1817 # Not doing the devel-srcpack that gnome-lib-minimal's setup.bash used to set
1818 # Not exporting GSDLEXTS variable either
1819 }
1820
1821# print STDERR "@@@@@ GEXTGNOME: ".$ENV{'GEXTGNOME'}."\n\tINSTALL".$ENV{'GEXTGNOME_INSTALLED'}."\n";
1822# print STDERR "\tPATH".$ENV{'PATH'}."\n";
1823# print STDERR "\tLD_LIB_PATH".$ENV{'LD_LIBRARY_PATH'}."\n" if $ENV{'LD_LIBRARY_PATH};
1824# print STDERR "\tDYLD_FALLBACK_LIB_PATH".$ENV{'DYLD_FALLBACK_LIBRARY_PATH'}."\n" if $ENV{'DYLD_FALLBACK_LIBRARY_PATH};
1825
1826 # if no GEXTGNOME, maybe users didn't need gnome-lib to run gnomelib/libiconv dependent binaries like hashfile, suffix, wget
1827 # (wvware is launched in a gnomelib env from its own script, but could possibly go through this script in future)
1828}
1829
1830
1831
[27374]1832## @function augmentINC()
1833#
1834# Prepend a path (if it exists) onto INC but only if it isn't already in INC
1835# @param $new_path The path to add
1836# @author jmt12
1837#
[27303]1838sub augmentINC
1839{
1840 my ($new_path) = @_;
1841 my $did_add_path = 0;
[27374]1842 # might need to be replaced with FileUtils::directoryExists() call eventually
[27303]1843 if (-d $new_path)
1844 {
1845 my $did_find_path = 0;
1846 foreach my $existing_path (@INC)
1847 {
1848 if ($existing_path eq $new_path)
1849 {
1850 $did_find_path = 1;
1851 last;
1852 }
1853 }
1854 if (!$did_find_path)
1855 {
1856 unshift(@INC, $new_path);
1857 $did_add_path = 1;
1858 }
1859 }
1860 return $did_add_path;
1861}
[27374]1862## augmentINC()
[27303]1863
[27374]1864
[4]18651;
Note: See TracBrowser for help on using the repository browser.