Merge branch 'master' into dependency-types
[ikiwiki] / 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         my %counts;
39         my $max = 0;
40         foreach my $page (use_pagespec($params{page}, $params{pages},
41                           # update when a displayed page is added or removed
42                           deptype => deptype("presence"))) {
43                 use IkiWiki::Render;
44
45                 my @backlinks = IkiWiki::backlink_pages($page);
46
47                 if (exists $params{among}) {
48                         # only consider backlinks from the amoung pages
49                         @backlinks = use_pagespec($params{page}, $params{among},
50                                 # update whenever links on those pages change
51                                 deptype => deptype("links"),
52                                 list => \@backlinks
53                         );
54                 }
55                 else {
56                         # update when any page with links changes,
57                         # in case the links point to our displayed pages
58                         add_depends($params{page}, "*", deptype("links"));
59                 }
60
61                 $counts{$page} = scalar(@backlinks);
62                 $max = $counts{$page} if $counts{$page} > $max;
63         }
64
65         if ($style eq 'table') {
66                 return "<table class='pageStats'>\n".
67                         join("\n", map {
68                                 "<tr><td>".
69                                 htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
70                                 "</td><td>".$counts{$_}."</td></tr>"
71                         }
72                         sort { $counts{$b} <=> $counts{$a} } keys %counts).
73                         "\n</table>\n" ;
74         }
75         else {
76                 # In case of misspelling, default to a page cloud
77
78                 my $res = "<div class='pagecloud'>\n";
79                 foreach my $page (sort keys %counts) {
80                         next unless $counts{$page} > 0;
81
82                         my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
83                         $res .= "<span class=\"$class\">".
84                                 htmllink($params{page}, $params{destpage}, $page).
85                                 "</span>\n";
86                 }
87                 $res .= "</div>\n";
88
89                 return $res;
90         }
91 }
92
93 1