X-Git-Url: https://sipb.mit.edu/gitweb.cgi/ikiwiki.git/blobdiff_plain/09902169560ec64800c5622d87fd2d85c246fc93..2ab895129679328822b0e997631a8cf521de0303:/ikiwiki diff --git a/ikiwiki b/ikiwiki index 4246c7e78..c2310cb37 100755 --- a/ikiwiki +++ b/ikiwiki @@ -1,21 +1,17 @@ #!/usr/bin/perl -T +$ENV{PATH}="/usr/local/bin:/usr/bin:/bin"; use warnings; use strict; -use File::Find; use Memoize; use File::Spec; use HTML::Template; +use Getopt::Long; -BEGIN { - $blosxom::version="is a proper perl module too much to ask?"; - do "/usr/bin/markdown"; -} - -$ENV{PATH}="/usr/local/bin:/usr/bin:/bin"; my (%links, %oldlinks, %oldpagemtime, %renderedfiles, %pagesources); -my %config=( +# Holds global config settings, also used by some modules. +our %config=( #{{{ wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)}, wiki_link_regexp => qr/\[\[([^\s]+)\]\]/, wiki_file_regexp => qr/(^[-A-Za-z0-9_.:\/+]+$)/, @@ -23,31 +19,59 @@ my %config=( wikiname => "wiki", default_pageext => ".mdwn", cgi => 0, - url => "", - cgiurl => "", - historyurl => "", svn => 1, + url => '', + cgiurl => '', + historyurl => '', anonok => 0, rebuild => 0, - wrapper => 0, + wrapper => undef, + wrappermode => undef, srcdir => undef, destdir => undef, templatedir => undef, -); + setup => undef, +); #}}} + +GetOptions( #{{{ + "setup=s" => \$config{setup}, + "wikiname=s" => \$config{wikiname}, + "verbose|v!" => \$config{verbose}, + "rebuild!" => \$config{rebuild}, + "wrapper=s" => sub { $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap" }, + "wrappermode=i" => \$config{wrappermode}, + "svn!" => \$config{svn}, + "anonok!" => \$config{anonok}, + "cgi!" => \$config{cgi}, + "url=s" => \$config{url}, + "cgiurl=s" => \$config{cgiurl}, + "historyurl=s" => \$config{historyurl}, + "exclude=s@" => sub { + $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/; + }, +) || usage(); + +if (! $config{setup}) { + usage() unless @ARGV == 3; + $config{srcdir} = possibly_foolish_untaint(shift); + $config{templatedir} = possibly_foolish_untaint(shift); + $config{destdir} = possibly_foolish_untaint(shift); + if ($config{cgi} && ! length $config{url}) { + error("Must specify url to wiki with --url when using --cgi"); + } +} +#}}} sub usage { #{{{ die "usage: ikiwiki [options] source templates dest\n"; } #}}} -sub error ($) { #{{{ +sub error { #{{{ if ($config{cgi}) { print "Content-type: text/html\n\n"; print misctemplate("Error", "

Error: @_

