[537] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # arcinfo.pm --
|
---|
| 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 |
|
---|
[4] | 27 | # This module stores information about the archives. At the moment
|
---|
| 28 | # this information just consists of the file name (relative to the
|
---|
| 29 | # directory the archives information file is in) and its OID.
|
---|
| 30 |
|
---|
| 31 | # This module assumes there is a one to one correspondance between
|
---|
| 32 | # a file in the archives directory and an OID.
|
---|
| 33 |
|
---|
| 34 | package arcinfo;
|
---|
| 35 |
|
---|
[10157] | 36 | use constant ORDER_OID_INDEX => 0;
|
---|
| 37 | use constant ORDER_SORT_INDEX => 1;
|
---|
| 38 |
|
---|
| 39 | use constant INFO_FILE_INDEX => 0;
|
---|
| 40 | use constant INFO_STATUS_INDEX => 1;
|
---|
| 41 |
|
---|
[15889] | 42 | use strict;
|
---|
| 43 |
|
---|
[18441] | 44 | use dbutil;
|
---|
[15889] | 45 |
|
---|
[10157] | 46 | # File format read in: OID <tab> Filename <tab> Optional-Index-Status
|
---|
| 47 |
|
---|
| 48 | # Index status can be:
|
---|
| 49 | # I = Index for the first time
|
---|
| 50 | # R = Reindex
|
---|
| 51 | # D = Delete
|
---|
| 52 | # B = Been indexed
|
---|
| 53 |
|
---|
[4] | 54 | sub new {
|
---|
| 55 | my ($class) = @_;
|
---|
[14] | 56 | my $self = {'info'=>{},
|
---|
[19774] | 57 | 'reverse-info'=>{},
|
---|
[15073] | 58 | 'order'=>[],
|
---|
| 59 | 'reverse_sort'=>0};
|
---|
[4] | 60 |
|
---|
| 61 | return bless $self, $class;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[18441] | 64 | sub _load_info_txt
|
---|
| 65 | {
|
---|
[4] | 66 | my $self = shift (@_);
|
---|
| 67 | my ($filename) = @_;
|
---|
| 68 |
|
---|
[12328] | 69 | if (defined $filename && -e $filename) {
|
---|
[4] | 70 | open (INFILE, $filename) ||
|
---|
| 71 | die "arcinfo::load_info couldn't read $filename\n";
|
---|
| 72 |
|
---|
| 73 | my ($line, @line);
|
---|
| 74 | while (defined ($line = <INFILE>)) {
|
---|
| 75 | $line =~ s/\cM|\cJ//g; # remove end-of-line characters
|
---|
| 76 | @line = split ("\t", $line); # filename,
|
---|
| 77 | if (scalar(@line) >= 2) {
|
---|
[14] | 78 | $self->add_info (@line);
|
---|
[4] | 79 | }
|
---|
| 80 | }
|
---|
| 81 | close (INFILE);
|
---|
| 82 | }
|
---|
[18441] | 83 |
|
---|
| 84 |
|
---|
[4] | 85 | }
|
---|
| 86 |
|
---|
[18441] | 87 | sub _load_info_gdbm
|
---|
| 88 | {
|
---|
[4] | 89 | my $self = shift (@_);
|
---|
| 90 | my ($filename) = @_;
|
---|
| 91 |
|
---|
[18441] | 92 | my $infodb_map = {};
|
---|
| 93 |
|
---|
| 94 | &dbutil::read_infodb_file_gdbm($filename,$infodb_map);
|
---|
| 95 |
|
---|
| 96 | foreach my $oid ( keys %$infodb_map ) {
|
---|
| 97 | my $vals = $infodb_map->{$oid};
|
---|
| 98 | # interested in doc-file and index-status
|
---|
| 99 |
|
---|
| 100 | my ($doc_file) = ($vals=~/^<doc-file>(.*)$/m);
|
---|
| 101 | my ($index_status) = ($vals=~/^<index-status>(.*)$/m);
|
---|
| 102 | $self->add_info ($oid,$doc_file,$index_status);
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[19774] | 106 |
|
---|
[18441] | 107 | sub load_info {
|
---|
| 108 | my $self = shift (@_);
|
---|
| 109 | my ($filename) = @_;
|
---|
| 110 |
|
---|
| 111 | $self->{'info'} = {};
|
---|
| 112 |
|
---|
| 113 | if ((defined $filename) && (-e $filename)) {
|
---|
| 114 | if ($filename =~ m/\.inf$/) {
|
---|
| 115 | $self->_load_info_txt($filename);
|
---|
| 116 | }
|
---|
| 117 | else {
|
---|
| 118 | $self->_load_info_gdbm($filename);
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | sub _load_filelist_gdbm
|
---|
| 124 | {
|
---|
| 125 | my $self = shift (@_);
|
---|
| 126 | my ($filename) = @_;
|
---|
| 127 |
|
---|
| 128 | my $infodb_map = {};
|
---|
| 129 |
|
---|
| 130 | &dbutil::read_infodb_keys_gdbm($filename,$infodb_map);
|
---|
| 131 |
|
---|
| 132 | foreach my $file ( keys %$infodb_map ) {
|
---|
[18456] | 133 | $self->{'prev_import_filelist'}->{$file} = 1;
|
---|
[18441] | 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 |
|
---|
[18456] | 138 | sub load_prev_import_filelist {
|
---|
[18441] | 139 | my $self = shift (@_);
|
---|
| 140 | my ($filename) = @_;
|
---|
| 141 |
|
---|
| 142 | $self->{'import-filelist'} = {};
|
---|
| 143 |
|
---|
| 144 | if ((defined $filename) && (-e $filename)) {
|
---|
| 145 | if ($filename =~ m/\.inf$/) {
|
---|
| 146 | # e.g. 'archives-src.inf' (which includes complete list of file
|
---|
| 147 | # from last time import.pl was run)
|
---|
| 148 | $self->_load_info_txt($filename);
|
---|
| 149 | }
|
---|
| 150 | else {
|
---|
| 151 | $self->_load_filelist_gdbm($filename);
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[19774] | 156 | sub load_revinfo_UNTESTED
|
---|
| 157 | {
|
---|
| 158 | my $self = shift (@_);
|
---|
| 159 | my ($rev_filename) = @_;
|
---|
| 160 |
|
---|
| 161 | my $rev_infodb_map = {};
|
---|
| 162 |
|
---|
| 163 | &dbutil::read_infodb_file_gdbm($rev_filename,$rev_infodb_map);
|
---|
| 164 |
|
---|
| 165 | foreach my $srcfile ( keys %$rev_infodb_map ) {
|
---|
| 166 | my $vals = $rev_infodb_map->{$srcfile};
|
---|
| 167 |
|
---|
| 168 | foreach my $OID ($vals =~ m/^<oid>(.*)$/gm) {
|
---|
| 169 | $self->add_revinfo($srcfile,$OID);
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 |
|
---|
[18441] | 175 | sub _save_info_txt {
|
---|
| 176 | my $self = shift (@_);
|
---|
| 177 | my ($filename) = @_;
|
---|
| 178 |
|
---|
[14] | 179 | my ($OID, $info);
|
---|
[4] | 180 |
|
---|
| 181 | open (OUTFILE, ">$filename") ||
|
---|
| 182 | die "arcinfo::save_info couldn't write $filename\n";
|
---|
[7904] | 183 |
|
---|
[14] | 184 | foreach $info (@{$self->get_OID_list()}) {
|
---|
[4] | 185 | if (defined $info) {
|
---|
[14] | 186 | print OUTFILE join("\t", @$info), "\n";
|
---|
[4] | 187 | }
|
---|
| 188 | }
|
---|
| 189 | close (OUTFILE);
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[18441] | 192 | sub _save_info_gdbm {
|
---|
| 193 | my $self = shift (@_);
|
---|
| 194 | my ($filename) = @_;
|
---|
| 195 |
|
---|
| 196 | # Not the most efficient operation, but will do for now
|
---|
| 197 |
|
---|
| 198 | # read it in
|
---|
| 199 | my $infodb_map = {};
|
---|
| 200 | &dbutil::read_infodb_file_gdbm($filename,$infodb_map);
|
---|
| 201 |
|
---|
| 202 | # change index-status values
|
---|
| 203 | foreach my $info (@{$self->get_OID_list()}) {
|
---|
| 204 | if (defined $info) {
|
---|
| 205 | my ($oid,$doc_file,$index_status) = @$info;
|
---|
| 206 | if (defined $infodb_map->{$oid}) {
|
---|
| 207 | my $vals_ref = \$infodb_map->{$oid};
|
---|
| 208 | $$vals_ref =~ s/^<index-status>(.*)$/<index-status>$index_status/m;
|
---|
| 209 | }
|
---|
| 210 | else {
|
---|
| 211 | print STDERR "Warning: $filename does not have key $oid\n";
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 | # write out again
|
---|
| 218 | my $infodb_handle = &dbutil::open_infodb_write_handle_gdbm($filename);
|
---|
| 219 | foreach my $oid ( keys %$infodb_map ) {
|
---|
| 220 | # consider making the following a method in dbutil
|
---|
| 221 | # e.g. write_infodb_rawentry_gdbm($infodb_handle,$oid,$vals);
|
---|
| 222 |
|
---|
| 223 | # no need to escape, as $infodb_map->{$oid} hasn't been unescaped
|
---|
| 224 | print $infodb_handle "[$oid]\n";
|
---|
| 225 | print $infodb_handle $infodb_map->{$oid};
|
---|
| 226 | print $infodb_handle '-' x 70, "\n";
|
---|
| 227 | }
|
---|
| 228 | &dbutil::close_infodb_write_handle_gdbm($infodb_handle);
|
---|
| 229 |
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[19774] | 232 | sub save_revinfo_gdbm {
|
---|
| 233 | my $self = shift (@_);
|
---|
| 234 | my ($rev_filename) = @_;
|
---|
| 235 |
|
---|
| 236 | # Output reverse lookup database
|
---|
| 237 |
|
---|
| 238 | my $rev_infodb_map = $self->{'reverse-info'};
|
---|
| 239 | my $rev_infodb_handle
|
---|
| 240 | = &dbutil::open_infodb_write_handle_gdbm($rev_filename,"append");
|
---|
| 241 |
|
---|
| 242 | foreach my $key ( keys %$rev_infodb_map ) {
|
---|
| 243 | my $val_hash = $rev_infodb_map->{$key};
|
---|
| 244 | &dbutil::write_infodb_entry_gdbm($rev_infodb_handle,$key,$val_hash);
|
---|
| 245 | }
|
---|
| 246 | &dbutil::close_infodb_write_handle_gdbm($rev_infodb_handle);
|
---|
| 247 |
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[18441] | 250 | sub save_info {
|
---|
| 251 | my $self = shift (@_);
|
---|
| 252 | my ($filename) = @_;
|
---|
| 253 |
|
---|
[20537] | 254 | if ($filename =~ m/(contents)|(\.inf)$/) {
|
---|
[18441] | 255 | $self->_save_info_txt($filename);
|
---|
| 256 | }
|
---|
| 257 | else {
|
---|
| 258 | $self->_save_info_gdbm($filename);
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[14] | 262 | sub delete_info {
|
---|
| 263 | my $self = shift (@_);
|
---|
| 264 | my ($OID) = @_;
|
---|
| 265 |
|
---|
| 266 | if (defined $self->{'info'}->{$OID}) {
|
---|
| 267 | delete $self->{'info'}->{$OID};
|
---|
| 268 |
|
---|
| 269 | my $i = 0;
|
---|
| 270 | while ($i < scalar (@{$self->{'order'}})) {
|
---|
[10157] | 271 | if ($self->{'order'}->[$i]->[ORDER_OID_INDEX] eq $OID) {
|
---|
[14] | 272 | splice (@{$self->{'order'}}, $i, 1);
|
---|
| 273 | last;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | $i ++;
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[4] | 281 | sub add_info {
|
---|
| 282 | my $self = shift (@_);
|
---|
[10157] | 283 | my ($OID, $doc_file, $index_status, $sortmeta) = @_;
|
---|
[1287] | 284 | $sortmeta = "" unless defined $sortmeta;
|
---|
[10157] | 285 | $index_status = "I" unless defined $index_status; # I = needs indexing
|
---|
[4] | 286 |
|
---|
[3416] | 287 | if (! defined($OID)) {
|
---|
| 288 | # only happens when no files can be processed?
|
---|
| 289 | return undef;
|
---|
| 290 | }
|
---|
[10157] | 291 |
|
---|
[18469] | 292 | if (defined $self->{'info'}->{$OID}) {
|
---|
| 293 | # test to see if we are in a reindex situation
|
---|
| 294 |
|
---|
| 295 | my $existing_status_info = $self->get_status_info($OID);
|
---|
| 296 |
|
---|
| 297 | if ($existing_status_info eq "D") {
|
---|
| 298 | # yes, we're in a reindexing situation
|
---|
| 299 | $self->delete_info ($OID);
|
---|
| 300 |
|
---|
| 301 |
|
---|
| 302 | # force setting to "reindex"
|
---|
| 303 | $index_status = "R";
|
---|
| 304 |
|
---|
| 305 | }
|
---|
| 306 | else {
|
---|
| 307 | # some other, possibly erroneous, situation has arisen
|
---|
| 308 | # where the document already seems to exist
|
---|
| 309 | print STDERR "Warning: $OID already exists with index status $existing_status_info\n";
|
---|
| 310 | print STDERR " Deleting previous version\n";
|
---|
| 311 |
|
---|
| 312 | $self->delete_info ($OID);
|
---|
| 313 | }
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[10157] | 316 | $self->{'info'}->{$OID} = [$doc_file,$index_status];
|
---|
[1287] | 317 | push (@{$self->{'order'}}, [$OID, $sortmeta]);
|
---|
[18469] | 318 |
|
---|
| 319 |
|
---|
[4] | 320 | }
|
---|
| 321 |
|
---|
[10157] | 322 | sub set_status_info {
|
---|
[4] | 323 | my $self = shift (@_);
|
---|
[10157] | 324 | my ($OID, $index_status) = @_;
|
---|
[4] | 325 |
|
---|
[10157] | 326 | my $OID_info = $self->{'info'}->{$OID};
|
---|
| 327 | $OID_info->[INFO_STATUS_INDEX] = $index_status;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 |
|
---|
| 331 | sub get_status_info {
|
---|
| 332 | my $self = shift (@_);
|
---|
| 333 | my ($OID) = @_;
|
---|
| 334 |
|
---|
| 335 | my $index_status = undef;
|
---|
| 336 |
|
---|
| 337 | my $OID_info = $self->{'info'}->{$OID};
|
---|
| 338 | if (defined $OID_info) {
|
---|
| 339 | $index_status = $OID_info->[INFO_STATUS_INDEX];
|
---|
| 340 | }
|
---|
| 341 | else {
|
---|
| 342 | die "Unable to find document id $OID\n";
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | return $index_status;
|
---|
| 346 |
|
---|
| 347 | }
|
---|
| 348 |
|
---|
[19774] | 349 |
|
---|
| 350 | sub add_reverseinfo {
|
---|
| 351 | my $self = shift (@_);
|
---|
| 352 | my ($key, $OID) = @_;
|
---|
| 353 |
|
---|
| 354 | my $existing_key = $self->{'reverse-info'}->{$key};
|
---|
| 355 | if (!defined $existing_key) {
|
---|
| 356 | $existing_key = {};
|
---|
| 357 | $self->{'reverse-info'}->{$key} = $existing_key;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | my $existing_oid = $existing_key->{'oid'};
|
---|
| 361 | if (!defined $existing_oid) {
|
---|
| 362 | $existing_oid = [];
|
---|
| 363 | $existing_key->{'oid'} = $existing_oid;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | push(@$existing_oid,$OID);
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 |
|
---|
[15073] | 370 | sub reverse_sort
|
---|
| 371 | {
|
---|
| 372 | my $self = shift(@_);
|
---|
| 373 | $self->{'reverse_sort'} = 1;
|
---|
| 374 | }
|
---|
[10157] | 375 |
|
---|
| 376 | # returns a list of the form [[OID, doc_file, index_status], ...]
|
---|
| 377 | sub get_OID_list
|
---|
| 378 | {
|
---|
| 379 | my $self = shift (@_);
|
---|
| 380 |
|
---|
| 381 | my $order = $self->{'order'};
|
---|
| 382 |
|
---|
[15073] | 383 | my @sorted_order;
|
---|
| 384 | if ($self->{'reverse_sort'}) {
|
---|
| 385 | @sorted_order = sort {$b->[ORDER_SORT_INDEX] cmp $a->[ORDER_SORT_INDEX]} @$order;
|
---|
| 386 | } else {
|
---|
| 387 | @sorted_order = sort {$a->[ORDER_SORT_INDEX] cmp $b->[ORDER_SORT_INDEX]} @$order;
|
---|
| 388 | }
|
---|
[10157] | 389 |
|
---|
[4] | 390 | my @list = ();
|
---|
| 391 |
|
---|
[10157] | 392 | foreach my $OID_order (@sorted_order) {
|
---|
| 393 | my $OID = $OID_order->[ORDER_OID_INDEX];
|
---|
| 394 | my $OID_info = $self->{'info'}->{$OID};
|
---|
| 395 |
|
---|
| 396 | push (@list, [$OID, $OID_info->[INFO_FILE_INDEX],
|
---|
| 397 | $OID_info->[INFO_STATUS_INDEX]]);
|
---|
[4] | 398 | }
|
---|
| 399 |
|
---|
| 400 | return \@list;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 | # returns a list of the form [[doc_file, OID], ...]
|
---|
| 404 | sub get_file_list {
|
---|
| 405 | my $self = shift (@_);
|
---|
| 406 |
|
---|
[10157] | 407 | my $order = $self->{'order'};
|
---|
| 408 |
|
---|
[15073] | 409 | my @sorted_order;
|
---|
| 410 | if ($self->{'reverse_sort'}) {
|
---|
| 411 | @sorted_order = sort {$b->[ORDER_SORT_INDEX] cmp $a->[ORDER_SORT_INDEX]} @$order;
|
---|
| 412 | } else {
|
---|
| 413 | @sorted_order = sort {$a->[ORDER_SORT_INDEX] cmp $b->[ORDER_SORT_INDEX]} @$order;
|
---|
| 414 | }
|
---|
[10157] | 415 |
|
---|
[4] | 416 | my @list = ();
|
---|
| 417 |
|
---|
[15889] | 418 | foreach my $OID_order (@sorted_order) {
|
---|
[10157] | 419 | my $OID = $OID_order->[ORDER_OID_INDEX];
|
---|
| 420 | my $OID_info = $self->{'info'}->{$OID};
|
---|
| 421 |
|
---|
| 422 | push (@list, [$OID_info->[INFO_FILE_INDEX], $OID]);
|
---|
[4] | 423 | }
|
---|
| 424 |
|
---|
| 425 | return \@list;
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 |
|
---|
| 429 | # returns a list of the form [doc_file]
|
---|
| 430 | sub get_info {
|
---|
| 431 | my $self = shift (@_);
|
---|
| 432 | my ($OID) = @_;
|
---|
| 433 |
|
---|
| 434 | if (defined $self->{'info'}->{$OID}) {
|
---|
| 435 | return $self->{'info'}->{$OID};
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 | return undef;
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 |
|
---|
[98] | 442 | # returns the number of documents so far
|
---|
| 443 | sub size {
|
---|
| 444 | my $self = shift (@_);
|
---|
| 445 | return (scalar(@{$self->{'order'}}));
|
---|
| 446 | }
|
---|
| 447 |
|
---|
[4] | 448 | 1;
|
---|
| 449 |
|
---|