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

Last change on this file since 19049 was 19049, checked in by davidb, 15 years ago

No need to 'use File::Basename' anymore

  • Property svn:executable set to *
File size: 8.7 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 util;
31use GDBMUtils;
32
33
34sub prime_doc_oid_count
35{
36 my ($archivedir) = @_;
37 my $oid_count_filename = &util::filename_cat ($archivedir, "OIDcount");
38
39 if (-e $oid_count_filename) {
40 if (open(OIDIN,"<$oid_count_filename")) {
41 my $OIDcount = <OIDIN>;
42 chomp $OIDcount;
43 close(OIDIN);
44
45 $doc::OIDcount = $OIDcount;
46 }
47 else {
48
49 print STDERR "Warning: unable to read document OID count from $oid_count_filename\n";
50 print STDERR "Setting value to 0\n";
51 }
52 }
53
54}
55
56sub store_doc_oid_count
57{
58 # Use the file "OIDcount" in the archives directory to record
59 # what value doc.pm got up to
60
61 my ($archivedir) = @_;
62 my $oid_count_filename = &util::filename_cat ($archivedir, "OIDcount");
63
64
65 if (open(OIDOUT,">$oid_count_filename")) {
66 print OIDOUT $doc::OIDcount, "\n";
67
68 close(OIDOUT);
69 }
70 else {
71 print STDERR "Warning: unable to store document OID count\n";
72 }
73}
74
75
76
77sub new_vs_old_import_diff
78{
79 my ($archive_info,$block_hash,$importdir) = @_;
80
81 # First convert all files to absolute form
82 # This is to support the situation where the import folder is not
83 # the default
84
85 my $prev_all_files = $archive_info->{'prev_import_filelist'};
86 my $full_prev_all_files = {};
87
88 foreach my $prev_file (keys %$prev_all_files) {
89
90 if (!&util::filename_is_absolute($prev_file)) {
91 my $full_prev_file = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},$prev_file);
92 $full_prev_all_files->{$full_prev_file} = $prev_file;
93 }
94 else {
95 $full_prev_all_files->{$prev_file} = $prev_file;
96 }
97 }
98
99
100 # Figure out which are the new files, existing files and so
101 # by implication the files from the previous import that are not
102 # there any more => mark them for deletion
103 foreach my $curr_file (keys %{$block_hash->{'all_files'}}) {
104
105 my $full_curr_file = $curr_file;
106
107 # entry in 'all_files' is moved to either 'existing_files',
108 # 'deleted_files', or 'new_files'
109
110 if (!&util::filename_is_absolute($curr_file)) {
111 # add in import dir to make absolute
112 $full_curr_file = &util::filename_cat($importdir,$curr_file);
113 }
114
115 if (defined $block_hash->{'file_blocks'}->{$full_curr_file}) {
116 # If in block list, we want to ignore it
117 delete $block_hash->{'all_files'}->{$curr_file};
118
119 if (defined $full_prev_all_files->{$full_curr_file}) {
120 # also make sure it is gone from 'previous' list so
121 # not mistaken for a file that needs to be deleted
122 delete $full_prev_all_files->{$full_curr_file};
123 }
124 next;
125 }
126
127 # figure of if new file or not
128 if (defined $full_prev_all_files->{$full_curr_file}) {
129
130 # had it before
131 $block_hash->{'existing_files'}->{$full_curr_file} = 1;
132
133 # Now remove it, so by end of loop only the files
134 # that need deleting are left
135
136 delete $full_prev_all_files->{$full_curr_file};
137 }
138 else {
139 $block_hash->{'new_files'}->{$full_curr_file} = 1;
140 }
141
142 delete $block_hash->{'all_files'}->{$curr_file};
143 }
144
145 # By this point full_prev_all_files contains the files
146 # mentioned in archiveinf-src.db but are not in the 'import'
147 # folder (or whatever was specified through -importdir ...)
148
149 # This list can contain files that were created in the 'tmp' or
150 # 'cache' areas (such as screen-size and thumbnail images).
151 #
152 # In building the final list of files to delete, we test to see if
153 # it exists on the filesystem and if it does (unusual for a file
154 # that's allegedly deleted!) , supress it from going into the final
155 # list
156
157 my $collectdir = $ENV{'GSDLCOLLECTDIR'};
158
159 my @deleted_files = values %$full_prev_all_files;
160 map { my $curr_file = $_;
161 my $full_curr_file = $curr_file;
162
163 if (!&util::filename_is_absolute($curr_file)) {
164 # add in import dir to make absolute
165
166 $full_curr_file = &util::filename_cat($collectdir,$curr_file);
167 }
168
169
170 if (!-e $full_curr_file) {
171 $block_hash->{'deleted_files'}->{$curr_file} = 1;
172 }
173 } @deleted_files;
174}
175
176
177sub _mark_docs_for_deletion
178{
179 my ($archive_info,$deleted_files_ref,$archivedir,$verbosity,$mode_text) = @_;
180
181 my $doc_db = "archiveinf-doc.gdb";
182 my $src_db = "archiveinf-src.gdb";
183 my $arcinfo_doc_filename = &util::filename_cat ($archivedir, $doc_db);
184 my $arcinfo_src_filename = &util::filename_cat ($archivedir, $src_db);
185
186
187 # record files marked for deletion in arcinfo
188 foreach my $file (@$deleted_files_ref) {
189 # use 'archiveinf-src' GDBM file to look up all the OIDs
190 # this file is used in (note in most cases, it's just one OID)
191
192 my $src_rec = GDBMUtils::gdbmRecordToHash($arcinfo_src_filename,$file);
193 my $oids = $src_rec->{'oid'};
194 foreach my $oid (@$oids) {
195
196 # Find out if it's an assoc file or main doc
197
198 my $doc_rec = GDBMUtils::gdbmRecordToHash($arcinfo_doc_filename,$oid);
199 if ($doc_rec->{'src-file'}->[0] eq $file) {
200 # It's the main doc
201 # => mark it for deletion
202
203 if ($verbosity>1) {
204 print STDERR "$oid ($file) marked to be $mode_text on next buildcol.pl\n";
205 }
206 $archive_info->set_status_info($oid,"D");
207
208 my $val = &GDBMUtils::gdbmDatabaseGet($arcinfo_doc_filename,$oid);
209 my ($index_status) = ($val =~ m/^<index-status>(.*)$/m);
210 if ($index_status ne "D") {
211 $val =~ s/^<index-status>(.*)$/<index-status>D/m;
212 &GDBMUtils::gdbmDatabaseSet($arcinfo_doc_filename,$oid,$val);
213 }
214 }
215 else {
216 # assoc file => mark it for re-indexing (safest thing to do)
217 my $curr_status = $archive_info->get_status_info($oid);
218
219
220 if (defined($curr_status) && (($curr_status ne "D") && ($curr_status ne "R"))) {
221 if ($verbosity > 1) {
222 print STDERR "$oid marked for (potential) reindexing\n";
223 print STDERR " (because associated file $file deleted)\n";
224 }
225 $archive_info->set_status_info($oid,"R");
226
227 my $val = &GDBMUtils::gdbmDatabaseGet($arcinfo_doc_filename,$oid);
228 $val =~ s/^<index-status>(.*)$/<index-status>R/m;
229 &GDBMUtils::gdbmDatabaseSet($arcinfo_doc_filename,$oid,$val);
230 }
231 }
232
233 GDBMUtils::gdbmDatabaseRemove($arcinfo_src_filename,$file);
234 }
235 }
236}
237
238
239sub mark_docs_for_deletion
240{
241 _mark_docs_for_deletion(@_,"deleted from index");
242}
243
244
245sub mark_docs_for_reindex
246{
247 my ($archive_info,$existing_files_ref,$archivedir,$verbosity) = @_;
248
249 # Reindexing is accomplished by deleting the previously indexed
250 # version of the document, and then allowing the new version to
251 # be indexed (as would a new document be indexed).
252 #
253 # The first step (marking for deletion) is implemented by this routine.
254 #
255 # By default in Greenstone a new version of an index will hash to
256 # a new unique OID, and the above strategy of reindex=delete+add
257 # works fine. A special case arises when a persistent OID is
258 # allocated to a document (for instance through a metadata field),
259 # and the second step to reindexing (see XXXX) detects this and
260 # deals with it appropriately.
261
262 my $doc_db = "archiveinf-doc.gdb";
263 my $arcinfo_doc_filename = &util::filename_cat ($archivedir, $doc_db);
264
265
266 my $archiveinf_timestamp = -M $arcinfo_doc_filename;
267
268 my $reindex_files_ref = [];
269
270 foreach my $existing_filename (@$existing_files_ref) {
271
272 if (-M $existing_filename < $archiveinf_timestamp) {
273 # file is newer than last build
274
275 my $existing_file = $existing_filename;
276 my $collectdir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'});
277
278 $existing_file =~ s/^$collectdir(\\|\/)?//;
279
280### print STDERR "**** Deleting existing file: $existing_file\n";
281
282 push(@$reindex_files_ref,$existing_file);
283 }
284
285 }
286
287 _mark_docs_for_deletion($archive_info,$reindex_files_ref,$archivedir,$verbosity, "reindex");
288
289 return @$reindex_files_ref;
290}
291
292
293
2941;
Note: See TracBrowser for help on using the repository browser.