]> sipb.mit.edu Git - ikiwiki.git/blob - plugins/externaldemo
bugfixes
[ikiwiki.git] / plugins / externaldemo
1 #!/usr/bin/perl
2 # Demo external plugin. Kinda pointless, since it's a perl script, but
3 # useful for testing or as an hint of how to write an external plugin in
4 # other languages.
5 use warnings;
6 use strict;
7
8 print STDERR "externaldemo plugin running as pid $$\n";
9
10 use RPC::XML;
11 use RPC::XML::Parser;
12 use IO::Handle;
13
14 # autoflush stdout
15 $|=1;
16
17 # Used to build up RPC calls as they're read from stdin.
18 my $accum="";
19
20 sub rpc_read {
21         # Read stdin, a line at a time, until a whole RPC call is accumulated.
22         # Parse to XML::RPC object and return.
23         while (<>) {
24                 $accum.=$_;
25
26                 # Kinda hackish approch to parse a single XML RPC out of the
27                 # accumulated input. Relies on calls always ending with a
28                 # newline, which ikiwiki's protocol requires be true.
29                 if ($accum =~ /^\s*(<\?xml\s.*?<\/(?:methodCall|methodResponse)>)\n(.*)/s) {
30                         $accum=$2; # the rest
31         
32                         # Now parse the XML RPC.
33                         my $r = RPC::XML::Parser->new->parse($1);
34                         if (! ref $r) {
35                                 die "error: XML RPC parse failure $r";
36                         }
37                         return $r;
38                 }
39         }
40
41         return undef;
42 }
43
44 sub rpc_handle {
45         # Handle an incoming XML RPC command.
46         my $r=rpc_read();
47         if (! defined $r) {
48                 return 0;
49         }
50         if ($r->isa("RPC::XML::request")) {
51                 my $name=$r->name;
52                 my @args=map { $_->value } @{$r->args};
53                 # Dispatch the requested function. This could be
54                 # done with a switch statement on the name, or
55                 # whatever. I'll use eval to call the function with
56                 # the name.
57                 my $ret = eval $name.'(@args)';
58                 die $@ if $@;
59         
60                 # Now send the repsonse from the function back,
61                 # followed by a newline.
62                 my $resp=RPC::XML::response->new($ret);
63                 $resp->serialize(\*STDOUT);
64                 print "\n";
65                 # stdout needs to be flushed here. If it isn't,
66                 # things will deadlock. Perl flushes it
67                 # automatically when $| is set.
68                 return 1;
69         }
70         elsif ($r->isa("RPC::XML::response")) {
71                 die "protocol error; got a response when expecting a request";
72         }
73 }
74
75 sub rpc_call {
76         # Make an XML RPC call and return the result.
77         my $command=shift;
78         my @params=@_;
79
80         my $req=RPC::XML::request->new($command, @params);
81         $req->serialize(\*STDOUT);
82         print "\n";
83         # stdout needs to be flushed here to prevent deadlock. Perl does it
84         # automatically when $| is set.
85         
86         my $r=rpc_read();
87         if ($r->isa("RPC::XML::response")) {
88                 return $r->value->value;
89         }
90         else {
91                 die "protocol error; got a request when expecting a response";
92         }
93 }
94
95 # Now on with the actual plugin. Let's do a simple preprocessor plugin.
96
97 sub import {
98         # The import function will be called by ikiwiki when the plugin is
99         # loaded. When it's imported, it needs to hook into the preprocessor
100         # stage of ikiwiki.
101         rpc_call("hook", type => "preprocess", id => "externaldemo", call => "preprocess");
102
103         # Here's an example of how to inject an arbitrary function into
104         # ikiwiki. Ikiwiki will be able to call bob() just like any other
105         # function. Note use of automatic memoization.
106         rpc_call("inject", name => "IkiWiki::bob", call => "bob",
107                 memoize => 1);
108
109         # Here's an exmaple of how to access values in %IkiWiki::config.
110         print STDERR "url is set to: ".
111                 rpc_call("getvar", "config", "url")."\n";
112
113         print STDERR "externaldemo plugin successfully imported\n";
114 }
115
116 sub preprocess {
117         # This function will be called when ikiwiki wants to preprocess
118         # something.
119         my %params=@_;
120
121         # Let's use IkiWiki's pagetitle function to turn the page name into
122         # a title.
123         my $title=rpc_call("pagetitle", $params{page});
124
125         return "externaldemo plugin preprocessing on $title!";
126 }
127
128 sub bob {
129         print STDERR "externaldemo plugin's bob called via RPC";
130 }
131
132 # Now all that's left to do is loop and handle each incoming RPC request.
133 while (rpc_handle()) { print STDERR "externaldemo plugin handled RPC request\n" }