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",
39 templatedir => "/usr/share/ikiwiki/templates",
44 eval q{use Getopt::Long};
46 "setup|s=s" => \$config{setup},
47 "wikiname=s" => \$config{wikiname},
48 "verbose|v!" => \$config{verbose},
49 "rebuild!" => \$config{rebuild},
50 "wrappermode=i" => \$config{wrappermode},
51 "svn!" => \$config{svn},
52 "anonok!" => \$config{anonok},
53 "rss!" => \$config{rss},
54 "cgi!" => \$config{cgi},
55 "url=s" => \$config{url},
56 "cgiurl=s" => \$config{cgiurl},
57 "historyurl=s" => \$config{historyurl},
58 "diffurl=s" => \$config{diffurl},
60 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
62 "adminuser=s@" => sub {
63 push @{$config{adminuser}}, $_[1]
65 "templatedir=s" => sub {
66 $config{templatedir}=possibly_foolish_untaint($_[1])
69 $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
73 if (! $config{setup}) {
74 usage() unless @ARGV == 2;
75 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
76 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
81 # wrapper passes a full config structure in the environment
83 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
88 sub checkconfig () { #{{{
89 if ($config{cgi} && ! length $config{url}) {
90 error("Must specify url to wiki with --url when using --cgi\n");
92 if ($config{rss} && ! length $config{url}) {
93 error("Must specify url to wiki with --url when using --rss\n");
96 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
97 unless exists $config{wikistatedir};
100 require IkiWiki::Rcs::SVN;
104 require IkiWiki::Rcs::Stub;
111 print "Content-type: text/html\n\n";
112 print misctemplate("Error", "<p>Error: @_</p>");
117 sub possibly_foolish_untaint ($) { #{{{
119 my ($untainted)=$tainted=~/(.*)/;
124 return unless $config{verbose};
125 if (! $config{cgi}) {
133 sub basename ($) { #{{{
140 sub dirname ($) { #{{{
147 sub pagetype ($) { #{{{
150 if ($page =~ /\.mdwn$/) {
158 sub pagename ($) { #{{{
161 my $type=pagetype($file);
163 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
167 sub htmlpage ($) { #{{{
170 return $page.".html";
173 sub readfile ($) { #{{{
177 error("cannot read a symlink ($file)");
181 open (IN, "$file") || error("failed to read $file: $!");
187 sub writefile ($$) { #{{{
192 error("cannot write to a symlink ($file)");
195 my $dir=dirname($file);
198 foreach my $s (split(m!/+!, $dir)) {
201 mkdir($d) || error("failed to create directory $d: $!");
206 open (OUT, ">$file") || error("failed to write $file: $!");
211 sub bestlink ($$) { #{{{
212 # Given a page and the text of a link on the page, determine which
213 # existing page that link best points to. Prefers pages under a
214 # subdirectory with the same name as the source page, failing that
215 # goes down the directory tree to the base looking for matching
223 $l.="/" if length $l;
226 if (exists $links{$l}) {
227 #debug("for $page, \"$link\", use $l");
230 } while $cwd=~s!/?[^/]+$!!;
232 #print STDERR "warning: page $page, broken link: $link\n";
236 sub isinlinableimage ($) { #{{{
239 $file=~/\.(png|gif|jpg|jpeg)$/;
242 sub pagetitle ($) { #{{{
244 $page=~s/__(\d+)__/&#$1;/g;
249 sub htmllink ($$;$$) { #{{{
252 my $noimageinline=shift; # don't turn links into inline html images
253 my $forcesubpage=shift; # force a link to a subpage
256 if (! $forcesubpage) {
257 $bestlink=bestlink($page, $link);
260 $bestlink="$page/".lc($link);
263 my $linktext=pagetitle($link);
265 return $linktext if length $bestlink && $page eq $bestlink;
267 # TODO BUG: %renderedfiles may not have it, if the linked to page
268 # was also added and isn't yet rendered! Note that this bug is
269 # masked by the bug mentioned below that makes all new files
271 if (! grep { $_ eq $bestlink } values %renderedfiles) {
272 $bestlink=htmlpage($bestlink);
274 if (! grep { $_ eq $bestlink } values %renderedfiles) {
275 return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$linktext"
278 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
280 if (! $noimageinline && isinlinableimage($bestlink)) {
281 return "<img src=\"$bestlink\">";
283 return "<a href=\"$bestlink\">$linktext</a>";
286 sub indexlink () { #{{{
287 return "<a href=\"$config{url}\">$config{wikiname}</a>";
290 sub lockwiki () { #{{{
291 # Take an exclusive lock on the wiki to prevent multiple concurrent
292 # run issues. The lock will be dropped on program exit.
293 if (! -d $config{wikistatedir}) {
294 mkdir($config{wikistatedir});
296 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
297 error ("cannot write to $config{wikistatedir}/lockfile: $!");
298 if (! flock(WIKILOCK, 2 | 4)) {
299 debug("wiki seems to be locked, waiting for lock");
300 my $wait=600; # arbitrary, but don't hang forever to
301 # prevent process pileup
303 return if flock(WIKILOCK, 2 | 4);
306 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
310 sub unlockwiki () { #{{{
314 sub loadindex () { #{{{
315 open (IN, "$config{wikistatedir}/index") || return;
317 $_=possibly_foolish_untaint($_);
319 my ($mtime, $file, $rendered, @links)=split(' ', $_);
320 my $page=pagename($file);
321 $pagesources{$page}=$file;
322 $oldpagemtime{$page}=$mtime;
323 $oldlinks{$page}=[@links];
324 $links{$page}=[@links];
325 $renderedfiles{$page}=$rendered;
330 sub saveindex () { #{{{
331 if (! -d $config{wikistatedir}) {
332 mkdir($config{wikistatedir});
334 open (OUT, ">$config{wikistatedir}/index") ||
335 error("cannot write to $config{wikistatedir}/index: $!");
336 foreach my $page (keys %oldpagemtime) {
337 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
338 join(" ", @{$links{$page}})."\n"
339 if $oldpagemtime{$page};
344 sub misctemplate ($$) { #{{{
348 my $template=HTML::Template->new(
349 filename => "$config{templatedir}/misc.tmpl"
353 indexlink => indexlink(),
354 wikiname => $config{wikiname},
355 pagebody => $pagebody,
357 return $template->output;
360 sub userinfo_get ($$) { #{{{
364 eval q{use Storable};
365 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
366 if (! defined $userdata || ! ref $userdata ||
367 ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
368 ! exists $userdata->{$user}->{$field}) {
371 return $userdata->{$user}->{$field};
374 sub userinfo_set ($$$) { #{{{
379 eval q{use Storable};
380 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
381 if (! defined $userdata || ! ref $userdata ||
382 ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
386 $userdata->{$user}->{$field}=$value;
387 my $oldmask=umask(077);
388 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
393 sub userinfo_setall ($$) { #{{{
397 eval q{use Storable};
398 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
399 if (! defined $userdata || ! ref $userdata) {
402 $userdata->{$user}=$info;
403 my $oldmask=umask(077);
404 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
409 sub is_admin ($) { #{{{
412 return grep { $_ eq $user_name } @{$config{adminuser}};
415 sub glob_match ($$) { #{{{
419 # turn glob into safe regexp
420 $glob=quotemeta($glob);
428 sub globlist_match ($$) { #{{{
430 my @globlist=split(" ", shift);
432 # check any negated globs first
433 foreach my $glob (@globlist) {
434 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
437 foreach my $glob (@globlist) {
438 return 1 if glob_match($page, $glob);
447 if ($config{setup}) {
448 require IkiWiki::Setup;
451 elsif ($config{wrapper}) {
453 require IkiWiki::Wrapper;
456 elsif ($config{cgi}) {
458 require IkiWiki::CGI;
463 loadindex() unless $config{rebuild};
464 require IkiWiki::Render;