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