summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/external.pm
blob: 300dee7ed5fe89f96eff5d7d3e547c2e210678a3 (plain)
  1. #!/usr/bin/perl
  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;
  6. use warnings;
  7. use strict;
  8. use IkiWiki 2.00;
  9. use RPC::XML;
  10. use RPC::XML::Parser;
  11. use IPC::Open2;
  12. use IO::Handle;
  13. my %plugins;
  14. sub import { #{{{
  15. my $self=shift;
  16. my $plugin=shift;
  17. return unless defined $plugin;
  18. my ($plugin_read, $plugin_write);
  19. my $pid = open2($plugin_read, $plugin_write,
  20. IkiWiki::possibly_foolish_untaint($plugin));
  21. # open2 doesn't respect "use open ':utf8'"
  22. binmode($plugin_read, ':utf8');
  23. binmode($plugin_write, ':utf8');
  24. $plugins{$plugin}={in => $plugin_read, out => $plugin_write, pid => $pid,
  25. accum => ""};
  26. $RPC::XML::ENCODING="utf-8";
  27. rpc_call($plugins{$plugin}, "import");
  28. } #}}}
  29. sub rpc_write ($$) { #{{{
  30. my $fh=shift;
  31. my $string=shift;
  32. $fh->print($string."\n");
  33. $fh->flush;
  34. } #}}}
  35. sub rpc_call ($$;@) { #{{{
  36. my $plugin=shift;
  37. my $command=shift;
  38. # send the command
  39. my $req=RPC::XML::request->new($command, @_);
  40. rpc_write($plugin->{out}, $req->as_string);
  41. # process incoming rpc until a result is available
  42. while ($_ = $plugin->{in}->getline) {
  43. $plugin->{accum}.=$_;
  44. while ($plugin->{accum} =~ /^\s*(<\?xml\s.*?<\/(?:methodCall|methodResponse)>)\n(.*)/s) {
  45. $plugin->{accum}=$2;
  46. my $r = RPC::XML::Parser->new->parse($1);
  47. error("XML RPC parser failure: $r") unless ref $r;
  48. if ($r->isa('RPC::XML::response')) {
  49. my $value=$r->value;
  50. if ($r->is_fault($value)) {
  51. # throw the error as best we can
  52. print STDERR $value->string."\n";
  53. return "";
  54. }
  55. elsif ($value->isa('RPC::XML::array')) {
  56. return @{$value->value};
  57. }
  58. elsif ($value->isa('RPC::XML::struct')) {
  59. return %{$value->value};
  60. }
  61. else {
  62. return $value->value;
  63. }
  64. }
  65. my $name=$r->name;
  66. my @args=map { $_->value } @{$r->args};
  67. # When dispatching a function, first look in
  68. # IkiWiki::RPC::XML. This allows overriding
  69. # IkiWiki functions with RPC friendly versions.
  70. my $ret;
  71. if (exists $IkiWiki::RPC::XML::{$name}) {
  72. $ret=$IkiWiki::RPC::XML::{$name}($plugin, @args);
  73. }
  74. elsif (exists $IkiWiki::{$name}) {
  75. $ret=$IkiWiki::{$name}(@args);
  76. }
  77. else {
  78. error("XML RPC call error, unknown function: $name");
  79. }
  80. my $string=eval { RPC::XML::response->new($ret)->as_string };
  81. if ($@ && ref $ret) {
  82. # One common reason for serialisation to
  83. # fail is a complex return type that cannot
  84. # be represented as an XML RPC response.
  85. # Handle this case by just returning 1.
  86. $string=eval { RPC::XML::response->new(1)->as_string };
  87. }
  88. if ($@) {
  89. error("XML response serialisation failed: $@");
  90. }
  91. rpc_write($plugin->{out}, $string);
  92. }
  93. }
  94. return undef;
  95. } #}}}
  96. package IkiWiki::RPC::XML;
  97. use Memoize;
  98. sub getvar ($$$) { #{{{
  99. my $plugin=shift;
  100. my $varname="IkiWiki::".shift;
  101. my $key=shift;
  102. no strict 'refs';
  103. my $ret=$varname->{$key};
  104. use strict 'refs';
  105. return $ret;
  106. } #}}}
  107. sub setvar ($$$;@) { #{{{
  108. my $plugin=shift;
  109. my $varname="IkiWiki::".shift;
  110. my $key=shift;
  111. no strict 'refs';
  112. my $ret=$varname->{$key}=@_;
  113. use strict 'refs';
  114. return $ret;
  115. } #}}}
  116. sub getstate ($$$$) { #{{{
  117. my $plugin=shift;
  118. my $page=shift;
  119. my $id=shift;
  120. my $key=shift;
  121. return $IkiWiki::pagestate{$page}{$id}{$key};
  122. } #}}}
  123. sub setstate ($$$$;@) { #{{{
  124. my $plugin=shift;
  125. my $page=shift;
  126. my $id=shift;
  127. my $key=shift;
  128. return $IkiWiki::pagestate{$page}{$id}{$key}=@_;
  129. } #}}}
  130. sub getargv ($) { #{{{
  131. my $plugin=shift;
  132. return @ARGV;
  133. } #}}}
  134. sub setargv ($@) { #{{{
  135. my $plugin=shift;
  136. @ARGV=@_;
  137. } #}}}
  138. sub inject ($@) { #{{{
  139. # Bind a given perl function name to a particular RPC request.
  140. my $plugin=shift;
  141. my %params=@_;
  142. if (! exists $params{name} || ! exists $params{call}) {
  143. die "inject needs name and call parameters";
  144. }
  145. my $sub = sub {
  146. IkiWiki::Plugin::external::rpc_call($plugin, $params{call}, @_)
  147. };
  148. eval qq{*$params{name}=\$sub};
  149. memoize($params{name}) if $params{memoize};
  150. return 1;
  151. } #}}}
  152. sub hook ($@) { #{{{
  153. # the call parameter is a function name to call, since XML RPC
  154. # cannot pass a function reference
  155. my $plugin=shift;
  156. my %params=@_;
  157. my $callback=$params{call};
  158. delete $params{call};
  159. IkiWiki::hook(%params, call => sub {
  160. my $ret=IkiWiki::Plugin::external::rpc_call($plugin, $callback, @_);
  161. return $ret;
  162. });
  163. } #}}}
  164. sub pagespec_match ($@) { #{{{
  165. # convert pagespec_match's return object into a XML RPC boolean
  166. my $plugin=shift;
  167. return RPC::XML::boolean->new(0 + IkiWiki::pagespec_march(@_));
  168. } #}}}
  169. 1