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

Last change on this file since 28211 was 28211, checked in by ak19, 11 years ago

No more absolute paths in archiveinf-doc.gdb and archiveinf-src.gdb

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