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