further thoughts
[ikiwiki] / doc / todo / support_for_plugins_written_in_other_languages.mdwn
1 ikiwiki should support writing plugins in other languages
2
3 While it should be possible to call ikiwiki from C, doing the callbacks in C is
4 probably hard. And accessing perl at all from C is ugly. It also doesn't
5 make it very easy to write plugins in an interpreted language, since that
6 would mean linking perl and eg, python in one binary. (Been there, done
7 that, never again.)
8
9 Instead, I'm considering using XML RPC to let ikiwiki communicate with a
10 child process that it can spawn. The child could then be any program,
11 written in any language. It could talk XML RPC via stdio. (This assumes
12 that most languages allow easily serialising XML::RPC calls and responses
13 to a file descriptor. Some XML RPC implementations may be hardcoded to use
14 http..)
15
16 Here's how it would basically look, not showing the actual XML RPC used to
17 pass values.
18
19         -> import
20         <- 1
21         <- hook type => preprocess, id => foo
22         -> 1
23         <- done 1
24         -> 1
25
26         -> callback type => preprocess, id => foo, page => bar
27         <- 1
28         <- getconfig url
29         -> "http://example.com", ...
30         <- debug "foo"
31         -> 1
32         <- done "my return value"
33         -> 1
34
35 From ikiwiki's POV:
36
37 * ikiwiki always initiates each conversation with a command
38 * After sending a command, ikiwiki reads commands, dispatches them, and 
39   returns the results, in a loop.
40 * The loop continues until the plugin calls the "done" command, with a value
41   that is the return value for the command that initiated the conversation.
42
43 From the plugin's POV:
44
45 * It's probably sitting in an XML::RPC loop.
46 * Get a command from ikiwiki.
47 * Disaptch the command to the appropriate function. The main commands seem
48   to be "import" and "callback"; callback can call any function that the
49   plugin has registered a hook for. Others commands might include
50   "rcs_*" for RCS plugins..
51 * The function can use XML::RPC to communicate with ikiwiki to get things
52   like config values; and to call ikiwiki functions.
53 * When the callback returns, use XML::RPC to send a "done" command to ikiwiki.
54
55 Simple enough, really. ikiwiki would need to add accessor functions for
56 all important variables, such as "getconfig" and "setconfig". It would
57 probably be easiest for ikiwiki to dispatch a command by just evaling
58 IkiWiki::$command.
59
60 Plugin programs could be dropped into /usr/share/ikiwiki/plugins/, and
61 load_plugin() would just open2 the plugin program and call import.