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