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 => "getsetup", id => "pagestats", call => \&getsetup);
22 hook(type => "preprocess", id => "pagestats", call => \&preprocess);
36 $params{pages}="*" unless defined $params{pages};
37 my $style = ($params{style} or 'cloud');
39 # Backwards compatibility
40 if (defined $params{show} && $params{show} =~ m/^\d+$/) {
41 $params{limit} = $params{show};
47 foreach my $page (pagespec_match_list($params{page}, $params{pages},
48 # update when a displayed page is added/removed
49 deptype => deptype("presence"))) {
52 my @backlinks = IkiWiki::backlink_pages($page);
54 if (exists $params{among}) {
55 # only consider backlinks from the amoung pages
56 @backlinks = pagespec_match_list(
57 $params{page}, $params{among},
58 # update whenever links on those pages change
59 deptype => deptype("links"),
64 # update when any page with links changes,
65 # in case the links point to our displayed pages
66 add_depends($params{page}, "*", deptype("links"));
69 $counts{$page} = scalar(@backlinks);
70 $max = $counts{$page} if $counts{$page} > $max;
73 if (exists $params{limit}) {
76 foreach my $key (sort { $counts{$b} <=> $counts{$a} } keys %counts) {
77 last if ++$i > $params{limit};
78 $show{$key}=$counts{$key};
83 if ($style eq 'table') {
84 return "<table class='".(exists $params{class} ? $params{class} : "pageStats")."'>\n".
87 htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
88 "</td><td>".$counts{$_}."</td></tr>"
90 sort { $counts{$b} <=> $counts{$a} } keys %counts).
94 # In case of misspelling, default to a page cloud
97 if ($style eq 'list') {
98 $res = "<ul class='".(exists $params{class} ? $params{class} : "list")."'>\n";
101 $res = "<div class='".(exists $params{class} ? $params{class} : "pagecloud")."'>\n";
103 foreach my $page (sort keys %counts) {
104 next unless $counts{$page} > 0;
106 my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
108 $res.="<li>" if $style eq 'list';
109 $res .= "<span class=\"$class\">".
110 htmllink($params{page}, $params{destpage}, $page).
112 $res.="</li>" if $style eq 'list';
115 if ($style eq 'list') {