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 | package arcinfo;
|
---|
35 |
|
---|
36 | use constant ORDER_OID_INDEX => 0;
|
---|
37 | use constant ORDER_SORT_INDEX => 1;
|
---|
38 |
|
---|
39 | use constant INFO_FILE_INDEX => 0;
|
---|
40 | use constant INFO_STATUS_INDEX => 1;
|
---|
41 |
|
---|
42 | use strict;
|
---|
43 |
|
---|
44 | use dbutil;
|
---|
45 |
|
---|
46 | # File format read in: OID <tab> Filename <tab> Optional-Index-Status
|
---|
47 |
|
---|
48 | # Index status can be:
|
---|
49 | # I = Index for the first time
|
---|
50 | # R = Reindex
|
---|
51 | # D = Delete
|
---|
52 | # B = Been indexed
|
---|
53 |
|
---|
54 | sub new {
|
---|
55 | my ($class) = @_;
|
---|
56 | my $self = {'info'=>{},
|
---|
57 | 'order'=>[],
|
---|
58 | 'reverse_sort'=>0};
|
---|
59 |
|
---|
60 | return bless $self, $class;
|
---|
61 | }
|
---|
62 |
|
---|
63 | sub _load_info_txt
|
---|
64 | {
|
---|
65 | my $self = shift (@_);
|
---|
66 | my ($filename) = @_;
|
---|
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 |
|
---|
84 | }
|
---|
85 |
|
---|
86 | sub _load_info_gdbm
|
---|
87 | {
|
---|
88 | my $self = shift (@_);
|
---|
89 | my ($filename) = @_;
|
---|
90 |
|
---|
91 | my $infodb_map = {};
|
---|
92 |
|
---|
93 | &dbutil::read_infodb_file_gdbm($filename,$infodb_map);
|
---|
94 |
|
---|
95 | foreach my $oid ( keys %$infodb_map ) {
|
---|
96 | my $vals = $infodb_map->{$oid};
|
---|
97 | # interested in doc-file and index-status
|
---|
98 |
|
---|
99 | my ($doc_file) = ($vals=~/^<doc-file>(.*)$/m);
|
---|
100 | my ($index_status) = ($vals=~/^<index-status>(.*)$/m);
|
---|
101 | $self->add_info ($oid,$doc_file,$index_status);
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | sub load_info {
|
---|
106 | my $self = shift (@_);
|
---|
107 | my ($filename) = @_;
|
---|
108 |
|
---|
109 | $self->{'info'} = {};
|
---|
110 |
|
---|
111 | if ((defined $filename) && (-e $filename)) {
|
---|
112 | if ($filename =~ m/\.inf$/) {
|
---|
113 | $self->_load_info_txt($filename);
|
---|
114 | }
|
---|
115 | else {
|
---|
116 | $self->_load_info_gdbm($filename);
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | sub _load_filelist_gdbm
|
---|
122 | {
|
---|
123 | my $self = shift (@_);
|
---|
124 | my ($filename) = @_;
|
---|
125 |
|
---|
126 | my $infodb_map = {};
|
---|
127 |
|
---|
128 | &dbutil::read_infodb_keys_gdbm($filename,$infodb_map);
|
---|
129 |
|
---|
130 | foreach my $file ( keys %$infodb_map ) {
|
---|
131 | $self->{'prev_import_filelist'}->{$file} = 1;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | sub load_prev_import_filelist {
|
---|
137 | my $self = shift (@_);
|
---|
138 | my ($filename) = @_;
|
---|
139 |
|
---|
140 | $self->{'import-filelist'} = {};
|
---|
141 |
|
---|
142 | if ((defined $filename) && (-e $filename)) {
|
---|
143 | if ($filename =~ m/\.inf$/) {
|
---|
144 | # e.g. 'archives-src.inf' (which includes complete list of file
|
---|
145 | # from last time import.pl was run)
|
---|
146 | $self->_load_info_txt($filename);
|
---|
147 | }
|
---|
148 | else {
|
---|
149 | $self->_load_filelist_gdbm($filename);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | sub _save_info_txt {
|
---|
155 | my $self = shift (@_);
|
---|
156 | my ($filename) = @_;
|
---|
157 |
|
---|
158 | my ($OID, $info);
|
---|
159 |
|
---|
160 | open (OUTFILE, ">$filename") ||
|
---|
161 | die "arcinfo::save_info couldn't write $filename\n";
|
---|
162 |
|
---|
163 | foreach $info (@{$self->get_OID_list()}) {
|
---|
164 | if (defined $info) {
|
---|
165 | print OUTFILE join("\t", @$info), "\n";
|
---|
166 | }
|
---|
167 | }
|
---|
168 | close (OUTFILE);
|
---|
169 | }
|
---|
170 |
|
---|
171 | sub _save_info_gdbm {
|
---|
172 | my $self = shift (@_);
|
---|
173 | my ($filename) = @_;
|
---|
174 |
|
---|
175 | # Not the most efficient operation, but will do for now
|
---|
176 |
|
---|
177 | # read it in
|
---|
178 | my $infodb_map = {};
|
---|
179 | &dbutil::read_infodb_file_gdbm($filename,$infodb_map);
|
---|
180 |
|
---|
181 | # change index-status values
|
---|
182 | foreach my $info (@{$self->get_OID_list()}) {
|
---|
183 | if (defined $info) {
|
---|
184 | my ($oid,$doc_file,$index_status) = @$info;
|
---|
185 | if (defined $infodb_map->{$oid}) {
|
---|
186 | my $vals_ref = \$infodb_map->{$oid};
|
---|
187 | $$vals_ref =~ s/^<index-status>(.*)$/<index-status>$index_status/m;
|
---|
188 | }
|
---|
189 | else {
|
---|
190 | print STDERR "Warning: $filename does not have key $oid\n";
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | # write out again
|
---|
197 | my $infodb_handle = &dbutil::open_infodb_write_handle_gdbm($filename);
|
---|
198 | foreach my $oid ( keys %$infodb_map ) {
|
---|
199 | # consider making the following a method in dbutil
|
---|
200 | # e.g. write_infodb_rawentry_gdbm($infodb_handle,$oid,$vals);
|
---|
201 |
|
---|
202 | # no need to escape, as $infodb_map->{$oid} hasn't been unescaped
|
---|
203 | print $infodb_handle "[$oid]\n";
|
---|
204 | print $infodb_handle $infodb_map->{$oid};
|
---|
205 | print $infodb_handle '-' x 70, "\n";
|
---|
206 | }
|
---|
207 | &dbutil::close_infodb_write_handle_gdbm($infodb_handle);
|
---|
208 |
|
---|
209 | }
|
---|
210 |
|
---|
211 | sub save_info {
|
---|
212 | my $self = shift (@_);
|
---|
213 | my ($filename) = @_;
|
---|
214 |
|
---|
215 | if ($filename =~ m/\.inf$/) {
|
---|
216 | $self->_save_info_txt($filename);
|
---|
217 | }
|
---|
218 | else {
|
---|
219 | $self->_save_info_gdbm($filename);
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | sub delete_info {
|
---|
224 | my $self = shift (@_);
|
---|
225 | my ($OID) = @_;
|
---|
226 |
|
---|
227 | if (defined $self->{'info'}->{$OID}) {
|
---|
228 | delete $self->{'info'}->{$OID};
|
---|
229 |
|
---|
230 | my $i = 0;
|
---|
231 | while ($i < scalar (@{$self->{'order'}})) {
|
---|
232 | if ($self->{'order'}->[$i]->[ORDER_OID_INDEX] eq $OID) {
|
---|
233 | splice (@{$self->{'order'}}, $i, 1);
|
---|
234 | last;
|
---|
235 | }
|
---|
236 |
|
---|
237 | $i ++;
|
---|
238 | }
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | sub add_info {
|
---|
243 | my $self = shift (@_);
|
---|
244 | my ($OID, $doc_file, $index_status, $sortmeta) = @_;
|
---|
245 | $sortmeta = "" unless defined $sortmeta;
|
---|
246 | $index_status = "I" unless defined $index_status; # I = needs indexing
|
---|
247 |
|
---|
248 | if (! defined($OID)) {
|
---|
249 | # only happens when no files can be processed?
|
---|
250 | return undef;
|
---|
251 | }
|
---|
252 |
|
---|
253 | if (defined $self->{'info'}->{$OID}) {
|
---|
254 | # test to see if we are in a reindex situation
|
---|
255 |
|
---|
256 | my $existing_status_info = $self->get_status_info($OID);
|
---|
257 |
|
---|
258 | if ($existing_status_info eq "D") {
|
---|
259 | # yes, we're in a reindexing situation
|
---|
260 | $self->delete_info ($OID);
|
---|
261 |
|
---|
262 |
|
---|
263 | # force setting to "reindex"
|
---|
264 | $index_status = "R";
|
---|
265 |
|
---|
266 | }
|
---|
267 | else {
|
---|
268 | # some other, possibly erroneous, situation has arisen
|
---|
269 | # where the document already seems to exist
|
---|
270 | print STDERR "Warning: $OID already exists with index status $existing_status_info\n";
|
---|
271 | print STDERR " Deleting previous version\n";
|
---|
272 |
|
---|
273 | $self->delete_info ($OID);
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | $self->{'info'}->{$OID} = [$doc_file,$index_status];
|
---|
278 | push (@{$self->{'order'}}, [$OID, $sortmeta]);
|
---|
279 |
|
---|
280 |
|
---|
281 | }
|
---|
282 |
|
---|
283 | sub set_status_info {
|
---|
284 | my $self = shift (@_);
|
---|
285 | my ($OID, $index_status) = @_;
|
---|
286 |
|
---|
287 | my $OID_info = $self->{'info'}->{$OID};
|
---|
288 | $OID_info->[INFO_STATUS_INDEX] = $index_status;
|
---|
289 | }
|
---|
290 |
|
---|
291 |
|
---|
292 | sub get_status_info {
|
---|
293 | my $self = shift (@_);
|
---|
294 | my ($OID) = @_;
|
---|
295 |
|
---|
296 | my $index_status = undef;
|
---|
297 |
|
---|
298 | my $OID_info = $self->{'info'}->{$OID};
|
---|
299 | if (defined $OID_info) {
|
---|
300 | $index_status = $OID_info->[INFO_STATUS_INDEX];
|
---|
301 | }
|
---|
302 | else {
|
---|
303 | die "Unable to find document id $OID\n";
|
---|
304 | }
|
---|
305 |
|
---|
306 | return $index_status;
|
---|
307 |
|
---|
308 | }
|
---|
309 |
|
---|
310 | sub reverse_sort
|
---|
311 | {
|
---|
312 | my $self = shift(@_);
|
---|
313 | $self->{'reverse_sort'} = 1;
|
---|
314 | }
|
---|
315 |
|
---|
316 | # returns a list of the form [[OID, doc_file, index_status], ...]
|
---|
317 | sub get_OID_list
|
---|
318 | {
|
---|
319 | my $self = shift (@_);
|
---|
320 |
|
---|
321 | my $order = $self->{'order'};
|
---|
322 |
|
---|
323 | my @sorted_order;
|
---|
324 | if ($self->{'reverse_sort'}) {
|
---|
325 | @sorted_order = sort {$b->[ORDER_SORT_INDEX] cmp $a->[ORDER_SORT_INDEX]} @$order;
|
---|
326 | } else {
|
---|
327 | @sorted_order = sort {$a->[ORDER_SORT_INDEX] cmp $b->[ORDER_SORT_INDEX]} @$order;
|
---|
328 | }
|
---|
329 |
|
---|
330 | my @list = ();
|
---|
331 |
|
---|
332 | foreach my $OID_order (@sorted_order) {
|
---|
333 | my $OID = $OID_order->[ORDER_OID_INDEX];
|
---|
334 | my $OID_info = $self->{'info'}->{$OID};
|
---|
335 |
|
---|
336 | push (@list, [$OID, $OID_info->[INFO_FILE_INDEX],
|
---|
337 | $OID_info->[INFO_STATUS_INDEX]]);
|
---|
338 | }
|
---|
339 |
|
---|
340 | return \@list;
|
---|
341 | }
|
---|
342 |
|
---|
343 | # returns a list of the form [[doc_file, OID], ...]
|
---|
344 | sub get_file_list {
|
---|
345 | my $self = shift (@_);
|
---|
346 |
|
---|
347 | my $order = $self->{'order'};
|
---|
348 |
|
---|
349 | my @sorted_order;
|
---|
350 | if ($self->{'reverse_sort'}) {
|
---|
351 | @sorted_order = sort {$b->[ORDER_SORT_INDEX] cmp $a->[ORDER_SORT_INDEX]} @$order;
|
---|
352 | } else {
|
---|
353 | @sorted_order = sort {$a->[ORDER_SORT_INDEX] cmp $b->[ORDER_SORT_INDEX]} @$order;
|
---|
354 | }
|
---|
355 |
|
---|
356 | my @list = ();
|
---|
357 |
|
---|
358 | foreach my $OID_order (@sorted_order) {
|
---|
359 | my $OID = $OID_order->[ORDER_OID_INDEX];
|
---|
360 | my $OID_info = $self->{'info'}->{$OID};
|
---|
361 |
|
---|
362 | push (@list, [$OID_info->[INFO_FILE_INDEX], $OID]);
|
---|
363 | }
|
---|
364 |
|
---|
365 | return \@list;
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 | # returns a list of the form [doc_file]
|
---|
370 | sub get_info {
|
---|
371 | my $self = shift (@_);
|
---|
372 | my ($OID) = @_;
|
---|
373 |
|
---|
374 | if (defined $self->{'info'}->{$OID}) {
|
---|
375 | return $self->{'info'}->{$OID};
|
---|
376 | }
|
---|
377 |
|
---|
378 | return undef;
|
---|
379 | }
|
---|
380 |
|
---|
381 |
|
---|
382 | # returns the number of documents so far
|
---|
383 | sub size {
|
---|
384 | my $self = shift (@_);
|
---|
385 | return (scalar(@{$self->{'order'}}));
|
---|
386 | }
|
---|
387 |
|
---|
388 | 1;
|
---|
389 |
|
---|