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