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, $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_call($plugins{$plugin}, "import");
35 sub rpc_write ($$) { #{{{
39 $fh->print($string."\n");
43 sub rpc_call ($$;@) { #{{{
48 my $req=RPC::XML::request->new($command, @_);
49 rpc_write($plugin->{out}, $req->as_string);
51 # process incoming rpc until a result is available
52 while ($_ = $plugin->{in}->getline) {
54 while ($plugin->{accum} =~ /^\s*(<\?xml\s.*?<\/(?:methodCall|methodResponse)>)\n(.*)/s) {
56 my $r = RPC::XML::Parser->new->parse($1);
57 error("XML RPC parser failure: $r") unless ref $r;
58 if ($r->isa('RPC::XML::response')) {
60 if ($value->isa('RPC::XML::array')) {
61 return @{$value->value};
63 elsif ($value->isa('RPC::XML::struct')) {
64 return %{$value->value};
66 elsif ($value->isa('RPC::XML::fault')) {
75 my @args=map { $_->value } @{$r->args};
77 # When dispatching a function, first look in
78 # IkiWiki::RPC::XML. This allows overriding
79 # IkiWiki functions with RPC friendly versions.
81 if (exists $IkiWiki::RPC::XML::{$name}) {
82 $ret=$IkiWiki::RPC::XML::{$name}($plugin, @args);
84 elsif (exists $IkiWiki::{$name}) {
85 $ret=$IkiWiki::{$name}(@args);
88 error("XML RPC call error, unknown function: $name");
91 my $string=eval { RPC::XML::response->new($ret)->as_string };
93 # One common reason for serialisation to
94 # fail is a complex return type that cannot
95 # be represented as an XML RPC response.
96 # Handle this case by just returning 1.
97 $string=eval { RPC::XML::response->new(1)->as_string };
100 error("XML response serialisation failed: $@");
102 rpc_write($plugin->{out}, $string);
109 package IkiWiki::RPC::XML;
111 sub getvar ($$$) { #{{{
113 my $varname="IkiWiki::".shift;
117 my $ret=$varname->{$key};
122 sub setvar ($$$;@) { #{{{
124 my $varname="IkiWiki::".shift;
128 my $ret=$varname->{$key}=@_;
133 sub inject ($@) { #{{{
134 # Bind a given perl function name to a particular RPC request.
138 if (! exists $params{name} || ! exists $params{call}) {
139 die "inject needs name and call parameters";
142 IkiWiki::Plugin::external::rpc_call($plugin, $params{call}, @_)
144 eval qq{*$params{name}=\$sub};
149 # the call parameter is a function name to call, since XML RPC
150 # cannot pass a function reference
154 my $callback=$params{call};
155 delete $params{call};
157 IkiWiki::hook(%params, call => sub {
158 IkiWiki::Plugin::external::rpc_call($plugin, $callback, @_)
162 sub pagespec_match ($@) { #{{{
163 # convert pagespec_match's return object into a XML RPC boolean
166 return RPC::XML::boolean->new(0 + IkiWiki::pagespec_march(@_));