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