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