[537] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # doc.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 | #
|
---|
[8894] | 10 | # This program is free software; you can redistr te it and/or modify
|
---|
[537] | 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 |
|
---|
[1241] | 26 | # base class to hold documents
|
---|
[4] | 27 |
|
---|
| 28 | package doc;
|
---|
[3834] | 29 | eval {require bytes};
|
---|
[4] | 30 |
|
---|
[832] | 31 | BEGIN {
|
---|
[1241] | 32 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
| 33 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib/dynamic/lib/site_perl/5.005/i686-linux");
|
---|
[832] | 34 | }
|
---|
| 35 |
|
---|
[15894] | 36 | use strict;
|
---|
[1241] | 37 | use unicode;
|
---|
| 38 | use util;
|
---|
| 39 | use ghtml;
|
---|
[8220] | 40 | use File::stat;
|
---|
[1241] | 41 | ##use hashdoc;
|
---|
[13172] | 42 | use docprint;
|
---|
[1241] | 43 |
|
---|
[4] | 44 | # the document type may be indexed_doc, nonindexed_doc, or
|
---|
| 45 | # classification
|
---|
| 46 |
|
---|
[18528] | 47 | our $OIDcount = 0;
|
---|
[2327] | 48 |
|
---|
[4] | 49 | sub new {
|
---|
| 50 | my $class = shift (@_);
|
---|
[18319] | 51 | my ($source_filename, $doc_type, $rename_method) = @_;
|
---|
[1374] | 52 |
|
---|
[13770] | 53 |
|
---|
[1241] | 54 | my $self = bless {'associated_files'=>[],
|
---|
| 55 | 'subsection_order'=>[],
|
---|
| 56 | 'next_subsection'=>1,
|
---|
| 57 | 'subsections'=>{},
|
---|
| 58 | 'metadata'=>[],
|
---|
[2327] | 59 | 'text'=>"",
|
---|
| 60 | 'OIDtype'=>"hash"}, $class;
|
---|
[4] | 61 |
|
---|
[10217] | 62 | # used to set lastmodified here, but this can screw up the HASH ids, so
|
---|
| 63 | # the docsave processor now calls set_lastmodified
|
---|
[13770] | 64 |
|
---|
| 65 | $self->{'source_path'} = $source_filename;
|
---|
[10217] | 66 |
|
---|
[7929] | 67 | if (defined $source_filename) {
|
---|
[15874] | 68 | $source_filename = &util::filename_within_collection($source_filename);
|
---|
[18508] | 69 | print STDERR "****** doc.pm::new(): no file rename method provided\n" unless $rename_method;
|
---|
[18319] | 70 | $self->set_source_filename ($source_filename, $rename_method);
|
---|
[7929] | 71 | }
|
---|
| 72 |
|
---|
[1374] | 73 | $self->set_doc_type ($doc_type) if defined $doc_type;
|
---|
[485] | 74 |
|
---|
[4] | 75 | return $self;
|
---|
| 76 | }
|
---|
[10217] | 77 | # set lastmodified for OAI purposes, added by GRB, moved by kjdon
|
---|
| 78 | sub set_lastmodified {
|
---|
| 79 | my $self = shift (@_);
|
---|
[4] | 80 |
|
---|
[13770] | 81 | my $source_path = $self->{'source_path'};
|
---|
| 82 |
|
---|
| 83 | if (defined $source_path && (-e $source_path)) {
|
---|
| 84 |
|
---|
| 85 | my $file_stat = stat($source_path);
|
---|
[10217] | 86 | my $mtime = $file_stat->mtime;
|
---|
| 87 | $self->add_utf8_metadata($self->get_top_section(), "lastmodified", $file_stat->mtime);
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[1241] | 91 | # clone the $self object
|
---|
| 92 | sub duplicate {
|
---|
| 93 | my $self = shift (@_);
|
---|
| 94 |
|
---|
| 95 | my $newobj = {};
|
---|
| 96 |
|
---|
[8716] | 97 | foreach my $k (keys %$self) {
|
---|
[1241] | 98 | $newobj->{$k} = &clone ($self->{$k});
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | bless $newobj, ref($self);
|
---|
| 102 | return $newobj;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | sub clone {
|
---|
| 106 | my ($from) = @_;
|
---|
| 107 | my $type = ref ($from);
|
---|
| 108 |
|
---|
| 109 | if ($type eq "HASH") {
|
---|
| 110 | my $to = {};
|
---|
[8716] | 111 | foreach my $key (keys %$from) {
|
---|
[1241] | 112 | $to->{$key} = &clone ($from->{$key});
|
---|
| 113 | }
|
---|
| 114 | return $to;
|
---|
| 115 | } elsif ($type eq "ARRAY") {
|
---|
| 116 | my $to = [];
|
---|
[8716] | 117 | foreach my $v (@$from) {
|
---|
[1241] | 118 | push (@$to, &clone ($v));
|
---|
| 119 | }
|
---|
| 120 | return $to;
|
---|
| 121 | } else {
|
---|
| 122 | return $from;
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[2327] | 126 | sub set_OIDtype {
|
---|
| 127 | my $self = shift (@_);
|
---|
[12268] | 128 | my ($type, $metadata) = @_;
|
---|
[1241] | 129 |
|
---|
[17025] | 130 | if (defined $type && $type =~ /^(hash|hash_on_file|hash_on_ga_xml|incremental|dirname|assigned)$/) {
|
---|
[2327] | 131 | $self->{'OIDtype'} = $type;
|
---|
| 132 | } else {
|
---|
| 133 | $self->{'OIDtype'} = "hash";
|
---|
| 134 | }
|
---|
[16792] | 135 |
|
---|
[12268] | 136 | if ($type =~ /^assigned$/) {
|
---|
| 137 | if (defined $metadata) {
|
---|
| 138 | $self->{'OIDmetadata'} = $metadata;
|
---|
| 139 | } else {
|
---|
| 140 | $self->{'OIDmetadata'} = "dc.Identifier";
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
[2327] | 143 | }
|
---|
| 144 |
|
---|
[1241] | 145 | sub set_source_filename {
|
---|
| 146 | my $self = shift (@_);
|
---|
[18319] | 147 | my ($source_filename, $rename_method) = @_;
|
---|
[1241] | 148 |
|
---|
[16578] | 149 | # Since the gsdlsourcefilename element goes into the doc.xml it has
|
---|
| 150 | # to be utf8. However, it should also *represent* the source filename
|
---|
| 151 | # (in the import directory) which may not be utf8 at all.
|
---|
[18319] | 152 | # For instance, if this meta element (gsdlsourcefilename) will be used
|
---|
| 153 | # by other applications that parse doc.xml in order to locate
|
---|
| 154 | # gsdlsourcefilename. Therefore, the solution is to URLencode or base64
|
---|
| 155 | # encode the real filename as this is a binary-to-text encoding meaning
|
---|
| 156 | # that the resulting string is ASCII (utf8). Decoding will give the original.
|
---|
[16578] | 157 |
|
---|
[18319] | 158 | # print STDERR "******URL/base64 encoding the gsdl_source_filename $source_filename ";
|
---|
| 159 |
|
---|
[16670] | 160 | # URLencode just the gsdl_source_filename, not the directory. Then prepend dir
|
---|
| 161 | my ($srcfilename,$dirname,$suffix)
|
---|
| 162 | = &File::Basename::fileparse($source_filename, "\\.[^\\.]+\$");
|
---|
[16792] | 163 | # print STDERR "-> $srcfilename -> ";
|
---|
[18319] | 164 | $srcfilename = &util::rename_file($srcfilename.$suffix, $rename_method);
|
---|
[16670] | 165 | $source_filename = &util::filename_cat($dirname, $srcfilename);
|
---|
[16792] | 166 | # print STDERR "$source_filename\n";
|
---|
[16670] | 167 |
|
---|
[16578] | 168 | $self->set_utf8_metadata_element ($self->get_top_section(),
|
---|
[1241] | 169 | "gsdlsourcefilename",
|
---|
| 170 | $source_filename);
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[7569] | 173 | sub set_converted_filename {
|
---|
| 174 | my $self = shift (@_);
|
---|
| 175 | my ($converted_filename) = @_;
|
---|
| 176 |
|
---|
[16578] | 177 | # we know the converted filename is utf8
|
---|
| 178 | $self->set_utf8_metadata_element ($self->get_top_section(),
|
---|
[7569] | 179 | "gsdlconvertedfilename",
|
---|
| 180 | $converted_filename);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[1241] | 183 | # returns the source_filename as it was provided
|
---|
| 184 | sub get_source_filename {
|
---|
| 185 | my $self = shift (@_);
|
---|
| 186 |
|
---|
| 187 | return $self->get_metadata_element ($self->get_top_section(), "gsdlsourcefilename");
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[7569] | 190 | # returns converted filename if available else returns source filename
|
---|
| 191 | sub get_filename_for_hashing {
|
---|
| 192 | my $self = shift (@_);
|
---|
| 193 |
|
---|
| 194 | my $filename = $self->get_metadata_element ($self->get_top_section(), "gsdlconvertedfilename");
|
---|
| 195 |
|
---|
| 196 | if (!defined $filename) {
|
---|
[11097] | 197 | my $plugin_name = $self->get_metadata_element ($self->get_top_section(), "Plugin");
|
---|
[10980] | 198 | # if NULPlug processed file, then don't give a filename
|
---|
[11097] | 199 | if (defined $plugin_name && $plugin_name eq "NULPlug") {
|
---|
[10980] | 200 | $filename = undef;
|
---|
[16670] | 201 | } else { # returns the URL encoded source filename!
|
---|
[10980] | 202 | $filename = $self->get_metadata_element ($self->get_top_section(), "gsdlsourcefilename");
|
---|
| 203 | }
|
---|
[7569] | 204 | }
|
---|
| 205 | return $filename;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[1241] | 208 | sub set_doc_type {
|
---|
| 209 | my $self = shift (@_);
|
---|
| 210 | my ($doc_type) = @_;
|
---|
| 211 |
|
---|
| 212 | $self->set_metadata_element ($self->get_top_section(),
|
---|
| 213 | "gsdldoctype",
|
---|
| 214 | $doc_type);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[10217] | 217 | # returns the gsdldoctype as it was provided
|
---|
[1241] | 218 | # the default of "indexed_doc" is used if no document
|
---|
| 219 | # type was provided
|
---|
| 220 | sub get_doc_type {
|
---|
| 221 | my $self = shift (@_);
|
---|
| 222 |
|
---|
| 223 | my $doc_type = $self->get_metadata_element ($self->get_top_section(), "gsdldoctype");
|
---|
| 224 | return $doc_type if (defined $doc_type);
|
---|
| 225 | return "indexed_doc";
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 |
|
---|
| 229 | # look up the reference to the a particular section
|
---|
| 230 | sub _lookup_section {
|
---|
| 231 | my $self = shift (@_);
|
---|
| 232 | my ($section) = @_;
|
---|
| 233 |
|
---|
| 234 | my ($num);
|
---|
| 235 | my $sectionref = $self;
|
---|
| 236 |
|
---|
| 237 | while (defined $section && $section ne "") {
|
---|
[12327] | 238 |
|
---|
[1241] | 239 | ($num, $section) = $section =~ /^\.?(\d+)(.*)$/;
|
---|
[12327] | 240 |
|
---|
| 241 | $num =~ s/^0+(\d)/$1/ if defined $num ; # remove leading 0s
|
---|
| 242 |
|
---|
[1241] | 243 | $section = "" unless defined $section;
|
---|
| 244 |
|
---|
[12327] | 245 |
|
---|
[1241] | 246 | if (defined $num && defined $sectionref->{'subsections'}->{$num}) {
|
---|
| 247 | $sectionref = $sectionref->{'subsections'}->{$num};
|
---|
| 248 | } else {
|
---|
| 249 | return undef;
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | return $sectionref;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[2327] | 256 | # calculate OID by hashing the contents of the document
|
---|
[1241] | 257 | sub _calc_OID {
|
---|
| 258 | my $self = shift (@_);
|
---|
| 259 | my ($filename) = @_;
|
---|
| 260 |
|
---|
| 261 | my $osexe = &util::get_os_exe();
|
---|
| 262 |
|
---|
| 263 | my $hashfile_exe = &util::filename_cat($ENV{'GSDLHOME'},"bin",
|
---|
| 264 | $ENV{'GSDLOS'},"hashfile$osexe");
|
---|
[8504] | 265 |
|
---|
[1241] | 266 | my $result = "NULL";
|
---|
[1679] | 267 |
|
---|
[1241] | 268 | if (-e "$hashfile_exe") {
|
---|
[1679] | 269 | # $result = `\"$hashfile_exe\" \"$filename\"`;
|
---|
| 270 | $result = `hashfile$osexe \"$filename\"`;
|
---|
[1241] | 271 | ($result) = $result =~ /:\s*([0-9a-f]+)/i;
|
---|
| 272 | } else {
|
---|
| 273 | print STDERR "doc::_calc_OID $hashfile_exe could not be found\n";
|
---|
| 274 | }
|
---|
| 275 | return "HASH$result";
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | # methods dealing with OID, not groups of them.
|
---|
| 279 |
|
---|
[2327] | 280 | # if $OID is not provided one is calculated
|
---|
[1241] | 281 | sub set_OID {
|
---|
| 282 | my $self = shift (@_);
|
---|
| 283 | my ($OID) = @_;
|
---|
[8504] | 284 |
|
---|
[8797] | 285 | my $use_hash_oid = 0;
|
---|
[17057] | 286 | # if an OID wasn't provided calculate one
|
---|
[1241] | 287 | if (!defined $OID) {
|
---|
| 288 | $OID = "NULL";
|
---|
[17025] | 289 | if ($self->{'OIDtype'} =~ /^hash/) {
|
---|
[8797] | 290 | $use_hash_oid = 1;
|
---|
| 291 | } elsif ($self->{'OIDtype'} eq "incremental") {
|
---|
[2327] | 292 | $OID = "D" . $OIDcount;
|
---|
| 293 | $OIDcount ++;
|
---|
[8716] | 294 |
|
---|
| 295 | } elsif ($self->{'OIDtype'} eq "dirname") {
|
---|
| 296 | $OID = 'J';
|
---|
| 297 | my $filename = $self->get_source_filename();
|
---|
[8797] | 298 | if (defined($filename)) { # && -e $filename) {
|
---|
[8716] | 299 | $OID = &File::Basename::dirname($filename);
|
---|
| 300 | if (defined $OID) {
|
---|
| 301 | $OID = 'J'.&File::Basename::basename($OID);
|
---|
[19617] | 302 | $OID = &util::tidy_up_oid($OID);
|
---|
[8716] | 303 | } else {
|
---|
[8797] | 304 | print STDERR "Failed to find base for filename ($filename)...generating hash id\n";
|
---|
| 305 | $use_hash_oid = 1;
|
---|
[8716] | 306 | }
|
---|
| 307 | } else {
|
---|
[8797] | 308 | print STDERR "Failed to find filename, generating hash id\n";
|
---|
| 309 | $use_hash_oid = 1;
|
---|
[8716] | 310 | }
|
---|
[2327] | 311 |
|
---|
[8797] | 312 | } elsif ($self->{'OIDtype'} eq "assigned") {
|
---|
[12268] | 313 | my $identifier = $self->get_metadata_element ($self->get_top_section(), $self->{'OIDmetadata'});
|
---|
[8797] | 314 | if (defined $identifier && $identifier ne "") {
|
---|
[17025] | 315 | $OID = $identifier;
|
---|
[19617] | 316 | $OID = &util::tidy_up_oid($OID);
|
---|
[8797] | 317 | } else {
|
---|
| 318 | # need a hash id
|
---|
[12268] | 319 | print STDERR "no $self->{'OIDmetadata'} metadata found, generating hash id\n";
|
---|
[8797] | 320 | $use_hash_oid = 1;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
[2327] | 323 | } else {
|
---|
[8797] | 324 | $use_hash_oid = 1;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | if ($use_hash_oid) {
|
---|
[17025] | 328 | my $hash_on_file = 1;
|
---|
| 329 | if ($self->{'OIDtype'} eq "hash_on_ga_xml") {
|
---|
| 330 | $hash_on_file = 0;
|
---|
| 331 | }
|
---|
| 332 | if ($hash_on_file) {
|
---|
| 333 | # "hash" OID - feed file to hashfile.exe
|
---|
| 334 | my $filename = $self->get_filename_for_hashing();
|
---|
| 335 | # -z: don't want to hash on the file if it is zero size
|
---|
| 336 | if (defined($filename) && -e $filename && !-z $filename) {
|
---|
| 337 | $OID = $self->_calc_OID ($filename);
|
---|
| 338 | } else {
|
---|
| 339 | $hash_on_file = 0;
|
---|
| 340 | }
|
---|
| 341 | }
|
---|
| 342 | if (!$hash_on_file) {
|
---|
| 343 | my $filename = &util::get_tmp_filename();
|
---|
[2327] | 344 | if (!open (OUTFILE, ">$filename")) {
|
---|
| 345 | print STDERR "doc::set_OID could not write to $filename\n";
|
---|
| 346 | } else {
|
---|
[13172] | 347 | my $doc_text = &docprint::get_section_xml($self, $self->get_top_section());
|
---|
| 348 | print OUTFILE $doc_text;
|
---|
[2327] | 349 | close (OUTFILE);
|
---|
| 350 | }
|
---|
| 351 | $OID = $self->_calc_OID ($filename);
|
---|
| 352 | &util::rm ($filename);
|
---|
[1374] | 353 | }
|
---|
[1241] | 354 | }
|
---|
| 355 | }
|
---|
| 356 | $self->set_metadata_element ($self->get_top_section(), "Identifier", $OID);
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | # this uses hashdoc (embedded c thingy) which is faster but still
|
---|
| 360 | # needs a little work to be suffiently stable
|
---|
| 361 | sub ___set_OID {
|
---|
| 362 | my $self = shift (@_);
|
---|
| 363 | my ($OID) = @_;
|
---|
| 364 |
|
---|
| 365 | # if an OID wasn't provided then calculate hash value based on document
|
---|
| 366 | if (!defined $OID)
|
---|
| 367 | {
|
---|
[13172] | 368 | my $hash_text = &docprint::get_section_xml($self, $self->get_top_section());
|
---|
[1241] | 369 | my $hash_len = length($hash_text);
|
---|
| 370 |
|
---|
| 371 | $OID = &hashdoc::buffer($hash_text,$hash_len);
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 | $self->set_metadata_element ($self->get_top_section(), "Identifier", $OID);
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | # returns the OID for this document
|
---|
| 378 | sub get_OID {
|
---|
| 379 | my $self = shift (@_);
|
---|
| 380 | my $OID = $self->get_metadata_element ($self->get_top_section(), "Identifier");
|
---|
| 381 | return $OID if (defined $OID);
|
---|
| 382 | return "NULL";
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | sub delete_OID {
|
---|
| 386 | my $self = shift (@_);
|
---|
| 387 |
|
---|
| 388 | $self->set_metadata_element ($self->get_top_section(), "Identifier", "NULL");
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 |
|
---|
| 392 | # methods for manipulating section names
|
---|
| 393 |
|
---|
| 394 | # returns the name of the top-most section (the top
|
---|
| 395 | # level of the document
|
---|
| 396 | sub get_top_section {
|
---|
| 397 | my $self = shift (@_);
|
---|
| 398 |
|
---|
| 399 | return "";
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | # returns a section
|
---|
| 403 | sub get_parent_section {
|
---|
| 404 | my $self = shift (@_);
|
---|
| 405 | my ($section) = @_;
|
---|
| 406 |
|
---|
| 407 | $section =~ s/(^|\.)\d+$//;
|
---|
| 408 |
|
---|
| 409 | return $section;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | # returns the first child section (or the end child
|
---|
| 413 | # if there isn't any)
|
---|
| 414 | sub get_begin_child {
|
---|
| 415 | my $self = shift (@_);
|
---|
| 416 | my ($section) = @_;
|
---|
| 417 |
|
---|
| 418 | my $section_ptr = $self->_lookup_section($section);
|
---|
| 419 | return "" unless defined $section_ptr;
|
---|
| 420 |
|
---|
| 421 | if (defined $section_ptr->{'subsection_order'}->[0]) {
|
---|
| 422 | return "$section.$section_ptr->{'subsection_order'}->[0]";
|
---|
| 423 | }
|
---|
| 424 |
|
---|
| 425 | return $self->get_end_child ($section);
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | # returns the next child of a parent section
|
---|
| 429 | sub get_next_child {
|
---|
| 430 | my $self = shift (@_);
|
---|
| 431 | my ($section) = @_;
|
---|
| 432 |
|
---|
| 433 | my $parent_section = $self->get_parent_section($section);
|
---|
| 434 | my $parent_section_ptr = $self->_lookup_section($parent_section);
|
---|
| 435 | return undef unless defined $parent_section_ptr;
|
---|
| 436 |
|
---|
| 437 | my ($section_num) = $section =~ /(\d+)$/;
|
---|
| 438 | return undef unless defined $section_num;
|
---|
| 439 |
|
---|
| 440 | my $i = 0;
|
---|
| 441 | my $section_order = $parent_section_ptr->{'subsection_order'};
|
---|
| 442 | while ($i < scalar(@$section_order)) {
|
---|
| 443 | last if $section_order->[$i] eq $section_num;
|
---|
| 444 | $i++;
|
---|
| 445 | }
|
---|
| 446 |
|
---|
| 447 | $i++; # the next child
|
---|
| 448 | if ($i < scalar(@$section_order)) {
|
---|
| 449 | return $section_order->[$i] if $parent_section eq "";
|
---|
| 450 | return "$parent_section.$section_order->[$i]";
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 | # no more sections in this level
|
---|
| 454 | return undef;
|
---|
| 455 | }
|
---|
| 456 |
|
---|
| 457 | # returns a reference to a list of children
|
---|
| 458 | sub get_children {
|
---|
| 459 | my $self = shift (@_);
|
---|
| 460 | my ($section) = @_;
|
---|
| 461 |
|
---|
| 462 | my $section_ptr = $self->_lookup_section($section);
|
---|
| 463 | return [] unless defined $section_ptr;
|
---|
| 464 |
|
---|
| 465 | my @children = @{$section_ptr->{'subsection_order'}};
|
---|
| 466 |
|
---|
| 467 | map {$_ = "$section.$_"; $_ =~ s/^\.+//;} @children;
|
---|
| 468 | return \@children;
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 | # returns the child section one past the last one (which
|
---|
| 472 | # is coded as "0")
|
---|
| 473 | sub get_end_child {
|
---|
| 474 | my $self = shift (@_);
|
---|
| 475 | my ($section) = @_;
|
---|
| 476 |
|
---|
| 477 | return $section . ".0" unless $section eq "";
|
---|
| 478 | return "0";
|
---|
| 479 | }
|
---|
| 480 |
|
---|
| 481 | # returns the next section in book order
|
---|
| 482 | sub get_next_section {
|
---|
| 483 | my $self = shift (@_);
|
---|
| 484 | my ($section) = @_;
|
---|
| 485 |
|
---|
| 486 | return undef unless defined $section;
|
---|
| 487 |
|
---|
| 488 | my $section_ptr = $self->_lookup_section($section);
|
---|
| 489 | return undef unless defined $section_ptr;
|
---|
| 490 |
|
---|
| 491 | # first try to find first child
|
---|
| 492 | if (defined $section_ptr->{'subsection_order'}->[0]) {
|
---|
| 493 | return $section_ptr->{'subsection_order'}->[0] if ($section eq "");
|
---|
| 494 | return "$section.$section_ptr->{'subsection_order'}->[0]";
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | do {
|
---|
| 498 | # try to find sibling
|
---|
| 499 | my $next_child = $self->get_next_child ($section);
|
---|
| 500 | return $next_child if (defined $next_child);
|
---|
| 501 |
|
---|
| 502 | # move up one level
|
---|
| 503 | $section = $self->get_parent_section ($section);
|
---|
| 504 | } while $section =~ /\d/;
|
---|
| 505 |
|
---|
| 506 | return undef;
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | sub is_leaf_section {
|
---|
| 510 | my $self = shift (@_);
|
---|
| 511 | my ($section) = @_;
|
---|
| 512 |
|
---|
| 513 | my $section_ptr = $self->_lookup_section($section);
|
---|
| 514 | return 1 unless defined $section_ptr;
|
---|
| 515 |
|
---|
| 516 | return (scalar (@{$section_ptr->{'subsection_order'}}) == 0);
|
---|
| 517 | }
|
---|
| 518 |
|
---|
| 519 | # methods for dealing with sections
|
---|
| 520 |
|
---|
| 521 | # returns the name of the inserted section
|
---|
| 522 | sub insert_section {
|
---|
| 523 | my $self = shift (@_);
|
---|
| 524 | my ($before_section) = @_;
|
---|
| 525 |
|
---|
| 526 | # get the child to insert before and its parent section
|
---|
| 527 | my $parent_section = "";
|
---|
| 528 | my $before_child = "0";
|
---|
| 529 | my @before_section = split (/\./, $before_section);
|
---|
| 530 | if (scalar(@before_section) > 0) {
|
---|
| 531 | $before_child = pop (@before_section);
|
---|
| 532 | $parent_section = join (".", @before_section);
|
---|
| 533 | }
|
---|
| 534 |
|
---|
| 535 | my $parent_section_ptr = $self->_lookup_section($parent_section);
|
---|
| 536 | if (!defined $parent_section_ptr) {
|
---|
| 537 | print STDERR "doc::insert_section couldn't find parent section " .
|
---|
| 538 | "$parent_section\n";
|
---|
| 539 | return;
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | # get the next section number
|
---|
| 543 | my $section_num = $parent_section_ptr->{'next_subsection'}++;
|
---|
| 544 |
|
---|
| 545 | my $i = 0;
|
---|
| 546 | while ($i < scalar(@{$parent_section_ptr->{'subsection_order'}}) &&
|
---|
| 547 | $parent_section_ptr->{'subsection_order'}->[$i] ne $before_child) {
|
---|
| 548 | $i++;
|
---|
| 549 | }
|
---|
| 550 |
|
---|
| 551 | # insert the section number into the order list
|
---|
| 552 | splice (@{$parent_section_ptr->{'subsection_order'}}, $i, 0, $section_num);
|
---|
| 553 |
|
---|
| 554 | # add this section to the parent section
|
---|
| 555 | my $section_ptr = {'subsection_order'=>[],
|
---|
| 556 | 'next_subsection'=>1,
|
---|
| 557 | 'subsections'=>{},
|
---|
| 558 | 'metadata'=>[],
|
---|
| 559 | 'text'=>""};
|
---|
| 560 | $parent_section_ptr->{'subsections'}->{$section_num} = $section_ptr;
|
---|
| 561 |
|
---|
| 562 | # work out the full section number
|
---|
| 563 | my $section = $parent_section;
|
---|
| 564 | $section .= "." unless $section eq "";
|
---|
| 565 | $section .= $section_num;
|
---|
| 566 |
|
---|
| 567 | return $section;
|
---|
| 568 | }
|
---|
| 569 |
|
---|
| 570 | # creates a pre-named section
|
---|
| 571 | sub create_named_section {
|
---|
| 572 | my $self = shift (@_);
|
---|
| 573 | my ($mastersection) = @_;
|
---|
| 574 |
|
---|
| 575 | my ($num);
|
---|
| 576 | my $section = $mastersection;
|
---|
| 577 | my $sectionref = $self;
|
---|
| 578 |
|
---|
| 579 | while ($section ne "") {
|
---|
| 580 | ($num, $section) = $section =~ /^\.?(\d+)(.*)$/;
|
---|
| 581 | $num =~ s/^0+(\d)/$1/; # remove leading 0s
|
---|
| 582 | $section = "" unless defined $section;
|
---|
| 583 |
|
---|
| 584 | if (defined $num) {
|
---|
| 585 | if (!defined $sectionref->{'subsections'}->{$num}) {
|
---|
| 586 | push (@{$sectionref->{'subsection_order'}}, $num);
|
---|
| 587 | $sectionref->{'subsections'}->{$num} = {'subsection_order'=>[],
|
---|
| 588 | 'next_subsection'=>1,
|
---|
| 589 | 'subsections'=>{},
|
---|
| 590 | 'metadata'=>[],
|
---|
| 591 | 'text'=>""};
|
---|
| 592 | if ($num >= $sectionref->{'next_subsection'}) {
|
---|
| 593 | $sectionref->{'next_subsection'} = $num + 1;
|
---|
| 594 | }
|
---|
| 595 | }
|
---|
| 596 | $sectionref = $sectionref->{'subsections'}->{$num};
|
---|
| 597 |
|
---|
| 598 | } else {
|
---|
| 599 | print STDERR "doc::create_named_section couldn't create section ";
|
---|
| 600 | print STDERR "$mastersection\n";
|
---|
| 601 | last;
|
---|
| 602 | }
|
---|
| 603 | }
|
---|
| 604 | }
|
---|
| 605 |
|
---|
| 606 | # returns a reference to a list of subsections
|
---|
| 607 | sub list_subsections {
|
---|
| 608 | my $self = shift (@_);
|
---|
| 609 | my ($section) = @_;
|
---|
| 610 |
|
---|
| 611 | my $section_ptr = $self->_lookup_section ($section);
|
---|
| 612 | if (!defined $section_ptr) {
|
---|
| 613 | print STDERR "doc::list_subsections couldn't find section $section\n";
|
---|
| 614 | return [];
|
---|
| 615 | }
|
---|
| 616 |
|
---|
| 617 | return [@{$section_ptr->{'subsection_order'}}];
|
---|
| 618 | }
|
---|
| 619 |
|
---|
| 620 | sub delete_section {
|
---|
| 621 | my $self = shift (@_);
|
---|
| 622 | my ($section) = @_;
|
---|
| 623 |
|
---|
| 624 | # my $section_ptr = {'subsection_order'=>[],
|
---|
| 625 | # 'next_subsection'=>1,
|
---|
| 626 | # 'subsections'=>{},
|
---|
| 627 | # 'metadata'=>[],
|
---|
| 628 | # 'text'=>""};
|
---|
| 629 |
|
---|
| 630 | # if this is the top section reset everything
|
---|
| 631 | if ($section eq "") {
|
---|
| 632 | $self->{'subsection_order'} = [];
|
---|
| 633 | $self->{'subsections'} = {};
|
---|
| 634 | $self->{'metadata'} = [];
|
---|
| 635 | $self->{'text'} = "";
|
---|
| 636 | return;
|
---|
| 637 | }
|
---|
| 638 |
|
---|
| 639 | # find the parent of the section to delete
|
---|
| 640 | my $parent_section = "";
|
---|
| 641 | my $child = "0";
|
---|
| 642 | my @section = split (/\./, $section);
|
---|
| 643 | if (scalar(@section) > 0) {
|
---|
| 644 | $child = pop (@section);
|
---|
| 645 | $parent_section = join (".", @section);
|
---|
| 646 | }
|
---|
| 647 |
|
---|
| 648 | my $parent_section_ptr = $self->_lookup_section($parent_section);
|
---|
| 649 | if (!defined $parent_section_ptr) {
|
---|
| 650 | print STDERR "doc::delete_section couldn't find parent section " .
|
---|
| 651 | "$parent_section\n";
|
---|
| 652 | return;
|
---|
| 653 | }
|
---|
| 654 |
|
---|
| 655 | # remove this section from the subsection_order list
|
---|
| 656 | my $i = 0;
|
---|
| 657 | while ($i < scalar (@{$parent_section_ptr->{'subsection_order'}})) {
|
---|
| 658 | if ($parent_section_ptr->{'subsection_order'}->[$i] eq $child) {
|
---|
| 659 | splice (@{$parent_section_ptr->{'subsection_order'}}, $i, 1);
|
---|
| 660 | last;
|
---|
| 661 | }
|
---|
| 662 | $i++;
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | # remove this section from the subsection hash
|
---|
| 666 | if (defined ($parent_section_ptr->{'subsections'}->{$child})) {
|
---|
| 667 | undef $parent_section_ptr->{'subsections'}->{$child};
|
---|
| 668 | }
|
---|
| 669 | }
|
---|
| 670 |
|
---|
| 671 | #--
|
---|
[4] | 672 | # methods for dealing with metadata
|
---|
| 673 |
|
---|
| 674 | # set_metadata_element and get_metadata_element are for metadata
|
---|
| 675 | # which should only have one value. add_meta_data and get_metadata
|
---|
| 676 | # are for metadata which can have more than one value.
|
---|
| 677 |
|
---|
[1241] | 678 | # returns the first metadata value which matches field
|
---|
[6111] | 679 |
|
---|
| 680 | # This version of get metadata element works much like the one above,
|
---|
| 681 | # except it allows for the namespace portion of a metadata element to
|
---|
| 682 | # be ignored, thus if you are searching for dc.Title, the first piece
|
---|
| 683 | # of matching metadata ending with the name Title (once any namespace
|
---|
| 684 | # is removed) would be returned.
|
---|
| 685 | # 28-11-2003 John Thompson
|
---|
[1241] | 686 | sub get_metadata_element {
|
---|
| 687 | my $self = shift (@_);
|
---|
[6111] | 688 | my ($section, $field, $ignore_namespace) = @_;
|
---|
[1241] | 689 | my ($data);
|
---|
| 690 |
|
---|
[6111] | 691 | $ignore_namespace = 0 unless defined $ignore_namespace;
|
---|
| 692 |
|
---|
[1241] | 693 | my $section_ptr = $self->_lookup_section($section);
|
---|
| 694 | if (!defined $section_ptr) {
|
---|
[8716] | 695 | print STDERR "doc::get_metadata_element couldn't find section ", $section, "\n";
|
---|
[1241] | 696 | return;
|
---|
| 697 | }
|
---|
| 698 |
|
---|
[6111] | 699 | # Remove the any namespace if we are being told to ignore them
|
---|
| 700 | if($ignore_namespace) {
|
---|
| 701 | $field =~ s/^\w*\.//;
|
---|
| 702 | }
|
---|
| 703 |
|
---|
[1241] | 704 | foreach $data (@{$section_ptr->{'metadata'}}) {
|
---|
[6111] | 705 |
|
---|
| 706 | my $data_name = $data->[0];
|
---|
[14966] | 707 |
|
---|
[6111] | 708 | # Remove the any namespace if we are being told to ignore them
|
---|
| 709 | if($ignore_namespace) {
|
---|
| 710 | $data_name =~ s/^\w*\.//;
|
---|
| 711 | }
|
---|
| 712 |
|
---|
| 713 | return $data->[1] if (scalar(@$data) >= 2 && $data_name eq $field);
|
---|
[1241] | 714 | }
|
---|
| 715 |
|
---|
| 716 | return undef; # was not found
|
---|
| 717 | }
|
---|
| 718 |
|
---|
| 719 | # returns a list of the form [value1, value2, ...]
|
---|
| 720 | sub get_metadata {
|
---|
| 721 | my $self = shift (@_);
|
---|
[6111] | 722 | my ($section, $field, $ignore_namespace) = @_;
|
---|
[1241] | 723 | my ($data);
|
---|
| 724 |
|
---|
[6111] | 725 | $ignore_namespace = 0 unless defined $ignore_namespace;
|
---|
| 726 |
|
---|
[1241] | 727 | my $section_ptr = $self->_lookup_section($section);
|
---|
| 728 | if (!defined $section_ptr) {
|
---|
[8716] | 729 | print STDERR "doc::get_metadata couldn't find section ",
|
---|
| 730 | $section, "\n";
|
---|
[1241] | 731 | return;
|
---|
| 732 | }
|
---|
| 733 |
|
---|
[6111] | 734 | # Remove the any namespace if we are being told to ignore them
|
---|
| 735 | if($ignore_namespace) {
|
---|
| 736 | $field =~ s/^\w*\.//;
|
---|
| 737 | }
|
---|
| 738 |
|
---|
[1241] | 739 | my @metadata = ();
|
---|
| 740 | foreach $data (@{$section_ptr->{'metadata'}}) {
|
---|
[6111] | 741 |
|
---|
| 742 | my $data_name = $data->[0];
|
---|
| 743 | # Remove the any namespace if we are being told to ignore them
|
---|
| 744 | if($ignore_namespace) {
|
---|
| 745 | $data_name =~ s/^\w*\.//;
|
---|
| 746 | }
|
---|
| 747 |
|
---|
| 748 | push (@metadata, $data->[1]) if ($data_name eq $field);
|
---|
[1241] | 749 | }
|
---|
[9241] | 750 |
|
---|
[1241] | 751 | return \@metadata;
|
---|
| 752 | }
|
---|
| 753 |
|
---|
| 754 | # returns a list of the form [[field,value],[field,value],...]
|
---|
| 755 | sub get_all_metadata {
|
---|
| 756 | my $self = shift (@_);
|
---|
| 757 | my ($section) = @_;
|
---|
| 758 |
|
---|
| 759 | my $section_ptr = $self->_lookup_section($section);
|
---|
| 760 | if (!defined $section_ptr) {
|
---|
[8716] | 761 | print STDERR "doc::get_all_metadata couldn't find section ", $section, "\n";
|
---|
[1241] | 762 | return;
|
---|
| 763 | }
|
---|
| 764 |
|
---|
| 765 | return $section_ptr->{'metadata'};
|
---|
| 766 | }
|
---|
| 767 |
|
---|
| 768 | # $value is optional
|
---|
| 769 | sub delete_metadata {
|
---|
| 770 | my $self = shift (@_);
|
---|
| 771 | my ($section, $field, $value) = @_;
|
---|
| 772 |
|
---|
| 773 | my $section_ptr = $self->_lookup_section($section);
|
---|
| 774 | if (!defined $section_ptr) {
|
---|
[8716] | 775 | print STDERR "doc::delete_metadata couldn't find section ", $section, "\n";
|
---|
[1241] | 776 | return;
|
---|
| 777 | }
|
---|
| 778 |
|
---|
| 779 | my $i = 0;
|
---|
| 780 | while ($i < scalar (@{$section_ptr->{'metadata'}})) {
|
---|
| 781 | if (($section_ptr->{'metadata'}->[$i]->[0] eq $field) &&
|
---|
| 782 | (!defined $value || $section_ptr->{'metadata'}->[$i]->[1] eq $value)) {
|
---|
| 783 | splice (@{$section_ptr->{'metadata'}}, $i, 1);
|
---|
| 784 | } else {
|
---|
| 785 | $i++;
|
---|
| 786 | }
|
---|
| 787 | }
|
---|
| 788 | }
|
---|
| 789 |
|
---|
| 790 | sub delete_all_metadata {
|
---|
| 791 | my $self = shift (@_);
|
---|
| 792 | my ($section) = @_;
|
---|
| 793 |
|
---|
| 794 | my $section_ptr = $self->_lookup_section($section);
|
---|
| 795 | if (!defined $section_ptr) {
|
---|
[8716] | 796 | print STDERR "doc::delete_all_metadata couldn't find section ", $section, "\n";
|
---|
[1241] | 797 | return;
|
---|
| 798 | }
|
---|
| 799 |
|
---|
| 800 | $section_ptr->{'metadata'} = [];
|
---|
| 801 | }
|
---|
| 802 |
|
---|
[4] | 803 | sub set_metadata_element {
|
---|
| 804 | my $self = shift (@_);
|
---|
| 805 | my ($section, $field, $value) = @_;
|
---|
| 806 |
|
---|
[97] | 807 | $self->set_utf8_metadata_element ($section, $field,
|
---|
[1870] | 808 | &unicode::ascii2utf8(\$value));
|
---|
[73] | 809 | }
|
---|
| 810 |
|
---|
| 811 | # set_utf8_metadata_element assumes the text has already been
|
---|
| 812 | # converted to the UTF-8 encoding.
|
---|
| 813 | sub set_utf8_metadata_element {
|
---|
| 814 | my $self = shift (@_);
|
---|
| 815 | my ($section, $field, $value) = @_;
|
---|
| 816 |
|
---|
[4] | 817 | $self->delete_metadata ($section, $field);
|
---|
[73] | 818 | $self->add_utf8_metadata ($section, $field, $value);
|
---|
[4] | 819 | }
|
---|
| 820 |
|
---|
| 821 |
|
---|
[73] | 822 | # add_metadata assumes the text is in (extended) ascii form. For
|
---|
[8220] | 823 | # text which has already been converted to the UTF-8 format use
|
---|
[73] | 824 | # add_utf8_metadata.
|
---|
[4] | 825 | sub add_metadata {
|
---|
| 826 | my $self = shift (@_);
|
---|
| 827 | my ($section, $field, $value) = @_;
|
---|
| 828 |
|
---|
[97] | 829 | $self->add_utf8_metadata ($section, $field,
|
---|
[1870] | 830 | &unicode::ascii2utf8(\$value));
|
---|
[73] | 831 | }
|
---|
| 832 |
|
---|
| 833 | sub add_utf8_metadata {
|
---|
| 834 | my $self = shift (@_);
|
---|
| 835 | my ($section, $field, $value) = @_;
|
---|
| 836 |
|
---|
[4] | 837 | my $section_ptr = $self->_lookup_section($section);
|
---|
| 838 | if (!defined $section_ptr) {
|
---|
[8716] | 839 | print STDERR "doc::add_utf8_metadata couldn't find section ", $section, "\n";
|
---|
[4] | 840 | return;
|
---|
| 841 | }
|
---|
[1732] | 842 | if (!defined $value) {
|
---|
| 843 | print STDERR "doc::add_utf8_metadata undefined value for $field\n";
|
---|
| 844 | return;
|
---|
| 845 | }
|
---|
| 846 | if (!defined $field) {
|
---|
| 847 | print STDERR "doc::add_utf8_metadata undefined metadata type \n";
|
---|
| 848 | return;
|
---|
| 849 | }
|
---|
[4] | 850 |
|
---|
[8894] | 851 | #print STDERR "###$field=$value\n";
|
---|
[7798] | 852 | # double check that the value is utf-8
|
---|
[16578] | 853 | if (!&unicode::check_is_utf8($value)) {
|
---|
[16670] | 854 | print STDERR "doc::add_utf8_metadata - warning: '$field''s value $value wasn't utf8.";
|
---|
[16578] | 855 | &unicode::ensure_utf8(\$value);
|
---|
[16670] | 856 | print STDERR " Tried converting to utf8: $value\n";
|
---|
[7798] | 857 | }
|
---|
| 858 |
|
---|
[4] | 859 | push (@{$section_ptr->{'metadata'}}, [$field, $value]);
|
---|
| 860 | }
|
---|
| 861 |
|
---|
| 862 |
|
---|
| 863 | # methods for dealing with text
|
---|
| 864 |
|
---|
[1241] | 865 | # returns the text for a section
|
---|
| 866 | sub get_text {
|
---|
| 867 | my $self = shift (@_);
|
---|
| 868 | my ($section) = @_;
|
---|
| 869 |
|
---|
| 870 | my $section_ptr = $self->_lookup_section($section);
|
---|
| 871 | if (!defined $section_ptr) {
|
---|
| 872 | print STDERR "doc::get_text couldn't find section " .
|
---|
| 873 | "$section\n";
|
---|
| 874 | return "";
|
---|
| 875 | }
|
---|
| 876 |
|
---|
| 877 | return $section_ptr->{'text'};
|
---|
| 878 | }
|
---|
| 879 |
|
---|
| 880 | # returns the (utf-8 encoded) length of the text for a section
|
---|
| 881 | sub get_text_length {
|
---|
| 882 | my $self = shift (@_);
|
---|
| 883 | my ($section) = @_;
|
---|
| 884 |
|
---|
| 885 | my $section_ptr = $self->_lookup_section($section);
|
---|
| 886 | if (!defined $section_ptr) {
|
---|
| 887 | print STDERR "doc::get_text_length couldn't find section " .
|
---|
| 888 | "$section\n";
|
---|
| 889 | return 0;
|
---|
| 890 | }
|
---|
| 891 |
|
---|
| 892 | return length ($section_ptr->{'text'});
|
---|
| 893 | }
|
---|
| 894 |
|
---|
| 895 | sub delete_text {
|
---|
| 896 | my $self = shift (@_);
|
---|
| 897 | my ($section) = @_;
|
---|
| 898 |
|
---|
| 899 | my $section_ptr = $self->_lookup_section($section);
|
---|
| 900 | if (!defined $section_ptr) {
|
---|
| 901 | print STDERR "doc::delete_text couldn't find section " .
|
---|
| 902 | "$section\n";
|
---|
| 903 | return;
|
---|
| 904 | }
|
---|
| 905 |
|
---|
| 906 | $section_ptr->{'text'} = "";
|
---|
| 907 | }
|
---|
| 908 |
|
---|
[73] | 909 | # add_text assumes the text is in (extended) ascii form. For
|
---|
| 910 | # text which has been already converted to the UTF-8 format
|
---|
| 911 | # use add_utf8_text.
|
---|
[4] | 912 | sub add_text {
|
---|
| 913 | my $self = shift (@_);
|
---|
| 914 | my ($section, $text) = @_;
|
---|
| 915 |
|
---|
[73] | 916 | # convert the text to UTF-8 encoded unicode characters
|
---|
| 917 | # and add the text
|
---|
[1870] | 918 | $self->add_utf8_text($section, &unicode::ascii2utf8(\$text));
|
---|
[73] | 919 | }
|
---|
| 920 |
|
---|
| 921 |
|
---|
| 922 | # add_utf8_text assumes the text to be added has already
|
---|
| 923 | # been converted to the UTF-8 encoding. For ascii text use
|
---|
| 924 | # add_text
|
---|
| 925 | sub add_utf8_text {
|
---|
| 926 | my $self = shift (@_);
|
---|
| 927 | my ($section, $text) = @_;
|
---|
| 928 |
|
---|
[4] | 929 | my $section_ptr = $self->_lookup_section($section);
|
---|
| 930 | if (!defined $section_ptr) {
|
---|
[73] | 931 | print STDERR "doc::add_utf8_text couldn't find section " .
|
---|
[4] | 932 | "$section\n";
|
---|
| 933 | return;
|
---|
| 934 | }
|
---|
| 935 |
|
---|
| 936 | $section_ptr->{'text'} .= $text;
|
---|
| 937 | }
|
---|
| 938 |
|
---|
[16950] | 939 | # returns the Source meta, which is the utf8 filename generated.
|
---|
| 940 | # Added a separate method here for convenience
|
---|
| 941 | sub get_source {
|
---|
[16924] | 942 | my $self = shift (@_);
|
---|
[16950] | 943 | return $self->get_metadata_element ($self->get_top_section(), "Source");
|
---|
| 944 | }
|
---|
| 945 |
|
---|
| 946 | # returns the SourceFile meta, which is the url reference to the URL-encoded
|
---|
| 947 | # version of Source (the utf8 filename). Added a separate method here for convenience
|
---|
| 948 | sub get_sourcefile {
|
---|
| 949 | my $self = shift (@_);
|
---|
| 950 | return $self->get_metadata_element ($self->get_top_section(), "SourceFile");
|
---|
| 951 | }
|
---|
| 952 |
|
---|
| 953 | # Get the actual name of the assocfile, a url-encoded string derived from SourceFile.
|
---|
| 954 | # The SourceFile meta is the (escaped) url reference to the url-encoded assocfile.
|
---|
| 955 | sub get_assocfile_from_sourcefile {
|
---|
| 956 | my $self = shift (@_);
|
---|
[16924] | 957 |
|
---|
| 958 | # get the SourceFile meta, which is a *URL* to a file on the filesystem
|
---|
| 959 | my $top_section = $self->get_top_section();
|
---|
| 960 | my $source_file = $self->get_metadata_element($top_section, "SourceFile");
|
---|
[4] | 961 |
|
---|
[16924] | 962 | # get the actual filename as it exists on the filesystem which this url refers to
|
---|
[16928] | 963 | $source_file = &unicode::url_to_filename($source_file);
|
---|
[16924] | 964 | my ($assocfilename) = $source_file =~ /([^\\\/]+)$/;
|
---|
| 965 | return $assocfilename;
|
---|
| 966 | }
|
---|
| 967 |
|
---|
[1241] | 968 | # methods for dealing with associated files
|
---|
| 969 |
|
---|
| 970 | # a file is associated with a document, NOT a section.
|
---|
| 971 | # if section is defined it is noted in the data structure
|
---|
| 972 | # only so that files associated from a particular section
|
---|
| 973 | # may be removed later (using delete_section_assoc_files)
|
---|
| 974 | sub associate_file {
|
---|
| 975 | my $self = shift (@_);
|
---|
| 976 | my ($real_filename, $assoc_filename, $mime_type, $section) = @_;
|
---|
| 977 | $mime_type = &ghtml::guess_mime_type ($real_filename) unless defined $mime_type;
|
---|
| 978 |
|
---|
| 979 | # remove all associated files with the same name
|
---|
| 980 | $self->delete_assoc_file ($assoc_filename);
|
---|
[8504] | 981 |
|
---|
[1241] | 982 | push (@{$self->{'associated_files'}},
|
---|
| 983 | [$real_filename, $assoc_filename, $mime_type, $section]);
|
---|
| 984 | }
|
---|
| 985 |
|
---|
| 986 | # returns a list of associated files in the form
|
---|
| 987 | # [[real_filename, assoc_filename, mimetype], ...]
|
---|
| 988 | sub get_assoc_files {
|
---|
| 989 | my $self = shift (@_);
|
---|
| 990 |
|
---|
| 991 | return $self->{'associated_files'};
|
---|
| 992 | }
|
---|
| 993 |
|
---|
[19494] | 994 |
|
---|
| 995 | sub metadata_file {
|
---|
| 996 | my $self = shift (@_);
|
---|
| 997 | my ($real_filename, $filename) = @_;
|
---|
| 998 |
|
---|
| 999 | push (@{$self->{'metadata_files'}},
|
---|
| 1000 | [$real_filename, $filename]);
|
---|
| 1001 | }
|
---|
| 1002 |
|
---|
| 1003 | sub get_meta_files {
|
---|
| 1004 | my $self = shift (@_);
|
---|
| 1005 |
|
---|
| 1006 | return $self->{'metadata_files'};
|
---|
| 1007 | }
|
---|
| 1008 |
|
---|
[1241] | 1009 | sub delete_section_assoc_files {
|
---|
| 1010 | my $self = shift (@_);
|
---|
| 1011 | my ($section) = @_;
|
---|
| 1012 |
|
---|
| 1013 | my $i=0;
|
---|
| 1014 | while ($i < scalar (@{$self->{'associated_files'}})) {
|
---|
| 1015 | if (defined $self->{'associated_files'}->[$i]->[3] &&
|
---|
| 1016 | $self->{'associated_files'}->[$i]->[3] eq $section) {
|
---|
| 1017 | splice (@{$self->{'associated_files'}}, $i, 1);
|
---|
| 1018 | } else {
|
---|
| 1019 | $i++;
|
---|
| 1020 | }
|
---|
| 1021 | }
|
---|
| 1022 | }
|
---|
| 1023 |
|
---|
| 1024 | sub delete_assoc_file {
|
---|
| 1025 | my $self = shift (@_);
|
---|
| 1026 | my ($assoc_filename) = @_;
|
---|
| 1027 |
|
---|
| 1028 | my $i=0;
|
---|
| 1029 | while ($i < scalar (@{$self->{'associated_files'}})) {
|
---|
| 1030 | if ($self->{'associated_files'}->[$i]->[1] eq $assoc_filename) {
|
---|
| 1031 | splice (@{$self->{'associated_files'}}, $i, 1);
|
---|
| 1032 | } else {
|
---|
| 1033 | $i++;
|
---|
| 1034 | }
|
---|
| 1035 | }
|
---|
| 1036 | }
|
---|
| 1037 |
|
---|
| 1038 | sub reset_nextsection_ptr {
|
---|
| 1039 | my $self = shift (@_);
|
---|
| 1040 | my ($section) = @_;
|
---|
| 1041 |
|
---|
| 1042 | my $section_ptr = $self->_lookup_section($section);
|
---|
| 1043 | $section_ptr->{'next_subsection'} = 1;
|
---|
| 1044 | }
|
---|
| 1045 |
|
---|
[4] | 1046 | 1;
|
---|