[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 |
|
---|
[18457] | 32 | use util;
|
---|
| 33 | use GDBMUtils;
|
---|
| 34 |
|
---|
[21306] | 35 | sub src_db_file {
|
---|
| 36 | my ($archivedir) = @_;
|
---|
| 37 | return &util::filename_cat ($archivedir, "archiveinf-src.gdb");
|
---|
| 38 | }
|
---|
[18528] | 39 |
|
---|
[21306] | 40 | sub doc_db_file {
|
---|
| 41 | my ($archivedir) = @_;
|
---|
| 42 | return &util::filename_cat ($archivedir, "archiveinf-doc.gdb");
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | sub oid_count_file {
|
---|
| 46 | my ($archivedir) = @_;
|
---|
| 47 | return &util::filename_cat ($archivedir, "OIDcount");
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 |
|
---|
[18528] | 51 | sub prime_doc_oid_count
|
---|
| 52 | {
|
---|
| 53 | my ($archivedir) = @_;
|
---|
[21306] | 54 | my $oid_count_filename = &oid_count_file($archivedir);
|
---|
[18528] | 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 |
|
---|
| 73 | sub 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) = @_;
|
---|
[21306] | 79 | my $oid_count_filename = &oid_count_file($archivedir);
|
---|
[18528] | 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 |
|
---|
[18457] | 94 | sub new_vs_old_import_diff
|
---|
| 95 | {
|
---|
[20578] | 96 | my ($archive_info,$block_hash,$importdir,$archivedir,$verbosity,$incremental_mode) = @_;
|
---|
[18457] | 97 |
|
---|
[20776] | 98 | # in this method, we want to know if metadata files are modified or not.
|
---|
[21306] | 99 | my $arcinfo_doc_filename = &doc_db_file($archivedir);
|
---|
[20776] | 100 |
|
---|
| 101 | my $archiveinf_timestamp = -M $arcinfo_doc_filename;
|
---|
| 102 |
|
---|
[18457] | 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 |
|
---|
[18469] | 121 |
|
---|
[18457] | 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',
|
---|
[20776] | 130 | # 'deleted_files', 'new_files', or 'new_or_modified_metadata_files'
|
---|
[18457] | 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 |
|
---|
[19498] | 137 | # figure out if new file or not
|
---|
[18457] | 138 | if (defined $full_prev_all_files->{$full_curr_file}) {
|
---|
[20776] | 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}) {
|
---|
[20578] | 144 |
|
---|
[20776] | 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 | }
|
---|
[20578] | 151 | }
|
---|
| 152 | else {
|
---|
[20776] | 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 | }
|
---|
[20578] | 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)
|
---|
[18457] | 176 |
|
---|
[20776] | 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;
|
---|
[20578] | 179 | }
|
---|
| 180 | else {
|
---|
| 181 | $block_hash->{'new_files'}->{$full_curr_file} = 1;
|
---|
| 182 | }
|
---|
[18457] | 183 | }
|
---|
[20578] | 184 |
|
---|
[18457] | 185 |
|
---|
| 186 | delete $block_hash->{'all_files'}->{$curr_file};
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[20578] | 189 |
|
---|
[21306] | 190 |
|
---|
| 191 |
|
---|
[20776] | 192 | # Deal with complication of new or modified metadata files by forcing
|
---|
[20578] | 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 |
|
---|
[20776] | 200 | foreach my $new_mdf (keys %{$block_hash->{'new_or_modified_metadata_files'}}) {
|
---|
[20578] | 201 | my ($fileroot,$situated_dir,$ext) = fileparse($new_mdf, "\\.[^\\.]+\$");
|
---|
| 202 |
|
---|
| 203 | $situated_dir =~ s/[\\\/]+$//; # remove tailing slashes
|
---|
[20769] | 204 | $situated_dir =~ s/\\/\\\\/g; # need to protect windows slash \ in regular expression
|
---|
| 205 |
|
---|
[20578] | 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'}}) {
|
---|
[20769] | 213 |
|
---|
[20578] | 214 | if ($existing_f =~ m/^$situated_dir/) {
|
---|
| 215 | push(@$reindex_files,$existing_f);
|
---|
| 216 | $block_hash->{'reindex_files'}->{$existing_f} = 1;
|
---|
[21306] | 217 | delete $block_hash->{'existing_files'}->{$existing_f};
|
---|
[20578] | 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 |
|
---|
[21306] | 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 |
|
---|
[20578] | 251 |
|
---|
[18469] | 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).
|
---|
[18457] | 258 | #
|
---|
[18469] | 259 | # In building the final list of files to delete, we test to see if
|
---|
[20578] | 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
|
---|
[18469] | 263 |
|
---|
| 264 | my $collectdir = $ENV{'GSDLCOLLECTDIR'};
|
---|
| 265 |
|
---|
[18457] | 266 | my @deleted_files = values %$full_prev_all_files;
|
---|
[18469] | 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;
|
---|
[20578] | 281 |
|
---|
| 282 |
|
---|
| 283 |
|
---|
[18457] | 284 | }
|
---|
| 285 |
|
---|
[19498] | 286 |
|
---|
[20788] | 287 | # this is used to delete "deleted" docs, and to remove old versions of "changed" docs
|
---|
[21306] | 288 | # $mode is 'delete' or 'reindex'
|
---|
| 289 | sub mark_docs_for_deletion
|
---|
[18457] | 290 | {
|
---|
[21306] | 291 | my ($archive_info,$block_hash,$deleted_files,$archivedir,$verbosity,$mode) = @_;
|
---|
[18457] | 292 |
|
---|
[21306] | 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);
|
---|
[18457] | 299 |
|
---|
| 300 |
|
---|
| 301 | # record files marked for deletion in arcinfo
|
---|
[19498] | 302 | foreach my $file (@$deleted_files) {
|
---|
[18457] | 303 | # use 'archiveinf-src' GDBM file to look up all the OIDs
|
---|
[19789] | 304 | # that this file is used in (note in most cases, it's just one OID)
|
---|
[18457] | 305 |
|
---|
| 306 | my $src_rec = GDBMUtils::gdbmRecordToHash($arcinfo_src_filename,$file);
|
---|
| 307 | my $oids = $src_rec->{'oid'};
|
---|
[20776] | 308 | my $file_record_deleted = 0;
|
---|
[20788] | 309 |
|
---|
| 310 | # delete the src record
|
---|
| 311 | GDBMUtils::gdbmDatabaseRemove($arcinfo_src_filename,$file);
|
---|
| 312 |
|
---|
[18457] | 313 | foreach my $oid (@$oids) {
|
---|
| 314 |
|
---|
[20788] | 315 | # find the source doc (the primary file that becomes this oid)
|
---|
[18457] | 316 | my $doc_rec = GDBMUtils::gdbmRecordToHash($arcinfo_doc_filename,$oid);
|
---|
[20776] | 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 | }
|
---|
[18457] | 321 |
|
---|
[20788] | 322 | if ($doc_source_file ne $file) {
|
---|
| 323 | # its an associated or metadata file
|
---|
| 324 |
|
---|
[20776] | 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;
|
---|
[20788] | 327 |
|
---|
[18457] | 328 | }
|
---|
[20788] | 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";
|
---|
[19498] | 333 | }
|
---|
[20788] | 334 | # mark oid for deletion (it will be deleted or reimported)
|
---|
| 335 | $archive_info->set_status_info($oid,"D");
|
---|
[21552] | 336 | my $val = &dbutil::read_infodb_entry("gdbm", $arcinfo_doc_filename, $oid);
|
---|
[20788] | 337 | $val =~ s/^<index-status>(.*)$/<index-status>D/m;
|
---|
| 338 | &GDBMUtils::gdbmDatabaseSet($arcinfo_doc_filename,$oid,$val);
|
---|
[19498] | 339 | }
|
---|
[18457] | 340 | }
|
---|
| 341 | }
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 |
|
---|
[18554] | 345 |
|
---|
[18457] | 346 | 1;
|
---|