2 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
9 use lib '.'; # For use without installation, removed by Makefile.
11 use vars qw{%config %links %oldlinks %oldpagemtime %renderedfiles %pagesources};
14 die "usage: ikiwiki [options] source dest\n";
17 sub getconfig () { #{{{
18 if (! exists $ENV{WRAPPED_OPTIONS}) {
20 wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)},
21 wiki_link_regexp => qr/\[\[([^\s\]]+)\]\]/,
22 wiki_file_regexp => qr/(^[-A-Za-z0-9_.:\/+]+$)/,
25 default_pageext => ".mdwn",
38 templatedir => "/usr/share/ikiwiki/templates",
43 eval q{use Getopt::Long};
45 "setup|s=s" => \$config{setup},
46 "wikiname=s" => \$config{wikiname},
47 "verbose|v!" => \$config{verbose},
48 "rebuild!" => \$config{rebuild},
49 "wrappermode=i" => \$config{wrappermode},
50 "svn!" => \$config{svn},
51 "anonok!" => \$config{anonok},
52 "cgi!" => \$config{cgi},
53 "url=s" => \$config{url},
54 "cgiurl=s" => \$config{cgiurl},
55 "historyurl=s" => \$config{historyurl},
56 "diffurl=s" => \$config{diffurl},
58 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
60 "adminuser=s@" => sub {
61 push @{$config{adminuser}}, $_[1]
63 "templatedir=s" => sub {
64 $config{templatedir}=possibly_foolish_untaint($_[1])
67 $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
71 if (! $config{setup}) {
72 usage() unless @ARGV == 2;
73 $config{srcdir} = possibly_foolish_untaint(shift);
74 $config{destdir} = possibly_foolish_untaint(shift);
79 # wrapper passes a full config structure in the environment
81 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
86 sub checkconfig () { #{{{
87 if ($config{cgi} && ! length $config{url}) {
88 error("Must specify url to wiki with --url when using --cgi");
91 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
92 unless exists $config{wikistatedir};
95 require IkiWiki::RCS::SVN;
99 require IkiWiki::RCS::Stub;
106 print "Content-type: text/html\n\n";
107 print misctemplate("Error", "<p>Error: @_</p>");
112 sub possibly_foolish_untaint ($) { #{{{
114 my ($untainted)=$tainted=~/(.*)/;
119 return unless $config{verbose};
120 if (! $config{cgi}) {
128 sub basename ($) { #{{{
135 sub dirname ($) { #{{{
142 sub pagetype ($) { #{{{
145 if ($page =~ /\.mdwn$/) {
153 sub pagename ($) { #{{{
156 my $type=pagetype($file);
158 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
162 sub htmlpage ($) { #{{{
165 return $page.".html";
168 sub readfile ($) { #{{{
172 error("cannot read a symlink ($file)");
176 open (IN, "$file") || error("failed to read $file: $!");
182 sub writefile ($$) { #{{{
187 error("cannot write to a symlink ($file)");
190 my $dir=dirname($file);
193 foreach my $s (split(m!/+!, $dir)) {
196 mkdir($d) || error("failed to create directory $d: $!");
201 open (OUT, ">$file") || error("failed to write $file: $!");
206 sub bestlink ($$) { #{{{
207 # Given a page and the text of a link on the page, determine which
208 # existing page that link best points to. Prefers pages under a
209 # subdirectory with the same name as the source page, failing that
210 # goes down the directory tree to the base looking for matching
218 $l.="/" if length $l;
221 if (exists $links{$l}) {
222 #debug("for $page, \"$link\", use $l");
225 } while $cwd=~s!/?[^/]+$!!;
227 #print STDERR "warning: page $page, broken link: $link\n";
231 sub isinlinableimage ($) { #{{{
234 $file=~/\.(png|gif|jpg|jpeg)$/;
237 sub htmllink ($$;$$) { #{{{
240 my $noimageinline=shift; # don't turn links into inline html images
241 my $forcesubpage=shift; # force a link to a subpage
244 if (! $forcesubpage) {
245 $bestlink=bestlink($page, $link);
248 $bestlink="$page/".lc($link);
251 return $link if length $bestlink && $page eq $bestlink;
253 # TODO BUG: %renderedfiles may not have it, if the linked to page
254 # was also added and isn't yet rendered! Note that this bug is
255 # masked by the bug mentioned below that makes all new files
257 if (! grep { $_ eq $bestlink } values %renderedfiles) {
258 $bestlink=htmlpage($bestlink);
260 if (! grep { $_ eq $bestlink } values %renderedfiles) {
261 return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$link"
264 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
266 if (! $noimageinline && isinlinableimage($bestlink)) {
267 return "<img src=\"$bestlink\">";
269 return "<a href=\"$bestlink\">$link</a>";
272 sub indexlink () { #{{{
273 return "<a href=\"$config{url}\">$config{wikiname}</a>";
276 sub lockwiki () { #{{{
277 # Take an exclusive lock on the wiki to prevent multiple concurrent
278 # run issues. The lock will be dropped on program exit.
279 if (! -d $config{wikistatedir}) {
280 mkdir($config{wikistatedir});
282 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
283 error ("cannot write to $config{wikistatedir}/lockfile: $!");
284 if (! flock(WIKILOCK, 2 | 4)) {
285 debug("wiki seems to be locked, waiting for lock");
286 my $wait=600; # arbitrary, but don't hang forever to
287 # prevent process pileup
289 return if flock(WIKILOCK, 2 | 4);
292 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
296 sub unlockwiki () { #{{{
300 sub loadindex () { #{{{
301 open (IN, "$config{wikistatedir}/index") || return;
303 $_=possibly_foolish_untaint($_);
305 my ($mtime, $file, $rendered, @links)=split(' ', $_);
306 my $page=pagename($file);
307 $pagesources{$page}=$file;
308 $oldpagemtime{$page}=$mtime;
309 $oldlinks{$page}=[@links];
310 $links{$page}=[@links];
311 $renderedfiles{$page}=$rendered;
316 sub saveindex () { #{{{
317 if (! -d $config{wikistatedir}) {
318 mkdir($config{wikistatedir});
320 open (OUT, ">$config{wikistatedir}/index") ||
321 error("cannot write to $config{wikistatedir}/index: $!");
322 foreach my $page (keys %oldpagemtime) {
323 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
324 join(" ", @{$links{$page}})."\n"
325 if $oldpagemtime{$page};
330 sub misctemplate ($$) { #{{{
334 my $template=HTML::Template->new(
335 filename => "$config{templatedir}/misc.tmpl"
339 indexlink => indexlink(),
340 wikiname => $config{wikiname},
341 pagebody => $pagebody,
343 return $template->output;
346 sub userinfo_get ($$) { #{{{
350 eval q{use Storable};
351 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
352 if (! defined $userdata || ! ref $userdata ||
353 ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
354 ! exists $userdata->{$user}->{$field}) {
357 return $userdata->{$user}->{$field};
360 sub userinfo_set ($$$) { #{{{
365 eval q{use Storable};
366 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
367 if (! defined $userdata || ! ref $userdata ||
368 ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
372 $userdata->{$user}->{$field}=$value;
373 my $oldmask=umask(077);
374 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
379 sub userinfo_setall ($$) { #{{{
383 eval q{use Storable};
384 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
385 if (! defined $userdata || ! ref $userdata) {
388 $userdata->{$user}=$info;
389 my $oldmask=umask(077);
390 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
395 sub is_admin ($) { #{{{
398 return grep { $_ eq $user_name } @{$config{adminuser}};
401 sub glob_match ($$) { #{{{
405 # turn glob into safe regexp
406 $glob=quotemeta($glob);
414 sub globlist_match ($$) { #{{{
416 my @globlist=split(" ", shift);
418 # check any negated globs first
419 foreach my $glob (@globlist) {
420 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
423 foreach my $glob (@globlist) {
424 return 1 if glob_match($page, $glob);
433 if ($config{setup}) {
434 require IkiWiki::Setup;
437 elsif ($config{wrapper}) {
439 require IkiWiki::Wrapper;
442 elsif ($config{cgi}) {
444 require IkiWiki::CGI;
449 loadindex() unless $config{rebuild};
450 require IkiWiki::Render;