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