Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-release > by-pkgid > d8544620e4ac7bee48ddb48c85d55709 > files > 440

ikiwiki-3.20190228-1.mga7.noarch.rpm

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>write</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />

<link rel="stylesheet" href="../style.css" type="text/css" />

<link rel="stylesheet" href="../local.css" type="text/css" />










</head>
<body>

<div class="page">

<div class="pageheader">
<div class="header">
<span>
<span class="parentlinks">

<a href="../index.html">ikiwiki</a>/ 

<a href="../plugins.html">plugins</a>/ 

</span>
<span class="title">
write

</span>
</span>



</div>









</div>





<div id="pagebody">

<div id="content" role="main">
<p>Ikiwiki's plugin interface allows all kinds of useful <a href="../plugins.html">plugins</a> to be
written to extend ikiwiki in many ways. Despite the length of this page,
it's not really hard. This page is a complete reference to everything a
plugin might want to do. There is also a quick <a href="./write/tutorial.html">tutorial</a>.</p>

<div class="notebox">
<p>Ikiwiki is a compiler</p>

<p>One thing to keep in mind when writing a plugin is that ikiwiki is a wiki
<em>compiler</em>. So plugins influence pages when they are built, not when they
are loaded. A plugin that inserts the current time into a page, for
example, will insert the build time.</p>

<p>Also, as a compiler, ikiwiki avoids rebuilding pages unless they have
changed, so a plugin that prints some random or changing thing on a page
will generate a static page that won't change until ikiwiki rebuilds the
page for some other reason, like the page being edited.</p>

<p>The <a href="./write/tutorial.html">tutorial</a> has some other examples of ways that ikiwiki being a
compiler may trip up the unwary.</p>
</div>

<div class="toc">
	<ol>
		<li class="L2"><a href="#index1h2">Highlevel view of ikiwiki</a>
		<ol>
			<li class="L3"><a href="#index1h3">compiler</a>
			</li>
			<li class="L3"><a href="#index2h3">cgi</a>
			</li>
		</ol>
		</li>
		<li class="L2"><a href="#index2h2">Types of plugins</a>
		</li>
		<li class="L2"><a href="#index3h2">Plugin interface</a>
		</li>
		<li class="L2"><a href="#index4h2">Registering plugins</a>
		</li>
		<li class="L2"><a href="#index5h2">Types of hooks</a>
		<ol>
			<li class="L3"><a href="#index3h3">getopt</a>
			</li>
			<li class="L3"><a href="#index4h3">checkconfig</a>
			</li>
			<li class="L3"><a href="#index5h3">refresh</a>
			</li>
			<li class="L3"><a href="#index6h3">needsbuild</a>
			</li>
			<li class="L3"><a href="#index7h3">scan</a>
			</li>
			<li class="L3"><a href="#index8h3">readtemplate</a>
			</li>
			<li class="L3"><a href="#index9h3">filter</a>
			</li>
			<li class="L3"><a href="#index10h3">preprocess</a>
			</li>
			<li class="L3"><a href="#index11h3">linkify</a>
			</li>
			<li class="L3"><a href="#index12h3">htmlize</a>
			</li>
			<li class="L3"><a href="#index13h3">indexhtml</a>
			</li>
			<li class="L3"><a href="#index14h3">pagetemplate</a>
			</li>
			<li class="L3"><a href="#index15h3">templatefile</a>
			</li>
			<li class="L3"><a href="#index16h3">pageactions</a>
			</li>
			<li class="L3"><a href="#index17h3">sanitize</a>
			</li>
			<li class="L3"><a href="#index18h3">format</a>
			</li>
			<li class="L3"><a href="#index19h3">build_affected</a>
			</li>
			<li class="L3"><a href="#index20h3">delete</a>
			</li>
			<li class="L3"><a href="#index21h3">rendered</a>
			</li>
			<li class="L3"><a href="#index22h3">changes</a>
			</li>
			<li class="L3"><a href="#index23h3">cgi</a>
			</li>
			<li class="L3"><a href="#index24h3">auth</a>
			</li>
			<li class="L3"><a href="#index25h3">sessioncgi</a>
			</li>
			<li class="L3"><a href="#index26h3">canedit</a>
			</li>
			<li class="L3"><a href="#index27h3">canremove</a>
			</li>
			<li class="L3"><a href="#index28h3">canrename</a>
			</li>
			<li class="L3"><a href="#index29h3">checkcontent</a>
			</li>
			<li class="L3"><a href="#index30h3">editcontent</a>
			</li>
			<li class="L3"><a href="#index31h3">formbuilder</a>
			</li>
			<li class="L3"><a href="#index32h3">savestate</a>
			</li>
			<li class="L3"><a href="#index33h3">renamepage</a>
			</li>
			<li class="L3"><a href="#index34h3">rename</a>
			</li>
			<li class="L3"><a href="#index35h3">getsetup</a>
			</li>
			<li class="L3"><a href="#index36h3">genwrapper</a>
			</li>
			<li class="L3"><a href="#index37h3">disable</a>
			</li>
		</ol>
		</li>
		<li class="L2"><a href="#index6h2">Exported variables</a>
		<ol>
			<li class="L3"><a href="#index38h3">%config</a>
			</li>
			<li class="L3"><a href="#index39h3">%pagestate</a>
			</li>
			<li class="L3"><a href="#index40h3">%wikistate</a>
			</li>
			<li class="L3"><a href="#index41h3">%links</a>
			</li>
			<li class="L3"><a href="#index42h3">%typedlinks</a>
			</li>
			<li class="L3"><a href="#index43h3">%pagesources</a>
			</li>
			<li class="L3"><a href="#index44h3">%destsources</a>
			</li>
		</ol>
		</li>
		<li class="L2"><a href="#index7h2">Library functions</a>
		<ol>
			<li class="L3"><a href="#index45h3">hook(@)</a>
			</li>
			<li class="L3"><a href="#index46h3">debug($)</a>
			</li>
			<li class="L3"><a href="#index47h3">error($;$)</a>
			</li>
			<li class="L3"><a href="#index48h3">template($;@)</a>
			</li>
			<li class="L3"><a href="#index49h3">template_depends($$;@)</a>
			</li>
			<li class="L3"><a href="#index50h3">htmlpage($)</a>
			</li>
			<li class="L3"><a href="#index51h3">pagespec_match_list($$;@)</a>
			</li>
			<li class="L3"><a href="#index52h3">add_depends($$;$)</a>
			</li>
			<li class="L3"><a href="#index53h3">pagespec_match($$;@)</a>
			</li>
			<li class="L3"><a href="#index54h3">deptype(@)</a>
			</li>
			<li class="L3"><a href="#index55h3">bestlink($$)</a>
			</li>
			<li class="L3"><a href="#index56h3">htmllink($$$;@)</a>
			</li>
			<li class="L3"><a href="#index57h3">readfile($;$)</a>
			</li>
			<li class="L3"><a href="#index58h3">writefile($$$;$$)</a>
			</li>
			<li class="L3"><a href="#index59h3">will_render($$)</a>
			</li>
			<li class="L3"><a href="#index60h3">pagetype($)</a>
			</li>
			<li class="L3"><a href="#index61h3">pagename($)</a>
			</li>
			<li class="L3"><a href="#index62h3">pagetitle($)</a>
			</li>
			<li class="L3"><a href="#index63h3">titlepage($)</a>
			</li>
			<li class="L3"><a href="#index64h3">linkpage($)</a>
			</li>
			<li class="L3"><a href="#index65h3">srcfile($;$)</a>
			</li>
			<li class="L3"><a href="#index66h3">add_underlay($)</a>
			</li>
			<li class="L3"><a href="#index67h3">displaytime($;$$)</a>
			</li>
			<li class="L3"><a href="#index68h3">gettext</a>
			</li>
			<li class="L3"><a href="#index69h3">ngettext</a>
			</li>
			<li class="L3"><a href="#index70h3">urlto($;$$)</a>
			</li>
			<li class="L3"><a href="#index71h3">newpagefile($$)</a>
			</li>
			<li class="L3"><a href="#index72h3">targetpage($$;$)</a>
			</li>
			<li class="L3"><a href="#index73h3">add_link($$;$)</a>
			</li>
			<li class="L3"><a href="#index74h3">add_autofile($$$)</a>
			</li>
		</ol>
		</li>
		<li class="L2"><a href="#index8h2">Miscellaneous</a>
		<ol>
			<li class="L3"><a href="#index75h3">Internal use pages</a>
			</li>
			<li class="L3"><a href="#index76h3">RCS plugins</a>
			</li>
			<li class="L3"><a href="#index77h3">rcs_find_changes($)</a>
			</li>
			<li class="L3"><a href="#index78h3">rcs_get_current_rev()</a>
			</li>
			<li class="L3"><a href="#index79h3">PageSpec plugins</a>
			</li>
			<li class="L3"><a href="#index80h3">Sorting plugins</a>
			</li>
			<li class="L3"><a href="#index81h3">Setup plugins</a>
			</li>
			<li class="L3"><a href="#index82h3">Function overriding</a>
			</li>
			<li class="L3"><a href="#index83h3">Javascript</a>
			</li>
		</ol>
		</li>
	</ol>
</div>

<h2><a name="index1h2"></a>Highlevel view of ikiwiki</h2>

