2 package IkiWiki::Plugin::pinger;
12 hook(type => "needsbuild", id => "pinger", call => \&needsbuild);
13 hook(type => "preprocess", id => "ping", call => \&preprocess);
14 hook(type => "delete", id => "pinger", call => \&ping);
15 hook(type => "change", id => "pinger", call => \&ping);
18 sub needsbuild (@) { #{{{
20 foreach my $page (keys %pagestate) {
21 if (exists $pagestate{$page}{pinger}) {
23 if (exists $pagesources{$page} &&
24 grep { $_ eq $pagesources{$page} } @$needsbuild) {
25 # remove state, will be re-added if
26 # the ping directive is still present
28 delete $pagestate{$page}{pinger};
34 sub preprocess (@) { #{{{
36 if (! exists $params{from} || ! exists $params{to}) {
37 error gettext("requires 'from' and 'to' parameters");
39 if ($params{from} eq $config{url}) {
40 $pagestate{$params{destpage}}{pinger}{$params{to}}=1;
41 $pages{$params{destpage}}=1;
42 return sprintf(gettext("Will ping %s"), $params{to});
45 return sprintf(gettext("Ignoring ping directive for wiki %s (this wiki is %s)"), $params{from}, $config{url});
50 if (! $pinged && %pages) {
54 eval q{use LWPx::ParanoidAgent};
56 $ua=LWPx::ParanoidAgent->new;
61 debug(gettext("LWP not found, not pinging"));
64 $ua=LWP::UserAgent->new;
66 $ua->timeout($config{pinger_timeout} || 15);
68 # daemonise here so slow pings don't slow down wiki updates
69 defined(my $pid = fork) or error("Can't fork: $!");
72 open STDIN, '/dev/null';
73 open STDOUT, '>/dev/null';
74 POSIX::setsid() or error("Can't start a new session: $!");
75 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
77 # Don't need to keep a lock on the wiki as a daemon.
78 IkiWiki::unlockwiki();
81 foreach my $page (%pages) {
82 if (exists $pagestate{$page}{pinger}) {
83 $urls{$_}=1 foreach keys %{$pagestate{$page}{pinger}};
86 foreach my $url (keys %urls) {
87 # Try to avoid pinging ourselves. If this check
88 # fails, it's not the end of the world, since we
89 # only ping when a page was changed, so a ping loop
90 # will still be avoided.
91 next if $url=~/^\Q$config{cgiurl}\E/;