summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/orphans.pm
blob: 702943f87d1cb96927270a2b609de192a7488aa7 (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. },
  17. }
  18. sub preprocess (@) {
  19. my %params=@_;
  20. $params{pages}="*" unless defined $params{pages};
  21. # Needs to update whenever a link changes, on any page
  22. # since any page could link to one of the pages we're
  23. # considering as orphans.
  24. add_depends($params{page}, "*", deptype("links"));
  25. my @orphans=pagespec_match_list($params{page}, $params{pages},
  26. # update when orphans are added/removed
  27. deptype => deptype("presence"),
  28. filter => sub {
  29. my $page=shift;
  30. # Filter out pages that other pages link to.
  31. return 1 if IkiWiki::backlink_pages($page);
  32. # Toplevel index is assumed to never be orphaned.
  33. return 1 if $page eq 'index';
  34. # If the page has a link to some other page, it's
  35. # indirectly linked via that page's backlinks.
  36. return 1 if grep {
  37. length $_ &&
  38. ($_ !~ /\/\Q$config{discussionpage}\E$/i || ! $config{discussion}) &&
  39. bestlink($page, $_) !~ /^(\Q$page\E|)$/
  40. } @{$links{$page}};
  41. return 0;
  42. },
  43. );
  44. return gettext("All pages have other pages linking to them.") unless @orphans;
  45. return "<ul>\n".
  46. join("\n",
  47. map {
  48. "<li>".
  49. htmllink($params{page}, $params{destpage}, $_,
  50. noimageinline => 1).
  51. "</li>"
  52. } sort @orphans).
  53. "</ul>\n";
  54. }
  55. 1