summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/external.pm
blob: e3504a30d510b83124516171a307b0a5656488cb (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. sub getvar ($$$) { #{{{
  94. my $plugin=shift;
  95. my $varname="IkiWiki::".shift;
  96. my $key=shift;
  97. no strict 'refs';
  98. my $ret=$varname->{$key};
  99. use strict 'refs';
  100. return $ret;
  101. } #}}}
  102. sub setvar ($$$;@) { #{{{
  103. my $plugin=shift;
  104. my $varname="IkiWiki::".shift;
  105. my $key=shift;
  106. no strict 'refs';
  107. my $ret=$varname->{$key}=@_;
  108. use strict 'refs';
  109. return $ret;
  110. } #}}}
  111. sub inject ($@) { #{{{
  112. # Bind a given perl function name to a particular RPC request.
  113. my $plugin=shift;
  114. my %params=@_;
  115. if (! exists $params{name} || ! exists $params{call}) {
  116. die "inject needs name and call parameters";
  117. }
  118. my $sub = sub {
  119. IkiWiki::Plugin::external::rpc_call($plugin, $params{call}, @_)
  120. };
  121. eval qq{*$params{name}=\$sub};
  122. return 1;
  123. } #}}}
  124. sub hook ($@) { #{{{
  125. # the call parameter is a function name to call, since XML RPC
  126. # cannot pass a function reference
  127. my $plugin=shift;
  128. my %params=@_;
  129. my $callback=$params{call};
  130. delete $params{call};
  131. IkiWiki::hook(%params, call => sub {
  132. IkiWiki::Plugin::external::rpc_call($plugin, $callback, @_)
  133. });
  134. } #}}}
  135. sub pagespec_match ($@) { #{{{
  136. # convert pagespec_match's return object into a XML RPC boolean
  137. my $plugin=shift;
  138. return RPC::XML::boolean->new(0 + IkiWiki::pagespec_march(@_));
  139. } #}}}
  140. 1