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