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,
32 $RPC::XML::ENCODING="utf-8";
34 rpc_call($plugins{$plugin}, "import");
37 sub rpc_write ($$) { #{{{
41 $fh->print($string."\n");
45 sub rpc_call ($$;@) { #{{{
50 my $req=RPC::XML::request->new($command, @_);
51 rpc_write($plugin->{out}, $req->as_string);
53 # process incoming rpc until a result is available
54 while ($_ = $plugin->{in}->getline) {
56 while ($plugin->{accum} =~ /^\s*(<\?xml\s.*?<\/(?:methodCall|methodResponse)>)\n(.*)/s) {
58 my $r = RPC::XML::Parser->new->parse($1);
59 error("XML RPC parser failure: $r") unless ref $r;
60 if ($r->isa('RPC::XML::response')) {
62 if ($r->is_fault($value)) {
63 # throw the error as best we can
64 print STDERR $value->string."\n";
67 elsif ($value->isa('RPC::XML::array')) {
68 return @{$value->value};
70 elsif ($value->isa('RPC::XML::struct')) {
71 return %{$value->value};
79 my @args=map { $_->value } @{$r->args};
81 # When dispatching a function, first look in
82 # IkiWiki::RPC::XML. This allows overriding
83 # IkiWiki functions with RPC friendly versions.
85 if (exists $IkiWiki::RPC::XML::{$name}) {
86 $ret=$IkiWiki::RPC::XML::{$name}($plugin, @args);
88 elsif (exists $IkiWiki::{$name}) {
89 $ret=$IkiWiki::{$name}(@args);
92 error("XML RPC call error, unknown function: $name");
95 my $string=eval { RPC::XML::response->new($ret)->as_string };
97 # One common reason for serialisation to
98 # fail is a complex return type that cannot
99 # be represented as an XML RPC response.
100 # Handle this case by just returning 1.
101 $string=eval { RPC::XML::response->new(1)->as_string };
104 error("XML response serialisation failed: $@");
106 rpc_write($plugin->{out}, $string);
113 package IkiWiki::RPC::XML;
116 sub getvar ($$$) { #{{{
118 my $varname="IkiWiki::".shift;
122 my $ret=$varname->{$key};
127 sub setvar ($$$;@) { #{{{
129 my $varname="IkiWiki::".shift;
134 my $ret=$varname->{$key}=$value;
139 sub getstate ($$$$) { #{{{
145 return $IkiWiki::pagestate{$page}{$id}{$key};
148 sub setstate ($$$$;@) { #{{{
154 return $IkiWiki::pagestate{$page}{$id}{$key}=@_;
157 sub getargv ($) { #{{{
163 sub setargv ($@) { #{{{
170 sub inject ($@) { #{{{
171 # Bind a given perl function name to a particular RPC request.
175 if (! exists $params{name} || ! exists $params{call}) {
176 die "inject needs name and call parameters";
179 IkiWiki::Plugin::external::rpc_call($plugin, $params{call}, @_)
181 eval qq{*$params{name}=\$sub};
182 memoize($params{name}) if $params{memoize};
187 # the call parameter is a function name to call, since XML RPC
188 # cannot pass a function reference
192 my $callback=$params{call};
193 delete $params{call};
195 IkiWiki::hook(%params, call => sub {
196 my $ret=IkiWiki::Plugin::external::rpc_call($plugin, $callback, @_);
201 sub pagespec_match ($@) { #{{{
202 # convert pagespec_match's return object into a XML RPC boolean
205 return RPC::XML::boolean->new(0 + IkiWiki::pagespec_march(@_));