- if ($form->submitted eq "Cancel") {
- print $q->redirect("$config{url}/".htmlpage($page));
- return;
- }
- if (! $form->submitted || ! $form->validate) {
- if ($form->field("do") eq "create") {
- if (exists $pagesources{lc($page)}) {
- # hmm, someone else made the page in the
- # meantime?
- print $q->redirect("$config{url}/".htmlpage($page));
- return;
- }
-
- 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=$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, 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;
- }
- $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"]);
- }
- 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;
- writefile("$config{srcdir}/$file", $content);
-
- my $message="web commit ";
- if ($session->param("name")) {
- $message.="by ".$session->param("name");
- }
- else {
- $message.="from $ENV{REMOTE_ADDR}";
- }
- if (defined $form->field('comments') &&
- length $form->field('comments')) {
- $message.=": ".$form->field('comments');
- }
-
- if ($config{svn}) {
- if ($newfile) {
- rcs_add($file);
- }
- # 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
- # caches and get the most recent version of the page.
- print $q->redirect("$config{url}/".htmlpage($page)."?updated");
- }