summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/external.pm
blob: 43d2cbb28451701d0661f05114d28eec1ca37613 (plain)
  1. #!/usr/bin/perl
  2. # Support for external plugins written in other languages.
  3. # Communication via XML RPC a pipe.
  4. # See externaldemo for an example of a plugin that uses this.
  5. package IkiWiki::Plugin::external;
  6. use warnings;
  7. use strict;
  8. use IkiWiki 2.00;
  9. use RPC::XML;
  10. use RPC::XML::Parser;
  11. use IPC::Open2;
  12. use IO::Handle;
  13. my %plugins;
  14. sub import { #{{{
  15. my $self=shift;
  16. my $plugin=shift;
  17. return unless defined $plugin;
  18. my ($plugin_read, $plugin_write);
  19. my $pid = open2($plugin_read, $plugin_write,
  20. IkiWiki::possibly_foolish_untaint($plugin));
  21. # open2 doesn't respect "use open ':utf8'"
  22. binmode($plugin_read, ':utf8');
  23. binmode($plugin_write, ':utf8');
  24. $plugins{$plugin}={in => $plugin_read, out => $plugin_write, pid => $pid,
  25. accum => ""};
  26. $RPC::XML::ENCODING="utf-8";
  27. rpc_call($plugins{$plugin}, "import");
  28. } #}}}
  29. sub rpc_write ($$) { #{{{
  30. my $fh=shift;
  31. my $string=shift;
  32. $fh->print($string."\n");
  33. $fh->flush;
  34. } #}}}
  35. sub rpc_call ($$;@) { #{{{
  36. my $plugin=shift;
  37. my $command=shift;
  38. # send the command
  39. my $req=RPC::XML::request->new($command, @_);
  40. rpc_write($plugin->{out}, $req->as_string);
  41. # process incoming rpc until a result is available
  42. while ($_ = $plugin->{in}->getline) {
  43. $plugin->{accum}.=$_;
  44. while ($plugin->{accum} =~ /^\s*(<\?xml\s.*?<\/(?:methodCall|methodResponse)>)\n(.*)/s) {
  45. $plugin->{accum}=$2;
  46. my $r = RPC::XML::Parser->new->parse($1);
  47. error("XML RPC parser failure: $r") unless ref $r;
  48. if ($r->isa('RPC::XML::response')) {
  49. my $value=$r->value;
  50. if ($r->is_fault($value)) {
  51. # throw the error as best we can
  52. print STDERR $value->string."\n";
  53. return "";
  54. }
  55. elsif ($value->isa('RPC::XML::array')) {
  56. return @{$value->value};
  57. }
  58. elsif ($value->isa('RPC::XML::struct')) {
  59. return %{$value->value};
  60. }
  61. else {
  62. return $value->value;
  63. }
  64. }
  65. my $name=$r->name;
  66. my @args=map { $_->value } @{$r->args};
  67. # When dispatching a function, first look in
  68. # IkiWiki::RPC::XML. This allows overriding
  69. # IkiWiki functions with RPC friendly versions.
  70. my $ret;
  71. if (exists $IkiWiki::RPC::XML::{$name}) {
  72. $ret=$IkiWiki::RPC::XML::{$name}($plugin, @args);
  73. }
  74. elsif (exists $IkiWiki::{$name}) {
  75. $ret=$IkiWiki::{$name}(@args);
  76. }
  77. else {
  78. error("XML RPC call error, unknown function: $name");
  79. }
  80. my $string=eval { RPC::XML::response->new($ret)->as_string };
  81. if ($@ && ref $ret) {
  82. # One common reason for serialisation to
  83. # fail is a complex return type that cannot
  84. # be represented as an XML RPC response.
  85. # Handle this case by just returning 1.
  86. $string=eval { RPC::XML::response->new(1)->as_string };
  87. }
  88. if ($@) {
  89. error("XML response serialisation failed: $@");
  90. }
  91. rpc_write($plugin->{out}, $string);
  92. }
  93. }
  94. return undef;
  95. } #}}}
  96. package IkiWiki::RPC::XML;
  97. use Memoize;
  98. sub getvar ($$$) { #{{{
  99. my $plugin=shift;
  100. my $varname="IkiWiki::".shift;
  101. my $key=shift;
  102. no strict 'refs';
  103. my $ret=$varname->{$key};
  104. use strict 'refs';
  105. return $ret;
  106. } #}}}
  107. sub setvar ($$$;@) { #{{{
  108. my $plugin=shift;
  109. my $varname="IkiWiki::".shift;
  110. my $key=shift;
  111. my $value=shift;
  112. no strict 'refs';
  113. my $ret=$varname->{$key}=$value;
  114. use strict 'refs';
  115. return $ret;
  116. } #}}}
  117. sub getstate ($$$$) { #{{{
  118. my $plugin=shift;
  119. my $page=shift;
  120. my $id=shift;
  121. my $key=shift;
  122. return $IkiWiki::pagestate{$page}{$id}{$key};
  123. } #}}}
  124. sub setstate ($$$$;@) { #{{{
  125. my $plugin=shift;
  126. my $page=shift;
  127. my $id=shift;
  128. my $key=shift;
  129. return $IkiWiki::pagestate{$page}{$id}{$key}=@_;
  130. } #}}}
  131. sub getargv ($) { #{{{
  132. my $plugin=shift;
  133. return \@ARGV;
  134. } #}}}
  135. sub setargv ($@) { #{{{
  136. my $plugin=shift;
  137. my $array=shift;
  138. @ARGV=@$array;
  139. } #}}}
  140. sub inject ($@) { #{{{
  141. # Bind a given perl function name to a particular RPC request.
  142. my $plugin=shift;
  143. my %params=@_;
  144. if (! exists $params{name} || ! exists $params{call}) {
  145. die "inject needs name and call parameters";
  146. }
  147. my $sub = sub {
  148. IkiWiki::Plugin::external::rpc_call($plugin, $params{call}, @_)
  149. };
  150. eval qq{*$params{name}=\$sub};
  151. memoize($params{name}) if $params{memoize};
  152. return 1;
  153. } #}}}
  154. sub hook ($@) { #{{{
  155. # the call parameter is a function name to call, since XML RPC
  156. # cannot pass a function reference
  157. my $plugin=shift;
  158. my %params=@_;
  159. my $callback=$params{call};
  160. delete $params{call};
  161. IkiWiki::hook(%params, call => sub {
  162. my $ret=IkiWiki::Plugin::external::rpc_call($plugin, $callback, @_);
  163. return $ret;
  164. });
  165. } #}}}
  166. sub pagespec_match ($@) { #{{{
  167. # convert pagespec_match's return object into a XML RPC boolean
  168. my $plugin=shift;
  169. return RPC::XML::boolean->new(0 + IkiWiki::pagespec_march(@_));
  170. } #}}}
  171. 1