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