source: main/trunk/greenstone2/perllib/inexport.pm@ 21619

Last change on this file since 21619 was 21619, checked in by mdewsnip, 14 years ago

Removed src_db_file() and doc_db_file() functions from inexport.pm. Part of making the code less GDBM-specific.

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