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

Last change on this file since 817 was 810, checked in by sjboddie, 25 years ago

plugins now take options, files are associated at build time as
well as import time

  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 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) = @_;
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
56sub setarchivedir {
57 my $self = shift (@_);
58 my ($archive_dir) = @_;
59
60 $self->{'archive_dir'} = $archive_dir;
61}
62
63sub 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 my ($dir, $afile) = $assoc_file->[1] =~ /^(.*?)([^\/\\]+)$/;
103 $dir = "" unless defined $dir;
104 if (-e $assoc_file->[0]) {
105 my $filepath = &util::filename_cat($archive_dir, $doc_dir, $afile);
106 &util::hard_link ($assoc_file->[0], $filepath);
107 $doc_obj->add_metadata ($doc_obj->get_top_section(),
108 "gsdlassocfile",
109 "$afile:$assoc_file->[2]:$dir");
110 } else {
111 print STDERR "docsave::process couldn't copy the associated file " .
112 "$assoc_file->[0] to $afile\n";
113 }
114 }
115
116 # save this document
117 my $doc_file = &util::filename_cat ($archive_dir, $doc_dir, "doc.gml");
118 my $short_doc_file = &util::filename_cat ($doc_dir, "doc.gml");
119
120 if (!open (OUTDOC, ">$doc_file")) {
121 print STDERR "docsave::process could not write to file $docfile\n";
122 return;
123 }
124 $doc_obj->output_section('docsave::OUTDOC', $doc_obj->get_top_section());
125 close OUTDOC;
126
127 if ($self->{'gzip'}) {
128 `gzip $doc_file`;
129 $doc_file .= ".gz";
130 $short_doc_file .= ".gz";
131 if (!-e $doc_file) {
132 print STDERR "error while gzipping: $doc_file doesn't exist\n";
133 return;
134 }
135 }
136
137 # store reference in the archive_info
138 $self->{'archive_info'}->add_info($OID, $short_doc_file);
139
140}
141
142
1431;
Note: See TracBrowser for help on using the repository browser.