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