]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/postsparkline.pm
removed
[git.ikiwiki.info.git] / IkiWiki / Plugin / postsparkline.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::postsparkline;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
8 sub import {
9         IkiWiki::loadplugin('sparkline');
10         hook(type => "getsetup", id => "postsparkline", call => \&getsetup);
11         hook(type => "preprocess", id => "postsparkline", call => \&preprocess);
12 }
14 sub getsetup () {
15         return 
16                 plugin => {
17                         safe => 1,
18                         rebuild => undef,
19                         section => "widget",
20                 },
21 }
23 sub preprocess (@) {
24         my %params=@_;
26         if (! exists $params{max}) {
27                 $params{max}=100;
28         }
30         if (! exists $params{pages}) {
31                 return "";
32         }
34         my $deptype;
35         if (! exists $params{time} || $params{time} ne 'mtime') {
36                 $params{timehash} = \%IkiWiki::pagectime;
37                 # need to update when pages are added or removed
38                 $deptype = deptype("presence");
39         }
40         else {
41                 $params{timehash} = \%IkiWiki::pagemtime;
42                 # need to update when pages are changed
43                 $deptype = deptype("content");
44         }
46         if (! exists $params{formula}) {
47                 error gettext("missing formula")
48         }
49         my $formula=$params{formula};
50         $formula=~s/[^a-zA-Z0-9]*//g;
51         $formula=IkiWiki::possibly_foolish_untaint($formula);
52         if (! length $formula ||
53             ! IkiWiki::Plugin::postsparkline::formula->can($formula)) {
54                 error gettext("unknown formula");
55         }
57         my @list=sort { $params{timehash}->{$b} <=> $params{timehash}->{$a} } 
58                 pagespec_match_list($params{page}, $params{pages},
59                         deptype => $deptype,
60                         filter => sub { $_[0] eq $params{page} },
61                 );
63         my @data=eval qq{IkiWiki::Plugin::postsparkline::formula::$formula(\\\%params, \@list)};
64         if ($@) {
65                 error $@;
66         }
68         if (! @data) {
69                 # generate an empty graph
70                 push @data, 0 foreach 1..($params{max} / 2);
71         }
73         my $color=exists $params{color} ? "($params{color})" : "";
75         delete $params{pages};
76         delete $params{formula};
77         delete $params{ftime};
78         delete $params{color};
79         return IkiWiki::Plugin::sparkline::preprocess(%params, 
80                 map { $_.$color => "" } reverse @data);
81 }
83 sub perfoo ($@) {
84         my $sub=shift;
85         my $params=shift;
86         
87         my $max=$params->{max};
88         my ($first, $prev, $cur);
89         my $count=0;
90         my @data;
91         foreach (@_) {
92                 $cur=$sub->($params->{timehash}->{$_});
93                 if (defined $prev) {
94                         if ($prev != $cur) {
95                                 push @data, "$prev,$count";
96                                 $count=0;
97                                 last if --$max <= 0;
99                                 for ($cur+1 .. $prev-1) {
100                                         push @data, "$_,0";
101                                         last if --$max == 0;
102                                 }
103                         }
104                 }
105                 else {
106                         $first=$cur;
107                 }
108                 $count++;
109                 $prev=$cur;
110         }
112         return @data;
115 package IkiWiki::Plugin::postsparkline::formula;
117 sub peryear (@) {
118         return IkiWiki::Plugin::postsparkline::perfoo(sub {
119                 return (localtime $_[0])[5];
120         }, @_);
123 sub permonth (@) {
124         return IkiWiki::Plugin::postsparkline::perfoo(sub {
125                 my ($month, $year)=(localtime $_[0])[4,5];
126                 return $year*12+$month;
127         }, @_);
130 sub perday (@) {
131         return IkiWiki::Plugin::postsparkline::perfoo(sub {
132                 my ($year, $yday)=(localtime $_[0])[5,7];
133                 return $year*365+$yday;
134         }, @_);
137 sub interval ($@) {
138         my $params=shift;
140         my $max=$params->{max};
141         my @data;
142         for (my $i=1; $i < @_; $i++) {
143                 push @data, $params->{timehash}->{$_[$i-1]} - $params->{timehash}->{$_[$i]};
144                 last if --$max <= 0;
145         }
146         return @data;