summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/brokenlinks.pm
blob: cf8f25281585fff56ee8b11dd9c58bdfc64b793c (plain)
  1. #!/usr/bin/perl
  2. # Provides a list of broken links.
  3. package IkiWiki::Plugin::brokenlinks;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 3.00;
  7. sub import {
  8. hook(type => "getsetup", id => "brokenlinks", call => \&getsetup);
  9. hook(type => "preprocess", id => "brokenlinks", 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 %broken;
  25. foreach my $page (pagespec_match_list([keys %links],
  26. $params{pages}, location => $params{page})) {
  27. my $discussion=gettext("Discussion");
  28. my %seen;
  29. foreach my $link (@{$links{$page}}) {
  30. next if $seen{$link};
  31. $seen{$link}=1;
  32. next if $link =~ /.*\/\Q$discussion\E/i && $config{discussion};
  33. my $bestlink=bestlink($page, $link);
  34. next if length $bestlink;
  35. push @{$broken{$link}}, $page;
  36. }
  37. }
  38. my @broken;
  39. foreach my $link (keys %broken) {
  40. my $page=$broken{$link}->[0];
  41. push @broken, sprintf(gettext("%s from %s"),
  42. htmllink($page, $params{destpage}, $link, noimageinline => 1),
  43. join(", ", map {
  44. htmllink($params{page}, $params{destpage}, $_, noimageinline => 1)
  45. } @{$broken{$link}}));
  46. }
  47. return gettext("There are no broken links!") unless %broken;
  48. return "<ul>\n"
  49. .join("\n",
  50. map {
  51. "<li>$_</li>"
  52. }
  53. sort @broken)
  54. ."</ul>\n";
  55. }
  56. 1