<p>Ikiwiki mostly has two modes of operation. It can either be running
as a compiler, building or updating a wiki; or as a cgi program, providing
user interface for editing pages, etc. Almost everything ikiwiki does
is accomplished by calling various hooks provided by plugins.</p>

<h3><a name="index1h3"></a>compiler</h3>

<p>As a compiler, ikiwiki starts by calling the
<a href="./write.html#refresh"><code>refresh</code></a> hook. Then it checks the wiki's source to
find new or changed pages. The <a href="./write.html#needsbuild"><code>needsbuild</code></a> hook
is then called to allow manipulation of the list of pages that need to be
built.</p>

<p>Now that it knows what pages it needs to build, ikiwiki runs two compile
passes. First, it runs <a href="./write.html#scan"><code>scan</code></a> hooks, which collect
metadata about the pages. Then it runs a page rendering pipeline, by calling
in turn these hooks: <a href="./write.html#filter"><code>filter</code></a>,
<a href="./write.html#preprocess"><code>preprocess</code></a>,
<a href="./write.html#linkify"><code>linkify</code></a>, <a href="./write.html#htmlize"><code>htmlize</code></a>,
<a href="./write.html#indexhtml"><code>indexhtml</code></a>,
<a href="./write.html#pagetemplate"><code>pagetemplate</code></a>,
<a href="./write.html#sanitize"><code>sanitize</code></a>, <a href="./write.html#format"><code>format</code></a>.</p>

<p>After all necessary pages are built, it calls the
<a href="./write.html#changes"><code>changes</code></a> hook. Finally, if a page was deleted, the
<a href="./write.html#delete"><code>delete</code></a> hook is called, and the files that page had
previously produced are removed.</p>

<h3><a name="index2h3"></a>cgi</h3>

<p>The flow between hooks when ikiwiki is run as a cgi is best illustrated by
an example.</p>

<p>Alice browses to a page and clicks Edit.</p>

<ul>
<li>Ikiwiki is run as a cgi. It assigns Alice a session cookie, and, by calling
the <a href="./write.html#auth"><code>auth</code></a> hooks, sees that she is not yet logged in.</li>
<li>The <a href="./write.html#sessioncgi"><code>sessioncgi</code></a> hooks are then called, and one
of them, from the <a href="./editpage.html">editpage</a> plugin, notices that the cgi has been told
"do=edit".</li>
<li>The <a href="./editpage.html">editpage</a> plugin calls the <a href="./write.html#canedit"><code>canedit</code></a> hook
to check if this page edit is allowed. The <a href="./signinedit.html">signinedit</a> plugin has a hook
that says not: Alice is not signed in.</li>
<li>The <a href="./signinedit.html">signinedit</a> plugin then launches the signin process. A signin page is
built by calling the <a href="./write.html#formbuilder"><code>formbuilder setup</code></a>
hook.</li>
</ul>

<p>Alice signs in with her openid.</p>

<ul>
<li>The <a href="./openid.html">openid</a> plugin's <a href="./write.html#formbuilder"><code>formbuilder</code></a> hook
sees that an openid was entered in the signin form, and redirects to Alice's
openid provider.</li>
<li>Alice's openid provider calls back to ikiwiki. The <a href="./openid.html">openid</a> plugin has an
<a href="./write.html#auth"><code>auth</code></a> hook that finishes the openid signin process.</li>
<li>Signin complete, ikiwiki returns to what Alice was doing before; editing
a page.</li>
<li>Now all the <a href="./write.html#canedit"><code>canedit</code></a> hooks are happy. The
<a href="./editpage.html">editpage</a> plugin calls
<a href="./write.html#formbuilder"><code>formbuilder setup</code></a> to display the page
editing form.</li>
</ul>

<p>Alice saves her change to the page.</p>

<ul>
<li>The <a href="./editpage.html">editpage</a> plugin's <a href="./write.html#formbuilder"><code>formbuilder</code></a> hook
sees that the Save button was pressed, and calls the
<a href="./write.html#checkcontent"><code>checkcontent</code></a> and
<a href="./write.html#editcontent"><code>editcontent</code></a> hooks.  Then it saves the page
to disk, and branches into the compiler part of ikiwiki to refresh the wiki.</li>
</ul>

<h2><a name="index2h2"></a>Types of plugins</h2>

<p>Most ikiwiki <a href="../plugins.html">plugins</a> are written in perl, like ikiwiki. This gives the
plugin full access to ikiwiki's internals, and is the most efficient.
However, plugins can actually be written in any language that supports XML
RPC. These are called <a href="./write/external.html">external</a> plugins.</p>

<p>A plugin written in perl is a perl module, in the <code>IkiWiki::Plugin</code>
namespace. The name of the plugin is typically in lowercase, such as
<code>IkiWiki::Plugin::inline</code>. Ikiwiki includes a <code>IkiWiki::Plugin::skeleton</code>
that can be fleshed out to make a useful plugin.
<code>IkiWiki::Plugin::pagecount</code> is another simple example. All perl plugins
should <code>use IkiWiki</code> to import the ikiwiki plugin interface. It's a good
idea to include the version number of the plugin interface that your plugin
expects: <code>use IkiWiki 3.00</code>.</p>

<p>An external plugin is an executable program. It can be written in any
language. Its interface to ikiwiki is via XML RPC, which it reads from
ikiwiki on its standard input, and writes to ikiwiki on its standard
output. For more details on writing external plugins, see <a href="./write/external.html">external</a>.</p>

<p>Despite these two types of plugins having such different interfaces,
they're the same as far as how they hook into ikiwiki. This document will
explain how to write both sorts of plugins, albeit with an emphasis on perl
plugins.</p>

<h2><a name="index3h2"></a>Plugin interface</h2>

<p>To import the ikiwiki plugin interface:</p>

<pre><code>use IkiWiki '3.00';
</code></pre>

<p>This will import several variables and functions into your plugin's
namespace. These variables and functions are the ones most plugins need,
and a special effort will be made to avoid changing them in incompatible
ways, and to document any changes that have to be made in the future.</p>

<p>Note that IkiWiki also provides other variables and functions that are not
exported by default. No guarantee is made about these in the future, so if
it's not exported, the wise choice is to not use it.</p>

<h2><a name="index4h2"></a>Registering plugins</h2>

<p>Plugins should, when imported, call <code>hook()</code> to hook into ikiwiki's
processing. The function uses named parameters, and use varies depending on
the type of hook being registered -- see below. A plugin can call
the function more than once to register multiple hooks.</p>

<p>All calls to <code>hook()</code> should be passed a "type" parameter, which gives the
type of hook, a "id" parameter, which should be a unique string for this
plugin, and a "call" parameter, which tells what function to call for the
hook.</p>

<p>An optional "last" parameter, if set to a true value, makes the hook run
after all other hooks of its type, and an optional "first" parameter makes
it run first. Useful if the hook depends on some other hook being run first.</p>

<h2><a name="index5h2"></a>Types of hooks</h2>

<p>In roughly the order they are called.</p>

<h3><a name="index3h3"></a>getopt</h3>

<pre><code>hook(type =&gt; "getopt", id =&gt; "foo", call =&gt; \&amp;getopt);
</code></pre>

<p>This allows for plugins to perform their own processing of command-line
options and so add options to the ikiwiki command line. It's called during
command line processing, with <code>@ARGV</code> full of any options that ikiwiki was
not able to process on its own. The function should process any options it
can, removing them from <code>@ARGV</code>, and probably recording the configuration
settings in <code>%config</code>. It should take care not to abort if it sees
an option it cannot process, and should just skip over those options and
leave them in <code>@ARGV</code>.</p>

<h3><a name="index4h3"></a>checkconfig</h3>

<pre><code>hook(type =&gt; "checkconfig", id =&gt; "foo", call =&gt; \&amp;checkconfig);
</code></pre>

<p>This is useful if the plugin needs to check for or modify ikiwiki's
configuration. It's called early in the startup process. <code>%config</code>
is populated at this point, but other state has not yet been loaded.
The function is passed no values. It's ok for the function to call
<code>error()</code> if something isn't configured right.</p>

<h3><a name="index5h3"></a><a name="refresh">refresh</a></h3>

<pre><code>hook(type =&gt; "refresh", id =&gt; "foo", call =&gt; \&amp;refresh);
</code></pre>

<p>This hook is called just before ikiwiki scans the wiki for changed files.
It's useful for plugins that need to create or modify a source page. The
function is passed no values.</p>

<h3><a name="index6h3"></a><a name="needsbuild">needsbuild</a></h3>

<pre><code>hook(type =&gt; "needsbuild", id =&gt; "foo", call =&gt; \&amp;needsbuild);
</code></pre>

<p>This allows a plugin to observe or even manipulate the list of files that
need to be built when the wiki is refreshed. </p>

<p>As its first parameter, the function is passed a reference to an array of
files that will be built. It should return an array reference that is a
modified version of its input. It can add or remove files from it.</p>

<p>The second parameter passed to the function is a reference to an array of
files that have been deleted.</p>

<h3><a name="index7h3"></a><a name="scan">scan</a></h3>

<pre><code>hook(type =&gt; "scan", id =&gt; "foo", call =&gt; \&amp;scan);
</code></pre>

<p>This hook is called early in the process of building the wiki, and is used
as a first pass scan of the page, to collect metadata about the page. It's
mostly used to scan the page for <a href="../ikiwiki/wikilink.html">WikiLinks</a>, and add
them to <code>%links</code>. Present in IkiWiki 2.40 and later.</p>

