summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/pagestats.pm
blob: f6398c535f29ce2768e7389bf4d8b7581407d61a (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 2.00;
  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}, location => $params{page})) {
  31. use IkiWiki::Render;
  32. my @bl = IkiWiki::backlinks($page);
  33. $counts{$page} = scalar(@bl);
  34. $max = $counts{$page} if $counts{$page} > $max;
  35. }
  36. }
  37. if ($style eq 'table') {
  38. return "<table class='pageStats'>\n".
  39. join("\n", map {
  40. "<tr><td>".
  41. htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
  42. "</td><td>".$counts{$_}."</td></tr>"
  43. }
  44. sort { $counts{$b} <=> $counts{$a} } keys %counts).
  45. "\n</table>\n" ;
  46. } else {
  47. # In case of misspelling, default to a page cloud
  48. my $res = "<div class='pagecloud'>\n";
  49. foreach my $page (sort keys %counts) {
  50. my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
  51. $res .= "<span class=\"$class\">".
  52. htmllink($params{page}, $params{destpage}, $page).
  53. "</span>\n";
  54. }
  55. $res .= "</div>\n";
  56. return $res;
  57. }
  58. } # }}}
  59. 1