summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/orphans.pm
blob: ae330b23b81e78abf227cbcb0a5cb757088e485a (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}, "*", links => 1);
  25. # Also needs to update whenever potential orphans are added or
  26. # removed.
  27. add_depends($params{page}, $params{pages}, presence => 1);
  28. my @orphans;
  29. foreach my $page (pagespec_match_list(
  30. [ grep { ! IkiWiki::backlink_pages($_) && $_ ne 'index' }
  31. keys %pagesources ],
  32. $params{pages}, location => $params{page})) {
  33. # If the page has a link to some other page, it's
  34. # indirectly linked to a page via that page's backlinks.
  35. next if grep {
  36. length $_ &&
  37. ($_ !~ /\/\Q$config{discussionpage}\E$/i || ! $config{discussion}) &&
  38. bestlink($page, $_) !~ /^(\Q$page\E|)$/
  39. } @{$links{$page}};
  40. push @orphans, $page;
  41. }
  42. return gettext("All pages have other pages linking to them.") unless @orphans;
  43. return "<ul>\n".
  44. join("\n",
  45. map {
  46. "<li>".
  47. htmllink($params{page}, $params{destpage}, $_,
  48. noimageinline => 1).
  49. "</li>"
  50. } sort @orphans).
  51. "</ul>\n";
  52. }
  53. 1