[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 |
|
---|
[17738] | 32 | use PrintInfo;
|
---|
[4] | 33 | use plugin;
|
---|
[136] | 34 | use util;
|
---|
[13188] | 35 | use metadatautil;
|
---|
[4] | 36 |
|
---|
[8737] | 37 | use File::Basename;
|
---|
[10254] | 38 | use strict;
|
---|
| 39 | no strict 'refs';
|
---|
[15870] | 40 | no strict 'subs';
|
---|
| 41 |
|
---|
[13545] | 42 | use Encode;
|
---|
[4] | 43 |
|
---|
| 44 | BEGIN {
|
---|
[17738] | 45 | @DirectoryPlugin::ISA = ('PrintInfo');
|
---|
[4] | 46 | }
|
---|
| 47 |
|
---|
[4744] | 48 | my $arguments =
|
---|
| 49 | [ { 'name' => "block_exp",
|
---|
[15870] | 50 | 'desc' => "{BasePlugin.block_exp}",
|
---|
[6408] | 51 | 'type' => "regexp",
|
---|
[4744] | 52 | 'deft' => &get_default_block_exp(),
|
---|
| 53 | 'reqd' => "no" },
|
---|
[13417] | 54 | # this option has been deprecated. leave it here for now so we can warn people not to use it
|
---|
[4744] | 55 | { 'name' => "use_metadata_files",
|
---|
[15870] | 56 | 'desc' => "{DirectoryPlugin.use_metadata_files}",
|
---|
[4744] | 57 | 'type' => "flag",
|
---|
[13188] | 58 | 'reqd' => "no",
|
---|
| 59 | 'hiddengli' => "yes" },
|
---|
[7686] | 60 | { 'name' => "recheck_directories",
|
---|
[15870] | 61 | 'desc' => "{DirectoryPlugin.recheck_directories}",
|
---|
[7686] | 62 | 'type' => "flag",
|
---|
[4744] | 63 | 'reqd' => "no" } ];
|
---|
[13188] | 64 |
|
---|
[15870] | 65 | my $options = { 'name' => "DirectoryPlugin",
|
---|
| 66 | 'desc' => "{DirectoryPlugin.desc}",
|
---|
[6408] | 67 | 'abstract' => "no",
|
---|
[4744] | 68 | 'inherits' => "yes",
|
---|
| 69 | 'args' => $arguments };
|
---|
[3540] | 70 |
|
---|
[4] | 71 | sub new {
|
---|
[10218] | 72 | my ($class) = shift (@_);
|
---|
| 73 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 74 | push(@$pluginlist, $class);
|
---|
[3540] | 75 |
|
---|
[15870] | 76 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 77 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[10218] | 78 |
|
---|
[17738] | 79 | my $self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists);
|
---|
[13188] | 80 |
|
---|
| 81 | if ($self->{'info_only'}) {
|
---|
| 82 | # don't worry about any options or initialisations etc
|
---|
| 83 | return bless $self, $class;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | # we have left this option in so we can warn people who are still using it
|
---|
[2813] | 87 | if ($self->{'use_metadata_files'}) {
|
---|
[15870] | 88 | die "ERROR: DirectoryPlugin -use_metadata_files option has been deprecated. Please remove the option and add MetadataXMLPlug to your plugin list instead!\n";
|
---|
[2813] | 89 | }
|
---|
[16391] | 90 |
|
---|
[17738] | 91 | $self->{'num_processed'} = 0;
|
---|
| 92 | $self->{'num_not_processed'} = 0;
|
---|
| 93 | $self->{'num_blocked'} = 0;
|
---|
| 94 | $self->{'num_archives'} = 0;
|
---|
| 95 |
|
---|
[8737] | 96 | $self->{'subdir_extrametakeys'} = {};
|
---|
| 97 |
|
---|
[4] | 98 | return bless $self, $class;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[17738] | 101 | # called once, at the start of processing
|
---|
| 102 | sub init {
|
---|
| 103 | my $self = shift (@_);
|
---|
| 104 | my ($verbosity, $outhandle, $failhandle) = @_;
|
---|
| 105 |
|
---|
| 106 | # verbosity is passed through from the processor
|
---|
| 107 | $self->{'verbosity'} = $verbosity;
|
---|
| 108 |
|
---|
| 109 | # as are the outhandle and failhandle
|
---|
| 110 | $self->{'outhandle'} = $outhandle if defined $outhandle;
|
---|
| 111 | $self->{'failhandle'} = $failhandle;
|
---|
| 112 |
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | # called once, after all passes have finished
|
---|
| 116 | sub deinit {
|
---|
| 117 | my ($self) = @_;
|
---|
| 118 |
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | # called at the beginning of each plugin pass (import has one, building has many)
|
---|
[10156] | 122 | sub begin {
|
---|
| 123 | my $self = shift (@_);
|
---|
| 124 | my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
|
---|
| 125 |
|
---|
| 126 | my $proc_package_name = ref $processor;
|
---|
| 127 |
|
---|
[12969] | 128 | if ($proc_package_name !~ /buildproc$/ && $self->{'incremental'} == 1) {
|
---|
[10156] | 129 |
|
---|
[12969] | 130 | # Only lookup timestamp info for import.pl, and only if incremental is set
|
---|
[10156] | 131 |
|
---|
| 132 | my $output_dir = $processor->getoutputdir();
|
---|
| 133 | my $archives_inf = &util::filename_cat($output_dir,"archives.inf");
|
---|
| 134 |
|
---|
| 135 | if ( -e $archives_inf ) {
|
---|
| 136 | $self->{'inf_timestamp'} = -M $archives_inf;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[17738] | 141 | # called at the end of each plugin pass
|
---|
[15870] | 142 | sub end {
|
---|
[17738] | 143 | my ($self) = shift (@_);
|
---|
[10156] | 144 |
|
---|
[15870] | 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
[17738] | 148 | # called if we are doing incremental building
|
---|
| 149 | sub set_incremental {
|
---|
| 150 | my $self = shift(@_);
|
---|
| 151 | my ($incremental) = @_;
|
---|
| 152 |
|
---|
| 153 | $self->{'incremental'} = $incremental;
|
---|
[15870] | 154 | }
|
---|
[17738] | 155 |
|
---|
[4] | 156 | # return 1 if this class might recurse using $pluginfo
|
---|
| 157 | sub is_recursive {
|
---|
| 158 | my $self = shift (@_);
|
---|
[2813] | 159 |
|
---|
[4] | 160 | return 1;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[2228] | 163 | sub get_default_block_exp {
|
---|
| 164 | my $self = shift (@_);
|
---|
[2813] | 165 |
|
---|
[17543] | 166 | return '(?i)(CVS|\.svn|Thumbs\.db|~)$';
|
---|
[2228] | 167 | }
|
---|
| 168 |
|
---|
[16391] | 169 | sub check_directory_path {
|
---|
[2228] | 170 |
|
---|
[16391] | 171 | my $self = shift(@_);
|
---|
| 172 | my ($dirname) = @_;
|
---|
[2813] | 173 |
|
---|
[2228] | 174 | return undef unless (-d $dirname);
|
---|
[16391] | 175 |
|
---|
[2228] | 176 | return 0 if ($self->{'block_exp'} ne "" && $dirname =~ /$self->{'block_exp'}/);
|
---|
[7932] | 177 |
|
---|
[16391] | 178 | my $outhandle = $self->{'outhandle'};
|
---|
| 179 |
|
---|
[2228] | 180 | # check to make sure we're not reading the archives or index directory
|
---|
| 181 | my $gsdlhome = quotemeta($ENV{'GSDLHOME'});
|
---|
[2813] | 182 | if ($dirname =~ m/^$gsdlhome\/.*?\/import.*?\/(archives|index)$/) {
|
---|
[15870] | 183 | print $outhandle "DirectoryPlugin: $dirname appears to be a reference to a Greenstone collection, skipping.\n";
|
---|
[2228] | 184 | return 0;
|
---|
[1755] | 185 | }
|
---|
[2813] | 186 |
|
---|
[1755] | 187 | # check to see we haven't got a cyclic path...
|
---|
| 188 | if ($dirname =~ m%(/.*){,41}%) {
|
---|
[15870] | 189 | print $outhandle "DirectoryPlugin: $dirname is 40 directories deep, is this a recursive path? if not increase constant in DirectoryPlugin.pm.\n";
|
---|
[2228] | 190 | return 0;
|
---|
[1755] | 191 | }
|
---|
[2813] | 192 |
|
---|
[1755] | 193 | # check to see we haven't got a cyclic path...
|
---|
| 194 | if ($dirname =~ m%.*?import/(.+?)/import/\1.*%) {
|
---|
[15870] | 195 | print $outhandle "DirectoryPlugin: $dirname appears to be in a recursive loop...\n";
|
---|
[2228] | 196 | return 0;
|
---|
[1755] | 197 | }
|
---|
[16391] | 198 |
|
---|
| 199 | return 1;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | # this may be called more than once
|
---|
| 203 | sub sort_out_associated_files {
|
---|
| 204 |
|
---|
| 205 | my $self = shift (@_);
|
---|
| 206 | my ($block_hash) = @_;
|
---|
| 207 | if (!scalar (keys %{$block_hash->{'shared_fileroot'}})) {
|
---|
| 208 | return;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | $self->{'assocfile_info'} = {} unless defined $self->{'assocfile_info'};
|
---|
| 212 | my $metadata = $self->{'assocfile_info'};
|
---|
| 213 | foreach my $prefix (keys %{$block_hash->{'shared_fileroot'}}) {
|
---|
| 214 | my $record = $block_hash->{'shared_fileroot'}->{$prefix};
|
---|
| 215 |
|
---|
| 216 | my $tie_to = $record->{'tie_to'};
|
---|
| 217 | my $exts = $record->{'exts'};
|
---|
| 218 |
|
---|
| 219 | if ((defined $tie_to) && (scalar (keys %$exts) > 0)) {
|
---|
| 220 | # set up fileblocks and assocfile_tobe
|
---|
| 221 | my $base_file = "$prefix$tie_to";
|
---|
| 222 | $metadata->{$base_file} = {} unless defined $metadata->{$base_file};
|
---|
| 223 | my $base_file_metadata = $metadata->{$base_file};
|
---|
| 224 |
|
---|
| 225 | $base_file_metadata->{'gsdlassocfile_tobe'} = [] unless defined $base_file_metadata->{'gsdlassocfile_tobe'};
|
---|
| 226 | my $assoc_tobe = $base_file_metadata->{'gsdlassocfile_tobe'};
|
---|
| 227 | foreach my $e (keys %$exts) {
|
---|
| 228 | # block the file
|
---|
| 229 | $block_hash->{'file_blocks'}->{"$prefix$e"} = 1;
|
---|
| 230 | # set up as an associatd file
|
---|
| 231 | print STDERR " $self->{'plugin_type'}: Associating $prefix$e with $tie_to version\n";
|
---|
| 232 | my $mime_type = ""; # let system auto detect this
|
---|
| 233 | push(@$assoc_tobe,"$prefix$e:$mime_type:");
|
---|
| 234 |
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 | } # foreach record
|
---|
| 238 |
|
---|
| 239 | $block_hash->{'shared_fileroot'} = undef;
|
---|
| 240 | $block_hash->{'shared_fileroot'} = {};
|
---|
| 241 |
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 |
|
---|
| 245 | # do block exp OR special blocking ???
|
---|
| 246 |
|
---|
| 247 | sub file_is_blocked {
|
---|
| 248 | my $self = shift (@_);
|
---|
| 249 | my ($block_hash, $filename_full_path) = @_;
|
---|
| 250 |
|
---|
| 251 | if (defined $block_hash->{'file_blocks'}->{$filename_full_path}) {
|
---|
| 252 | $self->{'num_blocked'} ++;
|
---|
| 253 | return 1;
|
---|
| 254 | }
|
---|
| 255 | # check Directory plugin's own block_exp
|
---|
| 256 | if ($self->{'block_exp'} ne "" && $filename_full_path =~ /$self->{'block_exp'}/) {
|
---|
| 257 | $self->{'num_blocked'} ++;
|
---|
| 258 | return 1; # blocked
|
---|
| 259 | }
|
---|
| 260 | return 0;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 |
|
---|
| 264 |
|
---|
| 265 | sub file_block_read {
|
---|
| 266 | my $self = shift (@_);
|
---|
| 267 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli) = @_;
|
---|
| 268 |
|
---|
| 269 | my $outhandle = $self->{'outhandle'};
|
---|
| 270 | my $verbosity = $self->{'verbosity'};
|
---|
[2813] | 271 |
|
---|
[16391] | 272 | # Calculate the directory name and ensure it is a directory and
|
---|
| 273 | # that it is not explicitly blocked.
|
---|
| 274 | my $dirname = $file;
|
---|
| 275 | $dirname = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
|
---|
| 276 |
|
---|
| 277 | my $directory_ok = $self->check_directory_path($dirname);
|
---|
| 278 | return $directory_ok unless (defined $directory_ok && $directory_ok == 1);
|
---|
| 279 |
|
---|
| 280 | $block_hash->{'file_blocks'} = {} unless defined $block_hash->{'file_blocks'};
|
---|
| 281 | $block_hash->{'shared_fileroot'} = {} unless defined $block_hash->{'shared_fileroot'};
|
---|
| 282 |
|
---|
| 283 | # Recur over directory contents.
|
---|
| 284 | my (@dir, $subfile);
|
---|
| 285 | #my $count = 0;
|
---|
| 286 |
|
---|
| 287 | print $outhandle "DirectoryPlugin block: getting directory $dirname\n" if ($verbosity > 2);
|
---|
| 288 |
|
---|
| 289 | # find all the files in the directory
|
---|
| 290 | if (!opendir (DIR, $dirname)) {
|
---|
| 291 | if ($gli) {
|
---|
| 292 | print STDERR "<ProcessingError n='$file' r='Could not read directory $dirname'>\n";
|
---|
| 293 | }
|
---|
| 294 | print $outhandle "DirectoryPlugin: WARNING - couldn't read directory $dirname\n";
|
---|
| 295 | return -1; # error in processing
|
---|
| 296 | }
|
---|
| 297 | @dir = readdir (DIR);
|
---|
| 298 | closedir (DIR);
|
---|
| 299 |
|
---|
| 300 | for (my $i = 0; $i < scalar(@dir); $i++) {
|
---|
| 301 | my $subfile = $dir[$i];
|
---|
| 302 | my $this_file_base_dir = $base_dir;
|
---|
| 303 | next if ($subfile =~ m/^\.\.?$/);
|
---|
| 304 |
|
---|
| 305 | # Recursively read each $subfile
|
---|
| 306 | print $outhandle "DirectoryPlugin block recurring: $subfile\n" if ($verbosity > 2);
|
---|
| 307 |
|
---|
| 308 | #$count += &plugin::file_block_read ($pluginfo, $this_file_base_dir,
|
---|
| 309 | &plugin::file_block_read ($pluginfo, $this_file_base_dir,
|
---|
| 310 | &util::filename_cat($file, $subfile),
|
---|
| 311 | $block_hash, $metadata, $gli);
|
---|
| 312 |
|
---|
| 313 | }
|
---|
| 314 | $self->sort_out_associated_files($block_hash);
|
---|
| 315 | #return $count;
|
---|
| 316 |
|
---|
| 317 | }
|
---|
[17738] | 318 |
|
---|
| 319 | # We don't do metadata_read
|
---|
| 320 | sub metadata_read {
|
---|
| 321 | my $self = shift (@_);
|
---|
| 322 | my ($pluginfo, $base_dir, $file, $block_hash, $extrametakeys, $extrametadata, $processor, $maxdocs, $gli) = @_;
|
---|
| 323 |
|
---|
| 324 | return undef;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 |
|
---|
[16391] | 328 | # return number of files processed, undef if can't process
|
---|
| 329 | # Note that $base_dir might be "" and that $file might
|
---|
| 330 | # include directories
|
---|
| 331 |
|
---|
| 332 | # This function passes around metadata hash structures. Metadata hash
|
---|
| 333 | # structures are hashes that map from a (scalar) key (the metadata element
|
---|
| 334 | # name) to either a scalar metadata value or a reference to an array of
|
---|
| 335 | # such values.
|
---|
| 336 |
|
---|
| 337 | sub read {
|
---|
| 338 | my $self = shift (@_);
|
---|
| 339 | my ($pluginfo, $base_dir, $file, $block_hash, $in_metadata, $processor, $maxdocs, $total_count, $gli) = @_;
|
---|
[17320] | 340 |
|
---|
[16391] | 341 | my $outhandle = $self->{'outhandle'};
|
---|
| 342 | my $verbosity = $self->{'verbosity'};
|
---|
| 343 |
|
---|
| 344 | # Calculate the directory name and ensure it is a directory and
|
---|
| 345 | # that it is not explicitly blocked.
|
---|
| 346 | my $dirname;
|
---|
| 347 | if ($file eq "") {
|
---|
| 348 | $dirname = $base_dir;
|
---|
| 349 | } else {
|
---|
| 350 | $dirname = $file;
|
---|
| 351 | $dirname = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | my $directory_ok = $self->check_directory_path($dirname);
|
---|
| 355 | return $directory_ok unless (defined $directory_ok && $directory_ok == 1);
|
---|
| 356 |
|
---|
[2228] | 357 | if (($verbosity > 2) && ((scalar keys %$in_metadata) > 0)) {
|
---|
[15870] | 358 | print $outhandle "DirectoryPlugin: metadata passed in: ",
|
---|
[2813] | 359 | join(", ", keys %$in_metadata), "\n";
|
---|
[2228] | 360 | }
|
---|
[2813] | 361 |
|
---|
[16391] | 362 |
|
---|
[2228] | 363 | # Recur over directory contents.
|
---|
| 364 | my (@dir, $subfile);
|
---|
| 365 | my $count = 0;
|
---|
[6332] | 366 |
|
---|
[16391] | 367 | print $outhandle "DirectoryPlugin read: getting directory $dirname\n" if ($verbosity > 2);
|
---|
[2813] | 368 |
|
---|
[2228] | 369 | # find all the files in the directory
|
---|
| 370 | if (!opendir (DIR, $dirname)) {
|
---|
[9584] | 371 | if ($gli) {
|
---|
| 372 | print STDERR "<ProcessingError n='$file' r='Could not read directory $dirname'>\n";
|
---|
| 373 | }
|
---|
[15870] | 374 | print $outhandle "DirectoryPlugin: WARNING - couldn't read directory $dirname\n";
|
---|
[7362] | 375 | return -1; # error in processing
|
---|
[2228] | 376 | }
|
---|
| 377 | @dir = readdir (DIR);
|
---|
| 378 | closedir (DIR);
|
---|
[7686] | 379 |
|
---|
| 380 | # Re-order the files in the list so any directories ending with .all are moved to the end
|
---|
[8716] | 381 | for (my $i = scalar(@dir) - 1; $i >= 0; $i--) {
|
---|
[7688] | 382 | if (-d &util::filename_cat($dirname, $dir[$i]) && $dir[$i] =~ /\.all$/) {
|
---|
[7686] | 383 | push(@dir, splice(@dir, $i, 1));
|
---|
| 384 | }
|
---|
| 385 | }
|
---|
| 386 |
|
---|
[13188] | 387 | # setup the metadata structures. we do a metadata_read pass to see if there is any additional metadata, then pass it to read
|
---|
| 388 |
|
---|
[2228] | 389 | my $additionalmetadata = 0; # is there extra metadata available?
|
---|
| 390 | my %extrametadata; # maps from filespec to extra metadata keys
|
---|
| 391 | my @extrametakeys; # keys of %extrametadata in order read
|
---|
[8512] | 392 |
|
---|
[16391] | 393 |
|
---|
[11919] | 394 | my $os_dirsep = &util::get_os_dirsep();
|
---|
[8737] | 395 | my $dirsep = &util::get_dirsep();
|
---|
[11919] | 396 | my $base_dir_regexp = $base_dir;
|
---|
| 397 | $base_dir_regexp =~ s/\//$os_dirsep/g;
|
---|
[8737] | 398 | my $local_dirname = $dirname;
|
---|
[11919] | 399 | $local_dirname =~ s/^$base_dir_regexp($os_dirsep)//;
|
---|
[8737] | 400 | $local_dirname .= $dirsep;
|
---|
| 401 |
|
---|
| 402 | if (defined $self->{'subdir_extrametakeys'}->{$local_dirname}) {
|
---|
| 403 | my $extrakeys = $self->{'subdir_extrametakeys'}->{$local_dirname};
|
---|
| 404 | foreach my $ek (@$extrakeys) {
|
---|
| 405 | my $extrakeys_re = $ek->{'re'};
|
---|
| 406 | my $extrakeys_md = $ek->{'md'};
|
---|
| 407 | push(@extrametakeys,$extrakeys_re);
|
---|
| 408 | $extrametadata{$extrakeys_re} = $extrakeys_md;
|
---|
| 409 | }
|
---|
| 410 | delete($self->{'subdir_extrametakeys'}->{$local_dirname});
|
---|
| 411 | }
|
---|
[8512] | 412 |
|
---|
[9145] | 413 | # apply metadata pass for each of the files in the directory
|
---|
[7686] | 414 | my $num_files = scalar(@dir);
|
---|
[8512] | 415 | for (my $i = 0; $i < scalar(@dir); $i++) {
|
---|
| 416 | my $subfile = $dir[$i];
|
---|
| 417 | my $this_file_base_dir = $base_dir;
|
---|
| 418 | last if ($maxdocs != -1 && $count >= $maxdocs);
|
---|
[9145] | 419 | next if ($subfile =~ m/^\.\.?$/);
|
---|
[16391] | 420 | my $file_subfile = &util::filename_cat($file, $subfile);
|
---|
| 421 | my $full_filename = &util::filename_cat($this_file_base_dir, $file_subfile);
|
---|
| 422 | if ($self->file_is_blocked($block_hash,$full_filename)) {
|
---|
| 423 | print STDERR "DirectoryPlugin: file $full_filename was blocked for metadata_read\n" if ($verbosity > 2);
|
---|
| 424 | next;
|
---|
| 425 | }
|
---|
| 426 |
|
---|
[8512] | 427 | # Recursively read each $subfile
|
---|
[15870] | 428 | print $outhandle "DirectoryPlugin metadata recurring: $subfile\n" if ($verbosity > 2);
|
---|
[8512] | 429 |
|
---|
| 430 | $count += &plugin::metadata_read ($pluginfo, $this_file_base_dir,
|
---|
[16391] | 431 | $file_subfile,$block_hash,
|
---|
[17313] | 432 | \@extrametakeys, \%extrametadata,
|
---|
[8512] | 433 | $processor, $maxdocs, $gli);
|
---|
| 434 | $additionalmetadata = 1;
|
---|
| 435 | }
|
---|
[16391] | 436 |
|
---|
[8737] | 437 | # filter out any extrametakeys that mention subdirectories and store
|
---|
| 438 | # for later use (i.e. when that sub-directory is being processed)
|
---|
| 439 |
|
---|
| 440 | foreach my $ek (@extrametakeys) {
|
---|
| 441 | my ($subdir_re,$extrakey_dir) = &File::Basename::fileparse($ek);
|
---|
| 442 | $extrakey_dir =~ s/\\\./\./g; # remove RE syntax
|
---|
| 443 |
|
---|
[9122] | 444 | my $dirsep_re = &util::get_re_dirsep();
|
---|
[15005] | 445 |
|
---|
| 446 | my $ek_non_re = $ek;
|
---|
| 447 | $ek_non_re =~ s/\\\./\./g; # remove RE syntax
|
---|
| 448 |
|
---|
| 449 | if ($ek_non_re =~ m/$dirsep_re/) { # specifies at least one directory
|
---|
[8737] | 450 | my $md = $extrametadata{$ek};
|
---|
| 451 |
|
---|
| 452 | my $subdir_extrametakeys = $self->{'subdir_extrametakeys'};
|
---|
| 453 |
|
---|
| 454 | my $subdir_rec = { 're' => $subdir_re, 'md' => $md };
|
---|
[15005] | 455 |
|
---|
[17320] | 456 | # when its looked up, it must be relative to the base dir
|
---|
| 457 | push(@{$subdir_extrametakeys->{"$local_dirname$extrakey_dir"}},$subdir_rec);
|
---|
| 458 | #push(@{$subdir_extrametakeys->{"$extrakey_dir"}},$subdir_rec);
|
---|
[8737] | 459 | }
|
---|
| 460 | }
|
---|
[8512] | 461 |
|
---|
| 462 | # import each of the files in the directory
|
---|
| 463 | $count=0;
|
---|
[7686] | 464 | for (my $i = 0; $i <= scalar(@dir); $i++) {
|
---|
| 465 | # When every file in the directory has been done, pause for a moment (figuratively!)
|
---|
| 466 | # If the -recheck_directories argument hasn't been provided, stop now (default)
|
---|
| 467 | # Otherwise, re-read the contents of the directory to check for new files
|
---|
| 468 | # Any new files are added to the @dir list and are processed as normal
|
---|
| 469 | # This is necessary when documents to be indexed are specified in bibliographic DBs
|
---|
| 470 | # These files are copied/downloaded and stored in a new folder at import time
|
---|
| 471 | if ($i == $num_files) {
|
---|
| 472 | last unless $self->{'recheck_directories'};
|
---|
| 473 |
|
---|
| 474 | # Re-read the files in the directory to see if there are any new files
|
---|
| 475 | last if (!opendir (DIR, $dirname));
|
---|
| 476 | my @dirnow = readdir (DIR);
|
---|
| 477 | closedir (DIR);
|
---|
| 478 |
|
---|
| 479 | # We're only interested if there are more files than there were before
|
---|
| 480 | last if (scalar(@dirnow) <= scalar(@dir));
|
---|
| 481 |
|
---|
| 482 | # Any new files are added to the end of @dir to get processed by the loop
|
---|
[8716] | 483 | my $j;
|
---|
[7686] | 484 | foreach my $subfilenow (@dirnow) {
|
---|
| 485 | for ($j = 0; $j < $num_files; $j++) {
|
---|
| 486 | last if ($subfilenow eq $dir[$j]);
|
---|
| 487 | }
|
---|
| 488 | if ($j == $num_files) {
|
---|
| 489 | # New file
|
---|
| 490 | push(@dir, $subfilenow);
|
---|
| 491 | }
|
---|
| 492 | }
|
---|
| 493 | # When the new files have been processed, check again
|
---|
| 494 | $num_files = scalar(@dir);
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | my $subfile = $dir[$i];
|
---|
[7932] | 498 | my $this_file_base_dir = $base_dir;
|
---|
[9853] | 499 | last if ($maxdocs != -1 && ($count + $total_count) >= $maxdocs);
|
---|
[2228] | 500 | next if ($subfile =~ /^\.\.?$/);
|
---|
[3108] | 501 |
|
---|
[16391] | 502 | my $file_subfile = &util::filename_cat($file, $subfile);
|
---|
| 503 | my $full_filename
|
---|
| 504 | = &util::filename_cat($this_file_base_dir,$file_subfile);
|
---|
| 505 |
|
---|
| 506 | if ($self->file_is_blocked($block_hash,$full_filename)) {
|
---|
| 507 | print STDERR "DirectoryPlugin: file $full_filename was blocked for read\n" if ($verbosity > 2);
|
---|
| 508 | next;
|
---|
| 509 | }
|
---|
[17313] | 510 | #print STDERR "processing $full_filename\n";
|
---|
[7932] | 511 | # Follow Windows shortcuts
|
---|
| 512 | if ($subfile =~ /(?i)\.lnk$/ && $ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 513 | require Win32::Shortcut;
|
---|
| 514 | my $shortcut = new Win32::Shortcut(&util::filename_cat($dirname, $subfile));
|
---|
| 515 | if ($shortcut) {
|
---|
| 516 | # The file to be processed is now the target of the shortcut
|
---|
| 517 | $this_file_base_dir = "";
|
---|
| 518 | $file = "";
|
---|
| 519 | $subfile = $shortcut->Path;
|
---|
| 520 | }
|
---|
| 521 | }
|
---|
| 522 |
|
---|
[3108] | 523 | # check for a symlink pointing back to a leading directory
|
---|
| 524 | if (-d "$dirname/$subfile" && -l "$dirname/$subfile") {
|
---|
| 525 | # readlink gives a "fatal error" on systems that don't implement
|
---|
| 526 | # symlinks. This assumes the the -l test above would fail on those.
|
---|
| 527 | my $linkdest=readlink "$dirname/$subfile";
|
---|
| 528 | if (!defined ($linkdest)) {
|
---|
| 529 | # system error - file not found?
|
---|
[15870] | 530 | warn "DirectoryPlugin: symlink problem - $!";
|
---|
[3108] | 531 | } else {
|
---|
| 532 | # see if link points to current or a parent directory
|
---|
| 533 | if ($linkdest =~ m@^[\./\\]+$@ ||
|
---|
| 534 | index($dirname, $linkdest) != -1) {
|
---|
[15870] | 535 | warn "DirectoryPlugin: Ignoring recursive symlink ($dirname/$subfile -> $linkdest)\n";
|
---|
[3108] | 536 | next;
|
---|
| 537 | ;
|
---|
| 538 | }
|
---|
| 539 | }
|
---|
| 540 | }
|
---|
| 541 |
|
---|
[15870] | 542 | print $outhandle "DirectoryPlugin: preparing metadata for $subfile\n" if ($verbosity > 2);
|
---|
[317] | 543 |
|
---|
[2228] | 544 | # Make a copy of $in_metadata to pass to $subfile
|
---|
[17313] | 545 | my $out_metadata = {};
|
---|
[13188] | 546 | &metadatautil::combine_metadata_structures($out_metadata, $in_metadata);
|
---|
[317] | 547 |
|
---|
[16391] | 548 | # check the assocfile_info
|
---|
| 549 | if (defined $self->{'assocfile_info'}->{$full_filename}) {
|
---|
| 550 | &metadatautil::combine_metadata_structures($out_metadata, $self->{'assocfile_info'}->{$full_filename});
|
---|
| 551 | }
|
---|
[17313] | 552 | ## encode the filename as perl5 doesn't handle unicode filenames
|
---|
| 553 |
|
---|
[13545] | 554 | my $tmpfile = Encode::encode_utf8($subfile);
|
---|
[17313] | 555 | #print STDERR "subfile = $subfile, tmpfile = $tmpfile\n";
|
---|
[2228] | 556 | # Next add metadata read in XML files (if it is supplied)
|
---|
| 557 | if ($additionalmetadata == 1) {
|
---|
| 558 | my ($filespec, $mdref);
|
---|
| 559 | foreach $filespec (@extrametakeys) {
|
---|
[17320] | 560 | ## use the utf8 encoded filename to do the filename comparison
|
---|
[13545] | 561 | if ($tmpfile =~ /^$filespec$/) {
|
---|
[2228] | 562 | print $outhandle "File \"$subfile\" matches filespec \"$filespec\"\n"
|
---|
| 563 | if ($verbosity > 2);
|
---|
| 564 | $mdref = $extrametadata{$filespec};
|
---|
[13188] | 565 | &metadatautil::combine_metadata_structures($out_metadata, $mdref);
|
---|
[2228] | 566 | }
|
---|
[4] | 567 | }
|
---|
| 568 | }
|
---|
[10156] | 569 |
|
---|
| 570 |
|
---|
| 571 | if (defined $self->{'inf_timestamp'}) {
|
---|
| 572 | my $inf_timestamp = $self->{'inf_timestamp'};
|
---|
| 573 |
|
---|
[16391] | 574 | if (! -d $full_filename) {
|
---|
| 575 | my $filename_timestamp = -M $full_filename;
|
---|
[10156] | 576 | if ($filename_timestamp > $inf_timestamp) {
|
---|
| 577 | # filename has been around for longer than inf
|
---|
| 578 | ##### print $outhandle "**** Skipping $subfile\n";
|
---|
| 579 | next;
|
---|
| 580 | }
|
---|
| 581 | }
|
---|
| 582 | }
|
---|
| 583 |
|
---|
[2228] | 584 | # Recursively read each $subfile
|
---|
[15870] | 585 | print $outhandle "DirectoryPlugin recurring: $subfile\n" if ($verbosity > 2);
|
---|
[8512] | 586 |
|
---|
| 587 | $count += &plugin::read ($pluginfo, $this_file_base_dir,
|
---|
[16391] | 588 | $file_subfile, $block_hash,
|
---|
[9853] | 589 | $out_metadata, $processor, $maxdocs, ($total_count + $count), $gli);
|
---|
[4] | 590 | }
|
---|
[7686] | 591 |
|
---|
[8512] | 592 | return $count;
|
---|
[2228] | 593 | }
|
---|
[4] | 594 |
|
---|
[17738] | 595 | sub compile_stats {
|
---|
| 596 | my $self = shift(@_);
|
---|
| 597 | my ($stats) = @_;
|
---|
| 598 | }
|
---|
| 599 |
|
---|
[4] | 600 | 1;
|
---|