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 IkiWiki::hook(type => "preprocess", id => "pagestats",
22 call => \&preprocess);
25 sub preprocess (@) { #{{{
27 $params{pages}="*" unless defined $params{pages};
28 my $style = ($params{style} or 'cloud');
30 # Needs to update whenever a page is added or removed, so
31 # register a dependency.
32 IkiWiki::add_depends($params{page}, $params{pages});
36 foreach my $page (%IkiWiki::links) {
37 if (IkiWiki::globlist_match($page, $params{pages})) {
38 my @bl = IkiWiki::backlinks($page);
39 $counts{$page} = scalar(@bl);
40 $max = $counts{$page} if $counts{$page} > $max;
44 if ($style eq 'table') {
45 return "<table class='pageStats'>\n".join("\n", map { "<tr><td>$_</td><td>".$counts{$_}."</td></tr>" }
46 sort { $counts{$b} <=> $counts{$a} } keys %counts)."\n</table>\n" ;
48 # In case of misspelling, default to a page cloud
50 my $res = "<div class='pagecloud'>\n";
51 foreach my $page (sort keys %counts) {
52 my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
53 $res .= "<span class=\"$class\">".
54 IkiWiki::htmllink($params{page}, $params{destpage}, $page).