]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/pinger.pm
po(_istranslation): fix unitialized value
[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 3.00;
8 my %pages;
9 my $pinged=0;
11 sub import {
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 => "change", id => "pinger", call => \&ping);
17 }
19 sub getsetup () {
20         return
21                 plugin => {
22                         safe => 1,
23                         rebuild => 0,
24                 },
25                 pinger_timeout => {
26                         type => "integer",
27                         example => 15,
28                         description => "how many seconds to try pinging before timing out",
29                         safe => 1,
30                         rebuild => 0,
31                 },
32 }
34 sub needsbuild (@) {
35         my $needsbuild=shift;
36         foreach my $page (keys %pagestate) {
37                 if (exists $pagestate{$page}{pinger}) {
38                         $pages{$page}=1;
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
43                                 # on rebuild.
44                                 delete $pagestate{$page}{pinger};
45                         }
46                 }
47         }
48 }
50 sub preprocess (@) {
51         my %params=@_;
52         if (! exists $params{from} || ! exists $params{to}) {
53                 error gettext("requires 'from' and 'to' parameters");
54         }
55         if ($params{from} eq $config{url}) {
56                 $pagestate{$params{destpage}}{pinger}{$params{to}}=1;
57                 $pages{$params{destpage}}=1;
58                 return sprintf(gettext("Will ping %s"), $params{to});
59         }
60         else {
61                 return sprintf(gettext("Ignoring ping directive for wiki %s (this wiki is %s)"), $params{from}, $config{url});
62         }
63 }
65 sub ping {
66         if (! $pinged && %pages) {
67                 $pinged=1;
68                 
69                 my $ua;
70                 eval q{use LWPx::ParanoidAgent};
71                 if (!$@) {
72                         $ua=LWPx::ParanoidAgent->new;
73                 }
74                 else {
75                         eval q{use LWP};
76                         if ($@) {
77                                 debug(gettext("LWP not found, not pinging"));
78                                 return;
79                         }
80                         $ua=LWP::UserAgent->new;
81                 }
82                 $ua->timeout($config{pinger_timeout} || 15);
83                 
84                 # daemonise here so slow pings don't slow down wiki updates
85                 defined(my $pid = fork) or error("Can't fork: $!");
86                 return if $pid;
87                 chdir '/';
88                 open STDIN, '/dev/null';
89                 open STDOUT, '>/dev/null';
90                 POSIX::setsid() or error("Can't start a new session: $!");
91                 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
92                 
93                 # Don't need to keep a lock on the wiki as a daemon.
94                 IkiWiki::unlockwiki();
95                 
96                 my %urls;
97                 foreach my $page (%pages) {
98                         if (exists $pagestate{$page}{pinger}) {
99                                 $urls{$_}=1 foreach keys %{$pagestate{$page}{pinger}};
100                         }
101                 }
102                 foreach my $url (keys %urls) {
103                         # Try to avoid pinging ourselves. If this check
104                         # fails, it's not the end of the world, since we
105                         # only ping when a page was changed, so a ping loop
106                         # will still be avoided.
107                         next if $url=~/^\Q$config{cgiurl}\E/;
108                         
109                         $ua->get($url);
110                 }
111                 
112                 exit 0;
113         }