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