summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/orphans.pm
blob: 945892d177d7723bbd0ac579bcd0da36a40fd147 (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 { length $_ && $_ !~/\/Discussion$/i && IkiWiki::bestlink($page, $_) ne $page } @{$IkiWiki::links{$page}};
  29. push @orphans, $page;
  30. }
  31. return "All pages are linked to by other pages." unless @orphans;
  32. return "<ul>\n".join("\n", map { "<li>".IkiWiki::htmllink($params{page}, $_, 1)."</li>" } sort @orphans)."</ul>\n";
  33. } # }}}
  34. 1