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