"); - exit 1; - } - else { - die @_; } + die @_; } #}}} sub debug ($) { #{{{ @@ -66,7 +90,7 @@ sub mtime ($) { #{{{ return (stat($page))[9]; } #}}} -sub possibly_foolish_untaint ($) { #{{{ +sub possibly_foolish_untaint { #{{{ my $tainted=shift; my ($untainted)=$tainted=~/(.*)/; return $untainted; @@ -142,21 +166,25 @@ sub writefile ($$) { #{{{ close OUT; } #}}} -sub findlinks ($) { #{{{ +sub findlinks ($$) { #{{{ my $content=shift; + my $page=shift; my @links; - while ($content =~ /$config{wiki_link_regexp}/g) { + while ($content =~ /(??$link" - } - else { - return "?$link" - } + return "?$link" } $bestlink=File::Spec->abs2rel($bestlink, dirname($page)); @@ -218,9 +247,11 @@ sub htmllink { #{{{ sub linkify ($$) { #{{{ my $content=shift; - my $file=shift; + my $page=shift; - $content =~ s/$config{wiki_link_regexp}/htmllink(pagename($file), $1)/eg; + $content =~ s{(\\?)$config{wiki_link_regexp}}{ + $1 ? "[[$2]]" : htmllink($page, $2) + }eg; return $content; } #}}} @@ -229,6 +260,13 @@ sub htmlize ($$) { #{{{ my $type=shift; my $content=shift; + if (! $INC{"/usr/bin/markdown"}) { + no warnings 'once'; + $blosxom::version="is a proper perl module too much to ask?"; + use warnings 'all'; + do "/usr/bin/markdown"; + } + if ($type eq '.mdwn') { return Markdown::Markdown($content); } @@ -271,14 +309,14 @@ sub parentlinks ($) { #{{{ my $skip=1; foreach my $dir (reverse split("/", $page)) { if (! $skip) { + $path.="../"; unshift @ret, { url => "$path$dir.html", page => $dir }; } else { $skip=0; } - $path.="../"; } - unshift @ret, { url => $path , page => $config{wikiname} }; + unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} }; return @ret; } #}}} @@ -321,20 +359,20 @@ sub finalize ($$) { #{{{ return $template->output; } #}}} -# Important security check. Make sure to call this before saving any files -# to the source directory. sub check_overwrite ($$) { #{{{ + # Important security check. Make sure to call this before saving + # any files to the source directory. my $dest=shift; my $src=shift; if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) { - error("$dest exists and was rendered from ". + error("$dest already exists and was rendered from ". join(" ",(grep { $renderedfiles{$_} eq $dest } keys %renderedfiles)). - ", not from $src before not overwriting"); + ", before, so not rendering from $src"); } } #}}} - + sub render ($) { #{{{ my $file=shift; @@ -343,9 +381,9 @@ sub render ($) { #{{{ if ($type ne 'unknown') { my $page=pagename($file); - $links{$page}=[findlinks($content)]; + $links{$page}=[findlinks($content, $page)]; - $content=linkify($content, $file); + $content=linkify($content, $page); $content=htmlize($type, $content); $content=finalize($content, $page); @@ -363,6 +401,29 @@ sub render ($) { #{{{ } } #}}} +sub lockwiki () { #{{{ + # Take an exclusive lock on the wiki to prevent multiple concurrent + # run issues. The lock will be dropped on program exit. + if (! -d "$config{srcdir}/.ikiwiki") { + mkdir("$config{srcdir}/.ikiwiki"); + } + open(WIKILOCK, ">$config{srcdir}/.ikiwiki/lockfile") || error ("cannot write to lockfile: $!"); + if (! flock(WIKILOCK, 2 | 4)) { + debug("wiki seems to be locked, waiting for lock"); + my $wait=600; # arbitrary, but don't hang forever to + # prevent process pileup + for (1..600) { + return if flock(WIKILOCK, 2 | 4); + sleep 1; + } + error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)"); + } +} #}}} + +sub unlockwiki () { #{{{ + close WIKILOCK; +} #}}} + sub loadindex () { #{{{ open (IN, "$config{srcdir}/.ikiwiki/index") || return; while () { @@ -432,6 +493,7 @@ sub rcs_recentchanges ($) { #{{{ my $num=shift; my @ret; + eval q{use CGI 'escapeHTML'}; eval q{use Date::Parse}; eval q{use Time::Duration}; @@ -447,7 +509,7 @@ sub rcs_recentchanges ($) { #{{{ my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/; my $state='start'; my ($rev, $user, $when, @pages, @message); - foreach (`LANG=C svn log -v '$svn_url'`) { + foreach (`LANG=C svn log --limit $num -v '$svn_url'`) { chomp; if ($state eq 'start' && /$div/) { $state='header'; @@ -487,7 +549,7 @@ sub rcs_recentchanges ($) { #{{{ @pages=@message=(); } elsif ($state eq 'body') { - push @message, {line => $_}, + push @message, {line => escapeHTML($_)}, } } } @@ -506,16 +568,19 @@ sub prune ($) { #{{{ } #}}} sub refresh () { #{{{ - # Find existing pages. + # find existing pages my %exists; my @files; + eval q{use File::Find}; find({ no_chdir => 1, wanted => sub { if (/$config{wiki_file_prune_regexp}/) { + no warnings 'once'; $File::Find::prune=1; + use warnings "all"; } - elsif (! -d $_) { + elsif (! -d $_ && ! -l $_) { my ($f)=/$config{wiki_file_regexp}/; # untaint if (! defined $f) { warn("skipping bad filename $_\n"); @@ -546,7 +611,7 @@ sub refresh () { #{{{ foreach my $page (keys %oldpagemtime) { if (! $exists{$page}) { debug("removing old page $page"); - push @del, $renderedfiles{$page}; + push @del, $pagesources{$page}; prune($config{destdir}."/".$renderedfiles{$page}); delete $renderedfiles{$page}; $oldpagemtime{$page}=0; @@ -598,8 +663,7 @@ FILE: foreach my $file (@files) { foreach my $file (keys %rendered, @del) { my $page=pagename($file); if (exists $links{$page}) { - foreach my $link (@{$links{$page}}) { - $link=bestlink($page, $link); + foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) { if (length $link && ! exists $oldlinks{$page} || ! grep { $_ eq $link } @{$oldlinks{$page}}) { @@ -608,8 +672,7 @@ FILE: foreach my $file (@files) { } } if (exists $oldlinks{$page}) { - foreach my $link (@{$oldlinks{$page}}) { - $link=bestlink($page, $link); + foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) { if (length $link && ! exists $links{$page} || ! grep { $_ eq $link } @{$links{$page}}) { @@ -628,9 +691,8 @@ FILE: foreach my $file (@files) { } } #}}} -# Generates a C wrapper program for running ikiwiki in a specific way. -# The wrapper may be safely made suid. -sub gen_wrapper () { #{{{ +sub gen_wrapper (@) { #{{{ + my %config=(@_); eval q{use Cwd 'abs_path'}; $config{srcdir}=abs_path($config{srcdir}); $config{destdir}=abs_path($config{destdir}); @@ -639,6 +701,10 @@ sub gen_wrapper () { #{{{ error("$this doesn't seem to be executable"); } + if ($config{setup}) { + error("cannot create a wrapper that uses a setup file"); + } + my @params=($config{srcdir}, $config{templatedir}, $config{destdir}, "--wikiname=$config{wikiname}"); push @params, "--verbose" if $config{verbose}; @@ -649,7 +715,7 @@ sub gen_wrapper () { #{{{ push @params, "--cgiurl=$config{cgiurl}" if length $config{cgiurl}; push @params, "--historyurl=$config{historyurl}" if length $config{historyurl}; push @params, "--anonok" if $config{anonok}; - my $params=join(" ", map { "\'$_\'" } @params); + my $params=join(" ", @params); my $call=''; foreach my $p ($this, $this, @params) { $call.=qq{"$p", }; @@ -700,12 +766,15 @@ $envsave } EOF close OUT; - if (system("gcc", "ikiwiki-wrap.c", "-o", "ikiwiki-wrap") != 0) { + if (system("gcc", "ikiwiki-wrap.c", "-o", possibly_foolish_untaint($config{wrapper})) != 0) { error("failed to compile ikiwiki-wrap.c"); } unlink("ikiwiki-wrap.c"); - print "successfully generated ikiwiki-wrap\n"; - exit 0; + if (defined $config{wrappermode} && + ! chmod(oct($config{wrappermode}), possibly_foolish_untaint($config{wrapper}))) { + error("chmod $config{wrapper}: $!"); + } + print "successfully generated $config{wrapper}\n"; } #}}} sub misctemplate ($$) { #{{{ @@ -966,7 +1035,17 @@ sub cgi_editpage ($$) { #{{{ print $q->redirect("$config{url}/".htmlpage($page)); return; } - if (! $form->submitted || ! $form->validate) { + elsif ($form->submitted eq "Preview") { + $form->tmpl_param("page_preview", + htmlize($config{default_pageext}, + linkify($form->field('content'), $page))); + } + else { + $form->tmpl_param("page_preview", ""); + } + + if (! $form->submitted || $form->submitted eq "Preview" || + ! $form->validate) { if ($form->field("do") eq "create") { if (exists $pagesources{lc($page)}) { # hmm, someone else made the page in the @@ -976,44 +1055,51 @@ sub cgi_editpage ($$) { #{{{ } my @page_locs; + my $best_loc; my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/; if (! defined $from || ! length $from || $from ne $form->param('from') || $from=~/$config{wiki_file_prune_regexp}/ || $from=~/^\//) { - @page_locs=$page; + @page_locs=$best_loc=$page; } else { my $dir=$from."/"; $dir=~s![^/]+/$!!; push @page_locs, $dir.$page; push @page_locs, "$from/$page"; + $best_loc="$from/$page"; while (length $dir) { $dir=~s![^/]+/$!!; push @page_locs, $dir.$page; } + + @page_locs = grep { ! exists + $pagesources{lc($_)} } @page_locs; } $form->tmpl_param("page_select", 1); $form->field(name => "page", type => 'select', - options => \@page_locs); + options => \@page_locs, value => $best_loc); $form->title("creating $page"); } elsif ($form->field("do") eq "edit") { - my $content=""; - if (exists $pagesources{lc($page)}) { - $content=readfile("$config{srcdir}/$pagesources{lc($page)}"); - $content=~s/\n/\r\n/g; + if (! length $form->field('content')) { + my $content=""; + if (exists $pagesources{lc($page)}) { + $content=readfile("$config{srcdir}/$pagesources{lc($page)}"); + $content=~s/\n/\r\n/g; + } + $form->field(name => "content", value => $content, + force => 1); } $form->tmpl_param("page_select", 0); - $form->field(name => "content", value => $content, - force => 1); $form->field(name => "page", type => 'hidden'); $form->title("editing $page"); } $form->tmpl_param("can_commit", $config{svn}); $form->tmpl_param("indexlink", indexlink()); - print $form->render(submit => ["Save Page", "Cancel"]); + print $form->render(submit => ["Save Page", "Preview", "Cancel"]); } else { # save page @@ -1045,12 +1131,16 @@ sub cgi_editpage ($$) { #{{{ if ($newfile) { rcs_add($file); } + # prevent deadlock with post-commit hook + unlockwiki(); # presumably the commit will trigger an update # of the wiki rcs_commit($message); } else { + loadindex(); refresh(); + saveindex(); } # The trailing question mark tries to avoid broken @@ -1104,35 +1194,27 @@ sub cgi () { #{{{ } } #}}} -# main {{{ -if (grep /^-/, @ARGV) { - eval {use Getopt::Long}; - GetOptions( - "wikiname=s" => \$config{wikiname}, - "verbose|v!" => \$config{verbose}, - "rebuild!" => \$config{rebuild}, - "wrapper!" => \$config{wrapper}, - "svn!" => \$config{svn}, - "anonok!" => \$config{anonok}, - "cgi!" => \$config{cgi}, - "url=s" => \$config{url}, - "cgiurl=s" => \$config{cgiurl}, - "historyurl=s" => \$config{historyurl}, - "exclude=s@" => sub { - $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/; - }, - ) || usage(); -} -usage() unless @ARGV == 3; -$config{srcdir} = possibly_foolish_untaint(shift); -$config{templatedir} = possibly_foolish_untaint(shift); -$config{destdir} = possibly_foolish_untaint(shift); +sub setup () { # {{{ + my $setup=possibly_foolish_untaint($config{setup}); + delete $config{setup}; + open (IN, $setup) || error("read $setup: $!\n"); + local $/=undef; + my $code=; + ($code)=$code=~/(.*)/s; + close IN; -if ($config{cgi} && ! length $config{url}) { - error("Must specify url to wiki with --url when using --cgi"); -} + eval $code; + error($@) if $@; + exit; +} #}}} -gen_wrapper() if $config{wrapper}; +# main {{{ +lockwiki(); +setup() if $config{setup}; +if ($config{wrapper}) { + gen_wrapper(%config); + exit; +} memoize('pagename'); memoize('bestlink'); loadindex() unless $config{rebuild};