]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/postsparkline.pm
Allow the openid plugin to be loaded but disabled, for its side-effect of defining...
[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                 },
20 }
22 sub preprocess (@) {
23         my %params=@_;
25         if (! exists $params{max}) {
26                 $params{max}=100;
27         }
29         if (! exists $params{pages}) {
30                 return "";
31         }
33         if (! exists $params{time} || $params{time} ne 'mtime') {
34                 $params{timehash} = \%IkiWiki::pagectime;
35         }
36         else {
37                 $params{timehash} = \%IkiWiki::pagemtime;
38         }
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         }
51         add_depends($params{page}, $params{pages});
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});
58         my @data=eval qq{IkiWiki::Plugin::postsparkline::formula::$formula(\\\%params, \@list)};
59         if ($@) {
60                 error $@;
61         }
63         if (! @data) {
64                 # generate an empty graph
65                 push @data, 0 foreach 1..($params{max} / 2);
66         }
68         my $color=exists $params{color} ? "($params{color})" : "";
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 }
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;
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         }
107         return @data;
110 package IkiWiki::Plugin::postsparkline::formula;
112 sub peryear (@) {
113         return IkiWiki::Plugin::postsparkline::perfoo(sub {
114                 return (localtime $_[0])[5];
115         }, @_);
118 sub permonth (@) {
119         return IkiWiki::Plugin::postsparkline::perfoo(sub {
120                 my ($month, $year)=(localtime $_[0])[4,5];
121                 return $year*12+$month;
122         }, @_);
125 sub perday (@) {
126         return IkiWiki::Plugin::postsparkline::perfoo(sub {
127                 my ($year, $yday)=(localtime $_[0])[5,7];
128                 return $year*365+$yday;
129         }, @_);
132 sub interval ($@) {
133         my $params=shift;
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;