2007-08-13 05:07:31 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# Support for external plugins written in other languages.
|
2008-08-03 22:40:12 +02:00
|
|
|
# Communication via XML RPC to a pipe.
|
2007-08-13 05:07:31 +02:00
|
|
|
# See externaldemo for an example of a plugin that uses this.
|
|
|
|
package IkiWiki::Plugin::external;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2007-08-13 05:07:31 +02:00
|
|
|
use RPC::XML;
|
|
|
|
use IPC::Open2;
|
|
|
|
use IO::Handle;
|
|
|
|
|
|
|
|
my %plugins;
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2007-08-13 05:07:31 +02:00
|
|
|
my $self=shift;
|
|
|
|
my $plugin=shift;
|
|
|
|
return unless defined $plugin;
|
|
|
|
|
|
|
|
my ($plugin_read, $plugin_write);
|
2007-10-14 00:18:33 +02:00
|
|
|
my $pid = open2($plugin_read, $plugin_write,
|
|
|
|
IkiWiki::possibly_foolish_untaint($plugin));
|
2007-08-13 05:07:31 +02:00
|
|
|
|
|
|
|
# open2 doesn't respect "use open ':utf8'"
|
|
|
|
binmode($plugin_read, ':utf8');
|
|
|
|
binmode($plugin_write, ':utf8');
|
|
|
|
|
|
|
|
$plugins{$plugin}={in => $plugin_read, out => $plugin_write, pid => $pid,
|
|
|
|
accum => ""};
|
2010-09-14 21:37:45 +02:00
|
|
|
|
2008-02-11 06:11:49 +01:00
|
|
|
$RPC::XML::ENCODING="utf-8";
|
2010-09-14 21:37:45 +02:00
|
|
|
$RPC::XML::FORCE_STRING_ENCODING="true";
|
2007-08-13 05:07:31 +02:00
|
|
|
|
|
|
|
rpc_call($plugins{$plugin}, "import");
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-08-13 05:07:31 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub rpc_write ($$) {
|
2007-08-13 05:07:31 +02:00
|
|
|
my $fh=shift;
|
|
|
|
my $string=shift;
|
|
|
|
|
|
|
|
$fh->print($string."\n");
|
|
|
|
$fh->flush;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-08-13 05:07:31 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub rpc_call ($$;@) {
|
2007-08-13 05:07:31 +02:00
|
|
|
my $plugin=shift;
|
|
|
|
my $command=shift;
|
|
|
|
|
|
|
|
# send the command
|
|
|
|
my $req=RPC::XML::request->new($command, @_);
|
|
|
|
rpc_write($plugin->{out}, $req->as_string);
|
|
|
|
|
|
|
|
# process incoming rpc until a result is available
|
|
|
|
while ($_ = $plugin->{in}->getline) {
|
|
|
|
$plugin->{accum}.=$_;
|
|
|
|
while ($plugin->{accum} =~ /^\s*(<\?xml\s.*?<\/(?:methodCall|methodResponse)>)\n(.*)/s) {
|
|
|
|
$plugin->{accum}=$2;
|
2009-09-29 19:35:30 +02:00
|
|
|
my $parser;
|
|
|
|
eval q{
|
|
|
|
use RPC::XML::ParserFactory;
|
|
|
|
$parser = RPC::XML::ParserFactory->new;
|
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
# old interface
|
|
|
|
eval q{
|
|
|
|
use RPC::XML::Parser;
|
|
|
|
$parser = RPC::XML::Parser->new;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
my $r=$parser->parse($1);
|
2007-08-13 05:07:31 +02:00
|
|
|
error("XML RPC parser failure: $r") unless ref $r;
|
|
|
|
if ($r->isa('RPC::XML::response')) {
|
|
|
|
my $value=$r->value;
|
2008-03-15 18:49:22 +01:00
|
|
|
if ($r->is_fault($value)) {
|
|
|
|
# throw the error as best we can
|
|
|
|
print STDERR $value->string."\n";
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
elsif ($value->isa('RPC::XML::array')) {
|
2007-08-13 05:07:31 +02:00
|
|
|
return @{$value->value};
|
|
|
|
}
|
|
|
|
elsif ($value->isa('RPC::XML::struct')) {
|
2008-03-21 19:12:12 +01:00
|
|
|
my %hash=%{$value->value};
|
|
|
|
|
|
|
|
# XML-RPC v1 does not allow for
|
|
|
|
# nil/null/None/undef values to be
|
2009-09-29 19:35:30 +02:00
|
|
|
# transmitted. The <nil/> extension
|
|
|
|
# is the right fix, but for
|
|
|
|
# back-compat, let external plugins send
|
2008-03-21 19:12:12 +01:00
|
|
|
# a hash with one key "null" pointing
|
|
|
|
# to an empty string.
|
|
|
|
if (exists $hash{null} &&
|
|
|
|
$hash{null} eq "" &&
|
|
|
|
int(keys(%hash)) == 1) {
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
return %hash;
|
2007-08-13 05:07:31 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $value->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my $name=$r->name;
|
|
|
|
my @args=map { $_->value } @{$r->args};
|
|
|
|
|
|
|
|
# When dispatching a function, first look in
|
|
|
|
# IkiWiki::RPC::XML. This allows overriding
|
|
|
|
# IkiWiki functions with RPC friendly versions.
|
|
|
|
my $ret;
|
|
|
|
if (exists $IkiWiki::RPC::XML::{$name}) {
|
|
|
|
$ret=$IkiWiki::RPC::XML::{$name}($plugin, @args);
|
|
|
|
}
|
|
|
|
elsif (exists $IkiWiki::{$name}) {
|
|
|
|
$ret=$IkiWiki::{$name}(@args);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error("XML RPC call error, unknown function: $name");
|
|
|
|
}
|
|
|
|
|
2008-03-21 19:12:12 +01:00
|
|
|
# XML-RPC v1 does not allow for nil/null/None/undef
|
|
|
|
# values to be transmitted, so until XML::RPC::Parser
|
|
|
|
# honours v2 (<nil/>), send a hash with one key "null"
|
|
|
|
# pointing to an empty string.
|
|
|
|
if (! defined $ret) {
|
|
|
|
$ret={"null" => ""};
|
|
|
|
}
|
|
|
|
|
2007-08-13 05:07:31 +02:00
|
|
|
my $string=eval { RPC::XML::response->new($ret)->as_string };
|
|
|
|
if ($@ && ref $ret) {
|
|
|
|
# One common reason for serialisation to
|
|
|
|
# fail is a complex return type that cannot
|
|
|
|
# be represented as an XML RPC response.
|
|
|
|
# Handle this case by just returning 1.
|
|
|
|
$string=eval { RPC::XML::response->new(1)->as_string };
|
|
|
|
}
|
|
|
|
if ($@) {
|
|
|
|
error("XML response serialisation failed: $@");
|
|
|
|
}
|
|
|
|
rpc_write($plugin->{out}, $string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return undef;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-08-13 05:07:31 +02:00
|
|
|
|
|
|
|
package IkiWiki::RPC::XML;
|
2007-08-13 09:00:53 +02:00
|
|
|
use Memoize;
|
2007-08-13 05:07:31 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getvar ($$$) {
|
2007-08-13 05:07:31 +02:00
|
|
|
my $plugin=shift;
|
|
|
|
my $varname="IkiWiki::".shift;
|
|
|
|
my $key=shift;
|
|
|
|
|
|
|
|
no strict 'refs';
|
|
|
|
my $ret=$varname->{$key};
|
|
|
|
use strict 'refs';
|
|
|
|
return $ret;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-08-13 05:07:31 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub setvar ($$$;@) {
|
2007-08-13 05:07:31 +02:00
|
|
|
my $plugin=shift;
|
|
|
|
my $varname="IkiWiki::".shift;
|
|
|
|
my $key=shift;
|
2008-03-19 20:18:38 +01:00
|
|
|
my $value=shift;
|
2007-08-13 05:07:31 +02:00
|
|
|
|
|
|
|
no strict 'refs';
|
2008-03-19 20:18:38 +01:00
|
|
|
my $ret=$varname->{$key}=$value;
|
2007-08-13 05:07:31 +02:00
|
|
|
use strict 'refs';
|
|
|
|
return $ret;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-08-13 05:07:31 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getstate ($$$$) {
|
2007-12-08 23:40:50 +01:00
|
|
|
my $plugin=shift;
|
|
|
|
my $page=shift;
|
|
|
|
my $id=shift;
|
|
|
|
my $key=shift;
|
|
|
|
|
|
|
|
return $IkiWiki::pagestate{$page}{$id}{$key};
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-12-08 23:40:50 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub setstate ($$$$;@) {
|
2007-12-08 23:40:50 +01:00
|
|
|
my $plugin=shift;
|
|
|
|
my $page=shift;
|
|
|
|
my $id=shift;
|
|
|
|
my $key=shift;
|
2008-03-19 20:49:37 +01:00
|
|
|
my $value=shift;
|
2007-12-08 23:40:50 +01:00
|
|
|
|
2008-03-19 20:49:37 +01:00
|
|
|
return $IkiWiki::pagestate{$page}{$id}{$key}=$value;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-12-08 23:40:50 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getargv ($) {
|
2008-03-15 19:19:49 +01:00
|
|
|
my $plugin=shift;
|
|
|
|
|
2008-03-19 20:12:59 +01:00
|
|
|
return \@ARGV;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-03-15 19:19:49 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub setargv ($@) {
|
2008-03-15 19:19:49 +01:00
|
|
|
my $plugin=shift;
|
2008-03-19 20:49:00 +01:00
|
|
|
my $array=shift;
|
2008-03-15 19:19:49 +01:00
|
|
|
|
2008-03-19 20:49:00 +01:00
|
|
|
@ARGV=@$array;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-03-15 19:19:49 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub inject ($@) {
|
2007-08-13 05:07:31 +02:00
|
|
|
# Bind a given perl function name to a particular RPC request.
|
|
|
|
my $plugin=shift;
|
|
|
|
my %params=@_;
|
|
|
|
|
|
|
|
if (! exists $params{name} || ! exists $params{call}) {
|
|
|
|
die "inject needs name and call parameters";
|
|
|
|
}
|
|
|
|
my $sub = sub {
|
|
|
|
IkiWiki::Plugin::external::rpc_call($plugin, $params{call}, @_)
|
|
|
|
};
|
2008-10-21 23:57:19 +02:00
|
|
|
$sub=memoize($sub) if $params{memoize};
|
|
|
|
|
|
|
|
# This will add it to the symbol table even if not present.
|
2008-10-21 23:03:08 +02:00
|
|
|
no warnings;
|
2007-08-13 05:07:31 +02:00
|
|
|
eval qq{*$params{name}=\$sub};
|
2008-10-21 23:03:08 +02:00
|
|
|
use warnings;
|
2008-10-21 23:57:19 +02:00
|
|
|
|
|
|
|
# This will ensure that everywhere it was exported to sees
|
|
|
|
# the injected version.
|
|
|
|
IkiWiki::inject(name => $params{name}, call => $sub);
|
2007-08-13 05:07:31 +02:00
|
|
|
return 1;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-08-13 05:07:31 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub hook ($@) {
|
2007-08-13 05:07:31 +02:00
|
|
|
# the call parameter is a function name to call, since XML RPC
|
|
|
|
# cannot pass a function reference
|
|
|
|
my $plugin=shift;
|
|
|
|
my %params=@_;
|
|
|
|
|
|
|
|
my $callback=$params{call};
|
|
|
|
delete $params{call};
|
|
|
|
|
|
|
|
IkiWiki::hook(%params, call => sub {
|
2008-08-06 07:05:44 +02:00
|
|
|
IkiWiki::Plugin::external::rpc_call($plugin, $callback, @_);
|
2007-08-13 05:07:31 +02:00
|
|
|
});
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-08-13 05:07:31 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub pagespec_match ($@) {
|
2009-04-23 21:45:30 +02:00
|
|
|
# convert return object into a XML RPC boolean
|
2007-08-13 05:07:31 +02:00
|
|
|
my $plugin=shift;
|
2009-05-06 18:57:37 +02:00
|
|
|
my $page=shift;
|
|
|
|
my $spec=shift;
|
2007-08-13 05:07:31 +02:00
|
|
|
|
2009-05-06 18:57:37 +02:00
|
|
|
return RPC::XML::boolean->new(0 + IkiWiki::pagespec_match(
|
|
|
|
$page, $spec, @_));
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-08-13 05:07:31 +02:00
|
|
|
|
|
|
|
1
|