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

Last change on this file since 834 was 834, checked in by davidb, 24 years ago

'groupsize' added

  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 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,$gzip,$groupsize) = @_;
43 my $self = new docproc ();
44
45 $self->{'collection'} = $collection;
46 $self->{'archive_info'} = $archive_info;
47 $self->{'verbosity'} = $verbosity;
48 $self->{'gzip'} = $gzip;
49 $self->{'groupsize'} = $groupsize;
50 $self->{'gs_count'} = 0;
51
52 # set a default for the archive directory
53 $self->{'archive_dir'} = "$ENV{'GSDLHOME'}/collect/$self->{'collection'}/archives";
54
55 return bless $self, $class;
56}
57
58sub setarchivedir {
59 my $self = shift (@_);
60 my ($archive_dir) = @_;
61
62 $self->{'archive_dir'} = $archive_dir;
63}
64
65sub process {
66 my $self = shift (@_);
67 my ($doc_obj) = @_;
68
69 my $archive_dir = $self->{'archive_dir'};
70 my $OID = $doc_obj->get_OID();
71 $OID = "NULL" unless defined $OID;
72
73 my $groupsize = $self->{'groupsize'};
74 my $gs_count = $self->{'gs_count'};
75 my $open_new_file = (($gs_count % $groupsize)==0);
76
77 # opening a new file, or document has assoicated files => directory needed
78 if (($open_new_file) || (scalar(@{$doc_obj->get_assoc_files()})>0))
79 {
80 # get the document's directory.
81 my $doc_info = $self->{'archive_info'}->get_info($OID);
82 my $doc_dir = "";
83 if (defined $doc_info && scalar(@$doc_info) >= 1) {
84 # this OID already has an assigned directory, use the
85 # same one.
86 $doc_dir = $doc_info->[0];
87 $doc_dir =~ s/\/?doc\.gml(\.gz)?$//;
88 } else {
89 # have to get a new document directory
90 my $doc_dir_rest = $OID;
91 my $doc_dir_num = 0;
92 do {
93 $doc_dir .= "/" if $doc_dir_num > 0;
94 if ($doc_dir_rest =~ s/^(.{1,8})//) {
95 $doc_dir .= $1;
96 $doc_dir_num++;
97 }
98 } while ($doc_dir_rest ne "" &&
99 ((-d &util::filename_cat ($archive_dir, "$doc_dir.dir")) ||
100 ($self->{'archive_info'}->size() >= 1024 && $doc_dir_num < 2)));
101 $doc_dir .= ".dir";
102
103 }
104
105 &util::mk_all_dir ("$archive_dir/$doc_dir");
106
107 # copy all the associated files, add this information as metadata
108 # to the document
109 my @assoc_files = ();
110 foreach $assoc_file (@{$doc_obj->get_assoc_files()}) {
111 my ($dir, $afile) = $assoc_file->[1] =~ /^(.*?)([^\/\\]+)$/;
112 $dir = "" unless defined $dir;
113 if (-e $assoc_file->[0]) {
114 my $filepath = &util::filename_cat($archive_dir, $doc_dir, $afile);
115 &util::hard_link ($assoc_file->[0], $filepath);
116 $doc_obj->add_metadata ($doc_obj->get_top_section(),
117 "gsdlassocfile",
118 "$afile:$assoc_file->[2]:$dir");
119 } else {
120 print STDERR "docsave::process couldn't copy the associated file " .
121 "$assoc_file->[0] to $afile\n";
122 }
123 }
124
125 if ($open_new_file)
126 {
127 # only if opening new file
128 my $doc_file
129 = &util::filename_cat ($archive_dir, $doc_dir, "doc.gml");
130 my $short_doc_file = &util::filename_cat ($doc_dir, "doc.gml");
131
132 if ($gs_count>0)
133 {
134 return if (!$self->close_file_output());
135 }
136
137 if (!open (OUTDOC, ">$doc_file")) {
138 print STDERR "docsave::process could not write to file $docfile\n";
139 return;
140 }
141 $self->{'gs_filename'} = $doc_file;
142 $self->{'gs_short_filename'} = $short_doc_file;
143 $self->{'gs_OID'} = $OID;
144 }
145 }
146
147 # save this document
148 $doc_obj->output_section('docsave::OUTDOC', $doc_obj->get_top_section());
149
150 $self->{'gs_count'}++;
151}
152
153sub close_file_output
154{
155 my ($self) = @_;
156
157 close OUTDOC;
158
159 my $OID = $self->{'gs_OID'};
160 my $short_doc_file = $self->{'gs_short_filename'};
161
162 if ($self->{'gzip'}) {
163 my $doc_file = $self->{'gs_filename'};
164 `gzip $doc_file`;
165 $doc_file .= ".gz";
166 $short_doc_file .= ".gz";
167 if (!-e $doc_file) {
168 print STDERR "error while gzipping: $doc_file doesn't exist\n";
169 return 0;
170 }
171 }
172
173 # store reference in the archive_info
174 $self->{'archive_info'}->add_info($OID, $short_doc_file);
175
176 return 1;
177}
178
1791;
Note: See TracBrowser for help on using the repository browser.