]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/postsparkline.pm
6f7558bc44d2375afcfb3a6a38a7680fe3ffbb1b
[ikiwiki.git] / 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                 return "[[postsparkline ".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                 return "[[postsparkline ".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                 return "[[postsparkline error $@]]";
57         }
58         delete $params{pages};
59         delete $params{formula};
60         delete $params{ftime};
61         return IkiWiki::Plugin::sparkline::preprocess(%params, 
62                 map { $_ => "" } reverse @data);
63 } # }}}
64
65 sub perfoo ($@) {
66         my $sub=shift;
67         my $params=shift;
68         
69         my $max=$params->{max};
70         my ($first, $prev, $cur);
71         my $count=0;
72         my @data;
73         foreach (@_) {
74                 $cur=$sub->($params->{timehash}->{$_});
75                 if (defined $prev) {
76                         if ($prev != $cur) {
77                                 push @data, "$prev,$count";
78                                 $count=0;
79                                 last if --$max <= 0;
80
81                                 for ($cur+1 .. $prev-1) {
82                                         push @data, "$_,0";
83                                         last if --$max == 0;
84                                 }
85                         }
86                 }
87                 else {
88                         $first=$cur;
89                 }
90                 $count++;
91                 $prev=$cur;
92         }
93
94         return @data;
95 }
96
97 package IkiWiki::Plugin::postsparkline::formula;
98
99 sub peryear (@) {
100         return IkiWiki::Plugin::postsparkline::perfoo(sub {
101                 return (localtime $_[0])[5];
102         }, @_);
103 }
104
105 sub permonth (@) {
106         return IkiWiki::Plugin::postsparkline::perfoo(sub {
107                 my ($month, $year)=(localtime $_[0])[4,5];
108                 return $year*12+$month;
109         }, @_);
110 }
111
112 sub perday (@) {
113         return IkiWiki::Plugin::postsparkline::perfoo(sub {
114                 my ($year, $yday)=(localtime $_[0])[5,7];
115                 return $year*365+$yday;
116         }, @_);
117 }
118
119 sub interval ($@) {
120         my $params=shift;
121
122         my $max=$params->{max};
123         my @data;
124         for (my $i=1; $i < @_; $i++) {
125                 push @data, $params->{timehash}->{$_[$i-1]} - $params->{timehash}->{$_[$i]};
126                 last if --$max <= 0;
127         }
128         return @data;
129 }
130
131 1