source: main/trunk/greenstone2/perllib/arcinfo.pm@ 36655

Last change on this file since 36655 was 33167, checked in by kjdon, 5 years ago

need to test for existence of rev info db before opening it - it won't be there if we are doing a full re-import

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 13.1 KB
RevLine 
[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
34package arcinfo;
35
[10157]36use constant ORDER_OID_INDEX => 0;
37use constant ORDER_SORT_INDEX => 1;
38
39use constant INFO_FILE_INDEX => 0;
40use constant INFO_STATUS_INDEX => 1;
41
[28637]42use constant INFO_GROUPPOS_INDEX => 3;
[15889]43use strict;
44
[21543]45use dbutil;
[15889]46
[21543]47
[10157]48# File format read in: OID <tab> Filename <tab> Optional-Index-Status
49
50# Index status can be:
51# I = Index for the first time
52# R = Reindex
53# D = Delete
54# B = Been indexed
55
[4]56sub new {
[21579]57 my $class = shift(@_);
58 my $infodbtype = shift(@_);
59
60 # If the infodbtype wasn't passed in, use the default from dbutil
61 if (!defined($infodbtype))
62 {
63 $infodbtype = &dbutil::get_default_infodb_type();
64 }
65
66 my $self = {'infodbtype' => $infodbtype,
67 'info'=>{},
[19774]68 'reverse-info'=>{},
[15073]69 'order'=>[],
[27697]70 'reverse_sort'=>0,
71 'sort'=>0};
[4]72
73 return bless $self, $class;
74}
75
[18441]76sub _load_info_txt
77{
[4]78 my $self = shift (@_);
79 my ($filename) = @_;
80
[31187]81 if (defined $filename && &FileUtils::fileExists($filename)) {
[4]82 open (INFILE, $filename) ||
83 die "arcinfo::load_info couldn't read $filename\n";
84
85 my ($line, @line);
86 while (defined ($line = <INFILE>)) {
87 $line =~ s/\cM|\cJ//g; # remove end-of-line characters
88 @line = split ("\t", $line); # filename,
89 if (scalar(@line) >= 2) {
[14]90 $self->add_info (@line);
[4]91 }
92 }
93 close (INFILE);
94 }
[18441]95
96
[4]97}
98
[21564]99sub _load_info_db
[18441]100{
[4]101 my $self = shift (@_);
102 my ($filename) = @_;
103
[18441]104 my $infodb_map = {};
105
[21579]106 &dbutil::read_infodb_file($self->{'infodbtype'}, $filename, $infodb_map);
[18441]107
108 foreach my $oid ( keys %$infodb_map ) {
109 my $vals = $infodb_map->{$oid};
110 # interested in doc-file and index-status
111
112 my ($doc_file) = ($vals=~/^<doc-file>(.*)$/m);
113 my ($index_status) = ($vals=~/^<index-status>(.*)$/m);
[20747]114 my ($sortmeta) = ($vals=~/^<sort-meta>(.*)$/m);
[28637]115 my ($group_position) = ($vals=~/^<group-position>(.*)$/m);
116 $self->add_info ($oid,$doc_file,$index_status,$sortmeta, $group_position);
[18441]117 }
118}
119
[19774]120
[18441]121sub load_info {
122 my $self = shift (@_);
123 my ($filename) = @_;
124
125 $self->{'info'} = {};
126
[31187]127 if ((defined $filename) && &FileUtils::fileExists($filename)) {
[18441]128 if ($filename =~ m/\.inf$/) {
129 $self->_load_info_txt($filename);
130 }
131 else {
[21564]132 $self->_load_info_db($filename);
[18441]133 }
134 }
135}
136
[21564]137sub _load_filelist_db
[18441]138{
139 my $self = shift (@_);
140 my ($filename) = @_;
141
142 my $infodb_map = {};
143
[21585]144 &dbutil::read_infodb_file($self->{'infodbtype'}, $filename, $infodb_map);
[18441]145
146 foreach my $file ( keys %$infodb_map ) {
[28211]147 # turn placeholders in the file keys of archiveinf-src file back to absolute paths
148 $file = &util::placeholders_to_abspath($file);
[18456]149 $self->{'prev_import_filelist'}->{$file} = 1;
[18441]150 }
151}
152
153
[18456]154sub load_prev_import_filelist {
[18441]155 my $self = shift (@_);
156 my ($filename) = @_;
157
158 $self->{'import-filelist'} = {};
159
[31187]160 if ((defined $filename) && &FileUtils::fileExists($filename)) {
[18441]161 if ($filename =~ m/\.inf$/) {
162 # e.g. 'archives-src.inf' (which includes complete list of file
163 # from last time import.pl was run)
164 $self->_load_info_txt($filename);
165 }
166 else {
[21564]167 $self->_load_filelist_db($filename);
[18441]168 }
169 }
170}
171
[32614]172sub load_rev_info
[19774]173{
174 my $self = shift (@_);
175 my ($rev_filename) = @_;
176
177 my $rev_infodb_map = {};
178
[33167]179 if ((defined $rev_filename) && &FileUtils::fileExists($rev_filename)) {
180 &dbutil::read_infodb_file($self->{'infodbtype'}, $rev_filename, $rev_infodb_map);
[19774]181
[33167]182 foreach my $srcfile ( keys %$rev_infodb_map ) {
183
184 my $vals = $rev_infodb_map->{$srcfile};
185
186 $srcfile = &util::placeholders_to_abspath($srcfile);
187 foreach my $OID ($vals =~ m/^<oid>(.*)$/gm) {
188 $self->add_reverseinfo($srcfile,$OID);
189 }
[19774]190 }
191 }
192}
193
194
[18441]195sub _save_info_txt {
196 my $self = shift (@_);
197 my ($filename) = @_;
198
[14]199 my ($OID, $info);
[4]200
201 open (OUTFILE, ">$filename") ||
202 die "arcinfo::save_info couldn't write $filename\n";
[7904]203
[14]204 foreach $info (@{$self->get_OID_list()}) {
[4]205 if (defined $info) {
[14]206 print OUTFILE join("\t", @$info), "\n";
[4]207 }
208 }
209 close (OUTFILE);
210}
211
[21564]212sub _save_info_db {
[18441]213 my $self = shift (@_);
214 my ($filename) = @_;
215
[21857]216 my $infodbtype = $self->{'infodbtype'};
217
[18441]218 # Not the most efficient operation, but will do for now
219
220 # read it in
221 my $infodb_map = {};
[21857]222 &dbutil::read_infodb_file($infodbtype, $filename, $infodb_map);
[18441]223
224 # change index-status values
225 foreach my $info (@{$self->get_OID_list()}) {
226 if (defined $info) {
227 my ($oid,$doc_file,$index_status) = @$info;
228 if (defined $infodb_map->{$oid}) {
229 my $vals_ref = \$infodb_map->{$oid};
230 $$vals_ref =~ s/^<index-status>(.*)$/<index-status>$index_status/m;
231 }
232 else {
233 print STDERR "Warning: $filename does not have key $oid\n";
234 }
235 }
236 }
237
238
239 # write out again
[21857]240 my $infodb_handle = &dbutil::open_infodb_write_handle($infodbtype, $filename);
[18441]241 foreach my $oid ( keys %$infodb_map ) {
[21857]242 my $vals = $infodb_map->{$oid};
243 &dbutil::write_infodb_rawentry($infodbtype,$infodb_handle,$oid,$vals);
[18441]244 }
[21857]245 &dbutil::close_infodb_write_handle($infodbtype, $infodb_handle);
[18441]246
247}
248
[21564]249sub save_revinfo_db {
[19774]250 my $self = shift (@_);
251 my ($rev_filename) = @_;
252 # Output reverse lookup database
253
254 my $rev_infodb_map = $self->{'reverse-info'};
[32614]255 my $rev_infodb_handle
256 = &dbutil::open_infodb_write_handle($self->{'infodbtype'}, $rev_filename);
257# = &dbutil::open_infodb_write_handle($self->{'infodbtype'}, $rev_filename, "append");
[19774]258
259 foreach my $key ( keys %$rev_infodb_map ) {
260 my $val_hash = $rev_infodb_map->{$key};
[28211]261 $key = &util::abspath_to_placeholders($key);
262
[21579]263 &dbutil::write_infodb_entry($self->{'infodbtype'}, $rev_infodb_handle, $key, $val_hash);
[19774]264 }
[21579]265 &dbutil::close_infodb_write_handle($self->{'infodbtype'}, $rev_infodb_handle);
[19774]266
267}
268
[18441]269sub save_info {
270 my $self = shift (@_);
271 my ($filename) = @_;
[32614]272
[20537]273 if ($filename =~ m/(contents)|(\.inf)$/) {
[18441]274 $self->_save_info_txt($filename);
275 }
276 else {
[21564]277 $self->_save_info_db($filename);
[18441]278 }
279}
280
[14]281sub delete_info {
282 my $self = shift (@_);
283 my ($OID) = @_;
284
285 if (defined $self->{'info'}->{$OID}) {
286 delete $self->{'info'}->{$OID};
287
288 my $i = 0;
289 while ($i < scalar (@{$self->{'order'}})) {
[10157]290 if ($self->{'order'}->[$i]->[ORDER_OID_INDEX] eq $OID) {
[14]291 splice (@{$self->{'order'}}, $i, 1);
292 last;
293 }
294
295 $i ++;
296 }
297 }
298}
299
[4]300sub add_info {
301 my $self = shift (@_);
[28637]302 my ($OID, $doc_file, $index_status, $sortmeta, $group_position) = @_;
[1287]303 $sortmeta = "" unless defined $sortmeta;
[10157]304 $index_status = "I" unless defined $index_status; # I = needs indexing
[3416]305 if (! defined($OID)) {
306 # only happens when no files can be processed?
307 return undef;
308 }
[10157]309
[18469]310 if (defined $self->{'info'}->{$OID}) {
311 # test to see if we are in a reindex situation
312
313 my $existing_status_info = $self->get_status_info($OID);
314
315 if ($existing_status_info eq "D") {
316 # yes, we're in a reindexing situation
317 $self->delete_info ($OID);
318
319
320 # force setting to "reindex"
321 $index_status = "R";
322
323 }
324 else {
325 # some other, possibly erroneous, situation has arisen
326 # where the document already seems to exist
327 print STDERR "Warning: $OID already exists with index status $existing_status_info\n";
328 print STDERR " Deleting previous version\n";
329
330 $self->delete_info ($OID);
331 }
332 }
333
[28637]334 $self->{'info'}->{$OID} = [$doc_file,$index_status,$sortmeta, $group_position];
[27697]335 push (@{$self->{'order'}}, [$OID, $sortmeta]); # ORDER_OID_INDEX and ORDER_SORT_INDEX
[18469]336
337
[4]338}
339
[10157]340sub set_status_info {
[4]341 my $self = shift (@_);
[10157]342 my ($OID, $index_status) = @_;
[4]343
[10157]344 my $OID_info = $self->{'info'}->{$OID};
345 $OID_info->[INFO_STATUS_INDEX] = $index_status;
346}
347
348
349sub get_status_info {
350 my $self = shift (@_);
351 my ($OID) = @_;
352
353 my $index_status = undef;
354
355 my $OID_info = $self->{'info'}->{$OID};
356 if (defined $OID_info) {
357 $index_status = $OID_info->[INFO_STATUS_INDEX];
358 }
359 else {
360 die "Unable to find document id $OID\n";
361 }
362
363 return $index_status;
364
365}
366
[28637]367sub get_group_position {
368 my $self = shift (@_);
369 my ($OID) = @_;
[19774]370
[28637]371 my $group_position = undef;
372 my $OID_info = $self->{'info'}->{$OID};
373 if (defined $OID_info) {
374 $group_position = $OID_info->[INFO_GROUPPOS_INDEX];
375 }
376 else {
377 die "Unable to find document id $OID\n";
378 }
379 return $group_position;
380
381}
[19774]382sub add_reverseinfo {
383 my $self = shift (@_);
384 my ($key, $OID) = @_;
385
386 my $existing_key = $self->{'reverse-info'}->{$key};
387 if (!defined $existing_key) {
388 $existing_key = {};
389 $self->{'reverse-info'}->{$key} = $existing_key;
390 }
391
392 my $existing_oid = $existing_key->{'oid'};
393 if (!defined $existing_oid) {
394 $existing_oid = [];
395 $existing_key->{'oid'} = $existing_oid;
396 }
397
[32614]398 for (@$existing_oid) {
399 if ($_ eq $OID) {
400 return; # already in the list
401 }
402 }
[19774]403 push(@$existing_oid,$OID);
[32614]404
[19774]405}
406
[32614]407sub remove_reverseinfo {
408 my $self = shift (@_);
409 my ($key, $OID) = @_;
410
411 my $existing_key = $self->{'reverse-info'}->{$key};
412 if (!defined $existing_key) {
413 ###print STDERR "trying to remove entry for $key, but its not there!\n";
414 return;
415 }
416 if (!defined $OID) {
417 ###print STDERR "no oid defined, removing whole entry\n";
418 delete $self->{'reverse-info'}->{$key};
419 return;
420 }
421 my $existing_oid = $existing_key->{'oid'};
422 if (!defined $existing_oid) {
423 ###print STDERR "trying to remove entry for $key, but it has no oid field!\n";
424 return;
425 }
426 for my $i (0..scalar(@$existing_oid)) {
427 if (@$existing_oid[$i] eq $OID) {
428 splice @$existing_oid, $i, 1;
429 if (scalar (@$existing_oid) ==0) {
430 ###print STDERRQ "have removed all oids, delete the main key\n";
431 delete $self->{'reverse-info'}->{$key};
432 }
433 return;
434 }
435 }
436}
437sub get_reverseinfo {
438 my $self = shift (@_);
439 my ($key) = @_;
440
441 if ($self->{'reverse-info'}->{$key}) {
442 return $self->{'reverse-info'}->{$key}->{'oid'};
443 }
444 return undef;
445}
446
[20802]447sub set_meta_file_flag {
448 my $self = shift (@_);
449 my ($key) = @_;
[19774]450
[20802]451 my $existing_key = $self->{'reverse-info'}->{$key};
452 if (!defined $existing_key) {
453 $existing_key = {};
454 $self->{'reverse-info'}->{$key} = $existing_key;
455 }
456
457 $existing_key->{'meta-file'} = ["1"];
458
459}
[15073]460sub reverse_sort
461{
462 my $self = shift(@_);
463 $self->{'reverse_sort'} = 1;
464}
[27697]465sub sort
466{
467 my $self = shift(@_);
468 $self->{'sort'} = 1;
469}
[10157]470
[27697]471
[10157]472# returns a list of the form [[OID, doc_file, index_status], ...]
473sub get_OID_list
474{
475 my $self = shift (@_);
476
477 my $order = $self->{'order'};
478
[15073]479 my @sorted_order;
480 if ($self->{'reverse_sort'}) {
481 @sorted_order = sort {$b->[ORDER_SORT_INDEX] cmp $a->[ORDER_SORT_INDEX]} @$order;
[27697]482 } elsif ($self->{'sort'}) {
[15073]483 @sorted_order = sort {$a->[ORDER_SORT_INDEX] cmp $b->[ORDER_SORT_INDEX]} @$order;
[27697]484 } else { # not sorting, don't bother
485 @sorted_order = @$order;
[15073]486 }
[10157]487
[4]488 my @list = ();
489
[10157]490 foreach my $OID_order (@sorted_order) {
491 my $OID = $OID_order->[ORDER_OID_INDEX];
492 my $OID_info = $self->{'info'}->{$OID};
493
494 push (@list, [$OID, $OID_info->[INFO_FILE_INDEX],
495 $OID_info->[INFO_STATUS_INDEX]]);
[4]496 }
497
498 return \@list;
499}
500
501# returns a list of the form [[doc_file, OID], ...]
502sub get_file_list {
503 my $self = shift (@_);
504
[10157]505 my $order = $self->{'order'};
506
[15073]507 my @sorted_order;
508 if ($self->{'reverse_sort'}) {
509 @sorted_order = sort {$b->[ORDER_SORT_INDEX] cmp $a->[ORDER_SORT_INDEX]} @$order;
[27697]510 } elsif ($self->{'sort'}) {
[15073]511 @sorted_order = sort {$a->[ORDER_SORT_INDEX] cmp $b->[ORDER_SORT_INDEX]} @$order;
[27697]512 } else { # not sorting, don't bother
513 @sorted_order = @$order;
[15073]514 }
[10157]515
[4]516 my @list = ();
517
[15889]518 foreach my $OID_order (@sorted_order) {
[10157]519 my $OID = $OID_order->[ORDER_OID_INDEX];
520 my $OID_info = $self->{'info'}->{$OID};
521
522 push (@list, [$OID_info->[INFO_FILE_INDEX], $OID]);
[4]523 }
524
525 return \@list;
526}
527
528
[28637]529# returns a list of the form [doc_file,index_status,$sort_meta, $group_position]
[4]530sub get_info {
531 my $self = shift (@_);
532 my ($OID) = @_;
533
534 if (defined $self->{'info'}->{$OID}) {
535 return $self->{'info'}->{$OID};
536 }
537
538 return undef;
539}
540
541
[20747]542
[98]543# returns the number of documents so far
544sub size {
545 my $self = shift (@_);
546 return (scalar(@{$self->{'order'}}));
547}
548
[4]5491;
550
Note: See TracBrowser for help on using the repository browser.