]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/pagestats.pm
need to make it really utf8 or the url encode won't work
[ikiwiki.git] / IkiWiki / Plugin / pagestats.pm
1 #!/usr/bin/perl
2 #
3 # Produce page statistics in various forms.
4 #
5 # Currently supported:
6 #   cloud: produces statistics in the form of a del.icio.us-style tag cloud
7 #          (default)
8 #   table: produces a table with the number of backlinks for each page
9 #
10 # By Enrico Zini.
11 package IkiWiki::Plugin::pagestats;
12
13 use warnings;
14 use strict;
15 use IkiWiki;
16
17 # Names of the HTML classes to use for the tag cloud
18 our @classes = ('smallestPC', 'smallPC', 'normalPC', 'bigPC', 'biggestPC' );
19
20 sub import { #{{{
21         IkiWiki::hook(type => "preprocess", id => "pagestats",
22                 call => \&preprocess);
23 } # }}}
24
25 sub preprocess (@) { #{{{
26         my %params=@_;
27         $params{pages}="*" unless defined $params{pages};
28         my $style = ($params{style} or 'cloud');
29         
30         # Needs to update whenever a page is added or removed, so
31         # register a dependency.
32         IkiWiki::add_depends($params{page}, $params{pages});
33         
34         my %counts;
35         my $max = 0;
36         foreach my $page (keys %IkiWiki::links) {
37                 if (IkiWiki::pagespec_match($page, $params{pages})) {
38                         my @bl = IkiWiki::backlinks($page);
39                         $counts{$page} = scalar(@bl);
40                         $max = $counts{$page} if $counts{$page} > $max;
41                 }
42         }
43
44         if ($style eq 'table') {
45                 return "<table class='pageStats'>\n".
46                         join("\n", map {
47                                 "<tr><td>".
48                                 IkiWiki::htmllink($params{page}, $params{destpage}, $_, 1).
49                                 "</td><td>".$counts{$_}."</td></tr>"
50                         }
51                         sort { $counts{$b} <=> $counts{$a} } keys %counts).
52                         "\n</table>\n" ;
53         } else {
54                 # In case of misspelling, default to a page cloud
55
56                 my $res = "<div class='pagecloud'>\n";
57                 foreach my $page (sort keys %counts) {
58                         my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
59                         $res .= "<span class=\"$class\">".
60                                 IkiWiki::htmllink($params{page}, $params{destpage}, $page).
61                                 "</span>\n";
62                 }
63                 $res .= "</div>\n";
64
65                 return $res;
66         }
67 } # }}}
68
69 1