<p>The function is passed named parameters "page" and "content". Its return
value is ignored.</p>

<h3><a name="index8h3"></a><a name="readtemplate">readtemplate</a></h3>

<pre><code>hook(type =&gt; "readtemplate", id =&gt; "foo", call =&gt; \&amp;readtemplate);
</code></pre>

<p>Runs on the raw source of a page or <code>*.tmpl</code> file that is being
used as a template, before it is parsed by <a href="http://search.cpan.org/search?mode=dist&amp;query=HTML%3A%3ATemplate">HTML::Template</a>.
For instance, the <a href="./templatebody.html">templatebody</a> plugin uses this to return
the content of the <a href="../ikiwiki/directive/templatebody.html">templatebody</a> directive (if there
is one) instead of the page's full content.</p>

<p>The function is passed named parameters:</p>

<ul>
<li><code>id</code>: the name under which the template was looked up,
such as <code>page.tmpl</code> or <code>note</code></li>
<li><code>page</code>: the name of the template as a page or attachment in the wiki,
such as <code>templates/note</code>, or <code>undef</code> if it's outside the wiki (e.g. in
<code>/usr/share/ikiwiki/templates</code>)</li>
<li><code>content</code>: the content of the corresponding file</li>
<li><code>untrusted</code>: true if the template was loaded from the wiki or an underlay,
false if it was loaded from a trusted location</li>
</ul>

<p>It should return the replacement content.</p>

<h3><a name="index9h3"></a><a name="filter">filter</a></h3>

<pre><code>hook(type =&gt; "filter", id =&gt; "foo", call =&gt; \&amp;filter);
</code></pre>

<p>Runs on the full raw source of a page, before anything else touches it, and
can make arbitrary changes. The function is passed named parameters "page",
"destpage", and "content". It should return the filtered content.</p>

<h3><a name="index10h3"></a><a name="preprocess">preprocess</a></h3>

<p>Adding a preprocessor <a href="../ikiwiki/directive.html">directive</a> is probably the most common use
of a plugin.</p>

<pre><code>    hook(type =&gt; "preprocess", id =&gt; "foo", call =&gt; \&amp;preprocess);
</code></pre>

<p>Replace "foo" with the command name that will be used for the preprocessor
directive.</p>

<p>Each time the directive is processed, the referenced function (<code>preprocess</code>
in the example above) is called. Whatever the function returns goes onto
the page in place of the directive. Or, if the function aborts using
<code>error()</code>, the directive will be replaced with the error message.</p>

<p>The function is passed named parameters. First come the parameters set
in the preprocessor directive. These are passed in the same order as
they're in the directive, and if the preprocessor directive contains a bare
parameter (example: <code>[[!foo  param]]</code>), that parameter will be passed with
an empty value.</p>

<p>After the parameters from the preprocessor directive some additional ones
are passed: A "page" parameter gives the name of the page that embedded the
preprocessor directive, while a "destpage" parameter gives the name of the
page the content is going to (different for inlined pages), and a "preview"
parameter is set to a true value if the page is being previewed.</p>

<p>If <code>hook</code> is passed an optional "scan" parameter, set to a true value, this
makes the hook be called during the preliminary scan that ikiwiki makes of
updated pages, before begining to render pages. This should be done if the
hook modifies data in <code>%links</code> (typically by calling <code>add_link</code>). Note that
doing so will make the hook be run twice per page build, so avoid doing it
for expensive hooks. (As an optimisation, if your preprocessor hook is
called in a void context, you can assume it's being run in scan mode, and
avoid doing expensive things at that point.)</p>

<p>Note that if the <a href="./htmlscrubber.html">htmlscrubber</a> is enabled, html in
preprocessor <a href="../ikiwiki/directive.html">directive</a> output is sanitised, which may limit what
your plugin can do. Also, the rest of the page content is not in html
format at preprocessor time. Text output by a preprocessor directive will
be linkified and passed through markdown (or whatever engine is used to
htmlize the page) along with the rest of the page.</p>

<h3><a name="index11h3"></a><a name="linkify">linkify</a></h3>

<pre><code>hook(type =&gt; "linkify", id =&gt; "foo", call =&gt; \&amp;linkify);
</code></pre>

<p>This hook is called to convert <a href="../ikiwiki/wikilink.html">WikiLinks</a> on the page into html
links. The function is passed named parameters "page", "destpage", and
"content". It should return the linkified content.  Present in IkiWiki 2.40
and later.</p>

<p>Plugins that implement linkify must also implement a scan hook, that scans
for the links on the page and adds them to <code>%links</code> (typically by calling
<code>add_link</code>).</p>

<h3><a name="index12h3"></a><a name="htmlize">htmlize</a></h3>

<pre><code>hook(type =&gt; "htmlize", id =&gt; "ext", call =&gt; \&amp;htmlize);
</code></pre>

<p>Runs on the source of a page and turns it into html. The id parameter
specifies the filename extension that a file must have to be htmlized using
this plugin. This is how you can add support for new and exciting markup
languages to ikiwiki.</p>

<p>The function is passed named parameters: "page" and "content" and should
return the htmlized content.</p>

<p>If <code>hook</code> is passed an optional "keepextension" parameter, set to a true
value, then the extension will not be stripped from the source filename when
generating the page.</p>

<p>If <code>hook</code> is passed an optional "noextension" parameter, set to a true
value, then the id parameter specifies not a filename extension, but
a whole filename that can be htmlized. This is useful for files
like <code>Makefile</code> that have no extension.</p>

<p>If <code>hook</code> is passed an optional "longname" parameter, this value is used
when prompting a user to choose a page type on the edit page form.</p>

<h3><a name="index13h3"></a><a name="indexhtml">indexhtml</a></h3>

<pre><code>hook(type =&gt; "indexhtml", id =&gt; "foo", call =&gt; \&amp;indexhtml);
</code></pre>

<p>This hook is called once the page has been converted to html (but before
the generated html is put in a template). The most common use is to
update search indexes. Added in ikiwiki 2.54.</p>

<p>The function is passed named parameters "page", "destpage", and "content".
Its return value is ignored.</p>

<h3><a name="index14h3"></a><a name="pagetemplate">pagetemplate</a></h3>

<pre><code>hook(type =&gt; "pagetemplate", id =&gt; "foo", call =&gt; \&amp;pagetemplate);
</code></pre>

<p><a href="../templates.html">Templates</a> are filled out for many different things in
ikiwiki, like generating a page, or part of a blog page, or an rss feed, or
a cgi. This hook allows modifying the variables available on those
templates. The function is passed named parameters. The "page" and
"destpage" parameters are the same as for a preprocess hook. The "template"
parameter is a <a href="http://search.cpan.org/search?mode=dist&amp;query=HTML%3A%3ATemplate">HTML::Template</a> object that is the template that
will be used to generate the page. The function can manipulate that
template object.</p>

<p>The most common thing to do is probably to call <code>&#036;template-&gt;param()</code> to add
a new custom parameter to the template.</p>

<h3><a name="index15h3"></a>templatefile</h3>

<pre><code>hook(type =&gt; "templatefile", id =&gt; "foo", call =&gt; \&amp;templatefile);
</code></pre>

<p>This hook allows plugins to change the <a href="../templates.html">template</a> that is
used for a page in the wiki. The hook is passed a "page" parameter, and
should return the name of the template file to use (relative to the
template directory), or undef if it doesn't want to change the default
("page.tmpl").</p>

<h3><a name="index16h3"></a>pageactions</h3>

<pre><code>hook(type =&gt; "pageactions", id =&gt; "foo", call =&gt; \&amp;pageactions);
</code></pre>

<p>This hook allows plugins to add arbitrary actions to the action bar on a
page (next to Edit, RecentChanges, etc). The hook is passed a "page"
parameter, and can return a list of html fragments to add to the action
bar.</p>

<h3><a name="index17h3"></a><a name="sanitize">sanitize</a></h3>

<pre><code>hook(type =&gt; "sanitize", id =&gt; "foo", call =&gt; \&amp;sanitize);
</code></pre>

<p>Use this to implement html sanitization or anything else that needs to
modify the body of a page after it has been fully converted to html.</p>

<p>The function is passed named parameters: "page", "destpage", and "content",
and should return the sanitized content.</p>

<h3><a name="index18h3"></a><a name="format">format</a></h3>

<pre><code>hook(type =&gt; "format", id =&gt; "foo", call =&gt; \&amp;format);
</code></pre>

<p>The difference between format and sanitize is that sanitize only acts on
the page body, while format can modify the entire html page including the
header and footer inserted by ikiwiki, the html document type, etc. (It
should not rely on always being passed the entire page, as it won't be
when the page is being previewed.)</p>

<p>The function is passed named parameters: "page" and "content", and 
should return the formatted content.</p>

<h3><a name="index19h3"></a>build_affected</h3>

<pre><code>hook(type =&gt; "build_affected", id =&gt; "foo", call =&gt; \&amp;build_affected);
</code></pre>

<p>This hook is called after the directly changed pages have been built,
and can cause extra pages to be built. If links and backlinks were provided
by a plugin, this would be where that plugin would rebuild pages whose
backlinks have changed, for instance. The <a href="./trail.html">trail</a> plugin uses this hook
to rebuild pages whose next or previous page has changed.</p>

