source: trunk/gsdl/perllib/docsave.pm@ 2048

Last change on this file since 2048 was 2048, checked in by sjboddie, 23 years ago

* empty log message *

  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1###########################################################################
2#
3# docsave.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# This document processor saves a document in the
27# archives directory of a collection
28
29
30package docsave;
31
32use arcinfo;
33use docproc;
34use util;
35
36
37sub BEGIN {
38 @ISA = ('docproc');
39}
40
41sub new {
42 my ($class, $collection, $archive_info, $verbosity,
43 $gzip, $groupsize, $outhandle) = @_;
44 my $self = new docproc ();
45
46
47 $groupsize=1 unless defined $groupsize;
48 $self->{'collection'} = $collection;
49 $self->{'archive_info'} = $archive_info;
50 $self->{'verbosity'} = $verbosity;
51 $self->{'gzip'} = $gzip;
52
53 $self->{'groupsize'} = $groupsize;
54 $self->{'gs_count'} = 0;
55
56 $self->{'outhandle'} = STDERR;
57 $self->{'outhandle'} = $outhandle if defined $outhandle;
58
59 # set a default for the archive directory
60 $self->{'archive_dir'} = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives");
61
62 $self->{'sortmeta'} = undef;
63
64 return bless $self, $class;
65}
66
67sub setarchivedir {
68 my $self = shift (@_);
69 my ($archive_dir) = @_;
70
71 &util::mk_all_dir ($archive_dir) unless -e $archive_dir;
72 $self->{'archive_dir'} = $archive_dir;
73}
74
75sub set_sortmeta {
76 my $self = shift (@_);
77 my ($sortmeta) = @_;
78
79 $self->{'sortmeta'} = $sortmeta;
80}
81
82sub process {
83 my $self = shift (@_);
84 my ($doc_obj) = @_;
85
86 my $outhandle = $self->{'outhandle'};
87
88 if ($self->{'groupsize'} > 1) {
89 $self->group_process ($doc_obj);
90
91 } else {
92 # groupsize is 1 (i.e. one document per GML file) so sortmeta
93 # may be used
94
95 my $OID = $doc_obj->get_OID();
96 $OID = "NULL" unless defined $OID;
97
98 # get document's directory
99 my $doc_dir = $self->get_doc_dir ($OID);
100
101 # copy all the associated files, add this information as metadata
102 # to the document
103 $self->process_assoc_files ($doc_obj, $doc_dir);
104
105 my $doc_file
106 = &util::filename_cat ($self->{'archive_dir'}, $doc_dir, "doc.gml");
107 my $short_doc_file = &util::filename_cat ($doc_dir, "doc.gml");
108
109 if (!open (OUTDOC, ">$doc_file")) {
110 print $outhandle "docsave::process could not write to file $doc_file\n";
111 return;
112 }
113
114 # save this document
115 $doc_obj->output_section('docsave::OUTDOC', $doc_obj->get_top_section());
116 close OUTDOC;
117
118 if ($self->{'gzip'}) {
119 my $doc_file = $self->{'gs_filename'};
120 `gzip $doc_file`;
121 $doc_file .= ".gz";
122 $short_doc_file .= ".gz";
123 if (!-e $doc_file) {
124 print $outhandle "error while gzipping: $doc_file doesn't exist\n";
125 return 0;
126 }
127 }
128
129 # do the sortmeta thing
130 my ($metadata);
131 if (defined ($self->{'sortmeta'})) {
132 $metadata = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), $self->{'sortmeta'});
133 }
134
135 # store reference in the archive_info
136 $self->{'archive_info'}->add_info($OID, $short_doc_file, $metadata);
137 }
138}
139
140sub group_process {
141 my $self = shift (@_);
142 my ($doc_obj) = @_;
143
144 my $outhandle = $self->{'outhandle'};
145
146 my $OID = $doc_obj->get_OID();
147 $OID = "NULL" unless defined $OID;
148
149 my $groupsize = $self->{'groupsize'};
150 my $gs_count = $self->{'gs_count'};
151 my $open_new_file = (($gs_count % $groupsize)==0);
152
153 # opening a new file, or document has assoicated files => directory needed
154 if (($open_new_file) || (scalar(@{$doc_obj->get_assoc_files()})>0)) {
155
156 # get document's directory
157 my $doc_dir = $self->get_doc_dir ($OID);
158
159 # copy all the associated files, add this information as metadata
160 # to the document
161 $self->process_assoc_files ($doc_obj, $doc_dir);
162
163
164 if ($open_new_file) {
165 # only if opening new file
166 my $doc_file
167 = &util::filename_cat ($self->{'archive_dir'}, $doc_dir, "doc.gml");
168 my $short_doc_file = &util::filename_cat ($doc_dir, "doc.gml");
169
170 if ($gs_count>0)
171 {
172 return if (!$self->close_file_output());
173 }
174
175 if (!open (OUTDOC, ">$doc_file")) {
176 print $outhandle "docsave::group_process could not write to file $doc_file\n";
177 return;
178 }
179 $self->{'gs_filename'} = $doc_file;
180 $self->{'gs_short_filename'} = $short_doc_file;
181 $self->{'gs_OID'} = $OID;
182 }
183 }
184
185 # save this document
186 $doc_obj->output_section('docsave::OUTDOC', $doc_obj->get_top_section());
187
188 $self->{'gs_count'}++;
189}
190
191
192sub get_doc_dir {
193 my $self = shift (@_);
194 my ($OID) = @_;
195
196 my $doc_info = $self->{'archive_info'}->get_info($OID);
197 my $doc_dir = "";
198 if (defined $doc_info && scalar(@$doc_info) >= 1) {
199 # this OID already has an assigned directory, use the
200 # same one.
201 $doc_dir = $doc_info->[0];
202 $doc_dir =~ s/\/?doc\.gml(\.gz)?$//;
203 } else {
204 # have to get a new document directory
205 my $doc_dir_rest = $OID;
206 my $doc_dir_num = 0;
207 do {
208 $doc_dir .= "/" if $doc_dir_num > 0;
209 if ($doc_dir_rest =~ s/^(.{1,8})//) {
210 $doc_dir .= $1;
211 $doc_dir_num++;
212 }
213 } while ($doc_dir_rest ne "" &&
214 ((-d &util::filename_cat ($self->{'archive_dir'}, "$doc_dir.dir")) ||
215 ($self->{'archive_info'}->size() >= 1024 && $doc_dir_num < 2)));
216 $doc_dir .= ".dir";
217
218 }
219
220 &util::mk_all_dir (&util::filename_cat ($self->{'archive_dir'}, $doc_dir));
221
222 return $doc_dir;
223}
224
225
226sub process_assoc_files {
227 my $self = shift (@_);
228 my ($doc_obj, $doc_dir) = @_;
229
230 my $outhandle = $self->{'outhandle'};
231
232 my @assoc_files = ();
233 foreach $assoc_file (@{$doc_obj->get_assoc_files()}) {
234 my ($dir, $afile) = $assoc_file->[1] =~ /^(.*?)([^\/\\]+)$/;
235 $dir = "" unless defined $dir;
236 if (-e $assoc_file->[0]) {
237 my $filepath = &util::filename_cat($self->{'archive_dir'}, $doc_dir, $afile);
238 &util::hard_link ($assoc_file->[0], $filepath);
239 $doc_obj->add_utf8_metadata ($doc_obj->get_top_section(),
240 "gsdlassocfile",
241 "$afile:$assoc_file->[2]:$dir");
242 } elsif ($self->{'verbosity'} > 2) {
243 print $outhandle "docsave::process couldn't copy the associated file " .
244 "$assoc_file->[0] to $afile\n";
245 }
246 }
247}
248
249
250sub close_file_output
251{
252 my ($self) = @_;
253
254 close OUTDOC;
255
256 my $OID = $self->{'gs_OID'};
257 my $short_doc_file = $self->{'gs_short_filename'};
258
259 if ($self->{'gzip'}) {
260 my $doc_file = $self->{'gs_filename'};
261 `gzip $doc_file`;
262 $doc_file .= ".gz";
263 $short_doc_file .= ".gz";
264 if (!-e $doc_file) {
265 my $outhandle = $self->{'outhandle'};
266 print $outhandle "error while gzipping: $doc_file doesn't exist\n";
267 return 0;
268 }
269 }
270
271 # store reference in the archive_info
272 $self->{'archive_info'}->add_info($OID, $short_doc_file);
273
274 return 1;
275}
276
2771;
Note: See TracBrowser for help on using the repository browser.