summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/pagestats.pm
blob: dbe69539d72311d8b43c752c9ae33ebe8017a763 (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 (keys %links) {
  38. if (pagespec_match($page, $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. }
  44. if ($style eq 'table') {
  45. return "<table class='pageStats'>\n".
  46. join("\n", map {
  47. "<tr><td>".
  48. htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
  49. "</td><td>".$counts{$_}."</td></tr>"
  50. }
  51. sort { $counts{$b} <=> $counts{$a} } keys %counts).
  52. "\n</table>\n" ;
  53. }
  54. else {
  55. # In case of misspelling, default to a page cloud
  56. my $res = "<div class='pagecloud'>\n";
  57. foreach my $page (sort keys %counts) {
  58. my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
  59. $res .= "<span class=\"$class\">".
  60. htmllink($params{page}, $params{destpage}, $page).
  61. "</span>\n";
  62. }
  63. $res .= "</div>\n";
  64. return $res;
  65. }
  66. }
  67. 1