]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/brokenlinks.pm
po/todo: take note of the commit that will need to be reverted later
[git.ikiwiki.info.git] / IkiWiki / Plugin / brokenlinks.pm
1 #!/usr/bin/perl
2 # Provides a list of broken links.
3 package IkiWiki::Plugin::brokenlinks;
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
9 sub import {
10         hook(type => "getsetup", id => "brokenlinks", call => \&getsetup);
11         hook(type => "preprocess", id => "brokenlinks", call => \&preprocess);
12 }
14 sub getsetup {
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => undef,
19                 },
20 }
22 sub preprocess (@) {
23         my %params=@_;
24         $params{pages}="*" unless defined $params{pages};
25         
26         # Needs to update whenever a page is added or removed, so
27         # register a dependency.
28         add_depends($params{page}, $params{pages});
29         
30         my %broken;
31         foreach my $page (keys %links) {
32                 if (pagespec_match($page, $params{pages}, location => $params{page})) {
33                         my $discussion=gettext("discussion");
34                         my %seen;
35                         foreach my $link (@{$links{$page}}) {
36                                 next if $seen{$link};
37                                 $seen{$link}=1;
38                                 next if $link =~ /.*\/\Q$discussion\E/i && $config{discussion};
39                                 my $bestlink=bestlink($page, $link);
40                                 next if length $bestlink;
41                                 push @{$broken{$link}}, $page;
42                         }
43                 }
44         }
46         my @broken;
47         foreach my $link (keys %broken) {
48                 my $page=$broken{$link}->[0];
49                 push @broken, sprintf(gettext("%s from %s"),
50                         htmllink($page, $params{destpage}, $link, noimageinline => 1),
51                         join(", ", map {
52                                 htmllink($params{page}, $params{destpage}, $_,  noimageinline => 1)
53                         } @{$broken{$link}}));
54         }
55         
56         return gettext("There are no broken links!") unless %broken;
57         return "<ul>\n"
58                 .join("\n",
59                         map {
60                                 "<li>$_</li>"
61                         }
62                         sort @broken)
63                 ."</ul>\n";
64 }
66 1