source: gsdl/trunk/perllib/arcinfo.pm@ 18430

Last change on this file since 18430 was 15889, checked in by mdewsnip, 16 years ago

Added "use strict", and fixed resulting problems.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
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
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
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
42use strict;
43
44
45# File format read in: OID <tab> Filename <tab> Optional-Index-Status
46
47# Index status can be:
48# I = Index for the first time
49# R = Reindex
50# D = Delete
51# B = Been indexed
52
53sub new {
54 my ($class) = @_;
55 my $self = {'info'=>{},
56 'order'=>[],
57 'reverse_sort'=>0};
58
59 return bless $self, $class;
60}
61
62sub load_info {
63 my $self = shift (@_);
64 my ($filename) = @_;
65
66 $self->{'info'} = {};
67
68 if (defined $filename && -e $filename) {
69 open (INFILE, $filename) ||
70 die "arcinfo::load_info couldn't read $filename\n";
71
72 my ($line, @line);
73 while (defined ($line = <INFILE>)) {
74 $line =~ s/\cM|\cJ//g; # remove end-of-line characters
75 @line = split ("\t", $line); # filename,
76 if (scalar(@line) >= 2) {
77 $self->add_info (@line);
78 }
79 }
80 close (INFILE);
81 }
82}
83
84sub save_info {
85 my $self = shift (@_);
86 my ($filename) = @_;
87
88 my ($OID, $info);
89
90 open (OUTFILE, ">$filename") ||
91 die "arcinfo::save_info couldn't write $filename\n";
92
93 foreach $info (@{$self->get_OID_list()}) {
94 if (defined $info) {
95 print OUTFILE join("\t", @$info), "\n";
96 }
97 }
98 close (OUTFILE);
99}
100
101sub delete_info {
102 my $self = shift (@_);
103 my ($OID) = @_;
104
105 if (defined $self->{'info'}->{$OID}) {
106 delete $self->{'info'}->{$OID};
107
108 my $i = 0;
109 while ($i < scalar (@{$self->{'order'}})) {
110 if ($self->{'order'}->[$i]->[ORDER_OID_INDEX] eq $OID) {
111 splice (@{$self->{'order'}}, $i, 1);
112 last;
113 }
114
115 $i ++;
116 }
117 }
118}
119
120sub add_info {
121 my $self = shift (@_);
122 my ($OID, $doc_file, $index_status, $sortmeta) = @_;
123 $sortmeta = "" unless defined $sortmeta;
124 $index_status = "I" unless defined $index_status; # I = needs indexing
125
126 if (! defined($OID)) {
127 # only happens when no files can be processed?
128 return undef;
129 }
130
131 $self->delete_info ($OID);
132 $self->{'info'}->{$OID} = [$doc_file,$index_status];
133 push (@{$self->{'order'}}, [$OID, $sortmeta]);
134}
135
136sub set_status_info {
137 my $self = shift (@_);
138 my ($OID, $index_status) = @_;
139
140 my $OID_info = $self->{'info'}->{$OID};
141 $OID_info->[INFO_STATUS_INDEX] = $index_status;
142}
143
144
145sub get_status_info {
146 my $self = shift (@_);
147 my ($OID) = @_;
148
149 my $index_status = undef;
150
151 my $OID_info = $self->{'info'}->{$OID};
152 if (defined $OID_info) {
153 $index_status = $OID_info->[INFO_STATUS_INDEX];
154 }
155 else {
156 die "Unable to find document id $OID\n";
157 }
158
159 return $index_status;
160
161}
162
163sub reverse_sort
164{
165 my $self = shift(@_);
166 $self->{'reverse_sort'} = 1;
167}
168
169# returns a list of the form [[OID, doc_file, index_status], ...]
170sub get_OID_list
171{
172 my $self = shift (@_);
173
174 my $order = $self->{'order'};
175
176 my @sorted_order;
177 if ($self->{'reverse_sort'}) {
178 @sorted_order = sort {$b->[ORDER_SORT_INDEX] cmp $a->[ORDER_SORT_INDEX]} @$order;
179 } else {
180 @sorted_order = sort {$a->[ORDER_SORT_INDEX] cmp $b->[ORDER_SORT_INDEX]} @$order;
181 }
182
183 my @list = ();
184
185 foreach my $OID_order (@sorted_order) {
186 my $OID = $OID_order->[ORDER_OID_INDEX];
187 my $OID_info = $self->{'info'}->{$OID};
188
189 push (@list, [$OID, $OID_info->[INFO_FILE_INDEX],
190 $OID_info->[INFO_STATUS_INDEX]]);
191 }
192
193 return \@list;
194}
195
196# returns a list of the form [[doc_file, OID], ...]
197sub get_file_list {
198 my $self = shift (@_);
199
200 my $order = $self->{'order'};
201
202 my @sorted_order;
203 if ($self->{'reverse_sort'}) {
204 @sorted_order = sort {$b->[ORDER_SORT_INDEX] cmp $a->[ORDER_SORT_INDEX]} @$order;
205 } else {
206 @sorted_order = sort {$a->[ORDER_SORT_INDEX] cmp $b->[ORDER_SORT_INDEX]} @$order;
207 }
208
209 my @list = ();
210
211 foreach my $OID_order (@sorted_order) {
212 my $OID = $OID_order->[ORDER_OID_INDEX];
213 my $OID_info = $self->{'info'}->{$OID};
214
215 push (@list, [$OID_info->[INFO_FILE_INDEX], $OID]);
216 }
217
218 return \@list;
219}
220
221
222# returns a list of the form [doc_file]
223sub get_info {
224 my $self = shift (@_);
225 my ($OID) = @_;
226
227 if (defined $self->{'info'}->{$OID}) {
228 return $self->{'info'}->{$OID};
229 }
230
231 return undef;
232}
233
234
235# returns the number of documents so far
236sub size {
237 my $self = shift (@_);
238 return (scalar(@{$self->{'order'}}));
239}
240
2411;
242
Note: See TracBrowser for help on using the repository browser.