source: gsdl/trunk/perllib/inexport.pm@ 20764

Last change on this file since 20764 was 20764, checked in by kjdon, 13 years ago

making the reverse lookup contain full paths not relative ones

  • Property svn:executable set to *
File size: 12.2 KB
Line 
1###########################################################################
2#
3# inexport.pm -- useful utilities to support import.pl and export.pl
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
26package inexport;
27
28use strict;
29
30use File::Basename;
31
32use util;
33use GDBMUtils;
34
35
36sub prime_doc_oid_count
37{
38 my ($archivedir) = @_;
39 my $oid_count_filename = &util::filename_cat ($archivedir, "OIDcount");
40
41 if (-e $oid_count_filename) {
42 if (open(OIDIN,"<$oid_count_filename")) {
43 my $OIDcount = <OIDIN>;
44 chomp $OIDcount;
45 close(OIDIN);
46
47 $doc::OIDcount = $OIDcount;
48 }
49 else {
50
51 print STDERR "Warning: unable to read document OID count from $oid_count_filename\n";
52 print STDERR "Setting value to 0\n";
53 }
54 }
55
56}
57
58sub store_doc_oid_count
59{
60 # Use the file "OIDcount" in the archives directory to record
61 # what value doc.pm got up to
62
63 my ($archivedir) = @_;
64 my $oid_count_filename = &util::filename_cat ($archivedir, "OIDcount");
65
66
67 if (open(OIDOUT,">$oid_count_filename")) {
68 print OIDOUT $doc::OIDcount, "\n";
69
70 close(OIDOUT);
71 }
72 else {
73 print STDERR "Warning: unable to store document OID count\n";
74 }
75}
76
77
78
79sub new_vs_old_import_diff
80{
81 my ($archive_info,$block_hash,$importdir,$archivedir,$verbosity,$incremental_mode) = @_;
82
83 # First convert all files to absolute form
84 # This is to support the situation where the import folder is not
85 # the default
86
87 my $prev_all_files = $archive_info->{'prev_import_filelist'};
88 my $full_prev_all_files = {};
89
90 foreach my $prev_file (keys %$prev_all_files) {
91
92 if (!&util::filename_is_absolute($prev_file)) {
93 my $full_prev_file = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},$prev_file);
94 $full_prev_all_files->{$full_prev_file} = $prev_file;
95 }
96 else {
97 $full_prev_all_files->{$prev_file} = $prev_file;
98 }
99 }
100
101
102 # Figure out which are the new files, existing files and so
103 # by implication the files from the previous import that are not
104 # there any more => mark them for deletion
105 foreach my $curr_file (keys %{$block_hash->{'all_files'}}) {
106
107 my $full_curr_file = $curr_file;
108
109 # entry in 'all_files' is moved to either 'existing_files',
110 # 'deleted_files', 'new_files', or 'new_metadata_files'
111
112 if (!&util::filename_is_absolute($curr_file)) {
113 # add in import dir to make absolute
114 $full_curr_file = &util::filename_cat($importdir,$curr_file);
115 }
116
117 if (defined $block_hash->{'file_blocks'}->{$full_curr_file}) {
118 # If in block list, we want to ignore it
119 delete $block_hash->{'all_files'}->{$curr_file};
120
121 if (defined $full_prev_all_files->{$full_curr_file}) {
122 # also make sure it is gone from 'previous' list so
123 # not mistaken for a file that needs to be deleted
124 delete $full_prev_all_files->{$full_curr_file};
125 }
126 next;
127 }
128
129 # figure out if new file or not
130 if (defined $full_prev_all_files->{$full_curr_file}) {
131
132 if ($incremental_mode eq "all") {
133
134 # had it before
135 $block_hash->{'existing_files'}->{$full_curr_file} = 1;
136
137 # Now remove it, so by end of loop only the files
138 # that need deleting are left
139
140 delete $full_prev_all_files->{$full_curr_file};
141 }
142 else {
143 # Warning in "onlyadd" mode, but had it before!
144 print STDERR "Warning: File $full_curr_file previously imported.\n";
145 print STDERR " Treating as new file\n";
146
147 $block_hash->{'new_files'}->{$full_curr_file} = 1;
148 delete $full_prev_all_files->{$full_curr_file};
149 }
150 }
151 else {
152 if ($block_hash->{'metadata_files'}->{$full_curr_file}) {
153 # the new file is the special sort of file greenstone uses
154 # to attach metadata to src documents
155 # i.e metadata.xml
156 # (but note, the filename used is not constrained in
157 # Greenstone to always be this)
158
159## print STDERR "***** Detected new metadata file: $full_curr_file\n";
160 $block_hash->{'new_metadata_files'}->{$full_curr_file} = 1;
161 }
162 else {
163 $block_hash->{'new_files'}->{$full_curr_file} = 1;
164 }
165 }
166
167
168 delete $block_hash->{'all_files'}->{$curr_file};
169 }
170
171
172 # Deal with complication of new metadata.xml files by forcing
173 # everything from this point down in the file hierarchy to
174 # be freshly imported.
175 #
176 # This may mean files that have not changed are reindexed, but does
177 # guarantee by the end of processing all new metadata is correctly
178 # associated with the relevant document(s).
179
180 foreach my $new_mdf (keys %{$block_hash->{'new_metadata_files'}}) {
181 my ($fileroot,$situated_dir,$ext) = fileparse($new_mdf, "\\.[^\\.]+\$");
182
183 $situated_dir =~ s/[\\\/]+$//; # remove tailing slashes
184
185 # Go through existing_files, and mark anything that is contained
186 # within 'situated_dir' to be reindexed (in case some of the metadata
187 # attaches to one of these files)
188
189 my $reindex_files = [];
190
191 foreach my $existing_f (keys %{$block_hash->{'existing_files'}}) {
192 # need to protect windows slash \ in regular expression?
193
194 if ($existing_f =~ m/^$situated_dir/) {
195 push(@$reindex_files,$existing_f);
196 $block_hash->{'reindex_files'}->{$existing_f} = 1;
197
198 }
199 }
200
201 # Reindexing is accomplished by putting them in th list for reindexing (line above)
202 # and then tagging the arcinfo version as to be deleted.
203
204 _mark_docs_for_deletion($archive_info,$block_hash,$reindex_files,$archivedir,$verbosity, "reindex");
205
206 # metadata file needs to be in new_files list so parsed by MetadataXMLPlug
207 # (or equivalent)
208 $block_hash->{'new_files'}->{$new_mdf} = 1;
209
210 }
211
212
213 # By this point full_prev_all_files contains the files
214 # mentioned in archiveinf-src.db but are not in the 'import'
215 # folder (or whatever was specified through -importdir ...)
216
217 # This list can contain files that were created in the 'tmp' or
218 # 'cache' areas (such as screen-size and thumbnail images).
219 #
220 # In building the final list of files to delete, we test to see if
221 # it exists on the filesystem and if it does (unusual for a "normal"
222 # file in import, but possible in the case of 'tmp' files),
223 # supress it from going into the final list
224
225 my $collectdir = $ENV{'GSDLCOLLECTDIR'};
226
227 my @deleted_files = values %$full_prev_all_files;
228 map { my $curr_file = $_;
229 my $full_curr_file = $curr_file;
230
231 if (!&util::filename_is_absolute($curr_file)) {
232 # add in import dir to make absolute
233
234 $full_curr_file = &util::filename_cat($collectdir,$curr_file);
235 }
236
237
238 if (!-e $full_curr_file) {
239 $block_hash->{'deleted_files'}->{$curr_file} = 1;
240 }
241 } @deleted_files;
242
243
244
245}
246
247
248sub is_assoc_file
249{
250 my ($file,$doc_rec) = @_;
251
252 my ($file_root,$dirname,$suffix) = fileparse($file, "\\.[^\\.]+\$");
253
254 foreach my $af (@{$doc_rec->{'assoc-file'}}) {
255 my $full_af = &util::filename_cat($dirname,$af);
256
257 return 1 if ($full_af eq $file);
258 }
259
260 return 0;
261}
262
263
264sub _mark_docs_for_deletion
265{
266 my ($archive_info,$block_hash,$deleted_files,$archivedir,$verbosity,$mode_text) = @_;
267
268 my $doc_db = "archiveinf-doc.gdb";
269 my $src_db = "archiveinf-src.gdb";
270 my $arcinfo_doc_filename = &util::filename_cat ($archivedir, $doc_db);
271 my $arcinfo_src_filename = &util::filename_cat ($archivedir, $src_db);
272
273
274 # record files marked for deletion in arcinfo
275 foreach my $file (@$deleted_files) {
276 # use 'archiveinf-src' GDBM file to look up all the OIDs
277 # that this file is used in (note in most cases, it's just one OID)
278
279 my $src_rec = GDBMUtils::gdbmRecordToHash($arcinfo_src_filename,$file);
280 my $oids = $src_rec->{'oid'};
281 foreach my $oid (@$oids) {
282 # Find out if it's a main doc, assoc file, or metadata
283
284 my $doc_rec = GDBMUtils::gdbmRecordToHash($arcinfo_doc_filename,$oid);
285
286 if (is_assoc_file($file,$doc_rec)) {
287 # assoc file => mark it for re-indexing (safest thing to do)
288 my $curr_status = $archive_info->get_status_info($oid);
289
290
291 if (defined($curr_status) && (($curr_status ne "D") && ($curr_status ne "R"))) {
292 if ($verbosity > 1) {
293 print STDERR "$oid marked for (potential) reindexing\n";
294 print STDERR " (because associated file $file deleted)\n";
295 }
296 $archive_info->set_status_info($oid,"R");
297
298 my $val = &GDBMUtils::gdbmDatabaseGet($arcinfo_doc_filename,$oid);
299 $val =~ s/^<index-status>(.*)$/<index-status>R/m;
300 &GDBMUtils::gdbmDatabaseSet($arcinfo_doc_filename,$oid,$val);
301 }
302 GDBMUtils::gdbmDatabaseRemove($arcinfo_src_filename,$file);
303 }
304 else {
305 # either src-file or metadata.xml file linking to src-file
306
307 my $src_file;
308
309 if ($doc_rec->{'src-file'}->[0] ne $file) {
310 # it's a metadata file attached to this OID
311 # => workout the src-file it matches to
312
313 $src_file = $doc_rec->{'src-file'}->[0];
314
315 my $src_filename = $src_file;
316 if (!&util::filename_is_absolute($src_file)) {
317 $src_filename = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},$src_file);
318 }
319
320 $block_hash->{'reindex_files'}->{$src_filename} = 1;
321
322 # remove the metadata file from the src-database
323 GDBMUtils::gdbmDatabaseRemove($arcinfo_src_filename,$file);
324 }
325 else {
326 # It's the main doc
327 # => make it the target and mark it for deletion
328 $src_file = $file;
329 }
330
331 # Whether the main file directly or indirectly, mark for deletion/reindex
332
333 my $val = &GDBMUtils::gdbmDatabaseGet($arcinfo_doc_filename,$oid);
334 my ($index_status) = ($val =~ m/^<index-status>(.*)$/m);
335
336 if ($index_status ne "D") {
337 if ($verbosity>1) {
338 print STDERR "$oid ($src_file) marked to be $mode_text on next buildcol.pl\n";
339 }
340 $archive_info->set_status_info($oid,"D");
341
342 $val =~ s/^<index-status>(.*)$/<index-status>D/m;
343 &GDBMUtils::gdbmDatabaseSet($arcinfo_doc_filename,$oid,$val);
344
345 GDBMUtils::gdbmDatabaseRemove($arcinfo_src_filename,$src_file);
346 }
347
348
349 }
350
351 }
352 }
353}
354
355
356sub mark_docs_for_deletion
357{
358 _mark_docs_for_deletion(@_,"deleted from index");
359}
360
361
362sub mark_docs_for_reindex
363{
364 my ($archive_info,$block_hash,$archivedir,$verbosity) = @_;
365
366 # Reindexing is accomplished by deleting the previously indexed
367 # version of the document, and then allowing the new version to
368 # be indexed (as would a new document be indexed).
369 #
370 # The first step (marking for deletion) is implemented by this routine.
371 #
372 # By default in Greenstone a new version of an index will hash to
373 # a new unique OID, and the above strategy of reindex=delete+add
374 # works fine. A special case arises when a persistent OID is
375 # allocated to a document (for instance through a metadata field),
376 # and the second step to reindexing (see XXXX) detects this and
377 # deals with it appropriately.
378
379 my @existing_files = sort keys %{$block_hash->{'existing_files'}};
380
381 my $doc_db = "archiveinf-doc.gdb";
382 my $arcinfo_doc_filename = &util::filename_cat ($archivedir, $doc_db);
383
384 my $archiveinf_timestamp = -M $arcinfo_doc_filename;
385
386 my $reindex_files = [];
387
388 foreach my $existing_filename (@existing_files) {
389 if (-M $existing_filename < $archiveinf_timestamp) {
390 # file is newer than last build
391
392 my $existing_file = $existing_filename;
393 #my $collectdir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'});
394
395 #my $collectdir_resafe = &util::filename_to_regex($collectdir);
396 #$existing_file =~ s/^$collectdir_resafe(\\|\/)?//;
397
398 print STDERR "**** Reindexing existing file: $existing_file\n";
399
400 push(@$reindex_files,$existing_file);
401 $block_hash->{'reindex_files'}->{$existing_filename} = 1;
402 }
403
404 }
405
406 _mark_docs_for_deletion($archive_info,$block_hash,$reindex_files,$archivedir,$verbosity, "reindex");
407
408}
409
410
411
4121;
Note: See TracBrowser for help on using the repository browser.