lib/TWiki/Contrib/MailerContrib/CompatibilityHacks.pm
author Colas Nahaboo <colas@nahaboo.net>
Mon, 11 Aug 2008 20:33:37 +0200
changeset 2 7bc60a767fa4
parent 0 414e01d06fd5
permissions -rw-r--r--
TWiki-4.2.2 Release
     1 # Hack for older TWiki versions
     2 package CompatibilityHacks;
     3 
     4 package IteratorHack;
     5 
     6 sub new {
     7     my ($class, $list) = @_;
     8     my $this = bless({list => $list, index => 0, next => undef }, $class);
     9     return $this;
    10 }
    11 
    12 sub hasNext {
    13     my( $this ) = @_;
    14     return 1 if $this->{next};
    15     if( $this->{index} < scalar(@{$this->{list}}) ) {
    16         $this->{next} = $this->{list}->[$this->{index}++];
    17         return 1;
    18     }
    19     return 0;
    20 }
    21 
    22 sub next {
    23     my $this = shift;
    24     $this->hasNext();
    25     my $n = $this->{next};
    26     $this->{next} = undef;
    27     return $n;
    28 }
    29 
    30 package TWiki::Func;
    31 
    32 sub eachChangeSince {
    33     my ($web, $since) = @_;
    34 
    35     my $changes;
    36     if( open(F, "<$TWiki::cfg{DataDir}/$web/.changes")) {
    37         local $/ = undef;
    38         $changes = <F>;
    39         close(F);
    40     }
    41 
    42     $changes ||= '';
    43 
    44     my @changes =
    45       map {
    46           # Create a hash for this line
    47           { topic => $_->[0], user => $_->[1], time => $_->[2],
    48               revision => $_->[3], more => $_->[4] };
    49       }
    50         grep {
    51             # Filter on time
    52             $_->[2] && $_->[2] >= $since
    53         }
    54           map {
    55               # Split line into an array
    56               my @row = split(/\t/, $_, 5);
    57               \@row;
    58           }
    59             reverse split( /[\r\n]+/, $changes);
    60 
    61     return new IteratorHack( \@changes );
    62 }
    63 
    64 1;