memoization for injected RPC functions is a very, very good thing
[ikiwiki] / doc / plugins / write / external.mdwn
1 External plugins are standalone, executable programs, that can be written
2 in any language. When ikiwiki starts up, it runs the program, and
3 communicates with it using XML RPC. If you want to [[write]] an external
4 plugin, read on..
5
6 ikiwiki contains one sample external plugin, named `externaldemo`. This is
7 written in perl, but is intended to be an example of how to write an
8 external plugin in your favorite programming language. Wow us at how much
9 easier you can do the same thing in your favorite language. ;-)
10
11 ## How external plugins use XML RPC
12
13 While XML RPC is typically used over http, ikiwiki doesn't do that.
14 Instead, the external plugin reads XML RPC data from stdin, and writes it
15 to stdout. To ease parsing, each separate XML RPC request or response must
16 start at the beginning of a line, and end with a newline. When outputting
17 XML RPC to stdout, be _sure_ to flush stdout. Failure to do so will result
18 in deadlock!
19
20 An external plugin should operate in a loop. First, read a command from
21 stdin, using XML RPC. Dispatch the command, and return its result to
22 stdout, also using XML RPC. After reading a command, and before returning
23 the result, the plugin can output XML RPC requests of its own, calling
24 functions in ikiwiki. Note: *Never* make an XML RPC request at any other
25 time. Ikiwiki won't be listening for it, and you will deadlock.
26
27 When ikiwiki starts up an external plugin, the first RPC it will make
28 is to call the plugin's `import()` function. That function typically makes
29 an RPC to ikiwiki's `hook()` function, registering a callback.
30
31 An external plugin can use XML RPC to call any of the exported functions
32 documented in the [[plugin_interface_documentation|write]]. It can also
33 actually call any non-exported IkiWiki function, but doing so is a good way
34 to break your plugin when ikiwiki changes. There is currently no versioned
35 interface like there is for perl plugins, but external plugins were first
36 supported in ikiwiki version 2.6.
37
38 ## Accessing data structures
39
40 Ikiwiki has a few global data structures such as `%config`, which holds
41 its configuration. External plugins can use the `getvar` and `setvar` RPCs
42 to access any such global hash. To get the "url" configuration value,
43 call `getvar("config", "url")`. To set it, call 
44 `setvar("config", "url", "http://example.com/)`.
45
46 ## Notes on function parameters
47
48 The [[plugin_interface_documentation|write]] talks about functions that take
49 "named parameters". When such a function is called over XML RPC, such named
50 parameters look like a list of keys and values:
51
52         page, foo, destpage, bar, magnify, 1
53
54 If a name is repeated in the list, the later value overrides the earlier
55 one:
56
57         name, Bob, age, 20, name, Sally, gender, female
58
59 In perl, boiling this down to an associative array of named parameters is
60 very easy:
61
62         sub foo {
63                 my %params=@list;
64
65 Other languages might not find it so easy. If not, it might be a good idea
66 to convert these named parameters into something more natural for the
67 language as part of their XML RPC interface.
68
69 ## Function injection
70
71 Some parts of ikiwiki are extensible by adding functions. For example, the
72 RCS interface relies on plugins providing several IkiWiki::rcs_* functions.
73 It's actually possible to do this from an external plugin too. 
74
75 To make your external plugin provide an `IkiWiki::rcs_update` function, for
76 example, make an RPC call to `inject`. Pass it named parameters "name" and
77 "call", where "name" is the name of the function to inject into perl (here
78 "Ikiwiki::rcs_update" and "call" is the RPC call ikiwiki will make whenever
79 that function is run.
80
81 If the RPC call is memoizable, you can also pass a "memoize" parameter, set
82 to 1.
83
84 ## Limitations of XML RPC
85
86 Since XML RPC can't pass around references to objects, it can't be used
87 with functions that take or return such references. That means you can't
88 use XML RPC for `cgi` or `formbuilder` hooks (which are passed CGI and
89 FormBuilder perl objects), or use it to call `template()` (which returns a
90 perl HTML::Template object).
91
92 Also. the `getopt` hook doesn't work, as ARGV is not available to the external
93 plugin.
94
95 ## Performance issues
96
97 Since each external plugin is a separate process, when ikiwiki is
98 configured to use lots of external plugins, it will start up slower, and
99 use more resources. One or two should not be a problem though.
100
101 There is some overhead in using XML RPC for function calls. Most plugins
102 should find it to be pretty minimal though. In one benchmark, ikiwiki was
103 able to perform 10000 simple XML RPC calls in 11 seconds -- 900 per second.
104
105 Using external plugins for hooks such as `sanitize` and `format`, which
106 pass around entire pages, and are run for each page rendered, will cause
107 more XML RPC overhead than usual, due to the larger number of calls, and the
108 large quantity of data conversion going on. In contrast, `preprocess` hooks
109 are called generally rarely, and pass around minimal data.
110
111 External plugins should avoid making RPC calls unnecessarily (ie, in a loop).
112 Memoizing the results of appropriate RPC calls is one good way to minimise the
113 number of calls.
114
115 Injecting a replacement for a commonly called ikiwiki function
116 could result in a lot more RPC calls than expected and slow
117 eveything down. `pagetitle`, for instance, is called about 100 times
118 per page build. Memoizing injected functions whenever possible is a very
119 good idea.
120
121 In general, use common sense, and your external plugin will probably
122 perform ok.