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