Changeset 2632


Ignore:
Timestamp:
2001-07-09T19:14:52+12:00 (23 years ago)
Author:
jrm21
Message:

added an option "-bymonth=1", to group by (eg) 2000-January, 2000-February etc.
It actually uses the _textmonth_ macros, so the month is in the correct
language. This is only until Dana gets proper classification hierarchy stuff
going at run-time.

File:
1 edited

Legend:

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

    r2503 r2632  
    2626# classifier plugin for sorting by date
    2727
    28 # no options - always sorts by 'Date' metadata
     28# always sorts by 'Date' metadata
    2929
    3030# date is assumed to be in the form yyyymmdd
    3131
    3232# at present dates are split by year - this should change
     33# jrm21 - added option "bymonth", which splits by year and month.
    3334
    3435package DateList;
     
    4344sub print_usage {
    4445    print STDERR "
    45   usage: classify DateList
    46 
    47 Classifier plugin for sorting by date.
    48 No options - always sorts by 'Date' metadata
    49 Date is assumed to be in the form yyyymmdd
    50 At present dates are split by year - this should change
    51 
    52 Any errors are Dana's problem.
     46  usage: classify DateList [options]
     47  options:
     48    -bymonth  [or bymonth=1]    Classify by year and month
     49
     50    Classifier plugin for sorting by date.
     51    Always sorts by 'Date' metadata.
     52    Date is assumed to be in the form yyyymmdd (all digits).
     53    By default dates are split by year - this should change.
     54
     55    Any errors are Dana's problem.
    5356";
    5457}
     
    5962
    6063    $self->{'list'} = {};
    61 
     64    if (!parsargv::parse(\@_,
     65                         q^bymonth^, \$self->{'bymonth'},
     66                         "allow_extra_options")) {
     67        &print_usage();
     68        die "\n";
     69    }
    6270    return bless $self, $class;
    6371}
     
    111119}
    112120
    113 # splitlist takes an ordered list of classifications (@$classlistref) and splits it
    114 # up into sub-sections by date
     121# splitlist takes an ordered list of classifications (@$classlistref) and
     122# splits it up into sub-sections by date
    115123sub splitlist {
    116124    my $self = shift (@_);
     
    131139    }
    132140
    133     # first split up the list into separate year classifications
    134     foreach $classification (@$classlistref) {
    135     my $date = $self->{'list'}->{$classification};
    136     $date =~ s/^(\d\d\d\d).*$/$1/;
    137     $classhash->{$date} = [] unless defined $classhash->{$date};
    138     push (@{$classhash->{$date}}, $classification);
    139     }
    140 
     141
     142    if ($self->{'bymonth'}) {
     143    # first split up the list into separate year+month classifications
     144    foreach $classification (@$classlistref) {
     145        my $date = $self->{'list'}->{$classification};
     146        $date =~ s/^(\d\d\d\d)(\d\d).*$/$1 _textmonth$2_/;
     147        # sanity check if month is zero
     148        if ($date =~ /00_$/) {
     149        $date =~ s/^(\d\d\d\d).*$/$1/g;
     150        }
     151        $classhash->{$date} = [] unless defined $classhash->{$date};
     152        push (@{$classhash->{$date}}, $classification);
     153    }
     154    } else {
     155    # first split up the list into separate year classifications
     156    foreach $classification (@$classlistref) {
     157        my $date = $self->{'list'}->{$classification};
     158        $date =~ s/^(\d\d\d\d).*$/$1/;
     159        $classhash->{$date} = [] unless defined $classhash->{$date};
     160        push (@{$classhash->{$date}}, $classification);
     161    }
     162    }
    141163    $classhash = $self->compactlist ($classhash);
    142164
     
    157179    my $compactedhash = {};
    158180    my @currentOIDs = ();
    159     my $currentfirstletter = "";
    160     my $currentlastletter = "";
     181    my $currentfirstdate = "";
     182    my $currentlastdate = "";
    161183    my $lastkey = "";
    162184
     
    168190    my $min = 10;
    169191    my $max = 30;
    170 
    171     foreach $subsection (sort keys %$classhashref) {
    172     $currentfirstletter = $subsection if $currentfirstletter eq "";
     192    foreach my $subsection (sort keys %$classhashref) {
     193    $currentfirstdate = $subsection if $currentfirstdate eq "";
    173194    if ((scalar (@currentOIDs) < $min) ||
    174195        ((scalar (@currentOIDs) + scalar (@{$classhashref->{$subsection}})) <= $max)) {
    175196        push (@currentOIDs, @{$classhashref->{$subsection}});
    176         $currentlastletter = $subsection;
     197        $currentlastdate = $subsection;
    177198    } else {
    178199
    179         if ($currentfirstletter eq $currentlastletter) {
    180         @{$compactedhash->{$currentfirstletter}} = @currentOIDs;
    181         $lastkey = $currentfirstletter;
     200        if ($currentfirstdate eq $currentlastdate) {
     201        @{$compactedhash->{$currentfirstdate}} = @currentOIDs;
     202        $lastkey = $currentfirstdate;
    182203        } else {
    183         @{$compactedhash->{"$currentfirstletter-$currentlastletter"}} = @currentOIDs;
    184         $lastkey = "$currentfirstletter-$currentlastletter";
     204        @{$compactedhash->{"$currentfirstdate-$currentlastdate"}} = @currentOIDs;
     205        $lastkey = "$currentfirstdate-$currentlastdate";
    185206        }
    186207        if (scalar (@{$classhashref->{$subsection}}) >= $max) {
    187208        $compactedhash->{$subsection} = $classhashref->{$subsection};
    188209        @currentOIDs = ();
    189         $currentfirstletter = "";
     210        $currentfirstdate = "";
    190211        $lastkey = $subsection;
    191212        } else {
    192213        @currentOIDs = @{$classhashref->{$subsection}};
    193         $currentfirstletter = $subsection;
    194         $currentlastletter = $subsection;
     214        $currentfirstdate = $subsection;
     215        $currentlastdate = $subsection;
    195216        }
    196217    }
     
    203224    @currentOIDs = (@{$compactedhash->{$lastkey}}, @currentOIDs);
    204225    delete $compactedhash->{$lastkey};
    205     @{$compactedhash->{"$newkey-$currentlastletter"}} = @currentOIDs;   
     226    @{$compactedhash->{"$newkey-$currentlastdate"}} = @currentOIDs;
    206227    } else {
    207     if ($currentfirstletter eq $currentlastletter) {
    208         @{$compactedhash->{$currentfirstletter}} = @currentOIDs;
     228    if ($currentfirstdate eq $currentlastdate) {
     229        @{$compactedhash->{$currentfirstdate}} = @currentOIDs;
    209230    } else {
    210         @{$compactedhash->{"$currentfirstletter-$currentlastletter"}} = @currentOIDs;
     231        @{$compactedhash->{"$currentfirstdate-$currentlastdate"}} = @currentOIDs;
    211232    }
    212233    }
Note: See TracChangeset for help on using the changeset viewer.