]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/pinger.pm
formatting etc
[git.ikiwiki.info.git] / IkiWiki / Plugin / pinger.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::pinger;
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
8 my %pages;
9 my $pinged=0;
11 sub import { #{{{
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);
16 } # }}}
18 sub needsbuild (@) { #{{{
19         my $needsbuild=shift;
20         foreach my $page (keys %pagestate) {
21                 if (exists $pagestate{$page}{pinger}) {
22                         $pages{$page}=1;
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
27                                 # on rebuild.
28                                 delete $pagestate{$page}{pinger};
29                         }
30                 }
31         }
32 } # }}}
34 sub preprocess (@) { #{{{
35         my %params=@_;
36         if (! exists $params{from} || ! exists $params{to}) {
37                 error gettext("requires 'from' and 'to' parameters");
38         }
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});
43         }
44         else {
45                 return sprintf(gettext("Ignoring ping directive for wiki %s (this wiki is %s)"), $params{from}, $config{url});
46         }
47 } # }}}
49 sub ping {
50         if (! $pinged && %pages) {
51                 $pinged=1;
52                 
53                 my $ua;
54                 eval q{use LWPx::ParanoidAgent};
55                 if (!$@) {
56                         $ua=LWPx::ParanoidAgent->new;
57                 }
58                 else {
59                         eval q{use LWP};
60                         if ($@) {
61                                 debug(gettext("LWP not found, not pinging"));
62                                 return;
63                         }
64                         $ua=LWP::UserAgent->new;
65                 }
66                 $ua->timeout($config{pinger_timeout} || 15);
67                 
68                 # daemonise here so slow pings don't slow down wiki updates
69                 defined(my $pid = fork) or error("Can't fork: $!");
70                 return if $pid;
71                 chdir '/';
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: $!");
76                 
77                 # Don't need to keep a lock on the wiki as a daemon.
78                 IkiWiki::unlockwiki();
79                 
80                 my %urls;
81                 foreach my $page (%pages) {
82                         if (exists $pagestate{$page}{pinger}) {
83                                 $urls{$_}=1 foreach keys %{$pagestate{$page}{pinger}};
84                         }
85                 }
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/;
92                         
93                         $ua->head($url);
94                 }
95                 
96                 exit 0;
97         }
98 }