]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - ikiwiki
history and folding
[git.ikiwiki.info.git] / ikiwiki
1 #!/usr/bin/perl -T
3 use warnings;
4 use strict;
5 use File::Find;
6 use Memoize;
7 use File::Spec;
9 BEGIN {
10         $blosxom::version="is a proper perl module too much to ask?";
11         do "/usr/bin/markdown";
12 }
14 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
15 my ($srcdir, $destdir, %links, %oldlinks, %oldpagemtime, %renderedfiles,
16     %pagesources);
17 my $wiki_link_regexp=qr/\[\[([^\s]+)\]\]/;
18 my $wiki_file_regexp=qr/(^[-A-Za-z0-9_.:\/+]+$)/;
19 my $wiki_file_prune_regexp=qr!((^|/).svn/|\.\.|^\.|\/\.|\.html?$)!;
20 my $verbose=0;
21 my $wikiname="wiki";
22 my $default_pagetype=".mdwn";
23 my $cgi=0;
24 my $url="";
25 my $cgiurl="";
26 my $historyurl="";
27 my $svn=1;
29 sub usage { #{{{
30         die "usage: ikiwiki [options] source dest\n";
31 } #}}}
33 sub error ($) { #{{{
34         if ($cgi) {
35                 print "Content-type: text/html\n\n";
36                 print "Error: @_\n";
37                 exit 1;
38         }
39         else {
40                 die @_;
41         }
42 } #}}}
44 sub debug ($) { #{{{
45         print "@_\n" if $verbose;
46 } #}}}
48 sub mtime ($) { #{{{
49         my $page=shift;
50         
51         return (stat($page))[9];
52 } #}}}
54 sub possibly_foolish_untaint ($) { #{{{
55         my $tainted=shift;
56         my ($untainted)=$tainted=~/(.*)/;
57         return $untainted;
58 } #}}}
60 sub basename ($) { #{{{
61         my $file=shift;
63         $file=~s!.*/!!;
64         return $file;
65 } #}}}
67 sub dirname ($) { #{{{
68         my $file=shift;
70         $file=~s!/?[^/]+$!!;
71         return $file;
72 } #}}}
74 sub pagetype ($) { #{{{
75         my $page=shift;
76         
77         if ($page =~ /\.mdwn$/) {
78                 return ".mdwn";
79         }
80         else {
81                 return "unknown";
82         }
83 } #}}}
85 sub pagename ($) { #{{{
86         my $file=shift;
88         my $type=pagetype($file);
89         my $page=$file;
90         $page=~s/\Q$type\E*$// unless $type eq 'unknown';
91         return $page;
92 } #}}}
94 sub htmlpage ($) { #{{{
95         my $page=shift;
97         return $page.".html";
98 } #}}}
100 sub readfile ($) { #{{{
101         my $file=shift;
103         local $/=undef;
104         open (IN, "$file") || error("failed to read $file: $!");
105         my $ret=<IN>;
106         close IN;
107         return $ret;
108 } #}}}
110 sub writefile ($$) { #{{{
111         my $file=shift;
112         my $content=shift;
114         my $dir=dirname($file);
115         if (! -d $dir) {
116                 my $d="";
117                 foreach my $s (split(m!/+!, $dir)) {
118                         $d.="$s/";
119                         if (! -d $d) {
120                                 mkdir($d) || error("failed to create directory $d: $!");
121                         }
122                 }
123         }
124         
125         open (OUT, ">$file") || error("failed to write $file: $!");
126         print OUT $content;
127         close OUT;
128 } #}}}
130 sub findlinks { #{{{
131         my $content=shift;
133         my @links;
134         while ($content =~ /$wiki_link_regexp/g) {
135                 push @links, lc($1);
136         }
137         return @links;
138 } #}}}
140 # Given a page and the text of a link on the page, determine which existing
141 # page that link best points to. Prefers pages under a subdirectory with
142 # the same name as the source page, failing that goes down the directory tree
143 # to the base looking for matching pages.
144 sub bestlink ($$) { #{{{
145         my $page=shift;
146         my $link=lc(shift);
147         
148         my $cwd=$page;
149         do {
150                 my $l=$cwd;
151                 $l.="/" if length $l;
152                 $l.=$link;
154                 if (exists $links{$l}) {
155                         #debug("for $page, \"$link\", use $l");
156                         return $l;
157                 }
158         } while $cwd=~s!/?[^/]+$!!;
160         #print STDERR "warning: page $page, broken link: $link\n";
161         return "";
162 } #}}}
164 sub isinlinableimage ($) { #{{{
165         my $file=shift;
166         
167         $file=~/\.(png|gif|jpg|jpeg)$/;
168 } #}}}
170 sub htmllink { #{{{
171         my $page=shift;
172         my $link=shift;
173         my $noimagelink=shift;
175         my $bestlink=bestlink($page, $link);
177         return $link if $page eq $bestlink;
178         
179         # TODO BUG: %renderedfiles may not have it, if the linked to page
180         # was also added and isn't yet rendered! Note that this bug is
181         # masked by the bug mentioned below that makes all new files
182         # be rendered twice.
183         if (! grep { $_ eq $bestlink } values %renderedfiles) {
184                 $bestlink=htmlpage($bestlink);
185         }
186         if (! grep { $_ eq $bestlink } values %renderedfiles) {
187                 return "<a href=\"$cgiurl?do=create&page=$link&from=$page\">?</a>$link"
188         }
189         
190         $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
191         
192         if (! $noimagelink && isinlinableimage($bestlink)) {
193                 return "<img src=\"$bestlink\">";
194         }
195         return "<a href=\"$bestlink\">$link</a>";
196 } #}}}
198 sub linkify ($$) { #{{{
199         my $content=shift;
200         my $file=shift;
202         $content =~ s/$wiki_link_regexp/htmllink(pagename($file), $1)/eg;
203         
204         return $content;
205 } #}}}
207 sub htmlize ($$) { #{{{
208         my $type=shift;
209         my $content=shift;
210         
211         if ($type eq '.mdwn') {
212                 return Markdown::Markdown($content);
213         }
214         else {
215                 error("htmlization of $type not supported");
216         }
217 } #}}}
219 sub linkbacks ($$) { #{{{
220         my $content=shift;
221         my $page=shift;
223         my @links;
224         foreach my $p (keys %links) {
225                 next if bestlink($page, $p) eq $page;
226                 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
227                         my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
228                         
229                         # Trim common dir prefixes from both pages.
230                         my $p_trimmed=$p;
231                         my $page_trimmed=$page;
232                         my $dir;
233                         1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
234                                 defined $dir &&
235                                 $p_trimmed=~s/^\Q$dir\E// &&
236                                 $page_trimmed=~s/^\Q$dir\E//;
237                                        
238                         push @links, "<a href=\"$href\">$p_trimmed</a>";
239                 }
240         }
242         $content.="<hr><p>Links: ".join(" ", sort @links)."</p>\n" if @links;
243         return $content;
244 } #}}}
246 sub finalize ($$) { #{{{
247         my $content=shift;
248         my $page=shift;
250         my $title=basename($page);
251         $title=~s/_/ /g;
252         
253         my $pagelink="";
254         my $path="";
255         foreach my $dir (reverse split("/", $page)) {
256                 if (length($pagelink)) {
257                         $pagelink="<a href=\"$path$dir.html\">$dir</a>/ $pagelink";
258                 }
259                 else {
260                         $pagelink=$dir;
261                 }
262                 $path.="../";
263         }
264         $path=~s/\.\.\/$/index.html/;
265         $pagelink="<a href=\"$path\">$wikiname</a>/ $pagelink";
266         
267         my @actions;
268         if (length $cgiurl) {
269                 push @actions, "<a href=\"$cgiurl?do=edit&page=$page\">Edit</a>";
270                 push @actions, "<a href=\"$cgiurl?do=recentchanges\">RecentChanges</a>";
271         }
272         if (length $historyurl) {
273                 my $url=$historyurl;
274                 $url=~s/\[\[\]\]/$page/g;
275                 push @actions, "<a href=\"$url\">History</a>";
276         }
277         
278         $content="<html>\n<head><title>$title</title></head>\n<body>\n".
279                   "<h1>$pagelink</h1>\n".
280                   "@actions\n<hr>\n".
281                   $content.
282                   "</body>\n</html>\n";
283         
284         return $content;
285 } #}}}
287 sub render ($) { #{{{
288         my $file=shift;
289         
290         my $type=pagetype($file);
291         my $content=readfile("$srcdir/$file");
292         if ($type ne 'unknown') {
293                 my $page=pagename($file);
294                 
295                 $links{$page}=[findlinks($content)];
296                 
297                 $content=linkify($content, $file);
298                 $content=htmlize($type, $content);
299                 $content=linkbacks($content, $page);
300                 $content=finalize($content, $page);
301                 
302                 writefile("$destdir/".htmlpage($page), $content);
303                 $oldpagemtime{$page}=time;
304                 $renderedfiles{$page}=htmlpage($page);
305         }
306         else {
307                 $links{$file}=[];
308                 writefile("$destdir/$file", $content);
309                 $oldpagemtime{$file}=time;
310                 $renderedfiles{$file}=$file;
311         }
312 } #}}}
314 sub loadindex () { #{{{
315         open (IN, "$srcdir/.index") || return;
316         while (<IN>) {
317                 $_=possibly_foolish_untaint($_);
318                 chomp;
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;
326         }
327         close IN;
328 } #}}}
330 sub saveindex () { #{{{
331         open (OUT, ">$srcdir/.index") || error("cannot write to .index: $!");
332         foreach my $page (keys %oldpagemtime) {
333                 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
334                         join(" ", @{$links{$page}})."\n"
335                                 if $oldpagemtime{$page};
336         }
337         close OUT;
338 } #}}}
340 sub rcs_update () { #{{{
341         if (-d "$srcdir/.svn") {
342                 if (system("svn", "update", "--quiet", $srcdir) != 0) {
343                         warn("svn update failed\n");
344                 }
345         }
346 } #}}}
348 sub rcs_commit ($) { #{{{
349         my $message=shift;
351         if (-d "$srcdir/.svn") {
352                 if (system("svn", "commit", "--quiet", "-m",
353                            possibly_foolish_untaint($message), $srcdir) != 0) {
354                         warn("svn commit failed\n");
355                 }
356         }
357 } #}}}
359 sub rcs_add ($) { #{{{
360         my $file=shift;
362         if (-d "$srcdir/.svn") {
363                 my $parent=dirname($file);
364                 while (! -d "$srcdir/$parent/.svn") {
365                         $file=$parent;
366                         $parent=dirname($file);
367                 }
368                 
369                 if (system("svn", "add", "--quiet", "$srcdir/$file") != 0) {
370                         warn("svn add failed\n");
371                 }
372         }
373 } #}}}
375 sub rcs_recentchanges ($) { #{{{
376         my $num=shift;
377         my @ret;
378         
379         eval q{use Date::Parse};
380         eval q{use Time::Duration};
381         
382         if (-d "$srcdir/.svn") {
383                 my $info=`LANG=C svn info $srcdir`;
384                 my ($svn_url)=$info=~/^URL: (.*)$/m;
386                 # FIXME: currently assumes that the wiki is somewhere
387                 # under trunk in svn, doesn't support other layouts.
388                 my ($svn_base)=$svn_url=~m!(/trunk(?:/.*)?)$!;
389                 
390                 my $div=qr/^--------------------+$/;
391                 my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
392                 my $state='start';
393                 my ($rev, $user, $when, @pages, $message);
394                 foreach (`LANG=C svn log -v '$svn_url'`) {
395                         chomp;
396                         if ($state eq 'start' && /$div/) {
397                                 $state='header';
398                         }
399                         elsif ($state eq 'header' && /$infoline/) {
400                                 $rev=$1;
401                                 $user=$2;
402                                 $when=concise(ago(time - str2time($3)));
403                         }
404                         elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/(.+)$/) {
405                                 push @pages, pagename($1) if length $1;
406                         }
407                         elsif ($state eq 'header' && /^$/) {
408                                 $state='body';
409                         }
410                         elsif ($state eq 'body' && /$div/) {
411                                 push @ret, { rev => $rev, user => $user,
412                                         when => $when, message => $message,
413                                         pages => [@pages] } if @pages;
414                                 return @ret if @ret >= $num;
415                                 
416                                 $state='header';
417                                 $message=$rev=$user=$when=undef;
418                                 @pages=();
419                         }
420                         elsif ($state eq 'body') {
421                                 $message.="$_<br>\n";
422                         }
423                 }
424         }
426         return @ret;
427 } #}}}
429 sub prune ($) { #{{{
430         my $file=shift;
432         unlink($file);
433         my $dir=dirname($file);
434         while (rmdir($dir)) {
435                 $dir=dirname($dir);
436         }
437 } #}}}
439 sub refresh () { #{{{
440         # Find existing pages.
441         my %exists;
442         my @files;
443         find({
444                 no_chdir => 1,
445                 wanted => sub {
446                         if (/$wiki_file_prune_regexp/) {
447                                 $File::Find::prune=1;
448                         }
449                         elsif (! -d $_) {
450                                 my ($f)=/$wiki_file_regexp/; # untaint
451                                 if (! defined $f) {
452                                         warn("skipping bad filename $_\n");
453                                 }
454                                 else {
455                                         $f=~s/^\Q$srcdir\E\/?//;
456                                         push @files, $f;
457                                         $exists{pagename($f)}=1;
458                                 }
459                         }
460                 },
461         }, $srcdir);
463         my %rendered;
465         # check for added or removed pages
466         my @add;
467         foreach my $file (@files) {
468                 my $page=pagename($file);
469                 if (! $oldpagemtime{$page}) {
470                         debug("new page $page");
471                         push @add, $file;
472                         $links{$page}=[];
473                         $pagesources{$page}=$file;
474                 }
475         }
476         my @del;
477         foreach my $page (keys %oldpagemtime) {
478                 if (! $exists{$page}) {
479                         debug("removing old page $page");
480                         push @del, $renderedfiles{$page};
481                         prune($destdir."/".$renderedfiles{$page});
482                         delete $renderedfiles{$page};
483                         $oldpagemtime{$page}=0;
484                         delete $pagesources{$page};
485                 }
486         }
487         
488         # render any updated files
489         foreach my $file (@files) {
490                 my $page=pagename($file);
491                 
492                 if (! exists $oldpagemtime{$page} ||
493                     mtime("$srcdir/$file") > $oldpagemtime{$page}) {
494                         debug("rendering changed file $file");
495                         render($file);
496                         $rendered{$file}=1;
497                 }
498         }
499         
500         # if any files were added or removed, check to see if each page
501         # needs an update due to linking to them
502         # TODO: inefficient; pages may get rendered above and again here;
503         # problem is the bestlink may have changed and we won't know until
504         # now
505         if (@add || @del) {
506 FILE:           foreach my $file (@files) {
507                         my $page=pagename($file);
508                         foreach my $f (@add, @del) {
509                                 my $p=pagename($f);
510                                 foreach my $link (@{$links{$page}}) {
511                                         if (bestlink($page, $link) eq $p) {
512                                                 debug("rendering $file, which links to $p");
513                                                 render($file);
514                                                 $rendered{$file}=1;
515                                                 next FILE;
516                                         }
517                                 }
518                         }
519                 }
520         }
522         # handle linkbacks; if a page has added/removed links, update the
523         # pages it links to
524         # TODO: inefficient; pages may get rendered above and again here;
525         # problem is the linkbacks could be wrong in the first pass render
526         # above
527         if (%rendered) {
528                 my %linkchanged;
529                 foreach my $file (keys %rendered, @del) {
530                         my $page=pagename($file);
531                         if (exists $links{$page}) {
532                                 foreach my $link (@{$links{$page}}) {
533                                         $link=bestlink($page, $link);
534                                         if (length $link &&
535                                             ! exists $oldlinks{$page} ||
536                                             ! grep { $_ eq $link } @{$oldlinks{$page}}) {
537                                                 $linkchanged{$link}=1;
538                                         }
539                                 }
540                         }
541                         if (exists $oldlinks{$page}) {
542                                 foreach my $link (@{$oldlinks{$page}}) {
543                                         $link=bestlink($page, $link);
544                                         if (length $link &&
545                                             ! exists $links{$page} ||
546                                             ! grep { $_ eq $link } @{$links{$page}}) {
547                                                 $linkchanged{$link}=1;
548                                         }
549                                 }
550                         }
551                 }
552                 foreach my $link (keys %linkchanged) {
553                         my $linkfile=$pagesources{$link};
554                         if (defined $linkfile) {
555                                 debug("rendering $linkfile, to update its linkbacks");
556                                 render($linkfile);
557                         }
558                 }
559         }
560 } #}}}
562 # Generates a C wrapper program for running ikiwiki in a specific way.
563 # The wrapper may be safely made suid.
564 sub gen_wrapper ($$) { #{{{
565         my ($svn, $rebuild)=@_;
567         eval q{use Cwd 'abs_path'};
568         $srcdir=abs_path($srcdir);
569         $destdir=abs_path($destdir);
570         my $this=abs_path($0);
571         if (! -x $this) {
572                 error("$this doesn't seem to be executable");
573         }
575         my @params=($srcdir, $destdir, "--wikiname=$wikiname");
576         push @params, "--verbose" if $verbose;
577         push @params, "--rebuild" if $rebuild;
578         push @params, "--nosvn" if !$svn;
579         push @params, "--cgi" if $cgi;
580         push @params, "--url=$url" if $url;
581         push @params, "--cgiurl=$cgiurl" if $cgiurl;
582         push @params, "--historyurl=$historyurl" if $historyurl;
583         my $params=join(" ", @params);
584         my $call='';
585         foreach my $p ($this, $this, @params) {
586                 $call.=qq{"$p", };
587         }
588         $call.="NULL";
589         
590         my @envsave;
591         push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
592                        CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE} if $cgi;
593         my $envsave="";
594         foreach my $var (@envsave) {
595                 $envsave.=<<"EOF"
596         if ((s=getenv("$var")))
597                 asprintf(&newenviron[i++], "%s=%s", "$var", s);
598 EOF
599         }
600         
601         open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
602         print OUT <<"EOF";
603 /* A wrapper for ikiwiki, can be safely made suid. */
604 #define _GNU_SOURCE
605 #include <stdio.h>
606 #include <unistd.h>
607 #include <stdlib.h>
608 #include <string.h>
610 extern char **environ;
612 int main (int argc, char **argv) {
613         /* Sanitize environment. */
614         char *s;
615         char *newenviron[$#envsave+3];
616         int i=0;
617 $envsave
618         newenviron[i++]="HOME=$ENV{HOME}";
619         newenviron[i]=NULL;
620         environ=newenviron;
622         if (argc == 2 && strcmp(argv[1], "--params") == 0) {
623                 printf("$params\\n");
624                 exit(0);
625         }
626         
627         execl($call);
628         perror("failed to run $this");
629         exit(1);
631 EOF
632         close OUT;
633         if (system("gcc", "ikiwiki-wrap.c", "-o", "ikiwiki-wrap") != 0) {
634                 error("failed to compile ikiwiki-wrap.c");
635         }
636         unlink("ikiwiki-wrap.c");
637         print "successfully generated ikiwiki-wrap\n";
638         exit 0;
639 } #}}}
641 sub cgi () { #{{{
642         eval q{use CGI};
643         my $q=CGI->new;
645         my $do=$q->param('do');
646         if (! defined $do || ! length $do) {
647                 error("\"do\" parameter missing");
648         }
649         
650         if ($do eq 'recentchanges') {
651                 my $list="<ul>\n";
652                 foreach my $change (rcs_recentchanges(100)) {
653                         $list.="<li>";
654                         $list.=join(", ", map { htmllink("", $_, 1) } @{$change->{pages}});
655                         $list.="<br>\n";
656                         $list.="changed ".$change->{when}." by ".
657                                htmllink("", $change->{user}, 1).
658                                ": <i>".$change->{message}."</i>\n";
659                         $list.="</li>\n";
660                 }
661                 $list.="</ul>\n";
662                 
663                 print $q->header,
664                       $q->start_html("RecentChanges"),
665                       $q->h1("<a href=\"$url\">$wikiname</a>/ RecentChanges"),
666                       $list,
667                       $q->end_form,
668                       $q->end_html;
669                 return;
670         }
671         
672         my ($page)=$q->param('page')=~/$wiki_file_regexp/;
673         if (! defined $page || ! length $page || $page ne $q->param('page') ||
674             $page=~/$wiki_file_prune_regexp/ || $page=~/^\//) {
675                 error("bad page name");
676         }
677         $page=lc($page);
678         
679         my $action=$q->request_uri;
680         $action=~s/\?.*//;
681         
682         if ($do eq 'create') {
683                 if (exists $pagesources{lc($page)}) {
684                         # hmm, someone else made the page in the meantime?
685                         print $q->redirect("$url/".htmlpage($page));
686                 }
688                 my @page_locs;
689                 my ($from)=$q->param('from')=~/$wiki_file_regexp/;
690                 if (! defined $from || ! length $from ||
691                     $from ne $q->param('from') ||
692                     $from=~/$wiki_file_prune_regexp/ || $from=~/^\//) {
693                         @page_locs=$page;
694                 }
695                 else {
696                         my $dir=$from."/";
697                         $dir=~s![^/]+/$!!;
698                         push @page_locs, $dir.$page;
699                         push @page_locs, "$from/$page";
700                         while (length $dir) {
701                                 $dir=~s![^/]+/$!!;
702                                 push @page_locs, $dir.$page;
703                         }
704                 }
705                 
706                 $q->param("do", "save");
707                 print $q->header,
708                       $q->start_html("Creating $page"),
709                       $q->h1("<a href=\"$url\">$wikiname</a>/ Creating $page"),
710                       $q->start_form(-action => $action),
711                       $q->hidden('do'),
712                       "Select page location:",
713                       $q->popup_menu('page', \@page_locs),
714                       $q->textarea(-name => 'content',
715                                -default => "",
716                                -rows => 20,
717                                -columns => 80),
718                       $q->br,
719                       "Optional comment about this change:",
720                       $q->br,
721                       $q->textfield(-name => "comments", -size => 80),
722                       $q->br,
723                       $q->submit("Save Page"),
724                       $q->end_form,
725                       $q->end_html;
726         }
727         elsif ($do eq 'edit') {
728                 my $content="";
729                 if (exists $pagesources{lc($page)}) {
730                         $content=readfile("$srcdir/$pagesources{lc($page)}");
731                         $content=~s/\n/\r\n/g;
732                 }
733                 $q->param("do", "save");
734                 print $q->header,
735                       $q->start_html("Editing $page"),
736                       $q->h1("<a href=\"$url\">$wikiname</a>/ Editing $page"),
737                       $q->start_form(-action => $action),
738                       $q->hidden('do'),
739                       $q->hidden('page'),
740                       $q->textarea(-name => 'content',
741                                -default => $content,
742                                -rows => 20,
743                                -columns => 80),
744                       $q->br,
745                       "Optional comment about this change:",
746                       $q->br,
747                       $q->textfield(-name => "comments", -size => 80),
748                       $q->br,
749                       $q->submit("Save Page"),
750                       $q->end_form,
751                       $q->end_html;
752         }
753         elsif ($do eq 'save') {
754                 my $file=$page.$default_pagetype;
755                 my $newfile=1;
756                 if (exists $pagesources{lc($page)}) {
757                         $file=$pagesources{lc($page)};
758                         $newfile=0;
759                 }
760                 
761                 my $content=$q->param('content');
762                 $content=~s/\r\n/\n/g;
763                 $content=~s/\r/\n/g;
764                 writefile("$srcdir/$file", $content);
765                 
766                 my $message="web commit from $ENV{REMOTE_ADDR}";
767                 if (defined $q->param('comments')) {
768                         $message.=": ".$q->param('comments');
769                 }
770                 
771                 if ($svn) {
772                         if ($newfile) {
773                                 rcs_add($file);
774                         }
775                         # presumably the commit will trigger an update
776                         # of the wiki
777                         rcs_commit($message);
778                 }
779                 else {
780                         refresh();
781                 }
782                 
783                 print $q->redirect("$url/".htmlpage($page));
784         }
785         else {
786                 error("unknown do parameter");
787         }
788 } #}}}
790 # main {{{
791 my $rebuild=0;
792 my $wrapper=0;
793 if (grep /^-/, @ARGV) {
794         eval {use Getopt::Long};
795         GetOptions(
796                 "wikiname=s" => \$wikiname,
797                 "verbose|v" => \$verbose,
798                 "rebuild" => \$rebuild,
799                 "wrapper" => \$wrapper,
800                 "svn!" => \$svn,
801                 "cgi" => \$cgi,
802                 "url=s" => \$url,
803                 "cgiurl=s" => \$cgiurl,
804                 "historyurl=s" => \$historyurl,
805         ) || usage();
807 usage() unless @ARGV == 2;
808 ($srcdir) = possibly_foolish_untaint(shift);
809 ($destdir) = possibly_foolish_untaint(shift);
811 if ($cgi && ! length $url) {
812         error("Must specify url to wiki with --url when using --cgi");
815 gen_wrapper($svn, $rebuild) if $wrapper;
816 memoize('pagename');
817 memoize('bestlink');
818 loadindex() unless $rebuild;
819 if ($cgi) {
820         cgi();
822 else {
823         rcs_update() if $svn;
824         refresh();
825         saveindex();
827 #}}}