summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/pagestats.pm
blob: 0958f5af6f173e673aa25f0109c0e80ee91aa08a (plain)
  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. use warnings;
  13. use strict;
  14. use IkiWiki;
  15. # Names of the HTML classes to use for the tag cloud
  16. our @classes = ('smallestPC', 'smallPC', 'normalPC', 'bigPC', 'biggestPC' );
  17. sub import { #{{{
  18. hook(type => "preprocess", id => "pagestats", call => \&preprocess);
  19. } # }}}
  20. sub preprocess (@) { #{{{
  21. my %params=@_;
  22. $params{pages}="*" unless defined $params{pages};
  23. my $style = ($params{style} or 'cloud');
  24. # Needs to update whenever a page is added or removed, so
  25. # register a dependency.
  26. add_depends($params{page}, $params{pages});
  27. my %counts;
  28. my $max = 0;
  29. foreach my $page (keys %links) {
  30. if (pagespec_match($page, $params{pages})) {
  31. my @bl = IkiWiki::backlinks($page);
  32. $counts{$page} = scalar(@bl);
  33. $max = $counts{$page} if $counts{$page} > $max;
  34. }
  35. }
  36. if ($style eq 'table') {
  37. return "<table class='pageStats'>\n".
  38. join("\n", map {
  39. "<tr><td>".
  40. htmllink($params{page}, $params{destpage}, $_, 1).
  41. "</td><td>".$counts{$_}."</td></tr>"
  42. }
  43. sort { $counts{$b} <=> $counts{$a} } keys %counts).
  44. "\n</table>\n" ;
  45. } else {
  46. # In case of misspelling, default to a page cloud
  47. my $res = "<div class='pagecloud'>\n";
  48. foreach my $page (sort keys %counts) {
  49. my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
  50. $res .= "<span class=\"$class\">".
  51. htmllink($params{page}, $params{destpage}, $page).
  52. "</span>\n";
  53. }
  54. $res .= "</div>\n";
  55. return $res;
  56. }
  57. } # }}}
  58. 1