* --getctime had bitrotted (well I only ever used it the once so far..),
[ikiwiki] / IkiWiki / Plugin / brokenlinks.pm
1 #!/usr/bin/perl
2 # Provides a list of broken links.
3 package IkiWiki::Plugin::brokenlinks;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8
9 sub import { #{{{
10         IkiWiki::hook(type => "preprocess", id => "brokenlinks",
11                 call => \&preprocess);
12 } # }}}
13
14 sub preprocess (@) { #{{{
15         my %params=@_;
16         $params{pages}="*" unless defined $params{pages};
17         
18         # Needs to update whenever a page is added or removed, so
19         # register a dependency.
20         IkiWiki::add_depends($params{page}, $params{pages});
21         
22         my @broken;
23         foreach my $page (%IkiWiki::links) {
24                 if (IkiWiki::globlist_match($page, $params{pages})) {
25                         foreach my $link (@{$IkiWiki::links{$page}}) {
26                                 next if $link =~ /.*\/discussion/i && $IkiWiki::config{discussion};
27                                 my $bestlink=IkiWiki::bestlink($page, $link);
28                                 next if length $bestlink;
29                                 push @broken,
30                                         IkiWiki::htmllink($page, $page, $link, 1).
31                                         " in ".
32                                         IkiWiki::htmllink($params{page}, $params{page}, $page, 1);
33                         }
34                 }
35         }
36         
37         return "There are no broken links!" unless @broken;
38         my %seen;
39         return "<ul>\n".join("\n", map { "<li>$_</li>" } grep { ! $seen{$_}++ } sort @broken)."</ul>\n";
40 } # }}}
41
42 1