]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/brokenlinks.pm
* Added plugin system, currently only supporting for PreProcessorDirectives.
[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
8 sub import { #{{{
9         IkiWiki::register_plugin("preprocess", "brokenlinks", \&preprocess);
10 } # }}}
11
12 sub preprocess (@) { #{{{
13         my %params=@_;
14         $params{pages}="*" unless defined $params{pages};
15         
16         # Needs to update whenever a page is added or removed, so
17         # register a dependency.
18         IkiWiki::add_depends($params{page}, $params{pages});
19         
20         my @broken;
21         foreach my $page (%IkiWiki::links) {
22                 if (IkiWiki::globlist_match($page, $params{pages})) {
23                         foreach my $link (@{$IkiWiki::links{$page}}) {
24                                 next if $link =~ /.*\/discussion/i;
25                                 my $bestlink=IkiWiki::bestlink($page, $link);
26                                 next if length $bestlink;
27                                 push @broken,
28                                         IkiWiki::htmllink($page, $link, 1).
29                                         " in ".
30                                         IkiWiki::htmllink($params{page}, $page, 1);
31                         }
32                 }
33         }
34         
35         return "There are no broken links!" unless @broken;
36         return "<ul>\n".join("\n", map { "<li>$_</li>" } sort @broken)."</ul>\n";
37 } # }}}
38
39 1