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