summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/external.pm
blob: a5afdc4bef4b28b849b8b25ed414d1f4757f17b8 (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 ($value->isa('RPC::XML::array')) {
  51. return @{$value->value};
  52. }
  53. elsif ($value->isa('RPC::XML::struct')) {
  54. return %{$value->value};
  55. }
  56. elsif ($value->isa('RPC::XML::fault')) {
  57. die $value->string;
  58. }
  59. else {
  60. return $value->value;
  61. }
  62. }
  63. my $name=$r->name;
  64. my @args=map { $_->value } @{$r->args};
  65. # When dispatching a function, first look in
  66. # IkiWiki::RPC::XML. This allows overriding
  67. # IkiWiki functions with RPC friendly versions.
  68. my $ret;
  69. if (exists $IkiWiki::RPC::XML::{$name}) {
  70. $ret=$IkiWiki::RPC::XML::{$name}($plugin, @args);
  71. }
  72. elsif (exists $IkiWiki::{$name}) {
  73. $ret=$IkiWiki::{$name}(@args);
  74. }
  75. else {
  76. error("XML RPC call error, unknown function: $name");
  77. }
  78. my $string=eval { RPC::XML::response->new($ret)->as_string };
  79. if ($@ && ref $ret) {
  80. # One common reason for serialisation to
  81. # fail is a complex return type that cannot
  82. # be represented as an XML RPC response.
  83. # Handle this case by just returning 1.
  84. $string=eval { RPC::XML::response->new(1)->as_string };
  85. }
  86. if ($@) {
  87. error("XML response serialisation failed: $@");
  88. }
  89. rpc_write($plugin->{out}, $string);
  90. }
  91. }
  92. return undef;
  93. } #}}}
  94. package IkiWiki::RPC::XML;
  95. use Memoize;
  96. sub getvar ($$$) { #{{{
  97. my $plugin=shift;
  98. my $varname="IkiWiki::".shift;
  99. my $key=shift;
  100. no strict 'refs';
  101. my $ret=$varname->{$key};
  102. use strict 'refs';
  103. return $ret;
  104. } #}}}
  105. sub setvar ($$$;@) { #{{{
  106. my $plugin=shift;
  107. my $varname="IkiWiki::".shift;
  108. my $key=shift;
  109. no strict 'refs';
  110. my $ret=$varname->{$key}=@_;
  111. use strict 'refs';
  112. return $ret;
  113. } #}}}
  114. sub getstate ($$$$) { #{{{
  115. my $plugin=shift;
  116. my $page=shift;
  117. my $id=shift;
  118. my $key=shift;
  119. return $IkiWiki::pagestate{$page}{$id}{$key};
  120. } #}}}
  121. sub setstate ($$$$;@) { #{{{
  122. my $plugin=shift;
  123. my $page=shift;
  124. my $id=shift;
  125. my $key=shift;
  126. return $IkiWiki::pagestate{$page}{$id}{$key}=@_;
  127. } #}}}
  128. sub inject ($@) { #{{{
  129. # Bind a given perl function name to a particular RPC request.
  130. my $plugin=shift;
  131. my %params=@_;
  132. if (! exists $params{name} || ! exists $params{call}) {
  133. die "inject needs name and call parameters";
  134. }
  135. my $sub = sub {
  136. IkiWiki::Plugin::external::rpc_call($plugin, $params{call}, @_)
  137. };
  138. eval qq{*$params{name}=\$sub};
  139. memoize($params{name}) if $params{memoize};
  140. return 1;
  141. } #}}}
  142. sub hook ($@) { #{{{
  143. # the call parameter is a function name to call, since XML RPC
  144. # cannot pass a function reference
  145. my $plugin=shift;
  146. my %params=@_;
  147. my $callback=$params{call};
  148. delete $params{call};
  149. IkiWiki::hook(%params, call => sub {
  150. IkiWiki::Plugin::external::rpc_call($plugin, $callback, @_)
  151. });
  152. } #}}}
  153. sub pagespec_match ($@) { #{{{
  154. # convert pagespec_match's return object into a XML RPC boolean
  155. my $plugin=shift;
  156. return RPC::XML::boolean->new(0 + IkiWiki::pagespec_march(@_));
  157. } #}}}
  158. 1