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;
20 return unless defined $plugin;
22 my ($plugin_read, $plugin_write);
23 my $pid = open2($plugin_read, $plugin_write,
24 IkiWiki::possibly_foolish_untaint($plugin));
26 # open2 doesn't respect "use open ':utf8'"
27 binmode($plugin_read, ':utf8');
28 binmode($plugin_write, ':utf8');
30 $plugins{$plugin}={in => $plugin_read, out => $plugin_write, pid => $pid,
33 rpc_call($plugins{$plugin}, "import");
36 sub rpc_write ($$) { #{{{
40 $fh->print($string."\n");
44 sub rpc_call ($$;@) { #{{{
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) {
57 my $r = RPC::XML::Parser->new->parse($1);
58 error("XML RPC parser failure: $r") unless ref $r;
59 if ($r->isa('RPC::XML::response')) {
61 if ($value->isa('RPC::XML::array')) {
62 return @{$value->value};
64 elsif ($value->isa('RPC::XML::struct')) {
65 return %{$value->value};
67 elsif ($value->isa('RPC::XML::fault')) {
76 my @args=map { $_->value } @{$r->args};
78 # When dispatching a function, first look in
79 # IkiWiki::RPC::XML. This allows overriding
80 # IkiWiki functions with RPC friendly versions.
82 if (exists $IkiWiki::RPC::XML::{$name}) {
83 $ret=$IkiWiki::RPC::XML::{$name}($plugin, @args);
85 elsif (exists $IkiWiki::{$name}) {
86 $ret=$IkiWiki::{$name}(@args);
89 error("XML RPC call error, unknown function: $name");
92 my $string=eval { RPC::XML::response->new($ret)->as_string };
94 # One common reason for serialisation to
95 # fail is a complex return type that cannot
96 # be represented as an XML RPC response.
97 # Handle this case by just returning 1.
98 $string=eval { RPC::XML::response->new(1)->as_string };
101 error("XML response serialisation failed: $@");
103 rpc_write($plugin->{out}, $string);
110 package IkiWiki::RPC::XML;
113 sub getvar ($$$) { #{{{
115 my $varname="IkiWiki::".shift;
119 my $ret=$varname->{$key};
124 sub setvar ($$$;@) { #{{{
126 my $varname="IkiWiki::".shift;
130 my $ret=$varname->{$key}=@_;
135 sub inject ($@) { #{{{
136 # Bind a given perl function name to a particular RPC request.
140 if (! exists $params{name} || ! exists $params{call}) {
141 die "inject needs name and call parameters";
144 IkiWiki::Plugin::external::rpc_call($plugin, $params{call}, @_)
146 eval qq{*$params{name}=\$sub};
147 memoize($params{name}) if $params{memoize};
152 # the call parameter is a function name to call, since XML RPC
153 # cannot pass a function reference
157 my $callback=$params{call};
158 delete $params{call};
160 IkiWiki::hook(%params, call => sub {
161 IkiWiki::Plugin::external::rpc_call($plugin, $callback, @_)
165 sub pagespec_match ($@) { #{{{
166 # convert pagespec_match's return object into a XML RPC boolean
169 return RPC::XML::boolean->new(0 + IkiWiki::pagespec_march(@_));