summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/orphans.pm
blob: 12b9d2e5236a1512dcb53f1a01b9d097b29d93c0 (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;
  7. sub import { #{{{
  8. IkiWiki::hook(type => "preprocess", id => "orphans",
  9. call => \&preprocess);
  10. } # }}}
  11. sub preprocess (@) { #{{{
  12. my %params=@_;
  13. $params{pages}="*" unless defined $params{pages};
  14. # Needs to update whenever a page is added or removed, so
  15. # register a dependency.
  16. IkiWiki::add_depends($params{page}, $params{pages});
  17. my %linkedto;
  18. foreach my $p (keys %IkiWiki::links) {
  19. map { $linkedto{IkiWiki::bestlink($p, $_)}=1 if length $_ }
  20. @{$IkiWiki::links{$p}};
  21. }
  22. my @orphans;
  23. foreach my $page (keys %IkiWiki::renderedfiles) {
  24. next if $linkedto{$page};
  25. next unless IkiWiki::globlist_match($page, $params{pages});
  26. # If the page has a link to some other page, it's
  27. # indirectly linked to a page via that page's backlinks.
  28. next if grep {
  29. length $_ &&
  30. ($_ !~ /\/Discussion$/i || ! $IkiWiki::config{discussion}) &&
  31. IkiWiki::bestlink($page, $_) !~ /^($page|)$/
  32. } @{$IkiWiki::links{$page}};
  33. push @orphans, $page;
  34. }
  35. return "All pages are linked to by other pages." unless @orphans;
  36. return "<ul>\n".join("\n", map { "<li>".IkiWiki::htmllink($params{page}, $params{page}, $_, 1)."</li>" } sort @orphans)."</ul>\n";
  37. } # }}}
  38. 1