root/main/trunk/greenstone2/perllib/inexport.pm @ 21306

Revision 21306, 10.5 KB (checked in by kjdon, 11 years ago)

mark_docs_for_reindex code moved into new_vs_old_import_diff, so this now generates new_files, deleted_files and reindex_files lists. Both deleted_files and reindex_files need to be marked for deletion, which is now done by import.pl. don't need separate make_docs_for_deletion/reindex, just pass in a mode arg. removed a method no longer used

  • Property svn:executable set to *
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
35sub src_db_file {
36    my ($archivedir) = @_;
37    return &util::filename_cat ($archivedir, "archiveinf-src.gdb");
38}
39
40sub doc_db_file {
41    my ($archivedir) = @_;
42    return &util::filename_cat ($archivedir, "archiveinf-doc.gdb");
43}
44
45sub oid_count_file {
46    my ($archivedir) = @_;
47    return &util::filename_cat ($archivedir, "OIDcount");
48}
49
50
51sub prime_doc_oid_count
52{
53    my ($archivedir) = @_;
54    my $oid_count_filename = &oid_count_file($archivedir);
55
56    if (-e $oid_count_filename) {
57    if (open(OIDIN,"<$oid_count_filename")) {
58        my $OIDcount = <OIDIN>;
59        chomp $OIDcount;       
60        close(OIDIN);
61
62        $doc::OIDcount = $OIDcount;     
63    }
64    else {
65       
66        print STDERR "Warning: unable to read document OID count from $oid_count_filename\n";
67        print STDERR "Setting value to 0\n";
68    }
69    }
70   
71}
72
73sub store_doc_oid_count
74{
75    # Use the file "OIDcount" in the archives directory to record
76    # what value doc.pm got up to
77
78    my ($archivedir) = @_;
79    my $oid_count_filename = &oid_count_file($archivedir);
80
81
82    if (open(OIDOUT,">$oid_count_filename")) {
83    print OIDOUT $doc::OIDcount, "\n";
84       
85    close(OIDOUT);
86    }
87    else {
88    print STDERR "Warning: unable to store document OID count\n";
89    }
90}
91
92
93
94sub new_vs_old_import_diff
95{
96    my ($archive_info,$block_hash,$importdir,$archivedir,$verbosity,$incremental_mode) = @_;
97
98    # in this method, we want to know if metadata files are modified or not.
99    my $arcinfo_doc_filename = &doc_db_file($archivedir);
100
101    my $archiveinf_timestamp = -M $arcinfo_doc_filename;
102
103    # First convert all files to absolute form
104    # This is to support the situation where the import folder is not
105    # the default
106   
107    my $prev_all_files = $archive_info->{'prev_import_filelist'};
108    my $full_prev_all_files = {};
109
110    foreach my $prev_file (keys %$prev_all_files) {
111
112    if (!&util::filename_is_absolute($prev_file)) {
113        my $full_prev_file = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},$prev_file);
114        $full_prev_all_files->{$full_prev_file} = $prev_file;
115    }
116    else {
117        $full_prev_all_files->{$prev_file} = $prev_file;
118    }
119    }
120
121
122    # Figure out which are the new files, existing files and so
123    # by implication the files from the previous import that are not
124    # there any more => mark them for deletion
125    foreach my $curr_file (keys %{$block_hash->{'all_files'}}) {
126   
127    my $full_curr_file = $curr_file;
128
129    # entry in 'all_files' is moved to either 'existing_files',
130    # 'deleted_files', 'new_files', or 'new_or_modified_metadata_files'
131
132    if (!&util::filename_is_absolute($curr_file)) {
133        # add in import dir to make absolute
134        $full_curr_file = &util::filename_cat($importdir,$curr_file);
135    }
136
137    # figure out if new file or not
138    if (defined $full_prev_all_files->{$full_curr_file}) {
139        # delete it so that only files that need deleting are left
140        delete $full_prev_all_files->{$full_curr_file};
141       
142        # had it before. is it a metadata file?
143        if ($block_hash->{'metadata_files'}->{$full_curr_file}) {
144       
145        # is it modified??
146        if (-M $full_curr_file < $archiveinf_timestamp) {
147            print STDERR "*** Detected a modified metadata file: $full_curr_file\n" if $verbosity > 2;
148            # its newer than last build
149            $block_hash->{'new_or_modified_metadata_files'}->{$full_curr_file} = 1;
150        }
151        }
152        else {
153        if ($incremental_mode eq "all") {
154           
155            # had it before
156            $block_hash->{'existing_files'}->{$full_curr_file} = 1;
157           
158        }
159        else {
160            # Warning in "onlyadd" mode, but had it before!
161            print STDERR "Warning: File $full_curr_file previously imported.\n";
162            print STDERR "         Treating as new file\n";
163           
164            $block_hash->{'new_files'}->{$full_curr_file} = 1;
165           
166        }
167        }
168    }
169    else {
170        if ($block_hash->{'metadata_files'}->{$full_curr_file}) {
171        # the new file is the special sort of file greenstone uses
172        # to attach metadata to src documents
173        # i.e metadata.xml
174        # (but note, the filename used is not constrained in
175        # Greenstone to always be this)
176
177        print STDERR "***** Detected new metadata file: $full_curr_file\n" if $verbosity > 2;
178        $block_hash->{'new_or_modified_metadata_files'}->{$full_curr_file} = 1;
179        }
180        else {
181        $block_hash->{'new_files'}->{$full_curr_file} = 1;
182        }
183    }
184
185   
186    delete $block_hash->{'all_files'}->{$curr_file};
187    }
188
189
190
191
192    # Deal with complication of new or modified metadata files by forcing
193    # everything from this point down in the file hierarchy to
194    # be freshly imported. 
195    #
196    # This may mean files that have not changed are reindexed, but does
197    # guarantee by the end of processing all new metadata is correctly
198    # associated with the relevant document(s).
199
200    foreach my $new_mdf (keys %{$block_hash->{'new_or_modified_metadata_files'}}) {
201    my ($fileroot,$situated_dir,$ext) = fileparse($new_mdf, "\\.[^\\.]+\$");
202
203    $situated_dir =~ s/[\\\/]+$//; # remove tailing slashes
204    $situated_dir =~ s/\\/\\\\/g;  # need to protect windows slash \ in regular expression
205   
206    # Go through existing_files, and mark anything that is contained
207    # within 'situated_dir' to be reindexed (in case some of the metadata
208    # attaches to one of these files)
209
210    my $reindex_files = [];
211
212    foreach my $existing_f (keys %{$block_hash->{'existing_files'}}) {
213   
214        if ($existing_f =~ m/^$situated_dir/) {
215        push(@$reindex_files,$existing_f);
216        $block_hash->{'reindex_files'}->{$existing_f} = 1;
217        delete $block_hash->{'existing_files'}->{$existing_f};
218
219        }
220    }
221   
222    # metadata file needs to be in new_files list so parsed by MetadataXMLPlug
223    # (or equivalent)
224    $block_hash->{'new_files'}->{$new_mdf} = 1;
225
226    }
227
228    # go through remaining existing files and work out what has changed and needs to be reindexed.
229    my @existing_files = sort keys %{$block_hash->{'existing_files'}};
230
231    my $reindex_files = [];
232
233    foreach my $existing_filename (@existing_files) {
234    if (-M $existing_filename < $archiveinf_timestamp) {
235        # file is newer than last build
236       
237        my $existing_file = $existing_filename;
238        #my $collectdir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'});
239
240        #my $collectdir_resafe = &util::filename_to_regex($collectdir);
241        #$existing_file =~ s/^$collectdir_resafe(\\|\/)?//;
242       
243        print STDERR "**** Reindexing existing file: $existing_file\n";
244
245        push(@$reindex_files,$existing_file);
246        $block_hash->{'reindex_files'}->{$existing_filename} = 1;
247    }
248
249    }
250
251   
252    # By this point full_prev_all_files contains the files
253    # mentioned in archiveinf-src.db but are not in the 'import'
254    # folder (or whatever was specified through -importdir ...)
255
256    # This list can contain files that were created in the 'tmp' or
257    # 'cache' areas (such as screen-size and thumbnail images).
258    #
259    # In building the final list of files to delete, we test to see if
260    # it exists on the filesystem and if it does (unusual for a "normal"
261    # file in import, but possible in the case of 'tmp' files),
262    # supress it from going into the final list
263
264    my $collectdir = $ENV{'GSDLCOLLECTDIR'};
265
266    my @deleted_files = values %$full_prev_all_files;
267    map { my $curr_file = $_;
268      my $full_curr_file = $curr_file;
269
270      if (!&util::filename_is_absolute($curr_file)) {
271          # add in import dir to make absolute
272
273          $full_curr_file = &util::filename_cat($collectdir,$curr_file);
274      }
275
276
277      if (!-e $full_curr_file) {
278          $block_hash->{'deleted_files'}->{$curr_file} = 1;
279      }
280      } @deleted_files;
281
282
283
284}
285
286
287# this is used to delete "deleted" docs, and to remove old versions of "changed" docs
288# $mode is 'delete' or 'reindex'
289sub mark_docs_for_deletion
290{
291    my ($archive_info,$block_hash,$deleted_files,$archivedir,$verbosity,$mode) = @_;
292
293    my $mode_text = "deleted from index";
294    if ($mode eq "reindex") {
295    $mode_text = "reindexed";
296    }
297    my $arcinfo_doc_filename = &doc_db_file($archivedir);
298    my $arcinfo_src_filename = &src_db_file($archivedir);
299
300
301    # record files marked for deletion in arcinfo
302    foreach my $file (@$deleted_files) {
303    # use 'archiveinf-src' GDBM file to look up all the OIDs
304    # that this file is used in (note in most cases, it's just one OID)
305   
306    my $src_rec = GDBMUtils::gdbmRecordToHash($arcinfo_src_filename,$file);
307    my $oids = $src_rec->{'oid'};
308    my $file_record_deleted = 0;
309
310    # delete the src record
311    GDBMUtils::gdbmDatabaseRemove($arcinfo_src_filename,$file);
312   
313    foreach my $oid (@$oids) {
314
315        # find the source doc (the primary file that becomes this oid)
316        my $doc_rec = GDBMUtils::gdbmRecordToHash($arcinfo_doc_filename,$oid);
317        my $doc_source_file = $doc_rec->{'src-file'}->[0];
318        if (!&util::filename_is_absolute($doc_source_file)) {
319        $doc_source_file = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},$doc_source_file);
320        }
321
322        if ($doc_source_file ne $file) {
323        # its an associated or metadata file
324       
325        # mark source doc for reimport as one of its assoc files has changed or deleted
326        $block_hash->{'reindex_files'}->{$doc_source_file} = 1;
327       
328        }
329        my $curr_status = $archive_info->get_status_info($oid);
330        if (defined($curr_status) && (($curr_status ne "D"))) {
331        if ($verbosity>1) {
332            print STDERR "$oid ($doc_source_file) marked to be $mode_text on next buildcol.pl\n";
333        }
334        # mark oid for deletion (it will be deleted or reimported)
335        $archive_info->set_status_info($oid,"D");
336        my $val = &GDBMUtils::gdbmDatabaseGet($arcinfo_doc_filename,$oid);
337        $val =~ s/^<index-status>(.*)$/<index-status>D/m;
338        &GDBMUtils::gdbmDatabaseSet($arcinfo_doc_filename,$oid,$val);
339        }
340    }
341    }
342}
343
344
345
3461;
Note: See TracBrowser for help on using the browser.