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