* Split off an IkiWiki.pm out of ikiwiki and have all the other modules use
[ikiwiki.git] / 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::register_plugin("preprocess", "brokenlinks", \&preprocess);
11 } # }}}
12
13 sub preprocess (@) { #{{{
14         my %params=@_;
15         $params{pages}="*" unless defined $params{pages};
16         
17         # Needs to update whenever a page is added or removed, so
18         # register a dependency.
19         IkiWiki::add_depends($params{page}, $params{pages});
20         
21         my @broken;
22         foreach my $page (%IkiWiki::links) {
23                 if (IkiWiki::globlist_match($page, $params{pages})) {
24                         foreach my $link (@{$IkiWiki::links{$page}}) {
25                                 next if $link =~ /.*\/discussion/i;
26                                 my $bestlink=IkiWiki::bestlink($page, $link);
27                                 next if length $bestlink;
28                                 push @broken,
29                                         IkiWiki::htmllink($page, $link, 1).
30                                         " in ".
31                                         IkiWiki::htmllink($params{page}, $page, 1);
32                         }
33                 }
34         }
35         
36         return "There are no broken links!" unless @broken;
37         return "<ul>\n".join("\n", map { "<li>$_</li>" } sort @broken)."</ul>\n";
38 } # }}}
39
40 1