<p>The function should currently ignore its parameters. It returns a list with
an even number of items (a hash in list context), where the first item of
each pair is a page name to be rebuilt (if it was not already rebuilt), and
the second is a log message resembling
<code>building plugins/write because the phase of the moon has changed</code>.</p>

<h3><a name="index20h3"></a><a name="delete">delete</a></h3>

<pre><code>hook(type =&gt; "delete", id =&gt; "foo", call =&gt; \&amp;delete);
</code></pre>

<p>After a page or pages is removed from the wiki, the referenced function
is called, and passed the names of the source files that were removed.</p>

<h3><a name="index21h3"></a>rendered</h3>

<pre><code>hook(type =&gt; "rendered", id =&gt; "foo", call =&gt; \&amp;rendered);
</code></pre>

<p>After ikiwiki renders a change or addition (but not deletion) to the
wiki, the referenced function is called, and passed the names of the
source files that were rendered.</p>

<p>(This hook used to be called "change", but that was not accurate.
For now, plugins using the old hook name will still work.)</p>

<h3><a name="index22h3"></a><a name="changes">changes</a></h3>

<pre><code>hook(type =&gt; "changes", id =&gt; "foo", call =&gt; \&amp;changes);
</code></pre>

<p>After ikiwiki renders changes to the wiki, the referenced function is
called, and passed the names of the source files that were added, modified,
or deleted.</p>

<h3><a name="index23h3"></a>cgi</h3>

<pre><code>hook(type =&gt; "cgi", id =&gt; "foo", call =&gt; \&amp;cgi);
</code></pre>

<p>Use this to hook into ikiwiki's cgi script. Each registered cgi hook is
called in turn, and passed a CGI object. The hook should examine the
parameters, and if it will handle this CGI request, output a page
(including the http headers) and terminate the program.</p>

<p>Note that cgi hooks are called as early as possible, before any ikiwiki
state is loaded, and with no session information.</p>

<h3><a name="index24h3"></a><a name="auth">auth</a></h3>

<pre><code>hook(type =&gt; "auth", id =&gt; "foo", call =&gt; \&amp;auth);
</code></pre>

<p>This hook can be used to implement an authentication method. When a user
needs to be authenticated, each registered auth hook is called in turn, and
passed a CGI object and a session object. </p>

<p>If the hook is able to authenticate the user, it should set the session
object's "name" parameter to the authenticated user's name. Note that
if the name is set to the name of a user who is not registered,
a basic registration of the user will be automatically performed.</p>

<p>Auth plugins can use the loginselector helper plugin to let the user
select which authentication method to use.</p>

<h3><a name="index25h3"></a><a name="sessioncgi">sessioncgi</a></h3>

<pre><code>hook(type =&gt; "sessioncgi", id =&gt; "foo", call =&gt; \&amp;sessioncgi);
</code></pre>

<p>Unlike the cgi hook, which is run as soon as possible, the sessioncgi hook
is only run once a session object is available. It is passed both a CGI
object and a session object. To check if the user is in fact signed in, you
can check if the session object has a "name" parameter set.</p>

<h3><a name="index26h3"></a><a name="canedit">canedit</a></h3>

<pre><code>hook(type =&gt; "canedit", id =&gt; "foo", call =&gt; \&amp;canedit);
</code></pre>

<p>This hook can be used to implement arbitrary access methods to control when
a page can be edited using the web interface (commits from revision control
bypass it). When a page is edited, each registered canedit hook is called
in turn, and passed the page name, a CGI object, and a session object.</p>

<p>If the hook has no opinion about whether the edit can proceed, return
<code>undef</code>, and the next plugin will be asked to decide. If edit can proceed,
the hook should return "". If the edit is not allowed by this hook, the
hook should return an error message for the user to see, or a function 
that can be run to log the user in or perform other action necessary for
them to be able to edit the page.</p>

<p>This hook should avoid directly redirecting the user to a signin page,
since it's sometimes used to test to see which pages in a set of pages a
user can edit.</p>

<h3><a name="index27h3"></a>canremove</h3>

<pre><code>hook(type =&gt; "canremove", id =&gt; "foo", call =&gt; \&amp;canremove);
</code></pre>

<p>This hook can be used to implement arbitrary access methods to control
when a page can be removed using the web interface (commits from
revision control bypass it). It works exactly like the <code>canedit</code> hook,
but is passed the named parameters <code>cgi</code> (a CGI object), <code>session</code>
(a session object) and <code>page</code> (the page subject to deletion).</p>

<h3><a name="index28h3"></a>canrename</h3>

<pre><code>hook(type =&gt; "canrename", id =&gt; "foo", call =&gt; \&amp;canrename);
</code></pre>

<p>This hook can be used to implement arbitrary access methods to control when
a page can be renamed using the web interface (commits from revision control
bypass it). It works exactly like the <code>canedit</code> hook,
but is passed the named parameters <code>cgi</code> (a CGI object), <code>session</code> (a
session object), <code>src</code>, <code>srcfile</code>, <code>dest</code> and <code>destfile</code>.</p>

<h3><a name="index29h3"></a><a name="checkcontent">checkcontent</a></h3>

<pre><code>hook(type =&gt; "checkcontent", id =&gt; "foo", call =&gt; \&amp;checkcontent);
</code></pre>

<p>This hook is called to check the content a user has entered on a page,
before it is saved, and decide if it should be allowed.</p>

<p>It is passed named parameters: <code>content</code>, <code>page</code>, <code>cgi</code>, and <code>session</code>. If
the content the user has entered is a comment, it may also be passed some
additional parameters: <code>author</code>, <code>url</code>, and <code>subject</code>. The <code>subject</code>
parameter may also be filled with the user's comment about the change.</p>

<p>Note: When the user edits an existing wiki page, this hook is also
passed a <code>diff</code> named parameter, which will include only the lines
that they added to the page, or modified.</p>

<p>The hook should return <code>undef</code> on success. If the content is disallowed, it
should return a message stating what the problem is, or a function
that can be run to perform whatever action is necessary to allow the user
to post the content.</p>

<h3><a name="index30h3"></a><a name="editcontent">editcontent</a></h3>

<pre><code>hook(type =&gt; "editcontent", id =&gt; "foo", call =&gt; \&amp;editcontent);
</code></pre>

<p>This hook is called when a page is saved (or previewed) using the web
interface. It is passed named parameters: <code>content</code>, <code>page</code>, <code>cgi</code>, and
<code>session</code>. These are, respectively, the new page content as entered by the
user, the page name, a <code>CGI</code> object, and the user's <code>CGI::Session</code>. </p>

<p>It can modify the content as desired, and should return the content.</p>

<h3><a name="index31h3"></a><a name="formbuilder">formbuilder</a></h3>

<pre><code>hook(type =&gt; "formbuilder_setup", id =&gt; "foo", call =&gt; \&amp;formbuilder_setup);
hook(type =&gt; "formbuilder", id =&gt; "foo", call =&gt; \&amp;formbuilder);
</code></pre>

<p>These hooks allow tapping into the parts of ikiwiki that use <a href="http://search.cpan.org/search?mode=dist&amp;query=CGI%3A%3AFormBuilder">CGI::FormBuilder</a> to generate web forms. These hooks are passed named
parameters: <code>cgi</code>, <code>session</code>, <code>form</code>, and <code>buttons</code>. These are, respectively,
the <code>CGI</code> object, the user's <code>CGI::Session</code>, a <code>CGI::FormBuilder</code>, and a
reference to an array of names of buttons to go on the form.</p>

<p>Each time a form is set up, the <code>formbuilder_setup</code> hook is called.
Typically the <code>formbuilder_setup</code> hook will check the form's title, and if
it's a form that it needs to modify, will call various methods to
add/remove/change fields, tweak the validation code for the fields, etc. It
will not validate or display the form.</p>

<p>Just before a form is displayed to the user, the <code>formbuilder</code> hook is
called. It can be used to validate the form, but should not display it.</p>

<h3><a name="index32h3"></a>savestate</h3>

<pre><code>hook(type =&gt; "savestate", id =&gt; "foo", call =&gt; \&amp;savestate);
</code></pre>

<p>This hook is called whenever ikiwiki normally saves its state, just before
the state is saved. The function can save other state, modify values before
they're saved, etc.</p>

<h3><a name="index33h3"></a>renamepage</h3>

<pre><code>hook(type =&gt; "renamepage", id =&gt; "foo", call =&gt; \&amp;renamepage);
</code></pre>

<p>This hook is called by the <a href="./rename.html">rename</a> plugin when it renames
something, once per page linking to the renamed page's old location.
The hook is passed named parameters: <code>page</code>, <code>oldpage</code>, <code>newpage</code>, and
<code>content</code>, and should try to modify the content of <code>page</code> to reflect
the name change. For example, by converting links to point to the
new page.</p>

<h3><a name="index34h3"></a>rename</h3>

<pre><code>hook(type =&gt; "rename", id =&gt; "foo", call =&gt; \&amp;rename);
</code></pre>

<p>When a page or set of pages is renamed, the referenced function is
called for every page, and is passed named parameters:</p>

<ul>
<li><code>torename</code>: a reference to a hash with keys: <code>src</code>, <code>srcfile</code>,
<code>dest</code>, <code>destfile</code>, <code>required</code>.</li>
<li><code>cgi</code>: a CGI object</li>
<li><code>session</code>: a session object.</li>
</ul>

