[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 |
|
---|
| 35 | package arcinfo;
|
---|
| 36 |
|
---|
| 37 | sub new {
|
---|
| 38 | my ($class) = @_;
|
---|
[14] | 39 | my $self = {'info'=>{},
|
---|
| 40 | 'order'=>[]};
|
---|
[4] | 41 |
|
---|
| 42 | return bless $self, $class;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | sub load_info {
|
---|
| 46 | my $self = shift (@_);
|
---|
| 47 | my ($filename) = @_;
|
---|
| 48 |
|
---|
| 49 | $self->{'info'} = {};
|
---|
[7904] | 50 |
|
---|
[4] | 51 | if (-e $filename) {
|
---|
| 52 | open (INFILE, $filename) ||
|
---|
| 53 | die "arcinfo::load_info couldn't read $filename\n";
|
---|
| 54 |
|
---|
| 55 | my ($line, @line);
|
---|
| 56 | while (defined ($line = <INFILE>)) {
|
---|
| 57 | $line =~ s/\cM|\cJ//g; # remove end-of-line characters
|
---|
| 58 | @line = split ("\t", $line); # filename,
|
---|
| 59 | if (scalar(@line) >= 2) {
|
---|
[14] | 60 | $self->add_info (@line);
|
---|
[4] | 61 | }
|
---|
| 62 | }
|
---|
| 63 | close (INFILE);
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | sub save_info {
|
---|
| 68 | my $self = shift (@_);
|
---|
| 69 | my ($filename) = @_;
|
---|
| 70 |
|
---|
[14] | 71 | my ($OID, $info);
|
---|
[4] | 72 |
|
---|
| 73 | open (OUTFILE, ">$filename") ||
|
---|
| 74 | die "arcinfo::save_info couldn't write $filename\n";
|
---|
[7904] | 75 |
|
---|
[14] | 76 | foreach $info (@{$self->get_OID_list()}) {
|
---|
[4] | 77 | if (defined $info) {
|
---|
[14] | 78 | print OUTFILE join("\t", @$info), "\n";
|
---|
[4] | 79 | }
|
---|
| 80 | }
|
---|
| 81 | close (OUTFILE);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[14] | 84 | sub delete_info {
|
---|
| 85 | my $self = shift (@_);
|
---|
| 86 | my ($OID) = @_;
|
---|
| 87 |
|
---|
| 88 | if (defined $self->{'info'}->{$OID}) {
|
---|
| 89 | delete $self->{'info'}->{$OID};
|
---|
| 90 |
|
---|
| 91 | my $i = 0;
|
---|
| 92 | while ($i < scalar (@{$self->{'order'}})) {
|
---|
[1287] | 93 | if ($self->{'order'}->[$i]->[0] eq $OID) {
|
---|
[14] | 94 | splice (@{$self->{'order'}}, $i, 1);
|
---|
| 95 | last;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | $i ++;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[4] | 103 | sub add_info {
|
---|
| 104 | my $self = shift (@_);
|
---|
[1287] | 105 | my ($OID, $doc_file, $sortmeta) = @_;
|
---|
| 106 | $sortmeta = "" unless defined $sortmeta;
|
---|
[4] | 107 |
|
---|
[3416] | 108 | if (! defined($OID)) {
|
---|
| 109 | # only happens when no files can be processed?
|
---|
| 110 | return undef;
|
---|
| 111 | }
|
---|
[14] | 112 | $self->delete_info ($OID);
|
---|
[4] | 113 | $self->{'info'}->{$OID} = [$doc_file];
|
---|
[1287] | 114 | push (@{$self->{'order'}}, [$OID, $sortmeta]);
|
---|
[4] | 115 | }
|
---|
| 116 |
|
---|
| 117 | # returns a list of the form [[OID, doc_file], ...]
|
---|
| 118 | sub get_OID_list {
|
---|
| 119 | my $self = shift (@_);
|
---|
| 120 |
|
---|
| 121 | my ($OID);
|
---|
| 122 | my @list = ();
|
---|
| 123 |
|
---|
[1287] | 124 | foreach $OID (sort {$a->[1] cmp $b->[1]} @{$self->{'order'}}) {
|
---|
| 125 | push (@list, [$OID->[0], $self->{'info'}->{$OID->[0]}->[0]]);
|
---|
[4] | 126 | }
|
---|
| 127 |
|
---|
| 128 | return \@list;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | # returns a list of the form [[doc_file, OID], ...]
|
---|
| 132 | sub get_file_list {
|
---|
| 133 | my $self = shift (@_);
|
---|
| 134 |
|
---|
| 135 | my ($OID);
|
---|
| 136 | my @list = ();
|
---|
| 137 |
|
---|
[1287] | 138 | foreach $OID (sort {$a->[1] cmp $b->[1]} @{$self->{'order'}}) {
|
---|
| 139 | push (@list, [$self->{'info'}->{$OID->[0]}->[0], $OID->[0]]);
|
---|
[4] | 140 | }
|
---|
| 141 |
|
---|
| 142 | return \@list;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 | # returns a list of the form [doc_file]
|
---|
| 147 | sub get_info {
|
---|
| 148 | my $self = shift (@_);
|
---|
| 149 | my ($OID) = @_;
|
---|
| 150 |
|
---|
| 151 | if (defined $self->{'info'}->{$OID}) {
|
---|
| 152 | return $self->{'info'}->{$OID};
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | return undef;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 |
|
---|
[98] | 159 | # returns the number of documents so far
|
---|
| 160 | sub size {
|
---|
| 161 | my $self = shift (@_);
|
---|
| 162 | return (scalar(@{$self->{'order'}}));
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[4] | 165 | 1;
|
---|
| 166 |
|
---|