2 package IkiWiki::Plugin::pinger;
12 hook(type => "getsetup", id => "pinger", call => \&getsetup);
13 hook(type => "needsbuild", id => "pinger", call => \&needsbuild);
14 hook(type => "preprocess", id => "ping", call => \&preprocess);
15 hook(type => "delete", id => "pinger", call => \&ping);
16 hook(type => "rendered", id => "pinger", call => \&ping);
28 description => "how many seconds to try pinging before timing out",
36 foreach my $page (keys %pagestate) {
37 if (exists $pagestate{$page}{pinger}) {
39 if (exists $pagesources{$page} &&
40 grep { $_ eq $pagesources{$page} } @$needsbuild) {
41 # remove state, will be re-added if
42 # the ping directive is still present
44 delete $pagestate{$page}{pinger};
53 if (! exists $params{from} || ! exists $params{to}) {
54 error gettext("requires 'from' and 'to' parameters");
56 if ($params{from} eq $config{url}) {
57 $pagestate{$params{destpage}}{pinger}{$params{to}}=1;
58 $pages{$params{destpage}}=1;
59 return sprintf(gettext("Will ping %s"), $params{to});
62 return sprintf(gettext("Ignoring ping directive for wiki %s (this wiki is %s)"), $params{from}, $config{url});
67 if (! $pinged && %pages) {
70 eval q{use Net::INET6Glue::INET_is_INET6}; # may not be available
74 # We pass the for_url parameter, even though it's
75 # undef, because that will make sure we crash if used
76 # with an older IkiWiki.pm that didn't automatically
77 # try to use LWPx::ParanoidAgent.
78 $ua=useragent(for_url => undef);
81 debug(gettext("LWP not found, not pinging").": $@");
84 $ua->timeout($config{pinger_timeout} || 15);
86 # daemonise here so slow pings don't slow down wiki updates
87 defined(my $pid = fork) or error("Can't fork: $!");
90 open STDIN, '/dev/null';
91 open STDOUT, '>/dev/null';
92 POSIX::setsid() or error("Can't start a new session: $!");
93 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
95 # Don't need to keep a lock on the wiki as a daemon.
96 IkiWiki::unlockwiki();
99 foreach my $page (%pages) {
100 if (exists $pagestate{$page}{pinger}) {
101 $urls{$_}=1 foreach keys %{$pagestate{$page}{pinger}};
104 foreach my $url (keys %urls) {
105 # Try to avoid pinging ourselves. If this check
106 # fails, it's not the end of the world, since we
107 # only ping when a page was changed, so a ping loop
108 # will still be avoided.
109 next if $url=~/^\Q$config{cgiurl}\E/;
110 my $local_cgiurl = IkiWiki::cgiurl();
111 next if $url=~/^\Q$local_cgiurl\E/;