]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/pagestats.pm
make success and failreason objects carry an influences hash
[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 3.00;
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         hook(type => "getsetup", id => "pagestats", call => \&getsetup);
22         hook(type => "preprocess", id => "pagestats", call => \&preprocess);
23 }
24
25 sub getsetup () {
26         return 
27                 plugin => {
28                         safe => 1,
29                         rebuild => undef,
30                 },
31 }
32
33 sub preprocess (@) {
34         my %params=@_;
35         $params{pages}="*" unless defined $params{pages};
36         my $style = ($params{style} or 'cloud');
37         
38         # Needs to update whenever a page is added or removed.
39         add_depends($params{page}, $params{pages}, exists => 1);
40         # Also needs to update when any page with links changes, 
41         # in case the links point to our displayed pages.
42         # (Among limits this further.)
43         add_depends($params{page}, exists $params{among} ? $params{among} : "*",
44                 links => 1); 
45         
46         my %counts;
47         my $max = 0;
48         foreach my $page (pagespec_match_list([keys %pagesources],
49                         $params{pages}, location => $params{page})) {
50                 use IkiWiki::Render;
51
52                 my @backlinks = IkiWiki::backlink_pages($page);
53
54                 if (exists $params{among}) {
55                         @backlinks = pagespec_match_list(\@backlinks,
56                                 $params{among}, location => $params{page});
57                 }
58
59                 $counts{$page} = scalar(@backlinks);
60                 $max = $counts{$page} if $counts{$page} > $max;
61         }
62
63         if ($style eq 'table') {
64                 return "<table class='pageStats'>\n".
65                         join("\n", map {
66                                 "<tr><td>".
67                                 htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
68                                 "</td><td>".$counts{$_}."</td></tr>"
69                         }
70                         sort { $counts{$b} <=> $counts{$a} } keys %counts).
71                         "\n</table>\n" ;
72         }
73         else {
74                 # In case of misspelling, default to a page cloud
75
76                 my $res = "<div class='pagecloud'>\n";
77                 foreach my $page (sort keys %counts) {
78                         next unless $counts{$page} > 0;
79
80                         my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
81                         $res .= "<span class=\"$class\">".
82                                 htmllink($params{page}, $params{destpage}, $page).
83                                 "</span>\n";
84                 }
85                 $res .= "</div>\n";
86
87                 return $res;
88         }
89 }
90
91 1