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

Last change on this file since 12328 was 12328, checked in by shaoqun, 15 years ago

added code to guard undefined value

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