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 %pagectime
12 %renderedfiles %pagesources};
15 die "usage: ikiwiki [options] source dest\n";
18 sub getconfig () { #{{{
19 if (! exists $ENV{WRAPPED_OPTIONS}) {
21 wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)},
22 wiki_link_regexp => qr/\[\[([^\s\]]+)\]\]/,
23 wiki_file_regexp => qr/(^[-A-Za-z0-9_.\&;:\/+]+$)/,
26 default_pageext => ".mdwn",
40 templatedir => "/usr/share/ikiwiki/templates",
45 eval q{use Getopt::Long};
47 "setup|s=s" => \$config{setup},
48 "wikiname=s" => \$config{wikiname},
49 "verbose|v!" => \$config{verbose},
50 "rebuild!" => \$config{rebuild},
51 "wrappermode=i" => \$config{wrappermode},
52 "svn!" => \$config{svn},
53 "anonok!" => \$config{anonok},
54 "rss!" => \$config{rss},
55 "cgi!" => \$config{cgi},
56 "url=s" => \$config{url},
57 "cgiurl=s" => \$config{cgiurl},
58 "historyurl=s" => \$config{historyurl},
59 "diffurl=s" => \$config{diffurl},
61 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
63 "adminuser=s@" => sub {
64 push @{$config{adminuser}}, $_[1]
66 "templatedir=s" => sub {
67 $config{templatedir}=possibly_foolish_untaint($_[1])
70 $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
74 if (! $config{setup}) {
75 usage() unless @ARGV == 2;
76 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
77 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
82 # wrapper passes a full config structure in the environment
84 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
89 sub checkconfig () { #{{{
90 if ($config{cgi} && ! length $config{url}) {
91 error("Must specify url to wiki with --url when using --cgi\n");
93 if ($config{rss} && ! length $config{url}) {
94 error("Must specify url to wiki with --url when using --rss\n");
97 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
98 unless exists $config{wikistatedir};
101 require IkiWiki::Rcs::SVN;
105 require IkiWiki::Rcs::Stub;
112 print "Content-type: text/html\n\n";
113 print misctemplate("Error", "<p>Error: @_</p>");
118 sub possibly_foolish_untaint ($) { #{{{
120 my ($untainted)=$tainted=~/(.*)/;
125 return unless $config{verbose};
126 if (! $config{cgi}) {
134 sub basename ($) { #{{{
141 sub dirname ($) { #{{{
148 sub pagetype ($) { #{{{
151 if ($page =~ /\.mdwn$/) {
159 sub pagename ($) { #{{{
162 my $type=pagetype($file);
164 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
168 sub htmlpage ($) { #{{{
171 return $page.".html";
174 sub readfile ($) { #{{{
178 error("cannot read a symlink ($file)");
182 open (IN, "$file") || error("failed to read $file: $!");
188 sub writefile ($$) { #{{{
193 error("cannot write to a symlink ($file)");
196 my $dir=dirname($file);
199 foreach my $s (split(m!/+!, $dir)) {
202 mkdir($d) || error("failed to create directory $d: $!");
207 open (OUT, ">$file") || error("failed to write $file: $!");
212 sub bestlink ($$) { #{{{
213 # Given a page and the text of a link on the page, determine which
214 # existing page that link best points to. Prefers pages under a
215 # subdirectory with the same name as the source page, failing that
216 # goes down the directory tree to the base looking for matching
224 $l.="/" if length $l;
227 if (exists $links{$l}) {
228 #debug("for $page, \"$link\", use $l");
231 } while $cwd=~s!/?[^/]+$!!;
233 #print STDERR "warning: page $page, broken link: $link\n";
237 sub isinlinableimage ($) { #{{{
240 $file=~/\.(png|gif|jpg|jpeg)$/;
243 sub pagetitle ($) { #{{{
245 $page=~s/__(\d+)__/&#$1;/g;
250 sub htmllink ($$;$$) { #{{{
253 my $noimageinline=shift; # don't turn links into inline html images
254 my $forcesubpage=shift; # force a link to a subpage
257 if (! $forcesubpage) {
258 $bestlink=bestlink($page, $link);
261 $bestlink="$page/".lc($link);
264 my $linktext=pagetitle($link);
266 return $linktext if length $bestlink && $page eq $bestlink;
268 # TODO BUG: %renderedfiles may not have it, if the linked to page
269 # was also added and isn't yet rendered! Note that this bug is
270 # masked by the bug mentioned below that makes all new files
272 if (! grep { $_ eq $bestlink } values %renderedfiles) {
273 $bestlink=htmlpage($bestlink);
275 if (! grep { $_ eq $bestlink } values %renderedfiles) {
276 return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$linktext"
279 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
281 if (! $noimageinline && isinlinableimage($bestlink)) {
282 return "<img src=\"$bestlink\">";
284 return "<a href=\"$bestlink\">$linktext</a>";
287 sub indexlink () { #{{{
288 return "<a href=\"$config{url}\">$config{wikiname}</a>";
291 sub lockwiki () { #{{{
292 # Take an exclusive lock on the wiki to prevent multiple concurrent
293 # run issues. The lock will be dropped on program exit.
294 if (! -d $config{wikistatedir}) {
295 mkdir($config{wikistatedir});
297 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
298 error ("cannot write to $config{wikistatedir}/lockfile: $!");
299 if (! flock(WIKILOCK, 2 | 4)) {
300 debug("wiki seems to be locked, waiting for lock");
301 my $wait=600; # arbitrary, but don't hang forever to
302 # prevent process pileup
304 return if flock(WIKILOCK, 2 | 4);
307 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
311 sub unlockwiki () { #{{{
315 sub loadindex () { #{{{
316 open (IN, "$config{wikistatedir}/index") || return;
318 $_=possibly_foolish_untaint($_);
322 foreach my $i (split(/ /, $_)) {
323 my ($item, $val)=split(/=/, $i, 2);
324 push @{$items{$item}}, $val;
327 next unless exists $items{src}; # skip bad lines for now
329 my $page=pagename($items{src}[0]);
330 if (! $config{rebuild}) {
331 $pagesources{$page}=$items{src}[0];
332 $oldpagemtime{$page}=$items{mtime}[0];
333 $oldlinks{$page}=[@{$items{link}}];
334 $links{$page}=[@{$items{link}}];
335 $renderedfiles{$page}=$items{dest}[0];
337 $pagectime{$page}=$items{ctime}[0];
342 sub saveindex () { #{{{
343 if (! -d $config{wikistatedir}) {
344 mkdir($config{wikistatedir});
346 open (OUT, ">$config{wikistatedir}/index") ||
347 error("cannot write to $config{wikistatedir}/index: $!");
348 foreach my $page (keys %oldpagemtime) {
349 my $line="mtime=$oldpagemtime{$page} ".
350 "ctime=$pagectime{$page} ".
351 "src=$pagesources{$page} ".
352 "dest=$renderedfiles{$page}";
353 if ($oldpagemtime{$page}) {
354 $line.=" link=$_" foreach @{$links{$page}};
356 print OUT $line."\n";
361 sub misctemplate ($$) { #{{{
365 my $template=HTML::Template->new(
366 filename => "$config{templatedir}/misc.tmpl"
370 indexlink => indexlink(),
371 wikiname => $config{wikiname},
372 pagebody => $pagebody,
374 return $template->output;
377 sub userinfo_get ($$) { #{{{
381 eval q{use Storable};
382 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
383 if (! defined $userdata || ! ref $userdata ||
384 ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
385 ! exists $userdata->{$user}->{$field}) {
388 return $userdata->{$user}->{$field};
391 sub userinfo_set ($$$) { #{{{
396 eval q{use Storable};
397 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
398 if (! defined $userdata || ! ref $userdata ||
399 ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
403 $userdata->{$user}->{$field}=$value;
404 my $oldmask=umask(077);
405 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
410 sub userinfo_setall ($$) { #{{{
414 eval q{use Storable};
415 my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
416 if (! defined $userdata || ! ref $userdata) {
419 $userdata->{$user}=$info;
420 my $oldmask=umask(077);
421 my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
426 sub is_admin ($) { #{{{
429 return grep { $_ eq $user_name } @{$config{adminuser}};
432 sub glob_match ($$) { #{{{
436 # turn glob into safe regexp
437 $glob=quotemeta($glob);
445 sub globlist_match ($$) { #{{{
447 my @globlist=split(" ", shift);
449 # check any negated globs first
450 foreach my $glob (@globlist) {
451 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
454 foreach my $glob (@globlist) {
455 return 1 if glob_match($page, $glob);
464 if ($config{setup}) {
465 require IkiWiki::Setup;
468 elsif ($config{wrapper}) {
470 require IkiWiki::Wrapper;
473 elsif ($config{cgi}) {
475 require IkiWiki::CGI;
481 require IkiWiki::Render;