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,
32 $RPC::XML::ENCODING="utf-8";
33 $RPC::XML::FORCE_STRING_ENCODING="true";
35 rpc_call($plugins{$plugin}, "import");
42 $fh->print($string."\n");
51 my $req=RPC::XML::request->new($command, @_);
52 rpc_write($plugin->{out}, $req->as_string);
54 # process incoming rpc until a result is available
55 while ($_ = $plugin->{in}->getline) {
57 while ($plugin->{accum} =~ /^\s*(<\?xml\s.*?<\/(?:methodCall|methodResponse)>)\n(.*)/s) {
61 use RPC::XML::ParserFactory;
62 $parser = RPC::XML::ParserFactory->new;
68 $parser = RPC::XML::Parser->new;
71 my $r=$parser->parse($1);
72 error("XML RPC parser failure: $r") unless ref $r;
73 if ($r->isa('RPC::XML::response')) {
75 if ($r->is_fault($value)) {
76 # throw the error as best we can
77 print STDERR $value->string."\n";
80 elsif ($value->isa('RPC::XML::array')) {
81 return @{$value->value};
83 elsif ($value->isa('RPC::XML::struct')) {
84 my %hash=%{$value->value};
86 # XML-RPC v1 does not allow for
87 # nil/null/None/undef values to be
88 # transmitted. The <nil/> extension
89 # is the right fix, but for
90 # back-compat, let external plugins send
91 # a hash with one key "null" pointing
93 if (exists $hash{null} &&
95 int(keys(%hash)) == 1) {
102 return $value->value;
107 my @args=map { $_->value } @{$r->args};
109 # When dispatching a function, first look in
110 # IkiWiki::RPC::XML. This allows overriding
111 # IkiWiki functions with RPC friendly versions.
113 if (exists $IkiWiki::RPC::XML::{$name}) {
114 $ret=$IkiWiki::RPC::XML::{$name}($plugin, @args);
116 elsif (exists $IkiWiki::{$name}) {
117 $ret=$IkiWiki::{$name}(@args);
120 error("XML RPC call error, unknown function: $name");
123 # XML-RPC v1 does not allow for nil/null/None/undef
124 # values to be transmitted, so until XML::RPC::Parser
125 # honours v2 (<nil/>), send a hash with one key "null"
126 # pointing to an empty string.
127 if (! defined $ret) {
131 my $string=eval { RPC::XML::response->new($ret)->as_string };
132 if ($@ && ref $ret) {
133 # One common reason for serialisation to
134 # fail is a complex return type that cannot
135 # be represented as an XML RPC response.
136 # Handle this case by just returning 1.
137 $string=eval { RPC::XML::response->new(1)->as_string };
140 error("XML response serialisation failed: $@");
142 rpc_write($plugin->{out}, $string);
149 package IkiWiki::RPC::XML;
154 my $varname="IkiWiki::".shift;
158 my $ret=$varname->{$key};
165 my $varname="IkiWiki::".shift;
170 my $ret=$varname->{$key}=$value;
175 sub getstate ($$$$) {
181 return $IkiWiki::pagestate{$page}{$id}{$key};
184 sub setstate ($$$$;@) {
191 return $IkiWiki::pagestate{$page}{$id}{$key}=$value;
208 # Bind a given perl function name to a particular RPC request.
212 if (! exists $params{name} || ! exists $params{call}) {
213 die "inject needs name and call parameters";
216 IkiWiki::Plugin::external::rpc_call($plugin, $params{call}, @_)
218 $sub=memoize($sub) if $params{memoize};
220 # This will add it to the symbol table even if not present.
222 eval qq{*$params{name}=\$sub};
225 # This will ensure that everywhere it was exported to sees
226 # the injected version.
227 IkiWiki::inject(name => $params{name}, call => $sub);
232 # the call parameter is a function name to call, since XML RPC
233 # cannot pass a function reference
237 my $callback=$params{call};
238 delete $params{call};
240 IkiWiki::hook(%params, call => sub {
241 IkiWiki::Plugin::external::rpc_call($plugin, $callback, @_);
245 sub pagespec_match ($@) {
246 # convert return object into a XML RPC boolean
251 return RPC::XML::boolean->new(0 + IkiWiki::pagespec_match(