summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/pagestats.pm
blob: 0765c1cfa1ac566343923bd8e1491fa822a29324 (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.
  33. add_depends($params{page}, $params{pages}, exists => 1);
  34. # Also needs to update when any page with links changes,
  35. # in case the links point to our displayed pages.
  36. # (Among limits this further.)
  37. add_depends($params{page}, exists $params{among} ? $params{among} : "*",
  38. links => 1);
  39. my %counts;
  40. my $max = 0;
  41. foreach my $page (pagespec_match_list([keys %links],
  42. $params{pages}, location => $params{page})) {
  43. use IkiWiki::Render;
  44. my @backlinks = IkiWiki::backlink_pages($page);
  45. if (exists $params{among}) {
  46. @backlinks = pagespec_match_list(\@backlinks,
  47. $params{among}, location => $params{page});
  48. }
  49. $counts{$page} = scalar(@backlinks);
  50. $max = $counts{$page} if $counts{$page} > $max;
  51. }
  52. if ($style eq 'table') {
  53. return "<table class='pageStats'>\n".
  54. join("\n", map {
  55. "<tr><td>".
  56. htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
  57. "</td><td>".$counts{$_}."</td></tr>"
  58. }
  59. sort { $counts{$b} <=> $counts{$a} } keys %counts).
  60. "\n</table>\n" ;
  61. }
  62. else {
  63. # In case of misspelling, default to a page cloud
  64. my $res = "<div class='pagecloud'>\n";
  65. foreach my $page (sort keys %counts) {
  66. next unless $counts{$page} > 0;
  67. my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
  68. $res .= "<span class=\"$class\">".
  69. htmllink($params{page}, $params{destpage}, $page).
  70. "</span>\n";
  71. }
  72. $res .= "</div>\n";
  73. return $res;
  74. }
  75. }
  76. 1