anonok is safe
[ikiwiki] / IkiWiki / Plugin / postsparkline.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::postsparkline;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 sub import { #{{{
9         IkiWiki::loadplugin('sparkline');
10         hook(type => "preprocess", id => "postsparkline", call => \&preprocess);
11 } # }}}
12
13 sub preprocess (@) { #{{{
14         my %params=@_;
15
16         if (! exists $params{max}) {
17                 $params{max}=100;
18         }
19
20         if (! exists $params{pages}) {
21                 return "";
22         }
23
24         if (! exists $params{time} || $params{time} ne 'mtime') {
25                 $params{timehash} = \%IkiWiki::pagectime;
26         }
27         else {
28                 $params{timehash} = \%IkiWiki::pagemtime;
29         }
30
31         if (! exists $params{formula}) {
32                 error gettext("missing formula")
33         }
34         my $formula=$params{formula};
35         $formula=~s/[^a-zA-Z0-9]*//g;
36         $formula=IkiWiki::possibly_foolish_untaint($formula);
37         if (! length $formula ||
38             ! IkiWiki::Plugin::postsparkline::formula->can($formula)) {
39                 error gettext("unknown formula");
40         }
41
42         add_depends($params{page}, $params{pages});
43
44         my @list;
45         foreach my $page (keys %pagesources) {
46                 next if $page eq $params{page};
47                 if (pagespec_match($page, $params{pages}, location => $params{page})) {
48                         push @list, $page;
49                 }
50         }
51         
52         @list = sort { $params{timehash}->{$b} <=> $params{timehash}->{$a} } @list;
53
54         my @data=eval qq{IkiWiki::Plugin::postsparkline::formula::$formula(\\\%params, \@list)};
55         if ($@) {
56                 error $@;
57         }
58
59         if (! @data) {
60                 # generate an empty graph
61                 push @data, 0 foreach 1..($params{max} / 2);
62         }
63
64         my $color=exists $params{color} ? "($params{color})" : "";
65
66         delete $params{pages};
67         delete $params{formula};
68         delete $params{ftime};
69         delete $params{color};
70         return IkiWiki::Plugin::sparkline::preprocess(%params, 
71                 map { $_.$color => "" } reverse @data);
72 } # }}}
73
74 sub perfoo ($@) {
75         my $sub=shift;
76         my $params=shift;
77         
78         my $max=$params->{max};
79         my ($first, $prev, $cur);
80         my $count=0;
81         my @data;
82         foreach (@_) {
83                 $cur=$sub->($params->{timehash}->{$_});
84                 if (defined $prev) {
85                         if ($prev != $cur) {
86                                 push @data, "$prev,$count";
87                                 $count=0;
88                                 last if --$max <= 0;
89
90                                 for ($cur+1 .. $prev-1) {
91                                         push @data, "$_,0";
92                                         last if --$max == 0;
93                                 }
94                         }
95                 }
96                 else {
97                         $first=$cur;
98                 }
99                 $count++;
100                 $prev=$cur;
101         }
102
103         return @data;
104 }
105
106 package IkiWiki::Plugin::postsparkline::formula;
107
108 sub peryear (@) {
109         return IkiWiki::Plugin::postsparkline::perfoo(sub {
110                 return (localtime $_[0])[5];
111         }, @_);
112 }
113
114 sub permonth (@) {
115         return IkiWiki::Plugin::postsparkline::perfoo(sub {
116                 my ($month, $year)=(localtime $_[0])[4,5];
117                 return $year*12+$month;
118         }, @_);
119 }
120
121 sub perday (@) {
122         return IkiWiki::Plugin::postsparkline::perfoo(sub {
123                 my ($year, $yday)=(localtime $_[0])[5,7];
124                 return $year*365+$yday;
125         }, @_);
126 }
127
128 sub interval ($@) {
129         my $params=shift;
130
131         my $max=$params->{max};
132         my @data;
133         for (my $i=1; $i < @_; $i++) {
134                 push @data, $params->{timehash}->{$_[$i-1]} - $params->{timehash}->{$_[$i]};
135                 last if --$max <= 0;
136         }
137         return @data;
138 }
139
140 1