root/gsdl/trunk/perllib/doc.pm @ 19617

Revision 19617, 28.2 KB (checked in by kjdon, 11 years ago)

use new util::tidy_up_oid instead of fiddling with it directly

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
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#
10# This program is free software; you can redistr   te 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# base class to hold documents
27
28package doc;
29eval {require bytes};
30
31BEGIN {
32    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
33    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/dynamic/lib/site_perl/5.005/i686-linux");
34}
35
36use strict;
37use unicode;
38use util;
39use ghtml;
40use File::stat;
41##use hashdoc;
42use docprint;
43
44# the document type may be indexed_doc, nonindexed_doc, or
45# classification
46
47our $OIDcount = 0;
48
49sub new {
50    my $class = shift (@_);
51    my ($source_filename, $doc_type, $rename_method) = @_;
52
53
54    my $self = bless {'associated_files'=>[],
55              'subsection_order'=>[],
56              'next_subsection'=>1,
57              'subsections'=>{},
58              'metadata'=>[],
59              'text'=>"",
60              'OIDtype'=>"hash"}, $class;
61
62    # used to set lastmodified here, but this can screw up the HASH ids, so
63    # the docsave processor now calls set_lastmodified
64
65    $self->{'source_path'} = $source_filename;
66   
67    if (defined $source_filename) {
68    $source_filename = &util::filename_within_collection($source_filename);
69    print STDERR "****** doc.pm::new(): no file rename method provided\n" unless $rename_method;
70    $self->set_source_filename ($source_filename, $rename_method);
71    }
72
73    $self->set_doc_type ($doc_type) if defined $doc_type;
74
75    return $self;
76}
77# set lastmodified for OAI purposes, added by GRB, moved by kjdon
78sub set_lastmodified {
79    my $self = shift (@_);
80
81    my $source_path = $self->{'source_path'};
82   
83    if (defined $source_path && (-e $source_path)) {
84
85        my $file_stat = stat($source_path);
86    my $mtime = $file_stat->mtime;
87    $self->add_utf8_metadata($self->get_top_section(), "lastmodified", $file_stat->mtime);
88    }
89}
90
91# clone the $self object
92sub duplicate {
93    my $self = shift (@_);
94
95    my $newobj = {};
96   
97    foreach my $k (keys %$self) {
98    $newobj->{$k} = &clone ($self->{$k});
99    }
100
101    bless $newobj, ref($self);
102    return $newobj;
103}
104
105sub clone {
106    my ($from) = @_;
107    my $type = ref ($from);
108
109    if ($type eq "HASH") {
110    my $to = {};
111    foreach my $key (keys %$from) {
112        $to->{$key} = &clone ($from->{$key});
113    }
114    return $to;
115    } elsif ($type eq "ARRAY") {
116    my $to = [];
117    foreach my $v (@$from) {
118        push (@$to, &clone ($v));
119    }
120    return $to;
121    } else {
122    return $from;
123    }
124}
125
126sub set_OIDtype {
127    my $self = shift (@_);
128    my ($type, $metadata) = @_;
129
130    if (defined $type && $type =~ /^(hash|hash_on_file|hash_on_ga_xml|incremental|dirname|assigned)$/) {
131    $self->{'OIDtype'} = $type;
132    } else {
133    $self->{'OIDtype'} = "hash";
134    }
135
136    if ($type =~ /^assigned$/) {
137    if (defined $metadata) {
138        $self->{'OIDmetadata'} = $metadata;
139    } else {
140        $self->{'OIDmetadata'} = "dc.Identifier";
141    }
142    }
143}
144
145sub set_source_filename {
146    my $self = shift (@_);
147    my ($source_filename, $rename_method) = @_;
148
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.
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.
157   
158#    print STDERR "******URL/base64 encoding the gsdl_source_filename $source_filename ";
159
160    # URLencode just the gsdl_source_filename, not the directory. Then prepend dir
161    my ($srcfilename,$dirname,$suffix)
162    = &File::Basename::fileparse($source_filename, "\\.[^\\.]+\$");
163#    print STDERR "-> $srcfilename -> ";
164    $srcfilename = &util::rename_file($srcfilename.$suffix, $rename_method);
165    $source_filename = &util::filename_cat($dirname, $srcfilename);
166#    print STDERR "$source_filename\n";
167   
168    $self->set_utf8_metadata_element ($self->get_top_section(),
169                 "gsdlsourcefilename",
170                 $source_filename);
171}
172
173sub set_converted_filename {
174    my $self = shift (@_);
175    my ($converted_filename) = @_;
176
177    # we know the converted filename is utf8
178    $self->set_utf8_metadata_element ($self->get_top_section(),
179                 "gsdlconvertedfilename",
180                 $converted_filename);
181}
182
183# returns the source_filename as it was provided
184sub get_source_filename {
185    my $self = shift (@_);
186
187    return $self->get_metadata_element ($self->get_top_section(), "gsdlsourcefilename");
188}
189
190# returns converted filename if available else returns source filename
191sub 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) {
197    my $plugin_name = $self->get_metadata_element ($self->get_top_section(), "Plugin");
198    # if NULPlug processed file, then don't give a filename
199    if (defined $plugin_name && $plugin_name eq "NULPlug") {
200        $filename = undef;
201    } else { # returns the URL encoded source filename!
202        $filename = $self->get_metadata_element ($self->get_top_section(), "gsdlsourcefilename");
203    }
204    }
205    return $filename;
206}
207
208sub 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
217# returns the gsdldoctype as it was provided
218# the default of "indexed_doc" is used if no document
219# type was provided
220sub 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
230sub _lookup_section {
231    my $self = shift (@_);
232    my ($section) = @_;
233
234    my ($num);
235    my $sectionref = $self;
236
237    while (defined $section && $section ne "") {
238   
239    ($num, $section) = $section =~ /^\.?(\d+)(.*)$/;
240   
241    $num =~ s/^0+(\d)/$1/ if defined $num ; # remove leading 0s
242   
243    $section = "" unless defined $section;
244   
245
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
256# calculate OID by hashing the contents of the document
257sub _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");
265
266    my $result = "NULL";
267   
268    if (-e "$hashfile_exe") {
269#   $result = `\"$hashfile_exe\" \"$filename\"`;
270    $result = `hashfile$osexe \"$filename\"`;
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
280# if $OID is not provided one is calculated
281sub set_OID {
282    my $self = shift (@_);
283    my ($OID) = @_;
284   
285    my $use_hash_oid = 0;
286    # if an OID wasn't provided calculate one
287    if (!defined $OID) {
288    $OID = "NULL";
289    if ($self->{'OIDtype'} =~ /^hash/) {
290        $use_hash_oid = 1;
291    } elsif ($self->{'OIDtype'} eq "incremental") {
292        $OID = "D" . $OIDcount;
293        $OIDcount ++;
294     
295    } elsif ($self->{'OIDtype'} eq "dirname") {
296        $OID = 'J';
297        my $filename = $self->get_source_filename();
298        if (defined($filename)) { # && -e $filename) {
299        $OID = &File::Basename::dirname($filename);
300        if (defined $OID) {
301            $OID = 'J'.&File::Basename::basename($OID);
302            $OID = &util::tidy_up_oid($OID);
303        } else {
304            print STDERR "Failed to find base for filename ($filename)...generating hash id\n";
305            $use_hash_oid = 1;
306        }
307        } else {
308        print STDERR "Failed to find filename, generating hash id\n";
309        $use_hash_oid = 1;
310        }   
311       
312    } elsif ($self->{'OIDtype'} eq "assigned") {
313        my $identifier = $self->get_metadata_element ($self->get_top_section(), $self->{'OIDmetadata'});
314        if (defined $identifier && $identifier ne "") {
315        $OID = $identifier;
316        $OID = &util::tidy_up_oid($OID);
317        } else {
318        # need a hash id
319        print STDERR "no $self->{'OIDmetadata'} metadata found, generating hash id\n";
320        $use_hash_oid = 1;
321        }
322               
323    } else {
324        $use_hash_oid = 1;
325    }
326
327    if ($use_hash_oid) {
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();
344        if (!open (OUTFILE, ">$filename")) {
345            print STDERR "doc::set_OID could not write to $filename\n";
346        } else {
347            my $doc_text = &docprint::get_section_xml($self, $self->get_top_section());
348            print OUTFILE $doc_text;
349            close (OUTFILE);
350        }
351        $OID = $self->_calc_OID ($filename);
352        &util::rm ($filename);
353        }
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
361sub ___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    {
368    my $hash_text = &docprint::get_section_xml($self, $self->get_top_section());
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
378sub 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
385sub 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
396sub get_top_section {
397    my $self = shift (@_);
398   
399    return "";
400}
401
402# returns a section
403sub 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)
414sub 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
429sub 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
458sub 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")
473sub 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
482sub 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
509sub 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
522sub 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
571sub 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
607sub 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
620sub 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#--
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
678# returns the first metadata value which matches field
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
686sub get_metadata_element {
687    my $self = shift (@_);
688    my ($section, $field, $ignore_namespace) = @_;
689    my ($data);
690
691    $ignore_namespace = 0 unless defined $ignore_namespace;
692
693    my $section_ptr = $self->_lookup_section($section);
694    if (!defined $section_ptr) {
695    print STDERR "doc::get_metadata_element couldn't find section ", $section, "\n";
696    return;
697    }
698
699    # Remove the any namespace if we are being told to ignore them
700    if($ignore_namespace) {
701    $field =~ s/^\w*\.//;
702    }
703
704    foreach $data (@{$section_ptr->{'metadata'}}) {
705
706    my $data_name = $data->[0];
707
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);
714    }
715   
716    return undef; # was not found
717}
718
719# returns a list of the form [value1, value2, ...]
720sub get_metadata {
721    my $self = shift (@_);
722    my ($section, $field, $ignore_namespace) = @_;
723    my ($data);
724
725    $ignore_namespace = 0 unless defined $ignore_namespace;
726
727    my $section_ptr = $self->_lookup_section($section);
728    if (!defined $section_ptr) {
729        print STDERR "doc::get_metadata couldn't find section ",
730        $section, "\n";
731        return;
732    }
733
734    # Remove the any namespace if we are being told to ignore them
735    if($ignore_namespace) {
736    $field =~ s/^\w*\.//;
737    }
738
739    my @metadata = ();
740    foreach $data (@{$section_ptr->{'metadata'}}) {
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);
749    }
750
751    return \@metadata;
752}
753
754# returns a list of the form [[field,value],[field,value],...]
755sub get_all_metadata {
756    my $self = shift (@_);
757    my ($section) = @_;
758
759    my $section_ptr = $self->_lookup_section($section);
760    if (!defined $section_ptr) {
761    print STDERR "doc::get_all_metadata couldn't find section ", $section, "\n";
762    return;
763    }
764   
765    return $section_ptr->{'metadata'};
766}
767
768# $value is optional
769sub 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) {
775    print STDERR "doc::delete_metadata couldn't find section ", $section, "\n";
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
790sub delete_all_metadata {
791    my $self = shift (@_);
792    my ($section) = @_;
793
794    my $section_ptr = $self->_lookup_section($section);
795    if (!defined $section_ptr) {
796    print STDERR "doc::delete_all_metadata couldn't find section ", $section, "\n";
797    return;
798    }
799   
800    $section_ptr->{'metadata'} = [];
801}
802
803sub set_metadata_element {
804    my $self = shift (@_);
805    my ($section, $field, $value) = @_;
806
807    $self->set_utf8_metadata_element ($section, $field,
808                      &unicode::ascii2utf8(\$value));
809}
810
811# set_utf8_metadata_element assumes the text has already been
812# converted to the UTF-8 encoding.
813sub set_utf8_metadata_element {
814    my $self = shift (@_);
815    my ($section, $field, $value) = @_;
816
817    $self->delete_metadata ($section, $field);
818    $self->add_utf8_metadata ($section, $field, $value);
819}
820
821
822# add_metadata assumes the text is in (extended) ascii form. For
823# text which has already been converted to the UTF-8 format use
824# add_utf8_metadata.
825sub add_metadata {
826    my $self = shift (@_);
827    my ($section, $field, $value) = @_;
828
829    $self->add_utf8_metadata ($section, $field,
830                  &unicode::ascii2utf8(\$value));
831}
832
833sub add_utf8_metadata {
834    my $self = shift (@_);
835    my ($section, $field, $value) = @_;
836
837    my $section_ptr = $self->_lookup_section($section);
838    if (!defined $section_ptr) {
839    print STDERR "doc::add_utf8_metadata couldn't find section ", $section, "\n";
840    return;
841    }
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    }
850   
851    #print STDERR "###$field=$value\n";
852    # double check that the value is utf-8
853    if (!&unicode::check_is_utf8($value)) {
854    print STDERR "doc::add_utf8_metadata - warning: '$field''s value $value wasn't utf8.";
855    &unicode::ensure_utf8(\$value);
856    print STDERR " Tried converting to utf8: $value\n";
857    }
858
859    push (@{$section_ptr->{'metadata'}}, [$field, $value]);
860}
861
862
863# methods for dealing with text
864
865# returns the text for a section
866sub 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
881sub 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
895sub 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
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.
912sub add_text {
913    my $self = shift (@_);
914    my ($section, $text) = @_;
915
916    # convert the text to UTF-8 encoded unicode characters
917    # and add the text
918    $self->add_utf8_text($section, &unicode::ascii2utf8(\$text));
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
925sub add_utf8_text {
926    my $self = shift (@_);
927    my ($section, $text) = @_;
928
929    my $section_ptr = $self->_lookup_section($section);
930    if (!defined $section_ptr) {
931    print STDERR "doc::add_utf8_text couldn't find section " .
932        "$section\n";
933    return;
934    }
935
936    $section_ptr->{'text'} .= $text;
937}
938
939# returns the Source meta, which is the utf8 filename generated.
940# Added a separate method here for convenience
941sub get_source {
942    my $self = shift (@_);
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
948sub 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.
955sub get_assocfile_from_sourcefile {
956    my $self = shift (@_);
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");
961
962    # get the actual filename as it exists on the filesystem which this url refers to
963    $source_file = &unicode::url_to_filename($source_file);
964    my ($assocfilename) = $source_file =~ /([^\\\/]+)$/;
965    return $assocfilename;
966}
967
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)
974sub 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);
981   
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], ...]
988sub get_assoc_files {
989    my $self = shift (@_);
990
991    return $self->{'associated_files'};
992}
993
994
995sub metadata_file {
996    my $self = shift (@_);
997    my ($real_filename, $filename) = @_;
998   
999    push (@{$self->{'metadata_files'}},
1000      [$real_filename, $filename]);
1001}
1002
1003sub get_meta_files {
1004    my $self = shift (@_);
1005
1006    return $self->{'metadata_files'};
1007}
1008
1009sub 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
1024sub 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
1038sub 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
10461;
Note: See TracBrowser for help on using the browser.