3 # Produce page statistics in various forms.
6 # cloud: produces statistics in the form of a del.icio.us-style tag cloud
8 # table: produces a table with the number of backlinks for each page
11 package IkiWiki::Plugin::pagestats;
17 # Names of the HTML classes to use for the tag cloud
18 our @classes = ('smallestPC', 'smallPC', 'normalPC', 'bigPC', 'biggestPC' );
21 hook(type => "preprocess", id => "pagestats", call => \&preprocess);
24 sub preprocess (@) { #{{{
26 $params{pages}="*" unless defined $params{pages};
27 my $style = ($params{style} or 'cloud');
29 # Needs to update whenever a page is added or removed, so
30 # register a dependency.
31 add_depends($params{page}, $params{pages});
35 foreach my $page (keys %links) {
36 if (pagespec_match($page, $params{pages}, location => $params{page})) {
38 $counts{$page} = scalar(IkiWiki::backlinks($page));
39 $max = $counts{$page} if $counts{$page} > $max;
43 if ($style eq 'table') {
44 return "<table class='pageStats'>\n".
47 htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
48 "</td><td>".$counts{$_}."</td></tr>"
50 sort { $counts{$b} <=> $counts{$a} } keys %counts).
53 # In case of misspelling, default to a page cloud
55 my $res = "<div class='pagecloud'>\n";
56 foreach my $page (sort keys %counts) {
57 my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
58 $res .= "<span class=\"$class\">".
59 htmllink($params{page}, $params{destpage}, $page).