]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/postsparkline.pm
linkmap: Add option to omit disconnected pages from the map.
[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 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         my $deptype;
34         if (! exists $params{time} || $params{time} ne 'mtime') {
35                 $params{timehash} = \%IkiWiki::pagectime;
36                 # need to update when pages are added or removed
37                 $deptype = deptype("presence");
38         }
39         else {
40                 $params{timehash} = \%IkiWiki::pagemtime;
41                 # need to update when pages are changed
42                 $deptype = deptype("content");
43         }
44
45         if (! exists $params{formula}) {
46                 error gettext("missing formula")
47         }
48         my $formula=$params{formula};
49         $formula=~s/[^a-zA-Z0-9]*//g;
50         $formula=IkiWiki::possibly_foolish_untaint($formula);
51         if (! length $formula ||
52             ! IkiWiki::Plugin::postsparkline::formula->can($formula)) {
53                 error gettext("unknown formula");
54         }
55
56         my @list=sort { $params{timehash}->{$b} <=> $params{timehash}->{$a} } 
57                 pagespec_match_list($params{page}, $params{pages},
58                         deptype => $deptype,
59                         filter => sub { $_[0] eq $params{page} },
60                 );
61
62         my @data=eval qq{IkiWiki::Plugin::postsparkline::formula::$formula(\\\%params, \@list)};
63         if ($@) {
64                 error $@;
65         }
66
67         if (! @data) {
68                 # generate an empty graph
69                 push @data, 0 foreach 1..($params{max} / 2);
70         }
71
72         my $color=exists $params{color} ? "($params{color})" : "";
73
74         delete $params{pages};
75         delete $params{formula};
76         delete $params{ftime};
77         delete $params{color};
78         return IkiWiki::Plugin::sparkline::preprocess(%params, 
79                 map { $_.$color => "" } reverse @data);
80 }
81
82 sub perfoo ($@) {
83         my $sub=shift;
84         my $params=shift;
85         
86         my $max=$params->{max};
87         my ($first, $prev, $cur);
88         my $count=0;
89         my @data;
90         foreach (@_) {
91                 $cur=$sub->($params->{timehash}->{$_});
92                 if (defined $prev) {
93                         if ($prev != $cur) {
94                                 push @data, "$prev,$count";
95                                 $count=0;
96                                 last if --$max <= 0;
97
98                                 for ($cur+1 .. $prev-1) {
99                                         push @data, "$_,0";
100                                         last if --$max == 0;
101                                 }
102                         }
103                 }
104                 else {
105                         $first=$cur;
106                 }
107                 $count++;
108                 $prev=$cur;
109         }
110
111         return @data;
112 }
113
114 package IkiWiki::Plugin::postsparkline::formula;
115
116 sub peryear (@) {
117         return IkiWiki::Plugin::postsparkline::perfoo(sub {
118                 return (localtime $_[0])[5];
119         }, @_);
120 }
121
122 sub permonth (@) {
123         return IkiWiki::Plugin::postsparkline::perfoo(sub {
124                 my ($month, $year)=(localtime $_[0])[4,5];
125                 return $year*12+$month;
126         }, @_);
127 }
128
129 sub perday (@) {
130         return IkiWiki::Plugin::postsparkline::perfoo(sub {
131                 my ($year, $yday)=(localtime $_[0])[5,7];
132                 return $year*365+$yday;
133         }, @_);
134 }
135
136 sub interval ($@) {
137         my $params=shift;
138
139         my $max=$params->{max};
140         my @data;
141         for (my $i=1; $i < @_; $i++) {
142                 push @data, $params->{timehash}->{$_[$i-1]} - $params->{timehash}->{$_[$i]};
143                 last if --$max <= 0;
144         }
145         return @data;
146 }
147
148 1