]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/external.pm
typo
[ikiwiki.git] / IkiWiki / Plugin / external.pm
index 4c2e5d2feb28ce5d842dcf75dab275a3e7855493..f0acc7e4e1b52a5787ca00b139a21eef0be7c4f5 100644 (file)
@@ -1,12 +1,12 @@
 #!/usr/bin/perl
 # Support for external plugins written in other languages.
-# Communication via XML RPC a pipe.
+# Communication via XML RPC to a pipe.
 # See externaldemo for an example of a plugin that uses this.
 package IkiWiki::Plugin::external;
 
 use warnings;
 use strict;
-use IkiWiki 2.00;
+use IkiWiki 3.00;
 use RPC::XML;
 use RPC::XML::Parser;
 use IPC::Open2;
@@ -14,7 +14,7 @@ use IO::Handle;
 
 my %plugins;
 
-sub import { #{{{
+sub import {
        my $self=shift;
        my $plugin=shift;
        return unless defined $plugin;
@@ -32,17 +32,17 @@ sub import { #{{{
        $RPC::XML::ENCODING="utf-8";
 
        rpc_call($plugins{$plugin}, "import");
-} #}}}
+}
 
-sub rpc_write ($$) { #{{{
+sub rpc_write ($$) {
        my $fh=shift;
        my $string=shift;
 
        $fh->print($string."\n");
        $fh->flush;
-} #}}}
+}
 
-sub rpc_call ($$;@) { #{{{
+sub rpc_call ($$;@) {
        my $plugin=shift;
        my $command=shift;
 
@@ -68,7 +68,22 @@ sub rpc_call ($$;@) { #{{{
                                        return @{$value->value};
                                }
                                elsif ($value->isa('RPC::XML::struct')) {
-                                       return %{$value->value};
+                                       my %hash=%{$value->value};
+
+                                       # XML-RPC v1 does not allow for
+                                       # nil/null/None/undef values to be
+                                       # transmitted, so until
+                                       # XML::RPC::Parser honours v2
+                                       # (<nil/>), external plugins send
+                                       # 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;
                                }
                                else {
                                        return $value->value;
@@ -92,6 +107,14 @@ sub rpc_call ($$;@) { #{{{
                                error("XML RPC call error, unknown function: $name");
                        }
 
+                       # 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" => ""};
+                       }
+
                        my $string=eval { RPC::XML::response->new($ret)->as_string };
                        if ($@ && ref $ret) {
                                # One common reason for serialisation to
@@ -108,12 +131,12 @@ sub rpc_call ($$;@) { #{{{
        }
 
        return undef;
-} #}}}
+}
 
 package IkiWiki::RPC::XML;
 use Memoize;
 
-sub getvar ($$$) { #{{{
+sub getvar ($$$) {
        my $plugin=shift;
        my $varname="IkiWiki::".shift;
        my $key=shift;
@@ -122,9 +145,9 @@ sub getvar ($$$) { #{{{
        my $ret=$varname->{$key};
        use strict 'refs';
        return $ret;
-} #}}}
+}
 
-sub setvar ($$$;@) { #{{{
+sub setvar ($$$;@) {
        my $plugin=shift;
        my $varname="IkiWiki::".shift;
        my $key=shift;
@@ -134,18 +157,18 @@ sub setvar ($$$;@) { #{{{
        my $ret=$varname->{$key}=$value;
        use strict 'refs';
        return $ret;
-} #}}}
+}
 
-sub getstate ($$$$) { #{{{
+sub getstate ($$$$) {
        my $plugin=shift;
        my $page=shift;
        my $id=shift;
        my $key=shift;
 
        return $IkiWiki::pagestate{$page}{$id}{$key};
-} #}}}
+}
 
-sub setstate ($$$$;@) { #{{{
+sub setstate ($$$$;@) {
        my $plugin=shift;
        my $page=shift;
        my $id=shift;
@@ -153,22 +176,22 @@ sub setstate ($$$$;@) { #{{{
        my $value=shift;
 
        return $IkiWiki::pagestate{$page}{$id}{$key}=$value;
-} #}}}
+}
 
-sub getargv ($) { #{{{
+sub getargv ($) {
        my $plugin=shift;
 
        return \@ARGV;
-} #}}}
+}
 
-sub setargv ($@) { #{{{
+sub setargv ($@) {
        my $plugin=shift;
        my $array=shift;
 
        @ARGV=@$array;
-} #}}}
+}
 
-sub inject ($@) { #{{{
+sub inject ($@) {
        # Bind a given perl function name to a particular RPC request.
        my $plugin=shift;
        my %params=@_;
@@ -179,12 +202,20 @@ sub inject ($@) { #{{{
        my $sub = sub {
                IkiWiki::Plugin::external::rpc_call($plugin, $params{call}, @_)
        };
+       $sub=memoize($sub) if $params{memoize};
+
+       # This will add it to the symbol table even if not present.
+       no warnings;
        eval qq{*$params{name}=\$sub};
-       memoize($params{name}) if $params{memoize};
+       use warnings;
+
+       # This will ensure that everywhere it was exported to sees
+       # the injected version.
+       IkiWiki::inject(name => $params{name}, call => $sub);
        return 1;
-} #}}}
+}
 
-sub hook ($@) { #{{{
+sub hook ($@) {
        # the call parameter is a function name to call, since XML RPC
        # cannot pass a function reference
        my $plugin=shift;
@@ -194,16 +225,22 @@ sub hook ($@) { #{{{
        delete $params{call};
 
        IkiWiki::hook(%params, call => sub {
-               my $ret=IkiWiki::Plugin::external::rpc_call($plugin, $callback, @_);
-               return $ret;
+               IkiWiki::Plugin::external::rpc_call($plugin, $callback, @_);
        });
-} #}}}
+}
+
+sub pagespec_match ($@) {
+       # convert return object into a XML RPC boolean
+       my $plugin=shift;
+
+       return RPC::XML::boolean->new(0 + IkiWiki::pagespec_match(@_));
+}
 
-sub pagespec_match ($@) { #{{{
-       # convert pagespec_match's return object into a XML RPC boolean
+sub pagespec_match_list ($@) {
+       # convert return object into a XML RPC boolean
        my $plugin=shift;
 
-       return RPC::XML::boolean->new(0 + IkiWiki::pagespec_march(@_));
-} #}}}
+       return RPC::XML::boolean->new(0 + IkiWiki::pagespec_match_list(@_));
+}
 
 1