summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/pagestats.pm
blob: 4313aa271e2f55259b04df7b7abfbd1a447ba1cb (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 (pagespec_match_list($params{page}, $params{pages},
  35. # update when a displayed page is added/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 = pagespec_match_list(
  42. $params{page}, $params{among},
  43. # update whenever links on those pages change
  44. deptype => deptype("links"),
  45. list => \@backlinks
  46. );
  47. }
  48. else {
  49. # update when any page with links changes,
  50. # in case the links point to our displayed pages
  51. add_depends($params{page}, "*", deptype("links"));
  52. }
  53. $counts{$page} = scalar(@backlinks);
  54. $max = $counts{$page} if $counts{$page} > $max;
  55. }
  56. if (exists $params{show}) {
  57. my $i=0;
  58. my %show;
  59. foreach my $key (sort { $counts{$b} <=> $counts{$a} } keys %counts) {
  60. last if ++$i > $params{show};
  61. $show{$key}=$counts{$key};
  62. }
  63. %counts=%show;
  64. }
  65. if ($style eq 'table') {
  66. return "<table class='pageStats'>\n".
  67. join("\n", map {
  68. "<tr><td>".
  69. htmllink($params{page}, $params{destpage}, $_, noimageinline => 1).
  70. "</td><td>".$counts{$_}."</td></tr>"
  71. }
  72. sort { $counts{$b} <=> $counts{$a} } keys %counts).
  73. "\n</table>\n" ;
  74. }
  75. else {
  76. # In case of misspelling, default to a page cloud
  77. my $res = "<div class='pagecloud'>\n";
  78. foreach my $page (sort keys %counts) {
  79. next unless $counts{$page} > 0;
  80. my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
  81. $res .= "<span class=\"$class\">".
  82. htmllink($params{page}, $params{destpage}, $page).
  83. "</span>\n";
  84. }
  85. $res .= "</div>\n";
  86. return $res;
  87. }
  88. }
  89. 1