<p>Such a hook function returns any additional rename hashes it wants to
add. This hook is applied recursively to returned additional rename
hashes, so that it handles the case where two plugins use the hook:
plugin A would see when plugin B adds a new file to be renamed.</p>

<h3><a name="index35h3"></a>getsetup</h3>

<pre><code>hook(type =&gt; "getsetup", id =&gt; "foo", call =&gt; \&amp;getsetup);
</code></pre>

<p>This hooks is not called during normal operation, but only when setting up 
the wiki, or generating a setup file. Plugins can use this hook to add
configuration options.</p>

<p>The hook is passed no parameters. It returns data about the configuration
options added by the plugin. It can also check if the plugin is usable, and
die if not, which will cause the plugin to not be offered in the configuration
interface.</p>

<p>The data returned is a list of <code>%config</code> options, followed by a hash
describing the option. There can also be an item named "plugin", which
describes the plugin as a whole. For example:</p>

<pre><code>    return
        plugin =&gt; {
            description =&gt; "description of this plugin",
            safe =&gt; 1,
            rebuild =&gt; 1,
            section =&gt; "misc",
        },
        option_foo =&gt; {
            type =&gt; "boolean",
            description =&gt; "enable foo?",
            advanced =&gt; 1,
            safe =&gt; 1,
            rebuild =&gt; 1,
                },
        option_bar =&gt; {
            type =&gt; "string",
            example =&gt; "hello",
            description =&gt; "option bar",
            safe =&gt; 1,
            rebuild =&gt; 0,
        },
</code></pre>

<ul>
<li><code>type</code> can be "boolean", "string", "integer", "pagespec",
or "internal" (used for values that are not user-visible). The type is
the type of the leaf values;  the <code>%config</code> option may be an array or
hash of these.</li>
<li><code>example</code> can be set to an example value.</li>
<li><code>description</code> is a short description of the option.</li>
<li><code>link</code> is a link to further information about the option. This can either
be a <a href="../ikiwiki/wikilink.html">WikiLink</a>, or an url.</li>
<li><code>htmldescription</code> is displayed instead of the description by websetup.</li>
<li><code>advanced</code> can be set to true if the option is more suitable for advanced
users.</li>
<li><code>safe</code> should be false if the option should not be displayed in unsafe
configuration methods, such as the web interface. Anything that specifies
a command to run, a path on disk, or a regexp should be marked as unsafe.
If a plugin is marked as unsafe, that prevents it from being
enabled/disabled.</li>
<li><code>rebuild</code> should be true if changing the option (or enabling/disabling
the plugin) will require a wiki rebuild, false if no rebuild is needed,
and undef if a rebuild could be needed in some circumstances, but is not
strictly required.</li>
<li><code>section</code> can optionally specify which section in the config file
the plugin fits in. The convention is to name the sections the
same as the tags used for <a href="../plugins.html">plugins</a> on this wiki.</li>
</ul>

<h3><a name="index36h3"></a>genwrapper</h3>

<pre><code>hook(type =&gt; "genwrapper", id =&gt; "foo", call =&gt; \&amp;genwrapper);
</code></pre>

<p>This hook is used to inject C code (which it returns) into the <code>main</code>
function of the ikiwiki wrapper when it is being generated. </p>

<p>The code runs before anything else -- in particular it runs before
the suid wrapper has sanitized its environment.</p>

<h3><a name="index37h3"></a>disable</h3>

<pre><code>hook(type =&gt; "disable", id =&gt; "foo", call =&gt; \&amp;disable);
</code></pre>

<p>This hook is only run when a previously enabled plugin gets disabled
during ikiwiki setup. Plugins can use this to perform cleanups.</p>

<h2><a name="index6h2"></a>Exported variables</h2>

<p>Several variables are exported to your plugin when you <code>use IkiWiki;</code></p>

<h3><a name="index38h3"></a><code>%config</code></h3>

<p>A plugin can access the wiki's configuration via the <code>%config</code>
hash. The best way to understand the contents of the hash is to look at
your ikiwiki setup file, which sets the hash content to configure the wiki.</p>

<h3><a name="index39h3"></a><code>%pagestate</code></h3>

<p>The <code>%pagestate</code> hash can be used by plugins to save state that they will need
next time ikiwiki is run. The hash holds per-page state, so to set a value,
use <code>&#036;pagestate{&#036;page}{&#036;id}{&#036;key}=&#036;value</code>, and to retrieve the value,
use <code>&#036;pagestate{&#036;page}{&#036;id}{&#036;key}</code>.</p>

<p>The <code>&#036;value</code> can be anything that perl's Storable module is capable of
serializing. <code>&#036;key</code> can be any string you like, but <code>&#036;id</code> must be the same
as the "id" parameter passed to <code>hook()</code> when registering the plugin. This
is so ikiwiki can know when to delete pagestate for plugins that are no
longer used.</p>

<p>When pages are deleted, ikiwiki automatically deletes their pagestate too.</p>

<p>Note that page state does not persist across wiki rebuilds, only across
wiki updates.</p>

<h3><a name="index40h3"></a><code>%wikistate</code></h3>

<p>The <code>%wikistate</code> hash can be used by a plugin to store persistant state
that is not bound to any one page. To set a value, use
<code>&#036;wikistate{&#036;id}{&#036;key}=&#036;value</code>, where <code>&#036;value</code> is anything Storable can
serialize, <code>&#036;key</code> is any string you like, and <code>&#036;id</code> must be the same as the
"id" parameter passed to <code>hook()</code> when registering the plugin, so that the
state can be dropped if the plugin is no longer used.</p>

<h3><a name="index41h3"></a><code>%links</code></h3>

<p>The <code>%links</code> hash can be used to look up the names of each page that
a page links to. The name of the page is the key; the value is an array
reference. Do not modify this hash directly; call <code>add_link()</code>.</p>

<pre><code>&#036;links{"foo"} = ["bar", "baz"];
</code></pre>

<h3><a name="index42h3"></a><code>%typedlinks</code></h3>

<p>The <code>%typedlinks</code> hash records links of specific types. Do not modify this
hash directly; call <code>add_link()</code>. The keys are page names, and the values
are hash references. In each page's hash reference, the keys are link types
defined by plugins, and the values are hash references with link targets
as keys, and 1 as a dummy value, something like this:</p>

<pre><code>&#036;typedlinks{"foo"} = {
    tag =&gt; { short_word =&gt; 1, metasyntactic_variable =&gt; 1 },
    next_page =&gt; { bar =&gt; 1 },
};
</code></pre>

<p>Ordinary <a href="../ikiwiki/wikilink.html">WikiLinks</a> appear in <code>%links</code>, but not in
<code>%typedlinks</code>.</p>

<h3><a name="index43h3"></a><code>%pagesources</code></h3>

<p>The <code>%pagesources</code> hash can be used to look up the source filename
of a page. So the key is the page name, and the value is the source
filename. Do not modify this hash.</p>

<p>Attachments also appear in this hash, with the same key and value.</p>

<pre><code>&#036;pagesources{"foo"} = "foo.mdwn";
&#036;pagesources{"logo/ikiwiki.png"} = "logo/ikiwiki.png";
</code></pre>

<h3><a name="index44h3"></a><code>%destsources</code></h3>

<p>The <code>%destsources</code> hash records the name of the source file used to
create each destination file. The key is the output filename (ie,
"foo/index.html"), and the value is the name of the page that it was built
from (eg, "foo"). Note that a single source file may create multiple
destination files. Do not modify this hash directly; call <code>will_render()</code>.</p>

<p>Attachments also appear in this hash, with the same key and value.</p>

<pre><code>&#036;destsources{"foo/index.html"} = "foo";
&#036;destsources{"logo/ikiwiki.png"} = "logo/ikiwiki.png";
</code></pre>

<h2><a name="index7h2"></a>Library functions</h2>

<p>Several functions are exported to your plugin when you <code>use IkiWiki;</code></p>

<h3><a name="index45h3"></a><code>hook(@)</code></h3>

<p>Hook into ikiwiki's processing. See the discussion of hooks above.</p>

<p>Note that in addition to the named parameters described above, a parameter
named <code>no_override</code> is supported, If it's set to a true value, then this hook
will not override any existing hook with the same id. This is useful if
the id can be controled by the user.</p>

<h3><a name="index46h3"></a><code>debug($)</code></h3>

<p>Logs a debugging message. These are supressed unless verbose mode is turned
on.</p>

<h3><a name="index47h3"></a><code>error($;$)</code></h3>

<p>Aborts with an error message. If the second parameter is passed, it is a
function that is called after the error message is printed, to do any final
cleanup.</p>

<p>If called inside a preprocess hook, error() does not abort the entire
wiki build, but instead replaces the preprocessor <a href="../ikiwiki/directive.html">directive</a> with
a version containing the error message.</p>

<p>In other hooks, error() is a fatal error, so use with care. Try to avoid
dying on bad input when building a page, as that will halt
the entire wiki build and make the wiki unusable.</p>

<h3><a name="index48h3"></a><code>template($;@)</code></h3>

<p>Creates and returns a <a href="http://search.cpan.org/search?mode=dist&amp;query=HTML%3A%3ATemplate">HTML::Template</a> object. (In a list context,
returns the parameters needed to construct the obhect.) </p>

<p>The first parameter is the name of the template file. The optional remaining
parameters are passed to <code>HTML::Template-&gt;new</code>.</p>

<p>Normally, the template file is first looked for in the templates/ subdirectory
of the srcdir. Failing that, it is looked for in the templatedir.</p>

