]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/pagestats.pm
call initLanguage after initTheme
[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, so
39         # register a dependency.
40         add_depends($params{page}, $params{pages});
41         
42         my %counts;
43         my $max = 0;
44         foreach my $page (pagespec_match_list([keys %links],
45                         $params{pages}, location => $params{page})) {
46                 use IkiWiki::Render;
47                 $counts{$page} = scalar(IkiWiki::backlinks($page));
48                 $max = $counts{$page} if $counts{$page} > $max;
49         }
50
51         if ($style eq 'table') {
52                 return "<table class='pageStats'>\n".
53                         join("\n", map {
54                                 "<tr><td>".
55                                 htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
56                                 "</td><td>".$counts{$_}."</td></tr>"
57                         }
58                         sort { $counts{$b} <=> $counts{$a} } keys %counts).
59                         "\n</table>\n" ;
60         }
61         else {
62                 # In case of misspelling, default to a page cloud
63
64                 my $res = "<div class='pagecloud'>\n";
65                 foreach my $page (sort keys %counts) {
66                         my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
67                         $res .= "<span class=\"$class\">".
68                                 htmllink($params{page}, $params{destpage}, $page).
69                                 "</span>\n";
70                 }
71                 $res .= "</div>\n";
72
73                 return $res;
74         }
75 }
76
77 1