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_.:\/+]+$)/,
close OUT;
} #}}}
-sub findlinks ($) { #{{{
+sub findlinks ($$) { #{{{
my $content=shift;
+ my $page=shift;
my @links;
while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
push @links, lc($1);
}
- return @links;
+ # Discussion links are a special case since they're not in the text
+ # of the page, but on its template.
+ return @links, "$page/discussion";
} #}}}
sub bestlink ($$) { #{{{
my $page=shift;
my $link=shift;
my $noimageinline=shift; # don't turn links into inline html images
- my $createsubpage=shift; # force creation of a subpage if page DNE
+ my $forcesubpage=shift; # force a link to a subpage
- my $bestlink=bestlink($page, $link);
+ my $bestlink;
+ if (! $forcesubpage) {
+ $bestlink=bestlink($page, $link);
+ }
+ else {
+ $bestlink="$page/".lc($link);
+ }
return $link if length $bestlink && $page eq $bestlink;
$bestlink=htmlpage($bestlink);
}
if (! grep { $_ eq $bestlink } values %renderedfiles) {
- if (! $createsubpage) {
- return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$link"
- }
- else {
- return "<a href=\"$config{cgiurl}?do=create&page=$page/$link\">?</a>$link"
- }
+ return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$link"
}
$bestlink=File::Spec->abs2rel($bestlink, dirname($page));
sub linkify ($$) { #{{{
my $content=shift;
- my $file=shift;
+ my $page=shift;
$content =~ s{(\\?)$config{wiki_link_regexp}}{
- $1 ? "[[$2]]" : htmllink(pagename($file), $2)
+ $1 ? "[[$2]]" : htmllink($page, $2)
}eg;
return $content;
", before, so not rendering from $src");
}
} #}}}
-
+
sub render ($) { #{{{
my $file=shift;
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);
}
} #}}}
+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 (<IN>) {
}
} #}}}
-sub rcs_commit ($) { #{{{
+sub rcs_prepedit ($) { #{{{
+ # Prepares to edit a file under revision control. Returns a token
+ # that must be passed into rcs_commit when the file is ready
+ # for committing.
+ # The file is relative to the srcdir.
+ my $file=shift;
+
+ if (-d "$config{srcdir}/.svn") {
+ # For subversion, return the revision of the file when
+ # editing begins.
+ my $rev=svn_info("Revision", "$config{srcdir}/$file");
+ return defined $rev ? $rev : "";
+ }
+} #}}}
+
+sub rcs_commit ($$$) { #{{{
+ # Tries to commit the page; returns undef on _success_ and
+ # a version of the page with the rcs's conflict markers on failure.
+ # The file is relative to the srcdir.
+ my $file=shift;
my $message=shift;
+ my $rcstoken=shift;
if (-d "$config{srcdir}/.svn") {
+ # Check to see if the page has been changed by someone
+ # else since rcs_prepedit was called.
+ my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
+ my $rev=svn_info("Revision", "$config{srcdir}/$file");
+ if ($rev != $oldrev) {
+ # Merge their changes into the file that we've
+ # changed.
+ chdir($config{srcdir}); # svn merge wants to be here
+ if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
+ "$config{srcdir}/$file") != 0) {
+ warn("svn merge -r$oldrev:$rev failed\n");
+ }
+ }
+
if (system("svn", "commit", "--quiet", "-m",
possibly_foolish_untaint($message),
- $config{srcdir}) != 0) {
- warn("svn commit failed\n");
+ "$config{srcdir}/$file") != 0) {
+ my $conflict=readfile("$config{srcdir}/$file");
+ if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
+ warn("svn revert failed\n");
+ }
+ return $conflict;
}
}
+ return undef # success
} #}}}
sub rcs_add ($) { #{{{
+ # filename is relative to the root of the srcdir
my $file=shift;
if (-d "$config{srcdir}/.svn") {
}
} #}}}
+sub svn_info ($$) { #{{{
+ my $field=shift;
+ my $file=shift;
+
+ my $info=`LANG=C svn info $file`;
+ my ($ret)=$info=~/^$field: (.*)$/m;
+ return $ret;
+} #}}}
+
sub rcs_recentchanges ($) { #{{{
my $num=shift;
my @ret;
+ eval q{use CGI 'escapeHTML'};
eval q{use Date::Parse};
eval q{use Time::Duration};
if (-d "$config{srcdir}/.svn") {
- my $info=`LANG=C svn info $config{srcdir}`;
- my ($svn_url)=$info=~/^URL: (.*)$/m;
+ my $svn_url=svn_info("URL", $config{srcdir});
# FIXME: currently assumes that the wiki is somewhere
# under trunk in svn, doesn't support other layouts.
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';
@pages=@message=();
}
elsif ($state eq 'body') {
- push @message, {line => $_},
+ push @message, {line => escapeHTML($_)},
}
}
}
} #}}}
sub refresh () { #{{{
- # Find existing pages.
+ # find existing pages
my %exists;
my @files;
-
eval q{use File::Find};
find({
no_chdir => 1,
$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");
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;
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}}) {
}
}
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}}) {
eval q{use CGI::FormBuilder};
my $form = CGI::FormBuilder->new(
- fields => [qw(do from page content comments)],
+ fields => [qw(do rcsinfo from page content comments)],
header => 1,
method => 'POST',
validate => {
table => 0,
template => "$config{templatedir}/editpage.tmpl"
);
+ my @buttons=("Save Page", "Preview", "Cancel");
my ($page)=$form->param('page')=~/$config{wiki_file_regexp}/;
if (! defined $page || ! length $page || $page ne $q->param('page') ||
error("bad page name");
}
$page=lc($page);
+
+ my $file=$page.$config{default_pageext};
+ my $newfile=1;
+ if (exists $pagesources{lc($page)}) {
+ $file=$pagesources{lc($page)};
+ $newfile=0;
+ }
$form->field(name => "do", type => 'hidden');
$form->field(name => "from", type => 'hidden');
+ $form->field(name => "rcsinfo", type => 'hidden');
$form->field(name => "page", value => "$page", force => 1);
$form->field(name => "comments", type => "text", size => 80);
$form->field(name => "content", type => "textarea", rows => 20,
cols => 80);
+ $form->tmpl_param("can_commit", $config{svn});
+ $form->tmpl_param("indexlink", indexlink());
+ $form->tmpl_param("helponformattinglink",
+ htmllink("", "HelpOnFormatting", 1));
+ if (! $form->submitted) {
+ $form->field(name => "rcsinfo", value => rcs_prepedit($file),
+ force => 1);
+ }
if ($form->submitted eq "Cancel") {
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", "");
+ }
+ $form->tmpl_param("page_conflict", "");
+
+ 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
}
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 (! defined $form->field('content') ||
+ ! 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 => \@buttons);
}
else {
# save page
- my $file=$page.$config{default_pageext};
- my $newfile=1;
- if (exists $pagesources{lc($page)}) {
- $file=$pagesources{lc($page)};
- $newfile=0;
- }
-
my $content=$form->field('content');
$content=~s/\r\n/\n/g;
$content=~s/\r/\n/g;
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);
+ my $conflict=rcs_commit($file, $message,
+ $form->field("rcsinfo"));
+
+ if (defined $conflict) {
+ $form->field(name => "rcsinfo", value => rcs_prepedit($file),
+ force => 1);
+ $form->tmpl_param("page_conflict", 1);
+ $form->field("content", value => $conflict, force => 1);
+ $form->field("do", "edit)");
+ $form->tmpl_param("page_select", 0);
+ $form->field(name => "page", type => 'hidden');
+ $form->title("editing $page");
+ print $form->render(submit => \@buttons);
+ return;
+ }
}
else {
loadindex();
($code)=$code=~/(.*)/s;
close IN;
- my (%setup);
eval $code;
error($@) if $@;
-
- gen_wrapper(%config, %setup, %{$setup{cgiwrapper}}) if $setup{cgiwrapper};
- gen_wrapper(%config, %setup, %{$setup{svnwrapper}}) if $setup{svnwrapper};
-
- print "$setup{wikiname} setup complete, now forcing a rebuild\n";
- foreach my $c (keys %setup) {
- $config{$c}=possibly_foolish_untaint($setup{$c})
- if defined $setup{$c} && ! ref $setup{$c};
- }
- $config{rebuild}=1;
- refresh();
- saveindex();
exit;
} #}}}
# main {{{
+lockwiki();
setup() if $config{setup};
if ($config{wrapper}) {
gen_wrapper(%config);