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;
19 return unless defined $plugin;
21 my ($plugin_read, $plugin_write);
22 my $pid = open2($plugin_read, $plugin_write,
23 IkiWiki::possibly_foolish_untaint($plugin));
25 # open2 doesn't respect "use open ':utf8'"
26 binmode($plugin_read, ':utf8');
27 binmode($plugin_write, ':utf8');
29 $plugins{$plugin}={in => $plugin_read, out => $plugin_write, pid => $pid,
31 $RPC::XML::ENCODING="utf-8";
33 rpc_call($plugins{$plugin}, "import");
40 $fh->print($string."\n");
49 my $req=RPC::XML::request->new($command, @_);
50 rpc_write($plugin->{out}, $req->as_string);
52 # process incoming rpc until a result is available
53 while ($_ = $plugin->{in}->getline) {
55 while ($plugin->{accum} =~ /^\s*(<\?xml\s.*?<\/(?:methodCall|methodResponse)>)\n(.*)/s) {
59 use RPC::XML::ParserFactory;
60 $parser = RPC::XML::ParserFactory->new;
66 $parser = RPC::XML::Parser->new;
69 my $r=$parser->parse($1);
70 error("XML RPC parser failure: $r") unless ref $r;
71 if ($r->isa('RPC::XML::response')) {
73 if ($r->is_fault($value)) {
74 # throw the error as best we can
75 print STDERR $value->string."\n";
78 elsif ($value->isa('RPC::XML::array')) {
79 return @{$value->value};
81 elsif ($value->isa('RPC::XML::struct')) {
82 my %hash=%{$value->value};
84 # XML-RPC v1 does not allow for
85 # nil/null/None/undef values to be
86 # transmitted. The <nil/> extension
87 # is the right fix, but for
88 # back-compat, let external plugins send
89 # a hash with one key "null" pointing
91 if (exists $hash{null} &&
93 int(keys(%hash)) == 1) {
100 return $value->value;
105 my @args=map { $_->value } @{$r->args};
107 # When dispatching a function, first look in
108 # IkiWiki::RPC::XML. This allows overriding
109 # IkiWiki functions with RPC friendly versions.
111 if (exists $IkiWiki::RPC::XML::{$name}) {
112 $ret=$IkiWiki::RPC::XML::{$name}($plugin, @args);
114 elsif (exists $IkiWiki::{$name}) {
115 $ret=$IkiWiki::{$name}(@args);
118 error("XML RPC call error, unknown function: $name");
121 # XML-RPC v1 does not allow for nil/null/None/undef
122 # values to be transmitted, so until XML::RPC::Parser
123 # honours v2 (<nil/>), send a hash with one key "null"
124 # pointing to an empty string.
125 if (! defined $ret) {
129 my $string=eval { RPC::XML::response->new($ret)->as_string };
130 if ($@ && ref $ret) {
131 # One common reason for serialisation to
132 # fail is a complex return type that cannot
133 # be represented as an XML RPC response.
134 # Handle this case by just returning 1.
135 $string=eval { RPC::XML::response->new(1)->as_string };
138 error("XML response serialisation failed: $@");
140 rpc_write($plugin->{out}, $string);
147 package IkiWiki::RPC::XML;
152 my $varname="IkiWiki::".shift;
156 my $ret=$varname->{$key};
163 my $varname="IkiWiki::".shift;
168 my $ret=$varname->{$key}=$value;
173 sub getstate ($$$$) {
179 return $IkiWiki::pagestate{$page}{$id}{$key};
182 sub setstate ($$$$;@) {
189 return $IkiWiki::pagestate{$page}{$id}{$key}=$value;
206 # Bind a given perl function name to a particular RPC request.
210 if (! exists $params{name} || ! exists $params{call}) {
211 die "inject needs name and call parameters";
214 IkiWiki::Plugin::external::rpc_call($plugin, $params{call}, @_)
216 $sub=memoize($sub) if $params{memoize};
218 # This will add it to the symbol table even if not present.
220 eval qq{*$params{name}=\$sub};
223 # This will ensure that everywhere it was exported to sees
224 # the injected version.
225 IkiWiki::inject(name => $params{name}, call => $sub);
230 # the call parameter is a function name to call, since XML RPC
231 # cannot pass a function reference
235 my $callback=$params{call};
236 delete $params{call};
238 IkiWiki::hook(%params, call => sub {
239 IkiWiki::Plugin::external::rpc_call($plugin, $callback, @_);
243 sub pagespec_match ($@) {
244 # convert return object into a XML RPC boolean
249 return RPC::XML::boolean->new(0 + IkiWiki::pagespec_match(