]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/external.pm
po: clarify code with some comments
[git.ikiwiki.info.git] / IkiWiki / Plugin / external.pm
index a30ef3f61889eb9195e96c7588dc68e19399afb9..4ce9c8babb62d45bacb3bb76a4773bd27cb09b5c 100644 (file)
@@ -1,6 +1,6 @@
 #!/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;
 
@@ -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
@@ -150,8 +173,9 @@ sub setstate ($$$$;@) { #{{{
        my $page=shift;
        my $id=shift;
        my $key=shift;
+       my $value=shift;
 
-       return $IkiWiki::pagestate{$page}{$id}{$key}=@_;
+       return $IkiWiki::pagestate{$page}{$id}{$key}=$value;
 } #}}}
 
 sub getargv ($) { #{{{
@@ -162,8 +186,9 @@ sub getargv ($) { #{{{
 
 sub setargv ($@) { #{{{
        my $plugin=shift;
+       my $array=shift;
 
-       @ARGV=@_;
+       @ARGV=@$array;
 } #}}}
 
 sub inject ($@) { #{{{
@@ -177,8 +202,16 @@ 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;
 } #}}}
 
@@ -192,8 +225,7 @@ 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, @_);
        });
 } #}}}