[537] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
[15870] | 3 | # DirectoryPlugin.pm --
|
---|
[537] | 4 | # A component of the Greenstone digital library software
|
---|
| 5 | # from the New Zealand Digital Library Project at the
|
---|
| 6 | # University of Waikato, New Zealand.
|
---|
| 7 | #
|
---|
| 8 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
| 9 | #
|
---|
| 10 | # This program is free software; you can redistribute it and/or modify
|
---|
| 11 | # it under the terms of the GNU General Public License as published by
|
---|
| 12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | # (at your option) any later version.
|
---|
| 14 | #
|
---|
| 15 | # This program is distributed in the hope that it will be useful,
|
---|
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | # GNU General Public License for more details.
|
---|
| 19 | #
|
---|
| 20 | # You should have received a copy of the GNU General Public License
|
---|
| 21 | # along with this program; if not, write to the Free Software
|
---|
| 22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | #
|
---|
| 24 | ###########################################################################
|
---|
| 25 |
|
---|
[15870] | 26 | # DirectoryPlugin is a plugin which recurses through directories processing
|
---|
| 27 | # each file it finds - which basically means passing it down the plugin
|
---|
| 28 | # pipeline
|
---|
[4] | 29 |
|
---|
[15870] | 30 | package DirectoryPlugin;
|
---|
[2228] | 31 |
|
---|
[24951] | 32 | use extrametautil;
|
---|
[17738] | 33 | use PrintInfo;
|
---|
[4] | 34 | use plugin;
|
---|
[136] | 35 | use util;
|
---|
[27306] | 36 | use FileUtils;
|
---|
[13188] | 37 | use metadatautil;
|
---|
[4] | 38 |
|
---|
[8737] | 39 | use File::Basename;
|
---|
[10254] | 40 | use strict;
|
---|
| 41 | no strict 'refs';
|
---|
[15870] | 42 | no strict 'subs';
|
---|
| 43 |
|
---|
[29760] | 44 | use Encode::Locale;
|
---|
[13545] | 45 | use Encode;
|
---|
[29763] | 46 | use Unicode::Normalize;
|
---|
[4] | 47 |
|
---|
[29795] | 48 | use Win32;
|
---|
| 49 |
|
---|
[4] | 50 | BEGIN {
|
---|
[17738] | 51 | @DirectoryPlugin::ISA = ('PrintInfo');
|
---|
[4] | 52 | }
|
---|
| 53 |
|
---|
[4744] | 54 | my $arguments =
|
---|
| 55 | [ { 'name' => "block_exp",
|
---|
[15870] | 56 | 'desc' => "{BasePlugin.block_exp}",
|
---|
[6408] | 57 | 'type' => "regexp",
|
---|
[4744] | 58 | 'deft' => &get_default_block_exp(),
|
---|
| 59 | 'reqd' => "no" },
|
---|
[13417] | 60 | # this option has been deprecated. leave it here for now so we can warn people not to use it
|
---|
[4744] | 61 | { 'name' => "use_metadata_files",
|
---|
[15870] | 62 | 'desc' => "{DirectoryPlugin.use_metadata_files}",
|
---|
[4744] | 63 | 'type' => "flag",
|
---|
[13188] | 64 | 'reqd' => "no",
|
---|
| 65 | 'hiddengli' => "yes" },
|
---|
[7686] | 66 | { 'name' => "recheck_directories",
|
---|
[15870] | 67 | 'desc' => "{DirectoryPlugin.recheck_directories}",
|
---|
[7686] | 68 | 'type' => "flag",
|
---|
[4744] | 69 | 'reqd' => "no" } ];
|
---|
[13188] | 70 |
|
---|
[15870] | 71 | my $options = { 'name' => "DirectoryPlugin",
|
---|
| 72 | 'desc' => "{DirectoryPlugin.desc}",
|
---|
[6408] | 73 | 'abstract' => "no",
|
---|
[4744] | 74 | 'inherits' => "yes",
|
---|
| 75 | 'args' => $arguments };
|
---|
[3540] | 76 |
|
---|
[4] | 77 | sub new {
|
---|
[10218] | 78 | my ($class) = shift (@_);
|
---|
| 79 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 80 | push(@$pluginlist, $class);
|
---|
[3540] | 81 |
|
---|
[15870] | 82 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 83 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[10218] | 84 |
|
---|
[17738] | 85 | my $self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists);
|
---|
[13188] | 86 |
|
---|
| 87 | if ($self->{'info_only'}) {
|
---|
| 88 | # don't worry about any options or initialisations etc
|
---|
| 89 | return bless $self, $class;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | # we have left this option in so we can warn people who are still using it
|
---|
[2813] | 93 | if ($self->{'use_metadata_files'}) {
|
---|
[15870] | 94 | die "ERROR: DirectoryPlugin -use_metadata_files option has been deprecated. Please remove the option and add MetadataXMLPlug to your plugin list instead!\n";
|
---|
[2813] | 95 | }
|
---|
[16391] | 96 |
|
---|
[17738] | 97 | $self->{'num_processed'} = 0;
|
---|
| 98 | $self->{'num_not_processed'} = 0;
|
---|
| 99 | $self->{'num_blocked'} = 0;
|
---|
| 100 | $self->{'num_archives'} = 0;
|
---|
| 101 |
|
---|
[8737] | 102 | $self->{'subdir_extrametakeys'} = {};
|
---|
| 103 |
|
---|
[4] | 104 | return bless $self, $class;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[17738] | 107 | # called once, at the start of processing
|
---|
| 108 | sub init {
|
---|
| 109 | my $self = shift (@_);
|
---|
| 110 | my ($verbosity, $outhandle, $failhandle) = @_;
|
---|
| 111 |
|
---|
| 112 | # verbosity is passed through from the processor
|
---|
| 113 | $self->{'verbosity'} = $verbosity;
|
---|
| 114 |
|
---|
| 115 | # as are the outhandle and failhandle
|
---|
| 116 | $self->{'outhandle'} = $outhandle if defined $outhandle;
|
---|
| 117 | $self->{'failhandle'} = $failhandle;
|
---|
| 118 |
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | # called once, after all passes have finished
|
---|
| 122 | sub deinit {
|
---|
| 123 | my ($self) = @_;
|
---|
| 124 |
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | # called at the beginning of each plugin pass (import has one, building has many)
|
---|
[10156] | 128 | sub begin {
|
---|
| 129 | my $self = shift (@_);
|
---|
| 130 | my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
|
---|
| 131 |
|
---|
[21586] | 132 | # Only lookup timestamp info for import.pl, and only if incremental is set
|
---|
[10156] | 133 | my $proc_package_name = ref $processor;
|
---|
[12969] | 134 | if ($proc_package_name !~ /buildproc$/ && $self->{'incremental'} == 1) {
|
---|
[21586] | 135 | # Get the infodbtype value for this collection from the arcinfo object
|
---|
| 136 | my $infodbtype = $processor->getoutputinfo()->{'infodbtype'};
|
---|
[23171] | 137 | $infodbtype = "gdbm" if $infodbtype eq "gdbm-txtgz"; # in archives, cannot use txtgz version
|
---|
[10156] | 138 | my $output_dir = $processor->getoutputdir();
|
---|
[21586] | 139 | my $archives_inf = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $output_dir);
|
---|
[18441] | 140 |
|
---|
[10156] | 141 | if ( -e $archives_inf ) {
|
---|
| 142 | $self->{'inf_timestamp'} = -M $archives_inf;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[21308] | 147 | sub remove_all {
|
---|
[21285] | 148 | my $self = shift (@_);
|
---|
| 149 | my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
|
---|
| 150 | }
|
---|
[21308] | 151 |
|
---|
[21315] | 152 |
|
---|
[21308] | 153 | sub remove_one {
|
---|
| 154 | my $self = shift (@_);
|
---|
[21315] | 155 | my ($file, $oids, $archivedir) = @_;
|
---|
| 156 | return undef; # this will never be called for directories (will it??)
|
---|
| 157 |
|
---|
[21308] | 158 | }
|
---|
| 159 |
|
---|
| 160 |
|
---|
[17738] | 161 | # called at the end of each plugin pass
|
---|
[15870] | 162 | sub end {
|
---|
[17738] | 163 | my ($self) = shift (@_);
|
---|
[10156] | 164 |
|
---|
[15870] | 165 | }
|
---|
| 166 |
|
---|
| 167 |
|
---|
[17738] | 168 |
|
---|
[4] | 169 | # return 1 if this class might recurse using $pluginfo
|
---|
| 170 | sub is_recursive {
|
---|
| 171 | my $self = shift (@_);
|
---|
[2813] | 172 |
|
---|
[4] | 173 | return 1;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[2228] | 176 | sub get_default_block_exp {
|
---|
| 177 | my $self = shift (@_);
|
---|
[2813] | 178 |
|
---|
[26867] | 179 | return '(?i)(CVS|\.svn|Thumbs\.db|OIDcount|\.DS_Store|~)$';
|
---|
[2228] | 180 | }
|
---|
| 181 |
|
---|
[16391] | 182 | sub check_directory_path {
|
---|
[2228] | 183 |
|
---|
[16391] | 184 | my $self = shift(@_);
|
---|
| 185 | my ($dirname) = @_;
|
---|
[2813] | 186 |
|
---|
[2228] | 187 | return undef unless (-d $dirname);
|
---|
[16391] | 188 |
|
---|
[2228] | 189 | return 0 if ($self->{'block_exp'} ne "" && $dirname =~ /$self->{'block_exp'}/);
|
---|
[7932] | 190 |
|
---|
[16391] | 191 | my $outhandle = $self->{'outhandle'};
|
---|
| 192 |
|
---|
[2228] | 193 | # check to make sure we're not reading the archives or index directory
|
---|
| 194 | my $gsdlhome = quotemeta($ENV{'GSDLHOME'});
|
---|
[2813] | 195 | if ($dirname =~ m/^$gsdlhome\/.*?\/import.*?\/(archives|index)$/) {
|
---|
[15870] | 196 | print $outhandle "DirectoryPlugin: $dirname appears to be a reference to a Greenstone collection, skipping.\n";
|
---|
[2228] | 197 | return 0;
|
---|
[1755] | 198 | }
|
---|
[2813] | 199 |
|
---|
[1755] | 200 | # check to see we haven't got a cyclic path...
|
---|
| 201 | if ($dirname =~ m%(/.*){,41}%) {
|
---|
[15870] | 202 | print $outhandle "DirectoryPlugin: $dirname is 40 directories deep, is this a recursive path? if not increase constant in DirectoryPlugin.pm.\n";
|
---|
[2228] | 203 | return 0;
|
---|
[1755] | 204 | }
|
---|
[2813] | 205 |
|
---|
[1755] | 206 | # check to see we haven't got a cyclic path...
|
---|
| 207 | if ($dirname =~ m%.*?import/(.+?)/import/\1.*%) {
|
---|
[15870] | 208 | print $outhandle "DirectoryPlugin: $dirname appears to be in a recursive loop...\n";
|
---|
[2228] | 209 | return 0;
|
---|
[1755] | 210 | }
|
---|
[16391] | 211 |
|
---|
| 212 | return 1;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | # this may be called more than once
|
---|
| 216 | sub sort_out_associated_files {
|
---|
| 217 |
|
---|
| 218 | my $self = shift (@_);
|
---|
| 219 | my ($block_hash) = @_;
|
---|
| 220 | if (!scalar (keys %{$block_hash->{'shared_fileroot'}})) {
|
---|
| 221 | return;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | $self->{'assocfile_info'} = {} unless defined $self->{'assocfile_info'};
|
---|
| 225 | my $metadata = $self->{'assocfile_info'};
|
---|
| 226 | foreach my $prefix (keys %{$block_hash->{'shared_fileroot'}}) {
|
---|
| 227 | my $record = $block_hash->{'shared_fileroot'}->{$prefix};
|
---|
| 228 |
|
---|
| 229 | my $tie_to = $record->{'tie_to'};
|
---|
| 230 | my $exts = $record->{'exts'};
|
---|
| 231 |
|
---|
| 232 | if ((defined $tie_to) && (scalar (keys %$exts) > 0)) {
|
---|
| 233 | # set up fileblocks and assocfile_tobe
|
---|
| 234 | my $base_file = "$prefix$tie_to";
|
---|
| 235 | $metadata->{$base_file} = {} unless defined $metadata->{$base_file};
|
---|
| 236 | my $base_file_metadata = $metadata->{$base_file};
|
---|
| 237 |
|
---|
| 238 | $base_file_metadata->{'gsdlassocfile_tobe'} = [] unless defined $base_file_metadata->{'gsdlassocfile_tobe'};
|
---|
| 239 | my $assoc_tobe = $base_file_metadata->{'gsdlassocfile_tobe'};
|
---|
| 240 | foreach my $e (keys %$exts) {
|
---|
| 241 | # block the file
|
---|
[23561] | 242 | &util::block_filename($block_hash,"$prefix$e");
|
---|
[16391] | 243 | # set up as an associatd file
|
---|
| 244 | print STDERR " $self->{'plugin_type'}: Associating $prefix$e with $tie_to version\n";
|
---|
| 245 | my $mime_type = ""; # let system auto detect this
|
---|
| 246 | push(@$assoc_tobe,"$prefix$e:$mime_type:");
|
---|
| 247 |
|
---|
| 248 | }
|
---|
| 249 | }
|
---|
| 250 | } # foreach record
|
---|
| 251 |
|
---|
| 252 | $block_hash->{'shared_fileroot'} = undef;
|
---|
| 253 | $block_hash->{'shared_fileroot'} = {};
|
---|
| 254 |
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 |
|
---|
| 258 | # do block exp OR special blocking ???
|
---|
| 259 |
|
---|
| 260 | sub file_is_blocked {
|
---|
| 261 | my $self = shift (@_);
|
---|
| 262 | my ($block_hash, $filename_full_path) = @_;
|
---|
| 263 |
|
---|
[23363] | 264 | $filename_full_path = &util::upgrade_if_dos_filename($filename_full_path);
|
---|
| 265 |
|
---|
[28489] | 266 | if (($ENV{'GSDLOS'} =~ m/^windows$/) && ($^O ne "cygwin")) {
|
---|
[23544] | 267 | # on windows, all block paths are lowercased.
|
---|
| 268 | my $lower_filename = lc ($filename_full_path);
|
---|
| 269 | if (defined $block_hash->{'file_blocks'}->{$lower_filename}) {
|
---|
| 270 | $self->{'num_blocked'} ++;
|
---|
| 271 | return 1;
|
---|
| 272 | }
|
---|
[16391] | 273 | }
|
---|
[23544] | 274 | else {
|
---|
| 275 | if (defined $block_hash->{'file_blocks'}->{$filename_full_path}) {
|
---|
| 276 | $self->{'num_blocked'} ++;
|
---|
| 277 | return 1;
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
[16391] | 280 | # check Directory plugin's own block_exp
|
---|
| 281 | if ($self->{'block_exp'} ne "" && $filename_full_path =~ /$self->{'block_exp'}/) {
|
---|
| 282 | $self->{'num_blocked'} ++;
|
---|
| 283 | return 1; # blocked
|
---|
| 284 | }
|
---|
| 285 | return 0;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 |
|
---|
| 289 |
|
---|
| 290 | sub file_block_read {
|
---|
| 291 | my $self = shift (@_);
|
---|
| 292 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli) = @_;
|
---|
| 293 |
|
---|
| 294 | my $outhandle = $self->{'outhandle'};
|
---|
| 295 | my $verbosity = $self->{'verbosity'};
|
---|
[2813] | 296 |
|
---|
[16391] | 297 | # Calculate the directory name and ensure it is a directory and
|
---|
| 298 | # that it is not explicitly blocked.
|
---|
| 299 | my $dirname = $file;
|
---|
[27306] | 300 | $dirname = &FileUtils::filenameConcatenate($base_dir, $file) if $base_dir =~ /\w/;
|
---|
[16391] | 301 |
|
---|
| 302 | my $directory_ok = $self->check_directory_path($dirname);
|
---|
| 303 | return $directory_ok unless (defined $directory_ok && $directory_ok == 1);
|
---|
| 304 |
|
---|
[18523] | 305 | print $outhandle "Global file scan checking directory: $dirname\n";
|
---|
| 306 |
|
---|
[18441] | 307 | $block_hash->{'all_files'} = {} unless defined $block_hash->{'all_files'};
|
---|
[20577] | 308 | $block_hash->{'metadata_files'} = {} unless defined $block_hash->{'metadata_files'};
|
---|
[18441] | 309 |
|
---|
[16391] | 310 | $block_hash->{'file_blocks'} = {} unless defined $block_hash->{'file_blocks'};
|
---|
| 311 | $block_hash->{'shared_fileroot'} = {} unless defined $block_hash->{'shared_fileroot'};
|
---|
| 312 |
|
---|
| 313 | # Recur over directory contents.
|
---|
| 314 | my (@dir, $subfile);
|
---|
| 315 | #my $count = 0;
|
---|
| 316 |
|
---|
| 317 | print $outhandle "DirectoryPlugin block: getting directory $dirname\n" if ($verbosity > 2);
|
---|
| 318 |
|
---|
| 319 | # find all the files in the directory
|
---|
| 320 | if (!opendir (DIR, $dirname)) {
|
---|
| 321 | if ($gli) {
|
---|
| 322 | print STDERR "<ProcessingError n='$file' r='Could not read directory $dirname'>\n";
|
---|
| 323 | }
|
---|
| 324 | print $outhandle "DirectoryPlugin: WARNING - couldn't read directory $dirname\n";
|
---|
| 325 | return -1; # error in processing
|
---|
| 326 | }
|
---|
[27578] | 327 | @dir = sort readdir (DIR);
|
---|
[16391] | 328 | closedir (DIR);
|
---|
| 329 |
|
---|
| 330 | for (my $i = 0; $i < scalar(@dir); $i++) {
|
---|
[23335] | 331 | my $raw_subfile = $dir[$i];
|
---|
| 332 | next if ($raw_subfile =~ m/^\.\.?$/);
|
---|
| 333 |
|
---|
[16391] | 334 | my $this_file_base_dir = $base_dir;
|
---|
[27306] | 335 | my $raw_file_subfile = &FileUtils::filenameConcatenate($file, $raw_subfile);
|
---|
[16391] | 336 |
|
---|
[23335] | 337 | # Recursively read each $raw_subfile
|
---|
[29745] | 338 | print $outhandle "DirectoryPlugin block recurring: ". Encode::decode("utf8", $raw_file_subfile) ."\n" if ($verbosity > 2);
|
---|
[29760] | 339 | print $outhandle "DirectoryPlugin block recurring: ". Encode::decode(locale =>$raw_file_subfile) ."\n" if ($verbosity > 2);
|
---|
[16391] | 340 |
|
---|
| 341 | #$count += &plugin::file_block_read ($pluginfo, $this_file_base_dir,
|
---|
[23335] | 342 |
|
---|
[16391] | 343 | &plugin::file_block_read ($pluginfo, $this_file_base_dir,
|
---|
[23335] | 344 | $raw_file_subfile,
|
---|
[16391] | 345 | $block_hash, $metadata, $gli);
|
---|
| 346 |
|
---|
| 347 | }
|
---|
| 348 | $self->sort_out_associated_files($block_hash);
|
---|
| 349 | #return $count;
|
---|
[23419] | 350 | return 1;
|
---|
[16391] | 351 |
|
---|
| 352 | }
|
---|
[17738] | 353 |
|
---|
| 354 | # We don't do metadata_read
|
---|
| 355 | sub metadata_read {
|
---|
| 356 | my $self = shift (@_);
|
---|
[19493] | 357 | my ($pluginfo, $base_dir, $file, $block_hash,
|
---|
| 358 | $extrametakeys, $extrametadata, $extrametafile,
|
---|
[23212] | 359 | $processor, $gli, $aux) = @_;
|
---|
[17738] | 360 |
|
---|
| 361 | return undef;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 |
|
---|
[16391] | 365 | # return number of files processed, undef if can't process
|
---|
| 366 | # Note that $base_dir might be "" and that $file might
|
---|
| 367 | # include directories
|
---|
| 368 |
|
---|
| 369 | # This function passes around metadata hash structures. Metadata hash
|
---|
| 370 | # structures are hashes that map from a (scalar) key (the metadata element
|
---|
| 371 | # name) to either a scalar metadata value or a reference to an array of
|
---|
| 372 | # such values.
|
---|
| 373 |
|
---|
| 374 | sub read {
|
---|
| 375 | my $self = shift (@_);
|
---|
| 376 | my ($pluginfo, $base_dir, $file, $block_hash, $in_metadata, $processor, $maxdocs, $total_count, $gli) = @_;
|
---|
[17320] | 377 |
|
---|
[16391] | 378 | my $outhandle = $self->{'outhandle'};
|
---|
| 379 | my $verbosity = $self->{'verbosity'};
|
---|
[24349] | 380 |
|
---|
[16391] | 381 | # Calculate the directory name and ensure it is a directory and
|
---|
| 382 | # that it is not explicitly blocked.
|
---|
| 383 | my $dirname;
|
---|
| 384 | if ($file eq "") {
|
---|
| 385 | $dirname = $base_dir;
|
---|
| 386 | } else {
|
---|
| 387 | $dirname = $file;
|
---|
[27306] | 388 | $dirname = &FileUtils::filenameConcatenate($base_dir, $file) if $base_dir =~ /\w/;
|
---|
[16391] | 389 | }
|
---|
[24932] | 390 |
|
---|
[16391] | 391 | my $directory_ok = $self->check_directory_path($dirname);
|
---|
| 392 | return $directory_ok unless (defined $directory_ok && $directory_ok == 1);
|
---|
| 393 |
|
---|
[2228] | 394 | if (($verbosity > 2) && ((scalar keys %$in_metadata) > 0)) {
|
---|
[15870] | 395 | print $outhandle "DirectoryPlugin: metadata passed in: ",
|
---|
[2813] | 396 | join(", ", keys %$in_metadata), "\n";
|
---|
[2228] | 397 | }
|
---|
[2813] | 398 |
|
---|
[16391] | 399 |
|
---|
[2228] | 400 | # Recur over directory contents.
|
---|
| 401 | my (@dir, $subfile);
|
---|
[6332] | 402 |
|
---|
[16391] | 403 | print $outhandle "DirectoryPlugin read: getting directory $dirname\n" if ($verbosity > 2);
|
---|
[2813] | 404 |
|
---|
[2228] | 405 | # find all the files in the directory
|
---|
| 406 | if (!opendir (DIR, $dirname)) {
|
---|
[9584] | 407 | if ($gli) {
|
---|
| 408 | print STDERR "<ProcessingError n='$file' r='Could not read directory $dirname'>\n";
|
---|
| 409 | }
|
---|
[15870] | 410 | print $outhandle "DirectoryPlugin: WARNING - couldn't read directory $dirname\n";
|
---|
[7362] | 411 | return -1; # error in processing
|
---|
[2228] | 412 | }
|
---|
[27578] | 413 | @dir = sort readdir (DIR);
|
---|
[29795] | 414 | map { $_ = &unicode::raw_filename_to_url_encoded($_); print STDERR "****$_\n"; } @dir;
|
---|
[2228] | 415 | closedir (DIR);
|
---|
[29795] | 416 | #$_ = &util::get_dirsep_tail(&util::upgrade_if_dos_filename(&FileUtils::filenameConcatenate($dirname, $_), 1));
|
---|
[7686] | 417 | # Re-order the files in the list so any directories ending with .all are moved to the end
|
---|
[8716] | 418 | for (my $i = scalar(@dir) - 1; $i >= 0; $i--) {
|
---|
[29795] | 419 | #print STDERR "*****file = $dir[$i]\n";
|
---|
| 420 | #my $long = &Win32::GetLongPathName(&util::filename_cat($dirname,$dir[$i]));
|
---|
| 421 | #print STDERR "long = $long\n";
|
---|
| 422 | #my $upgrade = &util::upgrade_if_dos_filename($dir[$i], 1);
|
---|
| 423 | #print STDERR "upgrade = $upgrade\n";
|
---|
[27306] | 424 | if (-d &FileUtils::filenameConcatenate($dirname, $dir[$i]) && $dir[$i] =~ /\.all$/) {
|
---|
[7686] | 425 | push(@dir, splice(@dir, $i, 1));
|
---|
| 426 | }
|
---|
| 427 | }
|
---|
| 428 |
|
---|
[13188] | 429 | # setup the metadata structures. we do a metadata_read pass to see if there is any additional metadata, then pass it to read
|
---|
| 430 |
|
---|
[2228] | 431 | my $additionalmetadata = 0; # is there extra metadata available?
|
---|
| 432 | my %extrametadata; # maps from filespec to extra metadata keys
|
---|
[19493] | 433 | my %extrametafile; # maps from filespec to the metadata.xml (or similar) file it came from
|
---|
[2228] | 434 | my @extrametakeys; # keys of %extrametadata in order read
|
---|
[8512] | 435 |
|
---|
[16391] | 436 |
|
---|
[24932] | 437 | my $os_dirsep = &util::get_os_dirsep();
|
---|
[8737] | 438 | my $dirsep = &util::get_dirsep();
|
---|
[11919] | 439 | my $base_dir_regexp = $base_dir;
|
---|
| 440 | $base_dir_regexp =~ s/\//$os_dirsep/g;
|
---|
[24932] | 441 |
|
---|
| 442 | # Want to get relative path of local_dirname within the base_directory
|
---|
| 443 | # but with URL style slashes.
|
---|
| 444 | my $local_dirname = &util::filename_within_directory_url_format($dirname, $base_dir);
|
---|
| 445 |
|
---|
[22896] | 446 | # if we are in import folder, then local_dirname will be empty
|
---|
| 447 | if ($local_dirname ne "") {
|
---|
[24932] | 448 | # look for extra metadata passed down from higher folders
|
---|
| 449 | $local_dirname .= "/"; # closing slash must be URL type slash also and not $dirsep;
|
---|
[22896] | 450 | if (defined $self->{'subdir_extrametakeys'}->{$local_dirname}) {
|
---|
| 451 | my $extrakeys = $self->{'subdir_extrametakeys'}->{$local_dirname};
|
---|
| 452 | foreach my $ek (@$extrakeys) {
|
---|
| 453 | my $extrakeys_re = $ek->{'re'};
|
---|
| 454 | my $extrakeys_md = $ek->{'md'};
|
---|
| 455 | my $extrakeys_mf = $ek->{'mf'};
|
---|
[24951] | 456 | &extrametautil::addmetakey(\@extrametakeys, $extrakeys_re);
|
---|
| 457 | &extrametautil::setmetadata(\%extrametadata, $extrakeys_re, $extrakeys_md);
|
---|
| 458 | &extrametautil::setmetafile(\%extrametafile, $extrakeys_re, $extrakeys_mf);
|
---|
[22896] | 459 | }
|
---|
| 460 | delete($self->{'subdir_extrametakeys'}->{$local_dirname});
|
---|
[8737] | 461 | }
|
---|
| 462 | }
|
---|
[23212] | 463 | # apply metadata pass for each of the files in the directory -- ignore
|
---|
| 464 | # maxdocs here
|
---|
[7686] | 465 | my $num_files = scalar(@dir);
|
---|
[8512] | 466 | for (my $i = 0; $i < scalar(@dir); $i++) {
|
---|
| 467 | my $subfile = $dir[$i];
|
---|
[23335] | 468 | next if ($subfile =~ m/^\.\.?$/);
|
---|
| 469 |
|
---|
[8512] | 470 | my $this_file_base_dir = $base_dir;
|
---|
[23335] | 471 | my $raw_subfile = &unicode::url_encoded_to_raw_filename($subfile);
|
---|
| 472 |
|
---|
[27306] | 473 | my $raw_file_subfile = &FileUtils::filenameConcatenate($file, $raw_subfile);
|
---|
| 474 | my $raw_full_filename = &FileUtils::filenameConcatenate($this_file_base_dir, $raw_file_subfile);
|
---|
[23335] | 475 |
|
---|
| 476 | if ($self->file_is_blocked($block_hash,$raw_full_filename)) {
|
---|
| 477 | print STDERR "DirectoryPlugin: file $raw_full_filename was blocked for metadata_read\n" if ($verbosity > 2);
|
---|
[16391] | 478 | next;
|
---|
| 479 | }
|
---|
| 480 |
|
---|
[23335] | 481 | # Recursively read each $raw_subfile
|
---|
| 482 | print $outhandle "DirectoryPlugin metadata recurring: $raw_subfile\n" if ($verbosity > 2);
|
---|
[8512] | 483 |
|
---|
[23212] | 484 | &plugin::metadata_read ($pluginfo, $this_file_base_dir,
|
---|
[23335] | 485 | $raw_file_subfile,$block_hash,
|
---|
[23212] | 486 | \@extrametakeys, \%extrametadata,
|
---|
| 487 | \%extrametafile,
|
---|
| 488 | $processor, $gli);
|
---|
[8512] | 489 | $additionalmetadata = 1;
|
---|
| 490 | }
|
---|
[16391] | 491 |
|
---|
[8737] | 492 | # filter out any extrametakeys that mention subdirectories and store
|
---|
| 493 | # for later use (i.e. when that sub-directory is being processed)
|
---|
[25094] | 494 | foreach my $ek (@extrametakeys) { # where each Extrametakey (which is a filename) is stored as a url-style regex
|
---|
| 495 |
|
---|
| 496 | my ($subdir_re,$extrakey_dir) = &util::url_fileparse($ek);
|
---|
| 497 |
|
---|
| 498 | if ($extrakey_dir ne "") {
|
---|
| 499 | # a subdir was specified
|
---|
[24951] | 500 | my $md = &extrametautil::getmetadata(\%extrametadata, $ek);
|
---|
| 501 | my $mf = &extrametautil::getmetafile(\%extrametafile, $ek);
|
---|
[8737] | 502 |
|
---|
| 503 | my $subdir_extrametakeys = $self->{'subdir_extrametakeys'};
|
---|
[19493] | 504 | my $subdir_rec = { 're' => $subdir_re, 'md' => $md, 'mf' => $mf };
|
---|
[15005] | 505 |
|
---|
[24951] | 506 | # when it's looked up, it must be relative to the base dir
|
---|
[17320] | 507 | push(@{$subdir_extrametakeys->{"$local_dirname$extrakey_dir"}},$subdir_rec);
|
---|
[8737] | 508 | }
|
---|
| 509 | }
|
---|
[8512] | 510 |
|
---|
| 511 | # import each of the files in the directory
|
---|
[23212] | 512 | my $count=0;
|
---|
[7686] | 513 | for (my $i = 0; $i <= scalar(@dir); $i++) {
|
---|
| 514 | # When every file in the directory has been done, pause for a moment (figuratively!)
|
---|
| 515 | # If the -recheck_directories argument hasn't been provided, stop now (default)
|
---|
| 516 | # Otherwise, re-read the contents of the directory to check for new files
|
---|
| 517 | # Any new files are added to the @dir list and are processed as normal
|
---|
| 518 | # This is necessary when documents to be indexed are specified in bibliographic DBs
|
---|
| 519 | # These files are copied/downloaded and stored in a new folder at import time
|
---|
| 520 | if ($i == $num_files) {
|
---|
| 521 | last unless $self->{'recheck_directories'};
|
---|
| 522 |
|
---|
| 523 | # Re-read the files in the directory to see if there are any new files
|
---|
| 524 | last if (!opendir (DIR, $dirname));
|
---|
[27578] | 525 | my @dirnow = sort readdir (DIR);
|
---|
[23335] | 526 | map { $_ = &unicode::raw_filename_to_url_encoded($_) } @dirnow;
|
---|
[7686] | 527 | closedir (DIR);
|
---|
| 528 |
|
---|
| 529 | # We're only interested if there are more files than there were before
|
---|
| 530 | last if (scalar(@dirnow) <= scalar(@dir));
|
---|
| 531 |
|
---|
| 532 | # Any new files are added to the end of @dir to get processed by the loop
|
---|
[8716] | 533 | my $j;
|
---|
[7686] | 534 | foreach my $subfilenow (@dirnow) {
|
---|
| 535 | for ($j = 0; $j < $num_files; $j++) {
|
---|
| 536 | last if ($subfilenow eq $dir[$j]);
|
---|
| 537 | }
|
---|
| 538 | if ($j == $num_files) {
|
---|
| 539 | # New file
|
---|
| 540 | push(@dir, $subfilenow);
|
---|
| 541 | }
|
---|
| 542 | }
|
---|
| 543 | # When the new files have been processed, check again
|
---|
| 544 | $num_files = scalar(@dir);
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 | my $subfile = $dir[$i];
|
---|
[9853] | 548 | last if ($maxdocs != -1 && ($count + $total_count) >= $maxdocs);
|
---|
[2228] | 549 | next if ($subfile =~ /^\.\.?$/);
|
---|
[29795] | 550 | print STDERR "poo poo poo\n";
|
---|
[23335] | 551 | my $this_file_base_dir = $base_dir;
|
---|
| 552 | my $raw_subfile = &unicode::url_encoded_to_raw_filename($subfile);
|
---|
[29795] | 553 | my $unicode_subfile = &util::raw_filename_to_unicode($dirname, $raw_subfile);
|
---|
| 554 | print STDERR "raw = $raw_subfile, unicode = $unicode_subfile\n";
|
---|
[29763] | 555 |
|
---|
[27306] | 556 | my $raw_file_subfile = &FileUtils::filenameConcatenate($file, $raw_subfile);
|
---|
[23335] | 557 | my $raw_full_filename
|
---|
[27306] | 558 | = &FileUtils::filenameConcatenate($this_file_base_dir,$raw_file_subfile);
|
---|
[23335] | 559 |
|
---|
| 560 | if ($self->file_is_blocked($block_hash,$raw_full_filename)) {
|
---|
| 561 | print STDERR "DirectoryPlugin: file $raw_full_filename was blocked for read\n" if ($verbosity > 2);
|
---|
[16391] | 562 | next;
|
---|
| 563 | }
|
---|
[29795] | 564 | print STDERR "** Dir Plugin processing $raw_full_filename\n";
|
---|
[7932] | 565 | # Follow Windows shortcuts
|
---|
[28489] | 566 | if ($raw_subfile =~ m/(?i)\.lnk$/ && (($ENV{'GSDLOS'} =~ m/^windows$/i) && ($^O ne "cygwin"))) {
|
---|
[7932] | 567 | require Win32::Shortcut;
|
---|
[27306] | 568 | my $shortcut = new Win32::Shortcut(&FileUtils::filenameConcatenate($dirname, $raw_subfile));
|
---|
[7932] | 569 | if ($shortcut) {
|
---|
| 570 | # The file to be processed is now the target of the shortcut
|
---|
| 571 | $this_file_base_dir = "";
|
---|
| 572 | $file = "";
|
---|
[23335] | 573 | $raw_subfile = $shortcut->Path;
|
---|
[7932] | 574 | }
|
---|
| 575 | }
|
---|
| 576 |
|
---|
[3108] | 577 | # check for a symlink pointing back to a leading directory
|
---|
[23335] | 578 | if (-d "$dirname/$raw_subfile" && -l "$dirname/$raw_subfile") {
|
---|
[3108] | 579 | # readlink gives a "fatal error" on systems that don't implement
|
---|
| 580 | # symlinks. This assumes the the -l test above would fail on those.
|
---|
[23335] | 581 | my $linkdest=readlink "$dirname/$raw_subfile";
|
---|
[3108] | 582 | if (!defined ($linkdest)) {
|
---|
| 583 | # system error - file not found?
|
---|
[15870] | 584 | warn "DirectoryPlugin: symlink problem - $!";
|
---|
[3108] | 585 | } else {
|
---|
| 586 | # see if link points to current or a parent directory
|
---|
| 587 | if ($linkdest =~ m@^[\./\\]+$@ ||
|
---|
| 588 | index($dirname, $linkdest) != -1) {
|
---|
[23335] | 589 | warn "DirectoryPlugin: Ignoring recursive symlink ($dirname/$raw_subfile -> $linkdest)\n";
|
---|
[3108] | 590 | next;
|
---|
| 591 | ;
|
---|
| 592 | }
|
---|
| 593 | }
|
---|
| 594 | }
|
---|
| 595 |
|
---|
[23335] | 596 | print $outhandle "DirectoryPlugin: preparing metadata for $raw_subfile\n" if ($verbosity > 2);
|
---|
[317] | 597 |
|
---|
[23335] | 598 | # Make a copy of $in_metadata to pass to $raw_subfile
|
---|
[17313] | 599 | my $out_metadata = {};
|
---|
[13188] | 600 | &metadatautil::combine_metadata_structures($out_metadata, $in_metadata);
|
---|
[317] | 601 |
|
---|
[16391] | 602 | # check the assocfile_info
|
---|
[23335] | 603 | if (defined $self->{'assocfile_info'}->{$raw_full_filename}) {
|
---|
| 604 | &metadatautil::combine_metadata_structures($out_metadata, $self->{'assocfile_info'}->{$raw_full_filename});
|
---|
[16391] | 605 | }
|
---|
[23335] | 606 |
|
---|
[29745] | 607 | ### $subfile by this point is url-encoded => all ASCII chars => no need to encode as UTF8
|
---|
[29763] | 608 | print STDERR "****** subfile = $subfile, raw_subfile = $raw_subfile\n";
|
---|
| 609 | print STDERR &unicode::debug_unicode_string("subfile = $subfile, raw_subfile = $raw_subfile\n");
|
---|
[29795] | 610 | print STDERR "looking up unicode version $unicode_subfile (". &unicode::debug_unicode_string($unicode_subfile).") \n";
|
---|
[29745] | 611 | # instead of using the subfile, we need unicode aware string
|
---|
| 612 | ###my $lookup_name = decode("utf8", $raw_subfile);
|
---|
[29795] | 613 | ####print STDERR "lookup nmae = $lookup_name ( \n";
|
---|
[2228] | 614 | # Next add metadata read in XML files (if it is supplied)
|
---|
| 615 | if ($additionalmetadata == 1) {
|
---|
[19493] | 616 | foreach my $filespec (@extrametakeys) {
|
---|
[23335] | 617 | ## use the url-encoded filename to do the filename comparison
|
---|
[29763] | 618 | print STDERR "### comparing against filespec $filespec (";
|
---|
[29760] | 619 | print STDERR &unicode::debug_unicode_string("$filespec");
|
---|
| 620 | print STDERR ")\n";
|
---|
[29745] | 621 | if ($unicode_subfile =~ /^$filespec$/) {
|
---|
| 622 | ###if ($lookup_name =~ /^$filespec$/) {
|
---|
[29795] | 623 | print $outhandle "^^^^^^^^^File \"$unicode_subfile\" matches filespec \"$filespec\"\n"
|
---|
[2228] | 624 | if ($verbosity > 2);
|
---|
[24951] | 625 | my $mdref = &extrametautil::getmetadata(\%extrametadata, $filespec);
|
---|
| 626 | my $mfref = &extrametautil::getmetafile(\%extrametafile, $filespec);
|
---|
[19493] | 627 |
|
---|
| 628 | # Add the list files where the metadata came from
|
---|
| 629 | # into the metadata table so we can track this
|
---|
| 630 | # This mechanism is similar to how gsdlassocfile works
|
---|
| 631 |
|
---|
| 632 | my @metafile_pair = ();
|
---|
| 633 | foreach my $l (keys %$mfref) {
|
---|
| 634 | my $f = $mfref->{$l};
|
---|
[19516] | 635 | push (@metafile_pair, "$f : $l");
|
---|
[19493] | 636 | }
|
---|
| 637 |
|
---|
| 638 | $mdref->{'gsdlmetafile'} = \@metafile_pair;
|
---|
| 639 |
|
---|
[13188] | 640 | &metadatautil::combine_metadata_structures($out_metadata, $mdref);
|
---|
[2228] | 641 | }
|
---|
[4] | 642 | }
|
---|
| 643 | }
|
---|
[10156] | 644 |
|
---|
| 645 | if (defined $self->{'inf_timestamp'}) {
|
---|
[18469] | 646 | # Look to see if it's a completely new file
|
---|
[10156] | 647 |
|
---|
[23335] | 648 | if (!$block_hash->{'new_files'}->{$raw_full_filename}) {
|
---|
[18469] | 649 | # Not a new file, must be an existing file
|
---|
| 650 | # Let' see if it's newer than the last import.pl
|
---|
| 651 |
|
---|
| 652 |
|
---|
[23335] | 653 | if (! -d $raw_full_filename) {
|
---|
| 654 | if (!$block_hash->{'reindex_files'}->{$raw_full_filename}) {
|
---|
[20577] | 655 | # filename has been around for longer than inf_timestamp
|
---|
[29745] | 656 | print $outhandle "**** Skipping $unicode_subfile\n" if ($verbosity >3);
|
---|
[18469] | 657 | next;
|
---|
| 658 | }
|
---|
| 659 | else {
|
---|
| 660 | # Remove old folder in archives (might hash to something different)
|
---|
| 661 | # *** should be doing this on a Del one as well
|
---|
| 662 | # but leave folder name?? and ensure hashs to
|
---|
| 663 | # same again??
|
---|
| 664 |
|
---|
| 665 | # Then let through as new doc??
|
---|
| 666 |
|
---|
| 667 | # mark to doc-oids that rely on it for re-indexing
|
---|
| 668 | }
|
---|
[10156] | 669 | }
|
---|
| 670 | }
|
---|
| 671 | }
|
---|
| 672 |
|
---|
[2228] | 673 | # Recursively read each $subfile
|
---|
[29745] | 674 | print $outhandle "DirectoryPlugin recurring: $unicode_subfile\n" if ($verbosity > 2);
|
---|
[8512] | 675 |
|
---|
| 676 | $count += &plugin::read ($pluginfo, $this_file_base_dir,
|
---|
[23335] | 677 | $raw_file_subfile, $block_hash,
|
---|
[9853] | 678 | $out_metadata, $processor, $maxdocs, ($total_count + $count), $gli);
|
---|
[4] | 679 | }
|
---|
[7686] | 680 |
|
---|
[8512] | 681 | return $count;
|
---|
[2228] | 682 | }
|
---|
[4] | 683 |
|
---|
[17738] | 684 | sub compile_stats {
|
---|
| 685 | my $self = shift(@_);
|
---|
| 686 | my ($stats) = @_;
|
---|
| 687 | }
|
---|
| 688 |
|
---|
[4] | 689 | 1;
|
---|