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