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 getstate ($$$$) { #{{{
141 return $IkiWiki::pagestate{$page}{$id}{$key};
144 sub setstate ($$$$;@) { #{{{
150 return $IkiWiki::pagestate{$page}{$id}{$key}=@_;
153 sub inject ($@) { #{{{
154 # Bind a given perl function name to a particular RPC request.
158 if (! exists $params{name} || ! exists $params{call}) {
159 die "inject needs name and call parameters";
162 IkiWiki::Plugin::external::rpc_call($plugin, $params{call}, @_)
164 eval qq{*$params{name}=\$sub};
165 memoize($params{name}) if $params{memoize};
170 # the call parameter is a function name to call, since XML RPC
171 # cannot pass a function reference
175 my $callback=$params{call};
176 delete $params{call};
178 IkiWiki::hook(%params, call => sub {
179 IkiWiki::Plugin::external::rpc_call($plugin, $callback, @_)
183 sub pagespec_match ($@) { #{{{
184 # convert pagespec_match's return object into a XML RPC boolean
187 return RPC::XML::boolean->new(0 + IkiWiki::pagespec_march(@_));