summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/orphans.pm
blob: cf74c9b794bb4977d0ed8eb17fbc6e36ed045ba0 (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 page is added or removed, so
  22. # register a dependency.
  23. add_depends($params{page}, $params{pages});
  24. my %linkedto;
  25. foreach my $p (keys %links) {
  26. map { $linkedto{bestlink($p, $_)}=1 if length $_ }
  27. @{$links{$p}};
  28. }
  29. my @orphans;
  30. my $discussion=gettext("discussion");
  31. foreach my $page (pagespec_match_list(
  32. [ grep { ! $linkedto{$_} && $_ ne 'index' }
  33. keys %pagesources ],
  34. $params{pages}, location => $params{page})) {
  35. # If the page has a link to some other page, it's
  36. # indirectly linked to a page via that page's backlinks.
  37. next if grep {
  38. length $_ &&
  39. ($_ !~ /\/\Q$discussion\E$/i || ! $config{discussion}) &&
  40. bestlink($page, $_) !~ /^(\Q$page\E|)$/
  41. } @{$links{$page}};
  42. push @orphans, $page;
  43. }
  44. return gettext("All pages are linked to by other pages.") 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