web commit from 66.118.98.137:
[ikiwiki] / 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         my $noimagelink=shift;
174
175         my $bestlink=bestlink($page, $link);
176
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 }
197
198 sub linkify ($$) {
199         my $content=shift;
200         my $file=shift;
201
202         $content =~ s/$wiki_link_regexp/htmllink(pagename($file), $1)/eg;
203         
204         return $content;
205 }
206
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 }
218
219 sub linkbacks ($$) {
220         my $content=shift;
221         my $page=shift;
222
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         }
241
242         $content.="<hr><p>Links: ".join(" ", sort @links)."</p>\n" if @links;
243         return $content;
244 }
245
246 sub finalize ($$) {
247         my $content=shift;
248         my $page=shift;
249
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         
273         $content="<html>\n<head><title>$title</title></head>\n<body>\n".
274                   "<h1>$pagelink</h1>\n".
275                   "@actions\n<hr>\n".
276                   $content.
277                   "</body>\n</html>\n";
278         
279         return $content;
280 }
281
282 sub render ($) {
283         my $file=shift;
284         
285         my $type=pagetype($file);
286         my $content=readfile("$srcdir/$file");
287         if ($type ne 'unknown') {
288                 my $page=pagename($file);
289                 
290                 $links{$page}=[findlinks($content)];
291                 
292                 $content=linkify($content, $file);
293                 $content=htmlize($type, $content);
294                 $content=linkbacks($content, $page);
295                 $content=finalize($content, $page);
296                 
297                 writefile("$destdir/".htmlpage($page), $content);
298                 $oldpagemtime{$page}=time;
299                 $renderedfiles{$page}=htmlpage($page);
300         }
301         else {
302                 $links{$file}=[];
303                 writefile("$destdir/$file", $content);
304                 $oldpagemtime{$file}=time;
305                 $renderedfiles{$file}=$file;
306         }
307 }
308
309 sub loadindex () {
310         open (IN, "$srcdir/.index") || return;
311         while (<IN>) {
312                 $_=possibly_foolish_untaint($_);
313                 chomp;
314                 my ($mtime, $file, $rendered, @links)=split(' ', $_);
315                 my $page=pagename($file);
316                 $pagesources{$page}=$file;
317                 $oldpagemtime{$page}=$mtime;
318                 $oldlinks{$page}=[@links];
319                 $links{$page}=[@links];
320                 $renderedfiles{$page}=$rendered;
321         }
322         close IN;
323 }       
324
325 sub saveindex () {
326         open (OUT, ">$srcdir/.index") || error("cannot write to .index: $!");
327         foreach my $page (keys %oldpagemtime) {
328                 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
329                         join(" ", @{$links{$page}})."\n"
330                                 if $oldpagemtime{$page};
331         }
332         close OUT;
333 }
334
335 sub rcs_update () {
336         if (-d "$srcdir/.svn") {
337                 if (system("svn", "update", "--quiet", $srcdir) != 0) {
338                         warn("svn update failed\n");
339                 }
340         }
341 }
342
343 sub rcs_commit ($) {
344         my $message=shift;
345
346         if (-d "$srcdir/.svn") {
347                 if (system("svn", "commit", "--quiet", "-m",
348                            possibly_foolish_untaint($message), $srcdir) != 0) {
349                         warn("svn commit failed\n");
350                 }
351         }
352 }
353
354 sub rcs_add ($) {
355         my $file=shift;
356
357         if (-d "$srcdir/.svn") {
358                 my $parent=dirname($file);
359                 while (! -d "$srcdir/$parent/.svn") {
360                         $file=$parent;
361                         $parent=dirname($file);
362                 }
363                 
364                 if (system("svn", "add", "--quiet", "$srcdir/$file") != 0) {
365                         warn("svn add failed\n");
366                 }
367         }
368 }
369
370 sub rcs_recentchanges ($) {
371         my $num=shift;
372         my @ret;
373         
374         eval q{use Date::Parse};
375         eval q{use Time::Duration};
376         
377         if (-d "$srcdir/.svn") {
378                 my $info=`LANG=C svn info $srcdir`;
379                 my ($svn_url)=$info=~/^URL: (.*)$/m;
380
381                 # FIXME: currently assumes that the wiki is somewhere
382                 # under trunk in svn, doesn't support other layouts.
383                 my ($svn_base)=$svn_url=~m!(/trunk(?:/.*)?)$!;
384                 
385                 my $div=qr/^--------------------+$/;
386                 my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
387                 my $state='start';
388                 my ($rev, $user, $when, @pages, $message);
389                 foreach (`LANG=C svn log -v '$svn_url'`) {
390                         chomp;
391                         if ($state eq 'start' && /$div/) {
392                                 $state='header';
393                         }
394                         elsif ($state eq 'header' && /$infoline/) {
395                                 $rev=$1;
396                                 $user=$2;
397                                 $when=concise(ago(time - str2time($3)));
398                         }
399                         elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/(.+)$/) {
400                                 push @pages, pagename($1) if length $1;
401                         }
402                         elsif ($state eq 'header' && /^$/) {
403                                 $state='body';
404                         }
405                         elsif ($state eq 'body' && /$div/) {
406                                 push @ret, { rev => $rev, user => $user,
407                                         when => $when, message => $message,
408                                         pages => [@pages] } if @pages;
409                                 return @ret if @ret >= $num;
410                                 
411                                 $state='header';
412                                 $message=$rev=$user=$when=undef;
413                                 @pages=();
414                         }
415                         elsif ($state eq 'body') {
416                                 $message.="$_<br>\n";
417                         }
418                 }
419         }
420
421         return @ret;
422 }
423
424 sub prune ($) {
425         my $file=shift;
426
427         unlink($file);
428         my $dir=dirname($file);
429         while (rmdir($dir)) {
430                 $dir=dirname($dir);
431         }
432 }
433
434 sub refresh () {
435         # Find existing pages.
436         my %exists;
437         my @files;
438         find({
439                 no_chdir => 1,
440                 wanted => sub {
441                         if (/$wiki_file_prune_regexp/) {
442                                 $File::Find::prune=1;
443                         }
444                         elsif (! -d $_) {
445                                 my ($f)=/$wiki_file_regexp/; # untaint
446                                 if (! defined $f) {
447                                         warn("skipping bad filename $_\n");
448                                 }
449                                 else {
450                                         $f=~s/^\Q$srcdir\E\/?//;
451                                         push @files, $f;
452                                         $exists{pagename($f)}=1;
453                                 }
454                         }
455                 },
456         }, $srcdir);
457
458         my %rendered;
459
460         # check for added or removed pages
461         my @add;
462         foreach my $file (@files) {
463                 my $page=pagename($file);
464                 if (! $oldpagemtime{$page}) {
465                         debug("new page $page");
466                         push @add, $file;
467                         $links{$page}=[];
468                         $pagesources{$page}=$file;
469                 }
470         }
471         my @del;
472         foreach my $page (keys %oldpagemtime) {
473                 if (! $exists{$page}) {
474                         debug("removing old page $page");
475                         push @del, $renderedfiles{$page};
476                         prune($destdir."/".$renderedfiles{$page});
477                         delete $renderedfiles{$page};
478                         $oldpagemtime{$page}=0;
479                         delete $pagesources{$page};
480                 }
481         }
482         
483         # render any updated files
484         foreach my $file (@files) {
485                 my $page=pagename($file);
486                 
487                 if (! exists $oldpagemtime{$page} ||
488                     mtime("$srcdir/$file") > $oldpagemtime{$page}) {
489                         debug("rendering changed file $file");
490                         render($file);
491                         $rendered{$file}=1;
492                 }
493         }
494         
495         # if any files were added or removed, check to see if each page
496         # needs an update due to linking to them
497         # TODO: inefficient; pages may get rendered above and again here;
498         # problem is the bestlink may have changed and we won't know until
499         # now
500         if (@add || @del) {
501 FILE:           foreach my $file (@files) {
502                         my $page=pagename($file);
503                         foreach my $f (@add, @del) {
504                                 my $p=pagename($f);
505                                 foreach my $link (@{$links{$page}}) {
506                                         if (bestlink($page, $link) eq $p) {
507                                                 debug("rendering $file, which links to $p");
508                                                 render($file);
509                                                 $rendered{$file}=1;
510                                                 next FILE;
511                                         }
512                                 }
513                         }
514                 }
515         }
516
517         # handle linkbacks; if a page has added/removed links, update the
518         # pages it links to
519         # TODO: inefficient; pages may get rendered above and again here;
520         # problem is the linkbacks could be wrong in the first pass render
521         # above
522         if (%rendered) {
523                 my %linkchanged;
524                 foreach my $file (keys %rendered, @del) {
525                         my $page=pagename($file);
526                         if (exists $links{$page}) {
527                                 foreach my $link (@{$links{$page}}) {
528                                         $link=bestlink($page, $link);
529                                         if (length $link &&
530                                             ! exists $oldlinks{$page} ||
531                                             ! grep { $_ eq $link } @{$oldlinks{$page}}) {
532                                                 $linkchanged{$link}=1;
533                                         }
534                                 }
535                         }
536                         if (exists $oldlinks{$page}) {
537                                 foreach my $link (@{$oldlinks{$page}}) {
538                                         $link=bestlink($page, $link);
539                                         if (length $link &&
540                                             ! exists $links{$page} ||
541                                             ! grep { $_ eq $link } @{$links{$page}}) {
542                                                 $linkchanged{$link}=1;
543                                         }
544                                 }
545                         }
546                 }
547                 foreach my $link (keys %linkchanged) {
548                         my $linkfile=$pagesources{$link};
549                         if (defined $linkfile) {
550                                 debug("rendering $linkfile, to update its linkbacks");
551                                 render($linkfile);
552                         }
553                 }
554         }
555 }
556
557 # Generates a C wrapper program for running ikiwiki in a specific way.
558 # The wrapper may be safely made suid.
559 sub gen_wrapper ($$) {
560         my ($svn, $rebuild)=@_;
561
562         eval q{use Cwd 'abs_path'};
563         $srcdir=abs_path($srcdir);
564         $destdir=abs_path($destdir);
565         my $this=abs_path($0);
566         if (! -x $this) {
567                 error("$this doesn't seem to be executable");
568         }
569
570         my @params=($srcdir, $destdir, "--wikiname=$wikiname");
571         push @params, "--verbose" if $verbose;
572         push @params, "--rebuild" if $rebuild;
573         push @params, "--nosvn" if !$svn;
574         push @params, "--cgi" if $cgi;
575         push @params, "--url=$url" if $url;
576         push @params, "--cgiurl=$cgiurl" if $cgiurl;
577         my $params=join(" ", @params);
578         my $call='';
579         foreach my $p ($this, $this, @params) {
580                 $call.=qq{"$p", };
581         }
582         $call.="NULL";
583         
584         my @envsave;
585         push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
586                        CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE} if $cgi;
587         my $envsave="";
588         foreach my $var (@envsave) {
589                 $envsave.=<<"EOF"
590         if ((s=getenv("$var")))
591                 asprintf(&newenviron[i++], "%s=%s", "$var", s);
592 EOF
593         }
594         
595         open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
596         print OUT <<"EOF";
597 /* A wrapper for ikiwiki, can be safely made suid. */
598 #define _GNU_SOURCE
599 #include <stdio.h>
600 #include <unistd.h>
601 #include <stdlib.h>
602 #include <string.h>
603
604 extern char **environ;
605
606 int main (int argc, char **argv) {
607         /* Sanitize environment. */
608         char *s;
609         char *newenviron[$#envsave+3];
610         int i=0;
611 $envsave
612         newenviron[i++]="HOME=$ENV{HOME}";
613         newenviron[i]=NULL;
614         environ=newenviron;
615
616         if (argc == 2 && strcmp(argv[1], "--params") == 0) {
617                 printf("$params\\n");
618                 exit(0);
619         }
620         
621         execl($call);
622         perror("failed to run $this");
623         exit(1);
624 }
625 EOF
626         close OUT;
627         if (system("gcc", "ikiwiki-wrap.c", "-o", "ikiwiki-wrap") != 0) {
628                 error("failed to compile ikiwiki-wrap.c");
629         }
630         unlink("ikiwiki-wrap.c");
631         print "successfully generated ikiwiki-wrap\n";
632         exit 0;
633 }
634
635 sub cgi () {
636         eval q{use CGI};
637         my $q=CGI->new;
638
639         my $do=$q->param('do');
640         if (! defined $do || ! length $do) {
641                 error("\"do\" parameter missing");
642         }
643         
644         if ($do eq 'recentchanges') {
645                 my $list="<ul>\n";
646                 foreach my $change (rcs_recentchanges(100)) {
647                         $list.="<li>";
648                         $list.=join(", ", map { htmllink("", $_, 1) } @{$change->{pages}});
649                         $list.="<br>\n";
650                         $list.="changed ".$change->{when}." by ".
651                                htmllink("", $change->{user}, 1).
652                                ": <i>".$change->{message}."</i>\n";
653                         $list.="</li>\n";
654                 }
655                 $list.="</ul>\n";
656                 
657                 print $q->header,
658                       $q->start_html("RecentChanges"),
659                       $q->h1("<a href=\"$url\">$wikiname</a>/ RecentChanges"),
660                       $list,
661                       $q->end_form,
662                       $q->end_html;
663                 return;
664         }
665         
666         my ($page)=$q->param('page')=~/$wiki_file_regexp/;
667         if (! defined $page || ! length $page || $page ne $q->param('page') ||
668             $page=~/$wiki_file_prune_regexp/ || $page=~/^\//) {
669                 error("bad page name");
670         }
671         $page=lc($page);
672         
673         my $action=$q->request_uri;
674         $action=~s/\?.*//;
675         
676         if ($do eq 'create') {
677                 if (exists $pagesources{lc($page)}) {
678                         # hmm, someone else made the page in the meantime?
679                         print $q->redirect("$url/".htmlpage($page));
680                 }
681
682                 my @page_locs;
683                 my ($from)=$q->param('from')=~/$wiki_file_regexp/;
684                 if (! defined $from || ! length $from ||
685                     $from ne $q->param('from') ||
686                     $from=~/$wiki_file_prune_regexp/ || $from=~/^\//) {
687                         @page_locs=$page;
688                 }
689                 else {
690                         my $dir=$from."/";
691                         $dir=~s![^/]+/$!!;
692                         push @page_locs, $dir.$page;
693                         push @page_locs, "$from/$page";
694                         while (length $dir) {
695                                 $dir=~s![^/]+/$!!;
696                                 push @page_locs, $dir.$page;
697                         }
698                 }
699                 
700                 $q->param("do", "save");
701                 print $q->header,
702                       $q->start_html("Creating $page"),
703                       $q->h1("<a href=\"$url\">$wikiname</a>/ Creating $page"),
704                       $q->start_form(-action => $action),
705                       $q->hidden('do'),
706                       "Select page location:",
707                       $q->popup_menu('page', \@page_locs),
708                       $q->textarea(-name => 'content',
709                                -default => "",
710                                -rows => 20,
711                                -columns => 80),
712                       $q->br,
713                       "Optional comment about this change:",
714                       $q->br,
715                       $q->textfield(-name => "comments", -size => 80),
716                       $q->br,
717                       $q->submit("Save Page"),
718                       $q->end_form,
719                       $q->end_html;
720         }
721         elsif ($do eq 'edit') {
722                 my $content="";
723                 if (exists $pagesources{lc($page)}) {
724                         $content=readfile("$srcdir/$pagesources{lc($page)}");
725                         $content=~s/\n/\r\n/g;
726                 }
727                 $q->param("do", "save");
728                 print $q->header,
729                       $q->start_html("Editing $page"),
730                       $q->h1("<a href=\"$url\">$wikiname</a>/ Editing $page"),
731                       $q->start_form(-action => $action),
732                       $q->hidden('do'),
733                       $q->hidden('page'),
734                       $q->textarea(-name => 'content',
735                                -default => $content,
736                                -rows => 20,
737                                -columns => 80),
738                       $q->br,
739                       "Optional comment about this change:",
740                       $q->br,
741                       $q->textfield(-name => "comments", -size => 80),
742                       $q->br,
743                       $q->submit("Save Page"),
744                       $q->end_form,
745                       $q->end_html;
746         }
747         elsif ($do eq 'save') {
748                 my $file=$page.$default_pagetype;
749                 my $newfile=1;
750                 if (exists $pagesources{lc($page)}) {
751                         $file=$pagesources{lc($page)};
752                         $newfile=0;
753                 }
754                 
755                 my $content=$q->param('content');
756                 $content=~s/\r\n/\n/g;
757                 $content=~s/\r/\n/g;
758                 writefile("$srcdir/$file", $content);
759                 
760                 my $message="web commit from $ENV{REMOTE_ADDR}";
761                 if (defined $q->param('comments')) {
762                         $message.=": ".$q->param('comments');
763                 }
764                 
765                 if ($svn) {
766                         if ($newfile) {
767                                 rcs_add($file);
768                         }
769                         # presumably the commit will trigger an update
770                         # of the wiki
771                         rcs_commit($message);
772                 }
773                 else {
774                         refresh();
775                 }
776                 
777                 print $q->redirect("$url/".htmlpage($page));
778         }
779         else {
780                 error("unknown do parameter");
781         }
782 }
783
784 my $rebuild=0;
785 my $wrapper=0;
786 if (grep /^-/, @ARGV) {
787         eval {use Getopt::Long};
788         GetOptions(
789                 "wikiname=s" => \$wikiname,
790                 "verbose|v" => \$verbose,
791                 "rebuild" => \$rebuild,
792                 "wrapper" => \$wrapper,
793                 "svn!" => \$svn,
794                 "cgi" => \$cgi,
795                 "url=s" => \$url,
796                 "cgiurl=s" => \$cgiurl,
797         ) || usage();
798 }
799 usage() unless @ARGV == 2;
800 ($srcdir) = possibly_foolish_untaint(shift);
801 ($destdir) = possibly_foolish_untaint(shift);
802
803 if ($cgi && ! length $url) {
804         error("Must specify url to wiki with --url when using --cgi");
805 }
806
807 gen_wrapper($svn, $rebuild) if $wrapper;
808 memoize('pagename');
809 memoize('bestlink');
810 loadindex() unless $rebuild;
811 if ($cgi) {
812         cgi();
813 }
814 else {
815         rcs_update() if $svn;
816         refresh();
817         saveindex();
818 }