Changeset 6970


Ignore:
Timestamp:
2004-03-08T16:27:43+13:00 (20 years ago)
Author:
kjdon
Message:

rewrote this so it handles a comma separated list of metadata. this was really old, and seemed to have a -section option - maybe this should be added back in as a proper option to the classifier?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/perllib/classify/SectionList.pm

    r6408 r6970  
    4444        'inherits' => "yes" };
    4545
    46 #  sub print_usage {
    47 #      print STDERR "
    48 #    usage: classify SectionList [options]
    49 #    options:
    50 
    51 #     -metadata X    Metadata field used for classification,
    52 #                    list will be sorted by this element.
    53 
    54 #     -buttonname X  (optional) Title field for this classification.
    55 #                    if not included title field will be Metaname.
    56 
    57 #     -sort X        (optional) Sort documents in list by this metadata field.
    58 #                    By default it will sort by Metaname, or (if this is not
    59 #                    set) in build (random) order.
    60 
    61 #  Same as List classifier but includes all sections of document
    62 #  (excluding top level) rather than just top level document.
    63 #  ";
    64 #  }
    6546
    6647sub new {
     
    7253    push( @{$option_list}, $options );
    7354
     55    #if ($self->{'info_only'}) {
     56    # created from classinfo.pl - don't need to parse the arguments
     57    #   return bless $self, $class;
     58    #}
    7459    return bless $self, $class;
    7560}
     
    7762sub classify {
    7863    my $self = shift (@_);
    79     my ($doc_obj, @options) = @_;
     64    my ($doc_obj) = @_;
    8065   
    81     my $thissection = undef;
    82 
    83     my $option;
    84     foreach $option (@options)
    85     {
    86     if ($option =~ m/^section=(\d+)$/i)
    87     {
    88         $thissection = $1;
    89     }
     66    # are we sorting the list??
     67    my $nosort = 0;
     68    if (defined $self->{'sortname'} && $self->{'sortname'} eq "nosort") {
     69    $nosort = 1;
    9070    }
    9171
     72    # do we want to put -section as an option for the classifier???
     73    #my $thissection = undef;
     74
     75    #my $option;
     76    #foreach $option (@options)
     77    #{
     78#   if ($option =~ m/^section=(\d+)$/i)
     79#   {
     80#       $thissection = $1;
     81#   }
     82#    }
     83
    9284    my $sortmeta = "";
    93     if (defined $self->{'sortname'}) {
     85    if (!$nosort && defined $self->{'sortname'}) {
    9486    if ($self->{'sortname'} =~ /^filename$/i) {
    9587        $sortmeta = $doc_obj->get_source_filename();
    9688    } else {
    97         $sortmeta = $doc_obj->get_metadata_element($doc_obj->get_top_section(),
    98                                $self->{'sortname'});
    99         if ($self->{'sortname'} eq "Creator") {
    100         &sorttools::format_string_name_english (\$sortmeta);
    101         } else {
    102         &sorttools::format_string_english (\$sortmeta);
     89        $sortmeta = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $self->{'sortname'});
     90        if (defined $sortmeta) {
     91        $sortmeta = $self->format_metadata_for_sorting($self->{'sortname'}, $sortmeta, $doc_obj);
    10392        }
    10493    }
     
    10695    }
    10796
    108     if (!defined $thissection)
    109     {
    110     $thissection = $doc_obj->get_next_section ($doc_obj->get_top_section());
    111     while (defined $thissection) {
    112         $self->classify_section($thissection, $doc_obj, $sortmeta);
    113         $thissection = $doc_obj->get_next_section ($thissection);
    114     }
     97 #   if (defined $thissection) {
     98#   $self->classify_section($thissection, $doc_obj, $sortmeta);
     99#    } else   
     100 #   {
     101    $thissection = $doc_obj->get_next_section ($doc_obj->get_top_section());
     102    while (defined $thissection) {
     103    $self->classify_section($thissection, $doc_obj, $nosort, $sortmeta);
     104    $thissection = $doc_obj->get_next_section ($thissection);
    115105    }
    116     else
    117     {
    118     $self->classify_section($thissection, $doc_obj, $sortmeta);
    119     }
     106    #}
    120107}
    121108
    122109sub classify_section {
    123110    my $self = shift (@_);
    124     my ($section, $doc_obj, $sortmeta) = @_;
     111    my ($section, $doc_obj, $nosort, $sortmeta) = @_;
    125112
    126113    my $doc_OID = $doc_obj->get_OID();
     114    $nosort = 0 unless defined $nosort;
    127115    $sortmeta = "" unless defined $sortmeta;
    128116
     117    my $metavalue;
     118    my $metaname;
     119    if (defined $self->{'meta_list'}) {
     120    # find the first available metadata
     121    foreach $m (@{$self->{'meta_list'}}) {
     122        $metavalue = $doc_obj->get_metadata_element($section, $m);
     123        $metaname = $m;
     124        last if defined $metavalue;
     125    }
     126    #if we haven't found a metavalue here, then the section shouldn't be included
     127    return unless defined $metavalue;
     128    }
     129   
     130    # we know the section should be included, add it now if we are not sorting
     131    if ($nosort) {
     132    push (@{$self->{'list'}}, "$doc_OID.$section");
     133    return;
     134    }
     135    # check that it hasn't been added already
     136    if (defined $self->{'list'}->{"$doc_OID.$section"}) {
     137    my $outhandle = $self->{'outhandle'};
     138    print $outhandle "WARNING: SectionList::classify called multiple times for $doc_OID.$section\n";
     139    }
     140
    129141    if (defined $self->{'sortname'}) {
    130     if (defined $self->{'metaname'}) {
    131 
    132         my $metavalue = $doc_obj->get_metadata_element ($section, $self->{'metaname'});
    133         if (defined $metavalue) {
    134         if (defined $self->{'list'}->{"$doc_OID.$section"}) {
    135             my $outhandle = $self->{'outhandle'};
    136             print $outhandle "WARNING: SectionList::classify called multiple times for $doc_OID.$section\n";
    137         }
    138         $self->{'list'}->{"$doc_OID.$section"} = $sortmeta;
    139         }
    140     } else {
    141         if (defined $self->{'list'}->{"$doc_OID.$section"}) {
    142         my $outhandle = $self->{'outhandle'};
    143         print $outhandle "WARNING: SectionList::classify called multiple times for $doc_OID.$section\n";
    144         }
    145         $self->{'list'}->{"$doc_OID.$section"} = $sortmeta;
    146     }
    147 
     142    # sorting on alternative metadata
     143    $self->{'list'}->{"$doc_OID.$section"} = $sortmeta;
    148144    } else {
    149     if (defined $self->{'metaname'}) {
    150 
    151         my $metavalue = $doc_obj->get_metadata_element ($section, $self->{'metaname'});
    152         if (defined $metavalue) {
    153         push (@{$self->{'list'}}, "$doc_OID.$section");
    154         }
    155     } else {
    156         push (@{$self->{'list'}}, "$doc_OID.$section");
    157     }
     145    # sortingon the classification metadata
     146    # do the same formatting on the meta value as for sort meta
     147    $metavalue = $self->format_metadata_for_sorting($metaname, $metavalue, $doc_obj);
     148    $self->{'list'}->{"$doc_OID.$section"} = $metavalue;
    158149    }
    159150}
    160 
    1611511;
Note: See TracChangeset for help on using the changeset viewer.