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) = @_;
|
---|
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 |
|
---|
50 | # set a default for the archive directory
|
---|
51 | $self->{'archive_dir'} = "$ENV{'GSDLHOME'}/collect/$self->{'collection'}/archives";
|
---|
52 |
|
---|
53 | return bless $self, $class;
|
---|
54 | }
|
---|
55 |
|
---|
56 | sub setarchivedir {
|
---|
57 | my $self = shift (@_);
|
---|
58 | my ($archive_dir) = @_;
|
---|
59 |
|
---|
60 | $self->{'archive_dir'} = $archive_dir;
|
---|
61 | }
|
---|
62 |
|
---|
63 | sub process {
|
---|
64 | my $self = shift (@_);
|
---|
65 | my ($doc_obj) = @_;
|
---|
66 |
|
---|
67 | my $archive_dir = $self->{'archive_dir'};
|
---|
68 | my $OID = $doc_obj->get_OID();
|
---|
69 | $OID = "NULL" unless defined $OID;
|
---|
70 |
|
---|
71 | # get the document's directory.
|
---|
72 | my $doc_info = $self->{'archive_info'}->get_info($OID);
|
---|
73 | my $doc_dir = "";
|
---|
74 | if (defined $doc_info && scalar(@$doc_info) >= 1) {
|
---|
75 | # this OID already has an assigned directory, use the
|
---|
76 | # same one.
|
---|
77 | $doc_dir = $doc_info->[0];
|
---|
78 | $doc_dir =~ s/\/?doc\.gml(\.gz)?$//;
|
---|
79 |
|
---|
80 | } else {
|
---|
81 | # have to get a new document directory
|
---|
82 | my $doc_dir_rest = $OID;
|
---|
83 | my $doc_dir_num = 0;
|
---|
84 | do {
|
---|
85 | $doc_dir .= "/" if $doc_dir_num > 0;
|
---|
86 | if ($doc_dir_rest =~ s/^(.{1,8})//) {
|
---|
87 | $doc_dir .= $1;
|
---|
88 | $doc_dir_num++;
|
---|
89 | }
|
---|
90 | } while ($doc_dir_rest ne "" &&
|
---|
91 | ((-d &util::filename_cat ($archive_dir, "$doc_dir.dir")) ||
|
---|
92 | ($self->{'archive_info'}->size() >= 1024 && $doc_dir_num < 2)));
|
---|
93 | $doc_dir .= ".dir";
|
---|
94 | }
|
---|
95 |
|
---|
96 | &util::mk_all_dir ("$archive_dir/$doc_dir");
|
---|
97 |
|
---|
98 | # copy all the associated files, add this information as metadata
|
---|
99 | # to the document
|
---|
100 | my @assoc_files = ();
|
---|
101 | foreach $assoc_file (@{$doc_obj->get_assoc_files()}) {
|
---|
102 | if (-e $assoc_file->[0]) {
|
---|
103 | my $afile = &util::filename_cat($archive_dir, $doc_dir, $assoc_file->[1]);
|
---|
104 | &util::cp ($assoc_file->[0], $afile);
|
---|
105 | $doc_obj->add_metadata ($doc_obj->get_top_section(),
|
---|
106 | "gsdlassocfile",
|
---|
107 | "$assoc_file->[1]:$assoc_file->[2]");
|
---|
108 | } else {
|
---|
109 | print STDERR "docsave::process couldn't copy the associated file " .
|
---|
110 | "$assoc_file->[0] to $afile\n";
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | # save this document
|
---|
115 | my $doc_file = &util::filename_cat ($archive_dir, $doc_dir, "doc.gml");
|
---|
116 | my $short_doc_file = &util::filename_cat ($doc_dir, "doc.gml");
|
---|
117 |
|
---|
118 | if (!open (OUTDOC, ">$doc_file")) {
|
---|
119 | print STDERR "docsave::process could not write to file $docfile\n";
|
---|
120 | return;
|
---|
121 | }
|
---|
122 | $doc_obj->output_section('docsave::OUTDOC', $doc_obj->get_top_section());
|
---|
123 | close OUTDOC;
|
---|
124 |
|
---|
125 | if ($self->{'gzip'}) {
|
---|
126 | `gzip $doc_file`;
|
---|
127 | $doc_file .= ".gz";
|
---|
128 | $short_doc_file .= ".gz";
|
---|
129 | if (!-e $doc_file) {
|
---|
130 | print STDERR "error while gzipping: $doc_file doesn't exist\n";
|
---|
131 | return;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | # store reference in the archive_info
|
---|
136 | $self->{'archive_info'}->add_info($OID, $short_doc_file);
|
---|
137 |
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | 1;
|
---|