1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # RecPlug.pm --
|
---|
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 |
|
---|
26 | # RecPlug is a plugin which recurses through directories processing
|
---|
27 | # each file it finds.
|
---|
28 |
|
---|
29 | # RecPlug has one option: use_metadata_files. When this is set, it will
|
---|
30 | # check each directory for an XML file called "metadata.xml" that specifies
|
---|
31 | # metadata for the files (and subdirectories) in the directory.
|
---|
32 | #
|
---|
33 | # Here's an example of a metadata file that uses three FileSet structures
|
---|
34 | # (ignore the # characters):
|
---|
35 |
|
---|
36 | #<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
---|
37 | #<!DOCTYPE DirectoryMetadata SYSTEM "http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd">
|
---|
38 | #<DirectoryMetadata>
|
---|
39 | # <FileSet>
|
---|
40 | # <FileName>nugget.*</FileName>
|
---|
41 | # <Description>
|
---|
42 | # <Metadata name="Title">Nugget Point, The Catlins</Metadata>
|
---|
43 | # <Metadata name="Place" mode="accumulate">Nugget Point</Metadata>
|
---|
44 | # </Description>
|
---|
45 | # </FileSet>
|
---|
46 | # <FileSet>
|
---|
47 | # <FileName>nugget-point-1.jpg</FileName>
|
---|
48 | # <Description>
|
---|
49 | # <Metadata name="Title">Nugget Point Lighthouse, The Catlins</Metadata>
|
---|
50 | # <Metadata name="Subject">Lighthouse</Metadata>
|
---|
51 | # </Description>
|
---|
52 | # </FileSet>
|
---|
53 | # <FileSet>
|
---|
54 | # <FileName>kaka-point-dir</FileName>
|
---|
55 | # <Description>
|
---|
56 | # <Metadata name="Title">Kaka Point, The Catlins</Metadata>
|
---|
57 | # </Description>
|
---|
58 | # </FileSet>
|
---|
59 | #</DirectoryMetadata>
|
---|
60 |
|
---|
61 | # Metadata elements are read and applied to files in the order they appear
|
---|
62 | # in the file.
|
---|
63 | #
|
---|
64 | # The FileName element describes the subfiles in the directory that the
|
---|
65 | # metadata applies to as a perl regular expression (a FileSet group may
|
---|
66 | # contain multiple FileName elements). So, <FileName>nugget.*</FileName>
|
---|
67 | # indicates that the metadata records in the following Description block
|
---|
68 | # apply to every subfile that starts with "nugget". For these files, a
|
---|
69 | # Title metadata element is set, overriding any old value that the Title
|
---|
70 | # might have had.
|
---|
71 | #
|
---|
72 | # Occasionally, we want to have multiple metadata values applied to a
|
---|
73 | # document; in this case we use the "mode=accumulate" attribute of the
|
---|
74 | # particular Metadata element. In the second metadata element of the first
|
---|
75 | # FileSet above, the "Place" metadata is accumulating, and may therefore be
|
---|
76 | # given several values. If we wanted to override these values and use a
|
---|
77 | # single metadata element again, we could set the mode attribute to
|
---|
78 | # "override" instead. Remember: every element is assumed to be in override
|
---|
79 | # mode unless you specify otherwise, so if you want to accumulate metadata
|
---|
80 | # for some field, every occurance must have "mode=accumulate" specified.
|
---|
81 | #
|
---|
82 | # The second FileSet element above applies to a specific file, called
|
---|
83 | # nugget-point-1.jpg. This element overrides the Title metadata set in the
|
---|
84 | # first FileSet, and adds a "Subject" metadata field.
|
---|
85 | #
|
---|
86 | # The third and final FileSet sets metadata for a subdirectory rather than
|
---|
87 | # a file. The metadata specified (a Title) will be passed into the
|
---|
88 | # subdirectory and applied to every file that occurs in the subdirectory
|
---|
89 | # (and to every subsubdirectory and its contents, and so on) unless the
|
---|
90 | # metadata is explictly overridden later in the import.
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 | package RecPlug;
|
---|
95 |
|
---|
96 | use BasPlug;
|
---|
97 | use plugin;
|
---|
98 | use util;
|
---|
99 | use metadatautil;
|
---|
100 |
|
---|
101 | use File::Basename;
|
---|
102 | use strict;
|
---|
103 | no strict 'refs';
|
---|
104 |
|
---|
105 | BEGIN {
|
---|
106 | @RecPlug::ISA = ('BasPlug');
|
---|
107 | }
|
---|
108 |
|
---|
109 | my $arguments =
|
---|
110 | [ { 'name' => "block_exp",
|
---|
111 | 'desc' => "{BasPlug.block_exp}",
|
---|
112 | 'type' => "regexp",
|
---|
113 | 'deft' => &get_default_block_exp(),
|
---|
114 | 'reqd' => "no" },
|
---|
115 | # this option has been deprecated. leave it here for now so we can warn people not to use it
|
---|
116 | { 'name' => "use_metadata_files",
|
---|
117 | 'desc' => "{RecPlug.use_metadata_files}",
|
---|
118 | 'type' => "flag",
|
---|
119 | 'reqd' => "no",
|
---|
120 | 'hiddengli' => "yes" },
|
---|
121 | { 'name' => "recheck_directories",
|
---|
122 | 'desc' => "{RecPlug.recheck_directories}",
|
---|
123 | 'type' => "flag",
|
---|
124 | 'reqd' => "no" } ];
|
---|
125 |
|
---|
126 | my $options = { 'name' => "RecPlug",
|
---|
127 | 'desc' => "{RecPlug.desc}",
|
---|
128 | 'abstract' => "no",
|
---|
129 | 'inherits' => "yes",
|
---|
130 | 'args' => $arguments };
|
---|
131 |
|
---|
132 | sub new {
|
---|
133 | my ($class) = shift (@_);
|
---|
134 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
135 | push(@$pluginlist, $class);
|
---|
136 |
|
---|
137 | if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
|
---|
138 | if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
|
---|
139 |
|
---|
140 | my $self = new BasPlug($pluginlist, $inputargs, $hashArgOptLists);
|
---|
141 |
|
---|
142 | if ($self->{'info_only'}) {
|
---|
143 | # don't worry about any options or initialisations etc
|
---|
144 | return bless $self, $class;
|
---|
145 | }
|
---|
146 |
|
---|
147 | # we have left this option in so we can warn people who are still using it
|
---|
148 | if ($self->{'use_metadata_files'}) {
|
---|
149 | die "ERROR: RecPlug -use_metadata_files option has been deprecated. Please remove the option and add MetadataXMLPlug to your plugin list instead!\n";
|
---|
150 | }
|
---|
151 |
|
---|
152 | $self->{'subdir_extrametakeys'} = {};
|
---|
153 |
|
---|
154 | return bless $self, $class;
|
---|
155 | }
|
---|
156 |
|
---|
157 | sub begin {
|
---|
158 | my $self = shift (@_);
|
---|
159 | my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
|
---|
160 |
|
---|
161 | my $proc_package_name = ref $processor;
|
---|
162 |
|
---|
163 | if ($proc_package_name !~ /buildproc$/ && $self->{'incremental'} == 1) {
|
---|
164 |
|
---|
165 | # Only lookup timestamp info for import.pl, and only if incremental is set
|
---|
166 |
|
---|
167 | my $output_dir = $processor->getoutputdir();
|
---|
168 | my $archives_inf = &util::filename_cat($output_dir,"archives.inf");
|
---|
169 |
|
---|
170 | if ( -e $archives_inf ) {
|
---|
171 | $self->{'inf_timestamp'} = -M $archives_inf;
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | $self->SUPER::begin($pluginfo, $base_dir, $processor, $maxdocs);
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | # return 1 if this class might recurse using $pluginfo
|
---|
180 | sub is_recursive {
|
---|
181 | my $self = shift (@_);
|
---|
182 |
|
---|
183 | return 1;
|
---|
184 | }
|
---|
185 |
|
---|
186 | sub get_default_block_exp {
|
---|
187 | my $self = shift (@_);
|
---|
188 |
|
---|
189 | return 'CVS';
|
---|
190 | }
|
---|
191 |
|
---|
192 | # return number of files processed, undef if can't process
|
---|
193 | # Note that $base_dir might be "" and that $file might
|
---|
194 | # include directories
|
---|
195 |
|
---|
196 | # This function passes around metadata hash structures. Metadata hash
|
---|
197 | # structures are hashes that map from a (scalar) key (the metadata element
|
---|
198 | # name) to either a scalar metadata value or a reference to an array of
|
---|
199 | # such values.
|
---|
200 |
|
---|
201 | sub read {
|
---|
202 | my $self = shift (@_);
|
---|
203 | my ($pluginfo, $base_dir, $file, $in_metadata, $processor, $maxdocs, $total_count, $gli) = @_;
|
---|
204 |
|
---|
205 | my $outhandle = $self->{'outhandle'};
|
---|
206 | my $verbosity = $self->{'verbosity'};
|
---|
207 | my $read_metadata_files = $self->{'use_metadata_files'};
|
---|
208 |
|
---|
209 | # Calculate the directory name and ensure it is a directory and
|
---|
210 | # that it is not explicitly blocked.
|
---|
211 | my $dirname = $file;
|
---|
212 | $dirname = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
|
---|
213 | return undef unless (-d $dirname);
|
---|
214 | return 0 if ($self->{'block_exp'} ne "" && $dirname =~ /$self->{'block_exp'}/);
|
---|
215 |
|
---|
216 | # check to make sure we're not reading the archives or index directory
|
---|
217 | my $gsdlhome = quotemeta($ENV{'GSDLHOME'});
|
---|
218 | if ($dirname =~ m/^$gsdlhome\/.*?\/import.*?\/(archives|index)$/) {
|
---|
219 | print $outhandle "RecPlug: $dirname appears to be a reference to a Greenstone collection, skipping.\n";
|
---|
220 | return 0;
|
---|
221 | }
|
---|
222 |
|
---|
223 | # check to see we haven't got a cyclic path...
|
---|
224 | if ($dirname =~ m%(/.*){,41}%) {
|
---|
225 | print $outhandle "RecPlug: $dirname is 40 directories deep, is this a recursive path? if not increase constant in RecPlug.pm.\n";
|
---|
226 | return 0;
|
---|
227 | }
|
---|
228 |
|
---|
229 | # check to see we haven't got a cyclic path...
|
---|
230 | if ($dirname =~ m%.*?import/(.+?)/import/\1.*%) {
|
---|
231 | print $outhandle "RecPlug: $dirname appears to be in a recursive loop...\n";
|
---|
232 | return 0;
|
---|
233 | }
|
---|
234 |
|
---|
235 | if (($verbosity > 2) && ((scalar keys %$in_metadata) > 0)) {
|
---|
236 | print $outhandle "RecPlug: metadata passed in: ",
|
---|
237 | join(", ", keys %$in_metadata), "\n";
|
---|
238 | }
|
---|
239 |
|
---|
240 | # Recur over directory contents.
|
---|
241 | my (@dir, $subfile);
|
---|
242 | my $count = 0;
|
---|
243 |
|
---|
244 | print $outhandle "RecPlug: getting directory $dirname\n" if ($verbosity);
|
---|
245 |
|
---|
246 | # find all the files in the directory
|
---|
247 | if (!opendir (DIR, $dirname)) {
|
---|
248 | if ($gli) {
|
---|
249 | print STDERR "<ProcessingError n='$file' r='Could not read directory $dirname'>\n";
|
---|
250 | }
|
---|
251 | print $outhandle "RecPlug: WARNING - couldn't read directory $dirname\n";
|
---|
252 | return -1; # error in processing
|
---|
253 | }
|
---|
254 | @dir = readdir (DIR);
|
---|
255 | closedir (DIR);
|
---|
256 |
|
---|
257 | # Re-order the files in the list so any directories ending with .all are moved to the end
|
---|
258 | for (my $i = scalar(@dir) - 1; $i >= 0; $i--) {
|
---|
259 | if (-d &util::filename_cat($dirname, $dir[$i]) && $dir[$i] =~ /\.all$/) {
|
---|
260 | push(@dir, splice(@dir, $i, 1));
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | # setup the metadata structures. we do a metadata_read pass to see if there is any additional metadata, then pass it to read
|
---|
265 |
|
---|
266 | my $additionalmetadata = 0; # is there extra metadata available?
|
---|
267 | my %extrametadata; # maps from filespec to extra metadata keys
|
---|
268 | my @extrametakeys; # keys of %extrametadata in order read
|
---|
269 |
|
---|
270 | my $os_dirsep = &util::get_os_dirsep();
|
---|
271 | my $dirsep = &util::get_dirsep();
|
---|
272 | my $base_dir_regexp = $base_dir;
|
---|
273 | $base_dir_regexp =~ s/\//$os_dirsep/g;
|
---|
274 | my $local_dirname = $dirname;
|
---|
275 | $local_dirname =~ s/^$base_dir_regexp($os_dirsep)//;
|
---|
276 | $local_dirname .= $dirsep;
|
---|
277 |
|
---|
278 | if (defined $self->{'subdir_extrametakeys'}->{$local_dirname}) {
|
---|
279 | my $extrakeys = $self->{'subdir_extrametakeys'}->{$local_dirname};
|
---|
280 | foreach my $ek (@$extrakeys) {
|
---|
281 | my $extrakeys_re = $ek->{'re'};
|
---|
282 | my $extrakeys_md = $ek->{'md'};
|
---|
283 | push(@extrametakeys,$extrakeys_re);
|
---|
284 | $extrametadata{$extrakeys_re} = $extrakeys_md;
|
---|
285 | }
|
---|
286 | delete($self->{'subdir_extrametakeys'}->{$local_dirname});
|
---|
287 | }
|
---|
288 |
|
---|
289 | # apply metadata pass for each of the files in the directory
|
---|
290 | my $out_metadata;
|
---|
291 | my $num_files = scalar(@dir);
|
---|
292 | for (my $i = 0; $i < scalar(@dir); $i++) {
|
---|
293 | my $subfile = $dir[$i];
|
---|
294 | my $this_file_base_dir = $base_dir;
|
---|
295 | last if ($maxdocs != -1 && $count >= $maxdocs);
|
---|
296 | next if ($subfile =~ m/^\.\.?$/);
|
---|
297 |
|
---|
298 | # Recursively read each $subfile
|
---|
299 | print $outhandle "RecPlug metadata recurring: $subfile\n" if ($verbosity > 2);
|
---|
300 |
|
---|
301 | $count += &plugin::metadata_read ($pluginfo, $this_file_base_dir,
|
---|
302 | &util::filename_cat($file, $subfile),
|
---|
303 | $out_metadata, \@extrametakeys, \%extrametadata,
|
---|
304 | $processor, $maxdocs, $gli);
|
---|
305 | $additionalmetadata = 1;
|
---|
306 | }
|
---|
307 |
|
---|
308 | # filter out any extrametakeys that mention subdirectories and store
|
---|
309 | # for later use (i.e. when that sub-directory is being processed)
|
---|
310 |
|
---|
311 | foreach my $ek (@extrametakeys) {
|
---|
312 | my ($subdir_re,$extrakey_dir) = &File::Basename::fileparse($ek);
|
---|
313 | $extrakey_dir =~ s/\\\./\./g; # remove RE syntax
|
---|
314 |
|
---|
315 | my $dirsep_re = &util::get_re_dirsep();
|
---|
316 | if ($ek =~ m/$dirsep_re/) { # specifies at least one directory
|
---|
317 | my $md = $extrametadata{$ek};
|
---|
318 |
|
---|
319 | my $subdir_extrametakeys = $self->{'subdir_extrametakeys'};
|
---|
320 |
|
---|
321 | my $subdir_rec = { 're' => $subdir_re, 'md' => $md };
|
---|
322 | push(@{$subdir_extrametakeys->{$extrakey_dir}},$subdir_rec);
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | # import each of the files in the directory
|
---|
327 | $count=0;
|
---|
328 | for (my $i = 0; $i <= scalar(@dir); $i++) {
|
---|
329 | # When every file in the directory has been done, pause for a moment (figuratively!)
|
---|
330 | # If the -recheck_directories argument hasn't been provided, stop now (default)
|
---|
331 | # Otherwise, re-read the contents of the directory to check for new files
|
---|
332 | # Any new files are added to the @dir list and are processed as normal
|
---|
333 | # This is necessary when documents to be indexed are specified in bibliographic DBs
|
---|
334 | # These files are copied/downloaded and stored in a new folder at import time
|
---|
335 | if ($i == $num_files) {
|
---|
336 | last unless $self->{'recheck_directories'};
|
---|
337 |
|
---|
338 | # Re-read the files in the directory to see if there are any new files
|
---|
339 | last if (!opendir (DIR, $dirname));
|
---|
340 | my @dirnow = readdir (DIR);
|
---|
341 | closedir (DIR);
|
---|
342 |
|
---|
343 | # We're only interested if there are more files than there were before
|
---|
344 | last if (scalar(@dirnow) <= scalar(@dir));
|
---|
345 |
|
---|
346 | # Any new files are added to the end of @dir to get processed by the loop
|
---|
347 | my $j;
|
---|
348 | foreach my $subfilenow (@dirnow) {
|
---|
349 | for ($j = 0; $j < $num_files; $j++) {
|
---|
350 | last if ($subfilenow eq $dir[$j]);
|
---|
351 | }
|
---|
352 | if ($j == $num_files) {
|
---|
353 | # New file
|
---|
354 | push(@dir, $subfilenow);
|
---|
355 | }
|
---|
356 | }
|
---|
357 | # When the new files have been processed, check again
|
---|
358 | $num_files = scalar(@dir);
|
---|
359 | }
|
---|
360 |
|
---|
361 | my $subfile = $dir[$i];
|
---|
362 | my $this_file_base_dir = $base_dir;
|
---|
363 | last if ($maxdocs != -1 && ($count + $total_count) >= $maxdocs);
|
---|
364 | next if ($subfile =~ /^\.\.?$/);
|
---|
365 |
|
---|
366 | # Follow Windows shortcuts
|
---|
367 | if ($subfile =~ /(?i)\.lnk$/ && $ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
368 | require Win32::Shortcut;
|
---|
369 | my $shortcut = new Win32::Shortcut(&util::filename_cat($dirname, $subfile));
|
---|
370 | if ($shortcut) {
|
---|
371 | # The file to be processed is now the target of the shortcut
|
---|
372 | $this_file_base_dir = "";
|
---|
373 | $file = "";
|
---|
374 | $subfile = $shortcut->Path;
|
---|
375 | }
|
---|
376 | }
|
---|
377 |
|
---|
378 | # check for a symlink pointing back to a leading directory
|
---|
379 | if (-d "$dirname/$subfile" && -l "$dirname/$subfile") {
|
---|
380 | # readlink gives a "fatal error" on systems that don't implement
|
---|
381 | # symlinks. This assumes the the -l test above would fail on those.
|
---|
382 | my $linkdest=readlink "$dirname/$subfile";
|
---|
383 | if (!defined ($linkdest)) {
|
---|
384 | # system error - file not found?
|
---|
385 | warn "RecPlug: symlink problem - $!";
|
---|
386 | } else {
|
---|
387 | # see if link points to current or a parent directory
|
---|
388 | if ($linkdest =~ m@^[\./\\]+$@ ||
|
---|
389 | index($dirname, $linkdest) != -1) {
|
---|
390 | warn "RecPlug: Ignoring recursive symlink ($dirname/$subfile -> $linkdest)\n";
|
---|
391 | next;
|
---|
392 | ;
|
---|
393 | }
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | print $outhandle "RecPlug: preparing metadata for $subfile\n" if ($verbosity > 2);
|
---|
398 |
|
---|
399 | # Make a copy of $in_metadata to pass to $subfile
|
---|
400 | $out_metadata = {};
|
---|
401 | &metadatautil::combine_metadata_structures($out_metadata, $in_metadata);
|
---|
402 |
|
---|
403 | # Next add metadata read in XML files (if it is supplied)
|
---|
404 | if ($additionalmetadata == 1) {
|
---|
405 |
|
---|
406 | my ($filespec, $mdref);
|
---|
407 | foreach $filespec (@extrametakeys) {
|
---|
408 | if ($subfile =~ /^$filespec$/) {
|
---|
409 | print $outhandle "File \"$subfile\" matches filespec \"$filespec\"\n"
|
---|
410 | if ($verbosity > 2);
|
---|
411 | $mdref = $extrametadata{$filespec};
|
---|
412 | &metadatautil::combine_metadata_structures($out_metadata, $mdref);
|
---|
413 | }
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 |
|
---|
418 | my $file_subfile = &util::filename_cat($file, $subfile);
|
---|
419 | my $filename_subfile
|
---|
420 | = &util::filename_cat($this_file_base_dir,$file_subfile);
|
---|
421 | if (defined $self->{'inf_timestamp'}) {
|
---|
422 | my $inf_timestamp = $self->{'inf_timestamp'};
|
---|
423 |
|
---|
424 | if (! -d $filename_subfile) {
|
---|
425 | my $filename_timestamp = -M $filename_subfile;
|
---|
426 | if ($filename_timestamp > $inf_timestamp) {
|
---|
427 | # filename has been around for longer than inf
|
---|
428 | ##### print $outhandle "**** Skipping $subfile\n";
|
---|
429 | next;
|
---|
430 | }
|
---|
431 | }
|
---|
432 | }
|
---|
433 |
|
---|
434 | # Recursively read each $subfile
|
---|
435 | print $outhandle "RecPlug recurring: $subfile\n" if ($verbosity > 2);
|
---|
436 |
|
---|
437 | $count += &plugin::read ($pluginfo, $this_file_base_dir,
|
---|
438 | $file_subfile,
|
---|
439 | $out_metadata, $processor, $maxdocs, ($total_count + $count), $gli);
|
---|
440 | }
|
---|
441 |
|
---|
442 | return $count;
|
---|
443 | }
|
---|
444 |
|
---|
445 | 1;
|
---|