<p>Wiki pages can be used as templates. This should be done only for templates
which it is safe to let wiki users edit. Enable it by passing a filename
with no ".tmpl" extension. Template pages are normally looked for in
the templates/ directory. If the page name starts with "/", a page
elsewhere in the wiki can be used.</p>

<p>If the template is not found, or contains a syntax error, an error is thrown.</p>

<h3><a name="index49h3"></a><code>template_depends($$;@)</code></h3>

<p>Use this instead of <code>template()</code> if the content of a template is being
included into a page. This causes the page to depend on the template,
so it will be updated if the template is modified.</p>

<p>Like <code>template()</code>, except the second parameter is the page.</p>

<h3><a name="index50h3"></a><code>htmlpage($)</code></h3>

<p>Passed a page name, returns the base name that will be used for a the html
page created from it. (Ie, it appends ".html".)</p>

<p>Use this when constructing the filename of a html file. Use <code>urlto</code> when
generating a link to a page.</p>

<h3><a name="index51h3"></a><code>pagespec_match_list($$;@)</code></h3>

<p>Passed a page name, and <a href="../ikiwiki/pagespec.html">PageSpec</a>, returns a list of pages
in the wiki that match the <a href="../ikiwiki/pagespec.html">PageSpec</a>. </p>

<p>The page will automatically be made to depend on the specified
<a href="../ikiwiki/pagespec.html">PageSpec</a>, so <code>add_depends</code> does not need to be called. This
is often significantly more efficient than calling <code>add_depends</code> and
<code>pagespec_match</code> in a loop. You should use this anytime a plugin
needs to match a set of pages and do something based on that list.</p>

<p>Unlike pagespec_match, this may throw an error if there is an error in
the pagespec.</p>

<p>Additional named parameters can be specified:</p>

<ul>
<li><code>deptype</code> optionally specifies the type of dependency to add. Use the
<code>deptype</code> function to generate a dependency type.</li>
<li><code>filter</code> is a reference to a function, that is called and passed a page,
and returns true if the page should be filtered out of the list.</li>
<li><code>sort</code> specifies a sort order for the list. See
<a href="../ikiwiki/pagespec/sorting.html">sorting</a> for the avilable sort methods. Note that
if a sort method is specified that depends on the
page content (such as 'meta(foo)'), the deptype needs to be set to
a content dependency.</li>
<li><code>reverse</code> if true, sorts in reverse.</li>
<li><code>num</code> if nonzero, specifies the maximum number of matching pages that
will be returned.</li>
<li><code>list</code> makes it only match amoung the specified list of pages.
Default is to match amoung all pages in the wiki.</li>
</ul>

<p>Any other named parameters are passed on to <code>pagespec_match</code>, to further
limit the match.</p>

<h3><a name="index52h3"></a><code>add_depends($$;$)</code></h3>

<p>Makes the specified page depend on the specified <a href="../ikiwiki/pagespec.html">PageSpec</a>.</p>

<p>By default, dependencies are full content dependencies, meaning that the
page will be updated whenever anything matching the PageSpec is modified.
This can be overridden by passing a <code>deptype</code> value as the third parameter.</p>

<h3><a name="index53h3"></a><code>pagespec_match($$;@)</code></h3>

<p>Passed a page name, and <a href="../ikiwiki/pagespec.html">PageSpec</a>, returns a true value if the
<a href="../ikiwiki/pagespec.html">PageSpec</a> matches the page.</p>

<p>Note that the return value is overloaded. If stringified, it will be a
message indicating why the PageSpec succeeded, or failed, to match the
page.</p>

<p>Additional named parameters can be passed, to further limit the match.
The most often used is "location", which specifies the location the
PageSpec should match against. If not passed, relative PageSpecs will match
relative to the top of the wiki.</p>

<h3><a name="index54h3"></a><code>deptype(@)</code></h3>

<p>Use this function to generate ikiwiki's internal representation of a
dependency type from one or more of these keywords:</p>

<ul>
<li><code>content</code> is the default. Any change to the content
of a page triggers the dependency.</li>
<li><code>presence</code> is only triggered by a change to the presence
of a page.</li>
<li><code>links</code> is only triggered by a change to the links of a page.
This includes when a link is added, removed, or changes what
it points to due to other changes. It does not include the
addition or removal of a duplicate link.</li>
</ul>

<p>If multiple types are specified, they are combined.</p>

<h3><a name="index55h3"></a><code>bestlink($$)</code></h3>

<p>Given a page and the text of a link on the page, determine which
existing page that link best points to. Prefers pages under a
subdirectory with the same name as the source page, failing that
goes down the directory tree to the base looking for matching
pages, as described in <a href="../ikiwiki/subpage/linkingrules.html">LinkingRules</a>.</p>

<h3><a name="index56h3"></a><code>htmllink($$$;@)</code></h3>

<p>Many plugins need to generate html links and add them to a page. This is
done by using the <code>htmllink</code> function. The usual way to call
<code>htmllink</code> is:</p>

