summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/pagestats.pm
blob: 8ab5d36662ce71716a1733e1a4b5de223c92602b (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 3.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 => "getsetup", id => "pagestats", call => \&getsetup);
  19. hook(type => "preprocess", id => "pagestats", call => \&preprocess);
  20. }
  21. sub getsetup () {
  22. return
  23. plugin => {
  24. safe => 1,
  25. rebuild => undef,
  26. },
  27. }
  28. sub preprocess (@) {
  29. my %params=@_;
  30. $params{pages}="*" unless defined $params{pages};
  31. my $style = ($params{style} or 'cloud');
  32. # Needs to update whenever a page is added or removed, so
  33. # register a dependency.
  34. add_depends($params{page}, $params{pages});
  35. my %counts;
  36. my $max = 0;
  37. foreach my $page (pagespec_match_list([keys %links],
  38. $params{pages}, location => $params{page})) {
  39. use IkiWiki::Render;
  40. $counts{$page} = scalar(IkiWiki::backlinks($page));
  41. $max = $counts{$page} if $counts{$page} > $max;
  42. }
  43. if ($style eq 'table') {
  44. return "<table class='pageStats'>\n".
  45. join("\n", map {
  46. "<tr><td>".
  47. htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
  48. "</td><td>".$counts{$_}."</td></tr>"
  49. }
  50. sort { $counts{$b} <=> $counts{$a} } keys %counts).
  51. "\n</table>\n" ;
  52. }
  53. else {
  54. # In case of misspelling, default to a page cloud
  55. my $res = "<div class='pagecloud'>\n";
  56. foreach my $page (sort keys %counts) {
  57. my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
  58. $res .= "<span class=\"$class\">".
  59. htmllink($params{page}, $params{destpage}, $page).
  60. "</span>\n";
  61. }
  62. $res .= "</div>\n";
  63. return $res;
  64. }
  65. }
  66. 1