summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/orphans.pm
blob: e3cc3c940882dd27ec7576f8ff8ee63eb8dd845e (plain)
  1. #!/usr/bin/perl
  2. # Provides a list of pages no other page links to.
  3. package IkiWiki::Plugin::orphans;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 3.00;
  7. sub import {
  8. hook(type => "getsetup", id => "orphans", call => \&getsetup);
  9. hook(type => "preprocess", id => "orphans", call => \&preprocess);
  10. }
  11. sub getsetup () {
  12. return
  13. plugin => {
  14. safe => 1,
  15. rebuild => undef,
  16. section => "widget",
  17. },
  18. }
  19. sub preprocess (@) {
  20. my %params=@_;
  21. $params{pages}="*" unless defined $params{pages};
  22. # Needs to update whenever a link changes, on any page
  23. # since any page could link to one of the pages we're
  24. # considering as orphans.
  25. add_depends($params{page}, "*", deptype("links"));
  26. my @orphans=pagespec_match_list($params{page}, $params{pages},
  27. # update when orphans are added/removed
  28. deptype => deptype("presence"),
  29. filter => sub {
  30. my $page=shift;
  31. # Filter out pages that other pages link to.
  32. return 1 if IkiWiki::backlink_pages($page);
  33. # Toplevel index is assumed to never be orphaned.
  34. return 1 if $page eq 'index';
  35. # If the page has a link to some other page, it's
  36. # indirectly linked via that page's backlinks.
  37. return 1 if grep {
  38. length $_ &&
  39. ($_ !~ /\/\Q$config{discussionpage}\E$/i || ! $config{discussion}) &&
  40. bestlink($page, $_) !~ /^(\Q$page\E|)$/
  41. } @{$links{$page}};
  42. return 0;
  43. },
  44. );
  45. return gettext("All pages have other pages linking to them.") unless @orphans;
  46. return "<ul>\n".
  47. join("\n",
  48. map {
  49. "<li>".
  50. htmllink($params{page}, $params{destpage}, $_,
  51. noimageinline => 1).
  52. "</li>"
  53. } sort @orphans).
  54. "</ul>\n";
  55. }
  56. 1