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