<pre><code>htmllink(&#036;page, &#036;page, &#036;link)
</code></pre>

<p>Why is <code>&#036;page</code> repeated? Because if a page is inlined inside another, and a
link is placed on it, the right way to make that link is actually:</p>

<pre><code>htmllink(&#036;page, &#036;destpage, &#036;link)
</code></pre>

<p>Here <code>&#036;destpage</code> is the inlining page. A <code>destpage</code> parameter is passed to
some of the hook functions above; the ones that are not passed it are not used
during inlining and don't need to worry about this issue.</p>

<p>After the three required parameters, named parameters can be used to
control some options. These are:</p>

<ul>
<li>noimageinline - set to true to avoid turning links into inline html images</li>
<li>forcesubpage  - set to force a link to a subpage</li>
<li>linktext - set to force the link text to something</li>
<li>anchor - set to make the link include an anchor</li>
<li>rel - set to add a rel attribute to the link</li>
<li>class - set to add a css class to the link</li>
<li>title - set to add a title attribute to the link</li>
</ul>

<h3><a name="index57h3"></a><code>readfile($;$)</code></h3>

<p>Given a filename, reads and returns the entire file.</p>

<p>The optional second parameter, if set to a true value, makes the file be read
in binary mode.</p>

<p>A failure to read the file will result in it dying with an error.</p>

<h3><a name="index58h3"></a><code>writefile($$$;$$)</code></h3>

<p>Given a filename, a directory to put it in, and the file's content,
writes a file. </p>

<p>The optional fourth parameter, if set to a true value, makes the file be
written in binary mode.</p>

<p>The optional fifth parameter can be used to pass a function reference that
will be called to handle writing to the file. The function will be called
and passed a file descriptor it should write to, and an error recovery
function it should call if the writing fails. (You will not normally need to
use this interface.)</p>

<p>A failure to write the file will result in it dying with an error.</p>

<p>If the destination directory doesn't exist, it will first be created.</p>

<p>The filename and directory are separate parameters because of
some security checks done to avoid symlink attacks. Before writing a file,
it checks to make sure there's not a symlink with its name, to avoid
following the symlink. If the filename parameter includes a subdirectory
to put the file in, it also checks if that subdirectory is a symlink, etc.
The directory parameter, however, is not checked for symlinks. So,
generally the directory parameter is a trusted toplevel directory like
the srcdir or destdir, and any subdirectories of this are included in the
filename parameter.</p>

<h3><a name="index59h3"></a><code>will_render($$)</code></h3>

<p>Given a page name and a destination file name (not including the base
destination directory), register that the page will result in that file
being rendered. </p>

<p>It's important to call this before writing to any file in the destination
directory, and it's important to call it consistently every time, even if
the file isn't really written this time -- unless you delete any old
version of the file. In particular, in preview mode, this should still be
called even if the file isn't going to be written to during the preview.</p>

<p>Ikiwiki uses this information to automatically clean up rendered files when
the page that rendered them goes away or is changed to no longer render
them. will_render also does a few important security checks.</p>

<h3><a name="index60h3"></a><code>pagetype($)</code></h3>

<p>Given the name of a source file, returns the type of page it is, if it's
a type that ikiwiki knowns how to htmlize. Otherwise, returns undef.</p>

<h3><a name="index61h3"></a><code>pagename($)</code></h3>

<p>Given the name of a source file, returns the name of the wiki page
that corresponds to that file.</p>

<h3><a name="index62h3"></a><code>pagetitle($)</code></h3>

<p>Give the name of a wiki page, returns a version suitable to be displayed as
the page's title. This is accomplished by de-escaping escaped characters in
the page name. "_" is replaced with a space, and '<strong>NN</strong>' is replaced by 
the UTF character with code NN.</p>

<h3><a name="index63h3"></a><code>titlepage($)</code></h3>

<p>This performs the inverse of <code>pagetitle</code>, ie, it converts a page title into
a wiki page name.</p>

<h3><a name="index64h3"></a><code>linkpage($)</code></h3>

<p>This converts text that could have been entered by the user as a
<a href="../ikiwiki/wikilink.html">WikiLink</a> into a wiki page name.</p>

<h3><a name="index65h3"></a><code>srcfile($;$)</code></h3>

<p>Given the name of a source file in the wiki, searches for the file in
the source directory and the underlay directories (most recently added
underlays first), and returns the full path to the first file found.</p>

<p>Normally srcfile will fail with an error message if the source file cannot
be found. The second parameter can be set to a true value to make it return
undef instead.</p>

<h3><a name="index66h3"></a><code>add_underlay($)</code></h3>

<p>Adds a directory to the set of underlay directories that ikiwiki will
search for files.</p>

<p>If the directory name is not absolute, ikiwiki will assume it is in
the parent directory of the configured underlaydir.</p>

<h3><a name="index67h3"></a><code>displaytime($;$$)</code></h3>

<p>Given a time, formats it for display.</p>

<p>The optional second parameter is a strftime format to use to format the
time.</p>

<p>If the third parameter is true, this is the publication time of a page.
(Ie, set the html5 pubdate attribute.)</p>

<h3><a name="index68h3"></a><code>gettext</code></h3>

<p>This is the standard gettext function, although slightly optimised.</p>

<h3><a name="index69h3"></a><code>ngettext</code></h3>

<p>This is the standard ngettext function, although slightly optimised.</p>

<h3><a name="index70h3"></a><code>urlto($;$$)</code></h3>

<p>Construct a relative url to the first parameter from the page named by the
second. The first parameter can be either a page name, or some other
destination file, as registered by <code>will_render</code>.</p>

<p>Provide a second parameter whenever possible, since this leads to better
behaviour for the <a href="./po.html">po</a> plugin and <code>file:///</code> URLs.</p>

<p>If the second parameter is not specified (or <code>undef</code>), the URL will be
valid from any page on the wiki, or from the CGI; if possible it'll
be a path starting with <code>/</code>, but an absolute URL will be used if
the wiki and the CGI are on different domains.</p>

<p>If the third parameter is passed and is true, the url will be a fully
absolute url. This is useful when generating an url to publish elsewhere.</p>

<h3><a name="index71h3"></a><code>newpagefile($$)</code></h3>

<p>This can be called when creating a new page, to determine what filename
to save the page to. It's passed a page name, and its type, and returns
the name of the file to create, relative to the srcdir.</p>

<h3><a name="index72h3"></a><code>targetpage($$;$)</code></h3>

<p>Passed a page and an extension, returns the filename that page will be
rendered to.</p>

<p>Optionally, a third parameter can be passed, to specify the preferred
filename of the page. For example, <code>targetpage("foo", "rss", "feed")</code>
will yield something like <code>foo/feed.rss</code>.</p>

<h3><a name="index73h3"></a><code>add_link($$;$)</code></h3>

<p>This adds a link to <code>%links</code>, ensuring that duplicate links are not
added. Pass it the page that contains the link, and the link text.</p>

<p>An optional third parameter sets the link type. If not specified,
it is an ordinary <a href="../ikiwiki/wikilink.html">WikiLink</a>.</p>

<h3><a name="index74h3"></a><code>add_autofile($$$)</code></h3>

<p>Sometimes you may want to add a file to the <code>srcdir</code> as a result of content
of other pages. For example, <a href="./tag.html">tag</a> pages can be automatically
created as needed. This function can be used to do that. </p>

<p>The three parameters are the filename to create (relative to the <code>srcdir</code>),
the name of the plugin, and a callback function. The callback will be
called if it is appropriate to automatically add the file, and should then
take care of creating it, and doing anything else it needs to (such as
checking it into revision control). Note that the callback may not always
be called. For example, if an automatically added file is deleted by the
user, ikiwiki will avoid re-adding it again.</p>

<p>This function needs to be called during the scan hook, or earlier in the
build process, in order to add the file early enough for it to be built.</p>

<h2><a name="index8h2"></a>Miscellaneous</h2>

<h3><a name="index75h3"></a>Internal use pages</h3>

<p>Sometimes it's useful to put pages in the wiki without the overhead of
having them be rendered to individual html files. Such internal use pages
are collected together to form the RecentChanges page, for example.</p>

<p>To make an internal use page, register a filename extension that starts
with "_". Internal use pages cannot be edited with the web interface,
generally shouldn't contain <a href="../ikiwiki/wikilink.html">WikiLinks</a> or preprocessor directives (use
either on them with extreme caution), and are not matched by regular
PageSpecs glob patterns, but instead only by a special <code>internal()</code>
<a href="../ikiwiki/pagespec.html">PageSpec</a>.</p>

<h3><a name="index76h3"></a>RCS plugins</h3>

<p>ikiwiki's support for <a href="../rcs.html">revision control systems</a> is also done via
plugins. See <a href="../rcs/details.html">RCS details</a> for some more info.</p>

<p>RCS plugins must register a number of hooks. Each hook has type 'rcs', 
and the 'id' field is set to the name of the hook. For example:</p>

<pre><code>hook(type =&gt; "rcs", id =&gt; "rcs_update", call =&gt; \&amp;rcs_update);
hook(type =&gt; "rcs", id =&gt; "rcs_prepedit", call =&gt; \&amp;rcs_prepedit);
</code></pre>

<h4><a name="index1h4"></a><code>rcs_update()</code></h4>

<p>Updates the working directory with any remote changes.</p>

<h4><a name="index2h4"></a><code>rcs_prepedit(&#036;)</code></h4>

<p>Is passed a file to prepare to edit. It can generate and return an arbitrary
token, that will be passed into <code>rcs_commit</code> when committing. For example,
it might return the current revision ID of the file, and use that
information later when merging changes.</p>

<h4><a name="index3h4"></a><code>rcs_commit(@)</code></h4>

<p>Passed named parameters: <code>file</code>, <code>message</code>, <code>token</code> (from <code>rcs_prepedit</code>),
and <code>session</code> (optional).</p>

<p>Should try to commit the file. Returns <code>undef</code> on <em>success</em> and a version
of the page with the rcs's conflict markers on failure.</p>

<h4><a name="index4h4"></a><code>rcs_commit_staged(@)</code></h4>

<p>Passed named parameters: <code>message</code>, and <code>session</code> (optional).</p>

<p>Should commit all staged changes. Returns undef on success, and an
error message on failure.</p>

<p>Changes can be staged by calls to <code>rcs_add</code>, <code>rcs_remove</code>, and
<code>rcs_rename</code>.</p>

<h4><a name="index5h4"></a><code>rcs_add(&#036;)</code></h4>

<p>Adds the passed file to the archive. The filename is relative to the root
of the srcdir.</p>

<p>Note that this should not commit the new file, it should only
prepare for it to be committed when rcs_commit (or <code>rcs_commit_staged</code>) is
called. Note that the file may be in a new subdir that is not yet in
to version control; the subdir can be added if so.</p>

<h4><a name="index6h4"></a><code>rcs_remove(&#036;)</code></h4>

<p>Remove a file. The filename is relative to the root of the srcdir.</p>

<p>Note that this should not commit the removal, it should only prepare for it
to be committed when <code>rcs_commit</code> (or <code>rcs_commit_staged</code>) is called.</p>

<h4><a name="index7h4"></a><code>rcs_rename(&#036;&#036;)</code></h4>

<p>Rename a file. The filenames are relative to the root of the srcdir.</p>

<p>Note that this should not commit the rename, it should only
prepare it for when <code>rcs_commit</code> (or <code>rcs_commit_staged</code>) is called.
The new filename may be in a new subdir, that is not yet added to
version control. If so, the subdir will exist already, and should
be added to revision control.</p>

<h4><a name="index8h4"></a><code>rcs_recentchanges(&#036;)</code></h4>

<p>Examine the RCS history and generate a list of recent changes.
The parameter is how many changes to return.</p>

<p>The data structure returned for each change is:</p>

<pre><code>{
    rev =&gt; # the RCSs id for this commit
    user =&gt; # user who made the change (may be an openid),
    nickname =&gt; # short name for user (optional; not an openid),

    committype =&gt; # either "web" or the name of the rcs,
    when =&gt; # time when the change was made,
    message =&gt; [
        { line =&gt; "commit message line 1" },
        { line =&gt; "commit message line 2" },
        # etc,
    ],
    pages =&gt; [
        {
            page =&gt; # name of page changed,
            diffurl =&gt; # optional url to a diff of changes
        },
        # repeat for each page changed in this commit,
    ],
}
</code></pre>

<h4><a name="index9h4"></a><code>rcs_diff(&#036;;&#036;)</code></h4>

<p>The first parameter is the rev from <code>rcs_recentchanges</code>.
The optional second parameter is how many lines to return (default: all).</p>

<p>Should return a list of lines of the diff (including \n) in list
context, and a string containing the whole diff in scalar context.</p>

<h4><a name="index10h4"></a><code>rcs_getctime(&#036;)</code></h4>

<p>This is used to get the page creation time for a file from the RCS, by looking
it up in the history.</p>

<p>If the RCS cannot determine a ctime for the file, return 0.</p>

<h4><a name="index11h4"></a><code>rcs_getmtime(&#036;)</code></h4>

<p>This is used to get the page modification time for a file from the RCS, by
looking it up in the history.</p>

<p>It's ok if this is not implemented, and throws an error.</p>

<p>If the RCS cannot determine a mtime for the file, return 0.</p>

<h4><a name="index12h4"></a><code>rcs_receive()</code></h4>

<p>This is called when ikiwiki is running as a pre-receive hook (or
equivalent), and is testing if changes pushed into the RCS from an
untrusted user should be accepted. This is optional, and doesn't make
sense to implement for all RCSs.</p>

<p>It should examine the incoming changes, and do any sanity 
checks that are appropriate for the RCS to limit changes to safe file adds,
removes, and changes. If something bad is found, it should die, to abort
the push. Otherwise, it should return a list of files that were changed,
in the form:</p>

<pre><code>{
    file =&gt; # name of file that was changed
    action =&gt; # either "add", "change", or "remove"
    path =&gt; # temp file containing the new file content, only
        # needed for "add"/"change", and only if the file
        # is an attachment, not a page
}
</code></pre>

<p>The list will then be checked to make sure that each change is one that
is allowed to be made via the web interface.</p>

<h4><a name="index13h4"></a><code>rcs_preprevert(&#036;)</code></h4>

<p>This is called by the revert web interface. It is passed a RCS-specific
change ID, and should determine what the effects would be of reverting
that change, and return the same data structure as <code>rcs_receive</code>.</p>

<p>Like <code>rcs_receive</code>, it should do whatever sanity checks are appropriate
for the RCS to limit changes to safe changes, and die if a change would
be unsafe to revert.</p>

<h4><a name="index14h4"></a><code>rcs_revert(&#036;)</code></h4>

<p>This is called by the revert web interface. It is passed a named
parameter rev that is the RCS-specific change ID to revert.</p>

<p>It should try to revert the specified rev, and leave the reversion staged
so <code>rcs_commit_staged</code> will complete it. It should return undef on <em>success</em>
and an error message on failure.</p>

<p>This hook and <code>rcs_preprevert</code> are optional, if not implemented, no revert
web interface will be available.</p>

<h3><a name="index77h3"></a><code>rcs_find_changes($)</code></h3>

<p>Finds changes committed since the passed RCS-specific rev. Returns
a hash of the files changed, a hash of the files deleted, and the
current rev.</p>

<p>This hook is optional.</p>

<h3><a name="index78h3"></a><code>rcs_get_current_rev()</code></h3>

<p>Gets a RCS-specific rev, which can later be passed to <code>rcs_find_changes</code>.</p>

<p>This hook is optional.</p>

<h3><a name="index79h3"></a>PageSpec plugins</h3>

<p>It's also possible to write plugins that add new functions to
<a href="../ikiwiki/pagespec.html">PageSpecs</a>. Such a plugin should add a function to the
IkiWiki::PageSpec package, that is named <code>match_foo</code>, where "foo()" is
how it will be accessed in a <a href="../ikiwiki/pagespec.html">PageSpec</a>. The function will be passed
two parameters: The name of the page being matched, and the thing to match
against. It may also be passed additional, named parameters.</p>

<p>It should return a IkiWiki::SuccessReason object if the match succeeds, or
an IkiWiki::FailReason object if the match fails. If the match cannot be
attempted at all, for any page, it can instead return an
IkiWiki::ErrorReason object explaining why.</p>

<p>When constructing these objects, you should also include information about
of any pages whose contents or other metadata influenced the result of the
match. Do this by passing a list of pages, followed by <code>deptype</code> values.</p>

<p>For example, "backlink(foo)" is influenced by the contents of page foo;
"link(foo)" and "title(bar)" are influenced by the contents of any page
they match; "created_before(foo)" is influenced by the metadata of foo;
while "glob(*)" is not influenced by the contents of any page.</p>

<h3><a name="index80h3"></a>Sorting plugins</h3>

<p>Similarly, it's possible to write plugins that add new functions as
<a href="../ikiwiki/pagespec/sorting.html">sorting</a> methods. To achieve this, add a function to
the IkiWiki::SortSpec package named <code>cmp_foo</code>, which will be used when sorting
by <code>foo</code> or <code>foo(...)</code> is requested.</p>

<p>The names of pages to be compared are in the global variables <code>&#036;a</code> and <code>&#036;b</code>
in the IkiWiki::SortSpec package. The function should return the same thing
as Perl's <code>cmp</code> and <code>&lt;=&gt;</code> operators: negative if <code>&#036;a</code> is less than <code>&#036;b</code>,
positive if <code>&#036;a</code> is greater, or zero if they are considered equal. It may
also raise an error using <code>error</code>, for instance if it needs a parameter but
one isn't provided.</p>

<p>The function will also be passed one or more parameters. The first is
<code>undef</code> if invoked as <code>foo</code>, or the parameter <code>"bar"</code> if invoked as <code>foo(bar)</code>;
it may also be passed additional, named parameters.</p>

<h3><a name="index81h3"></a>Setup plugins</h3>

<p>The ikiwiki setup file is loaded using a pluggable mechanism. If you look
at the top of a setup file, it starts with 'use IkiWiki::Setup::Standard',
and the rest of the file is passed to that module's import method.</p>

<p>It's possible to write other modules in the <code>IkiWiki::Setup::</code> namespace that
can be used to configure ikiwiki in different ways. These modules should,
when imported, populate <code>&#036;IkiWiki::Setup::raw_setup</code> with a reference
to a hash containing all the config items. They should also implement a
<code>gendump</code> function.</p>

<p>By the way, to parse a ikiwiki setup file and populate <code>%config</code>, a
program just needs to do something like:
<code>use IkiWiki::Setup; IkiWiki::Setup::load(&#036;filename)</code></p>

<h3><a name="index82h3"></a>Function overriding</h3>

<p>Sometimes using ikiwiki's pre-defined hooks is not enough. Your plugin
may need to replace one of ikiwiki's own functions with a modified version,
or wrap one of the functions.</p>

<p>For example, your plugin might want to override <code>displaytime</code>, to change
the html markup used when displaying a date. Or it might want to override
<code>IkiWiki::formattime</code>, to change how a date is formatted. Or perhaps you
want to override <code>bestlink</code> and change how ikiwiki deals with <a href="../ikiwiki/wikilink.html">WikiLinks</a>.</p>

<p>By venturing into this territory, your plugin is becoming tightly tied to
ikiwiki's internals. And it might break if those internals change. But
don't let that stop you, if you're brave.</p>

<p>Ikiwiki provides an <code>inject()</code> function, that is a powerful way to replace
any function with one of your own. This even allows you to inject a
replacement for an exported function, like <code>bestlink</code>. Everything that
imports that function will get your version instead. Pass it the name of
the function to replace, and a new function to call. </p>

<p>For example, here's how to replace <code>displaytime</code> with a version using HTML 5
markup:</p>

<pre><code>inject(name =&gt; 'IkiWiki::displaytime', call =&gt; sub {
    return "&lt;time&gt;".formattime(@_)."&lt;/time&gt;";
});
</code></pre>

<p>Here's how to wrap <code>bestlink</code> with a version that tries to handle
plural words:</p>

<pre><code>my &#036;origbestlink=\&amp;bestlink;
inject(name =&gt; 'IkiWiki::bestlink', call =&gt; \&amp;mybestlink);

sub deplural (&#036;) {
    my &#036;word=shift;
    &#036;word =~ s/e?s&#036;//; # just an example :-)
    return &#036;word;
}

sub mybestlink (&#036;&#036;) {
    my &#036;page=shift;
    my &#036;link=shift;
    my &#036;ret=&#036;origbestlink-&gt;(&#036;page, &#036;link);
    if (! length &#036;ret) {
        &#036;ret=&#036;origbestlink-&gt;(&#036;page, deplural(&#036;link));
    }
    return &#036;ret;
}
</code></pre>

<h3><a name="index83h3"></a>Javascript</h3>

<p>Some plugins use javascript to make ikiwiki look a bit more web-2.0-ish.</p>

<p>All javascript code should be put in <code>.js</code> files in the <code>javascript</code>
underlay, and plugins using those files can enable use of the underlay by
calling <code>add_underlay("javascript");</code> in their <code>import</code> function.</p>

<p>You'll have to arrange for <code>&lt;script&gt;</code> tags to be added to the pages that
use your javascript. This can be done using a <code>format</code> hook.</p>

<p>Ikiwiki provides some utility functions in <code>ikiwiki.js</code>, for use by other
javascript code. These include:</p>

<h4><a name="index15h4"></a><code>getElementsByClass(cls, node, tag)</code></h4>

<p>Returns an array of elements with the given class. The node and tag are
optional and define what document node and element names to search.</p>

<h4><a name="index16h4"></a><code>hook(name, call)</code></h4>

<p>The function <code>call</code> will be run as part of the hook named <code>name</code>.</p>

<p>Note that to hook into <code>window.onload</code>, you can use the `onload' hook.</p>

<h4><a name="index17h4"></a><code>run_hooks(name)</code></h4>

<p>Runs the hooks with the specified name.</p>

</div>







</div>

<div id="footer" class="pagefooter" role="contentinfo">

<div id="pageinfo">






<div id="backlinks">
Links:

<a href="./contrib/org_mode.html">contrib/org mode</a>

<a href="./contrib/signinview.html">contrib/signinview</a>

<a href="./contrib/texinfo.html">contrib/texinfo</a>

<a href="../news/ikiwiki_version_2.0.html">news/ikiwiki version 2.0</a>

<a href="../plugins.html">plugins</a>

<a href="../rcs.html">rcs</a>

<a href="../roadmap.html">roadmap</a>

<a href="./write/external.html">write/external</a>

<a href="./write/tutorial.html">write/tutorial</a>


</div>






<div class="pagedate">
Last edited <span class="date">Tue Feb 26 23:01:54 2019</span>
<!-- Created <span class="date">Tue Feb 26 23:01:54 2019</span> -->
</div>

</div>


<!-- from ikiwiki -->
</div>

</div>

</body>
</html>