summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/external.pm
blob: f0acc7e4e1b52a5787ca00b139a21eef0be7c4f5 (plain)
  1. #!/usr/bin/perl
  2. # Support for external plugins written in other languages.
  3. # Communication via XML RPC to 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 3.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. my %hash=%{$value->value};
  60. # XML-RPC v1 does not allow for
  61. # nil/null/None/undef values to be
  62. # transmitted, so until
  63. # XML::RPC::Parser honours v2
  64. # (<nil/>), external plugins send
  65. # a hash with one key "null" pointing
  66. # to an empty string.
  67. if (exists $hash{null} &&
  68. $hash{null} eq "" &&
  69. int(keys(%hash)) == 1) {
  70. return undef;
  71. }
  72. return %hash;
  73. }
  74. else {
  75. return $value->value;
  76. }
  77. }
  78. my $name=$r->name;
  79. my @args=map { $_->value } @{$r->args};
  80. # When dispatching a function, first look in
  81. # IkiWiki::RPC::XML. This allows overriding
  82. # IkiWiki functions with RPC friendly versions.
  83. my $ret;
  84. if (exists $IkiWiki::RPC::XML::{$name}) {
  85. $ret=$IkiWiki::RPC::XML::{$name}($plugin, @args);
  86. }
  87. elsif (exists $IkiWiki::{$name}) {
  88. $ret=$IkiWiki::{$name}(@args);
  89. }
  90. else {
  91. error("XML RPC call error, unknown function: $name");
  92. }
  93. # XML-RPC v1 does not allow for nil/null/None/undef
  94. # values to be transmitted, so until XML::RPC::Parser
  95. # honours v2 (<nil/>), send a hash with one key "null"
  96. # pointing to an empty string.
  97. if (! defined $ret) {
  98. $ret={"null" => ""};
  99. }
  100. my $string=eval { RPC::XML::response->new($ret)->as_string };
  101. if ($@ && ref $ret) {
  102. # One common reason for serialisation to
  103. # fail is a complex return type that cannot
  104. # be represented as an XML RPC response.
  105. # Handle this case by just returning 1.
  106. $string=eval { RPC::XML::response->new(1)->as_string };
  107. }
  108. if ($@) {
  109. error("XML response serialisation failed: $@");
  110. }
  111. rpc_write($plugin->{out}, $string);
  112. }
  113. }
  114. return undef;
  115. }
  116. package IkiWiki::RPC::XML;
  117. use Memoize;
  118. sub getvar ($$$) {
  119. my $plugin=shift;
  120. my $varname="IkiWiki::".shift;
  121. my $key=shift;
  122. no strict 'refs';
  123. my $ret=$varname->{$key};
  124. use strict 'refs';
  125. return $ret;
  126. }
  127. sub setvar ($$$;@) {
  128. my $plugin=shift;
  129. my $varname="IkiWiki::".shift;
  130. my $key=shift;
  131. my $value=shift;
  132. no strict 'refs';
  133. my $ret=$varname->{$key}=$value;
  134. use strict 'refs';
  135. return $ret;
  136. }
  137. sub getstate ($$$$) {
  138. my $plugin=shift;
  139. my $page=shift;
  140. my $id=shift;
  141. my $key=shift;
  142. return $IkiWiki::pagestate{$page}{$id}{$key};
  143. }
  144. sub setstate ($$$$;@) {
  145. my $plugin=shift;
  146. my $page=shift;
  147. my $id=shift;
  148. my $key=shift;
  149. my $value=shift;
  150. return $IkiWiki::pagestate{$page}{$id}{$key}=$value;
  151. }
  152. sub getargv ($) {
  153. my $plugin=shift;
  154. return \@ARGV;
  155. }
  156. sub setargv ($@) {
  157. my $plugin=shift;
  158. my $array=shift;
  159. @ARGV=@$array;
  160. }
  161. sub inject ($@) {
  162. # Bind a given perl function name to a particular RPC request.
  163. my $plugin=shift;
  164. my %params=@_;
  165. if (! exists $params{name} || ! exists $params{call}) {
  166. die "inject needs name and call parameters";
  167. }
  168. my $sub = sub {
  169. IkiWiki::Plugin::external::rpc_call($plugin, $params{call}, @_)
  170. };
  171. $sub=memoize($sub) if $params{memoize};
  172. # This will add it to the symbol table even if not present.
  173. no warnings;
  174. eval qq{*$params{name}=\$sub};
  175. use warnings;
  176. # This will ensure that everywhere it was exported to sees
  177. # the injected version.
  178. IkiWiki::inject(name => $params{name}, call => $sub);
  179. return 1;
  180. }
  181. sub hook ($@) {
  182. # the call parameter is a function name to call, since XML RPC
  183. # cannot pass a function reference
  184. my $plugin=shift;
  185. my %params=@_;
  186. my $callback=$params{call};
  187. delete $params{call};
  188. IkiWiki::hook(%params, call => sub {
  189. IkiWiki::Plugin::external::rpc_call($plugin, $callback, @_);
  190. });
  191. }
  192. sub pagespec_match ($@) {
  193. # convert return object into a XML RPC boolean
  194. my $plugin=shift;
  195. return RPC::XML::boolean->new(0 + IkiWiki::pagespec_match(@_));
  196. }
  197. sub pagespec_match_list ($@) {
  198. # convert return object into a XML RPC boolean
  199. my $plugin=shift;
  200. return RPC::XML::boolean->new(0 + IkiWiki::pagespec_match_list(@_));
  201. }
  202. 1