add --diffurl, if set RecentChanges has links to svn diffs
[ikiwiki] / ikiwiki
1 #!/usr/bin/perl -T
2 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
3
4 use warnings;
5 use strict;
6 use Memoize;
7 use File::Spec;
8 use HTML::Template;
9 use Getopt::Long;
10
11 my (%links, %oldlinks, %oldpagemtime, %renderedfiles, %pagesources);
12
13 # Holds global config settings, also used by some modules.
14 our %config=( #{{{
15         wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)},
16         wiki_link_regexp => qr/\[\[([^\s\]]+)\]\]/,
17         wiki_file_regexp => qr/(^[-A-Za-z0-9_.:\/+]+$)/,
18         verbose => 0,
19         wikiname => "wiki",
20         default_pageext => ".mdwn",
21         cgi => 0,
22         svn => 1,
23         url => '',
24         cgiurl => '',
25         historyurl => '',
26         diffurl => '',
27         anonok => 0,
28         rebuild => 0,
29         wrapper => undef,
30         wrappermode => undef,
31         srcdir => undef,
32         destdir => undef,
33         templatedir => undef,
34         setup => undef,
35 ); #}}}
36
37 GetOptions( #{{{
38         "setup=s" => \$config{setup},
39         "wikiname=s" => \$config{wikiname},
40         "verbose|v!" => \$config{verbose},
41         "rebuild!" => \$config{rebuild},
42         "wrapper=s" => sub { $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap" },
43         "wrappermode=i" => \$config{wrappermode},
44         "svn!" => \$config{svn},
45         "anonok!" => \$config{anonok},
46         "cgi!" => \$config{cgi},
47         "url=s" => \$config{url},
48         "cgiurl=s" => \$config{cgiurl},
49         "historyurl=s" => \$config{historyurl},
50         "diffurl=s" => \$config{diffurl},
51         "exclude=s@" => sub {
52                 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
53         },
54 ) || usage();
55
56 if (! $config{setup}) {
57         usage() unless @ARGV == 3;
58         $config{srcdir} = possibly_foolish_untaint(shift);
59         $config{templatedir} = possibly_foolish_untaint(shift);
60         $config{destdir} = possibly_foolish_untaint(shift);
61         if ($config{cgi} && ! length $config{url}) {
62                 error("Must specify url to wiki with --url when using --cgi");
63         }
64 }
65 #}}}
66
67 sub usage { #{{{
68         die "usage: ikiwiki [options] source templates dest\n";
69 } #}}}
70
71 sub error { #{{{
72         if ($config{cgi}) {
73                 print "Content-type: text/html\n\n";
74                 print misctemplate("Error", "<p>Error: @_</p>");
75         }
76         die @_;
77 } #}}}
78
79 sub debug ($) { #{{{
80         return unless $config{verbose};
81         if (! $config{cgi}) {
82                 print "@_\n";
83         }
84         else {
85                 print STDERR "@_\n";
86         }
87 } #}}}
88
89 sub mtime ($) { #{{{
90         my $page=shift;
91         
92         return (stat($page))[9];
93 } #}}}
94
95 sub possibly_foolish_untaint { #{{{
96         my $tainted=shift;
97         my ($untainted)=$tainted=~/(.*)/;
98         return $untainted;
99 } #}}}
100
101 sub basename ($) { #{{{
102         my $file=shift;
103
104         $file=~s!.*/!!;
105         return $file;
106 } #}}}
107
108 sub dirname ($) { #{{{
109         my $file=shift;
110
111         $file=~s!/?[^/]+$!!;
112         return $file;
113 } #}}}
114
115 sub pagetype ($) { #{{{
116         my $page=shift;
117         
118         if ($page =~ /\.mdwn$/) {
119                 return ".mdwn";
120         }
121         else {
122                 return "unknown";
123         }
124 } #}}}
125
126 sub pagename ($) { #{{{
127         my $file=shift;
128
129         my $type=pagetype($file);
130         my $page=$file;
131         $page=~s/\Q$type\E*$// unless $type eq 'unknown';
132         return $page;
133 } #}}}
134
135 sub htmlpage ($) { #{{{
136         my $page=shift;
137
138         return $page.".html";
139 } #}}}
140
141 sub readfile ($) { #{{{
142         my $file=shift;
143
144         local $/=undef;
145         open (IN, "$file") || error("failed to read $file: $!");
146         my $ret=<IN>;
147         close IN;
148         return $ret;
149 } #}}}
150
151 sub writefile ($$) { #{{{
152         my $file=shift;
153         my $content=shift;
154
155         my $dir=dirname($file);
156         if (! -d $dir) {
157                 my $d="";
158                 foreach my $s (split(m!/+!, $dir)) {
159                         $d.="$s/";
160                         if (! -d $d) {
161                                 mkdir($d) || error("failed to create directory $d: $!");
162                         }
163                 }
164         }
165         
166         open (OUT, ">$file") || error("failed to write $file: $!");
167         print OUT $content;
168         close OUT;
169 } #}}}
170
171 sub findlinks ($$) { #{{{
172         my $content=shift;
173         my $page=shift;
174
175         my @links;
176         while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
177                 push @links, lc($1);
178         }
179         # Discussion links are a special case since they're not in the text
180         # of the page, but on its template.
181         return @links, "$page/discussion";
182 } #}}}
183
184 sub bestlink ($$) { #{{{
185         # Given a page and the text of a link on the page, determine which
186         # existing page that link best points to. Prefers pages under a
187         # subdirectory with the same name as the source page, failing that
188         # goes down the directory tree to the base looking for matching
189         # pages.
190         my $page=shift;
191         my $link=lc(shift);
192         
193         my $cwd=$page;
194         do {
195                 my $l=$cwd;
196                 $l.="/" if length $l;
197                 $l.=$link;
198
199                 if (exists $links{$l}) {
200                         #debug("for $page, \"$link\", use $l");
201                         return $l;
202                 }
203         } while $cwd=~s!/?[^/]+$!!;
204
205         #print STDERR "warning: page $page, broken link: $link\n";
206         return "";
207 } #}}}
208
209 sub isinlinableimage ($) { #{{{
210         my $file=shift;
211         
212         $file=~/\.(png|gif|jpg|jpeg)$/;
213 } #}}}
214
215 sub htmllink { #{{{
216         my $page=shift;
217         my $link=shift;
218         my $noimageinline=shift; # don't turn links into inline html images
219         my $forcesubpage=shift; # force a link to a subpage
220
221         my $bestlink;
222         if (! $forcesubpage) {
223                 $bestlink=bestlink($page, $link);
224         }
225         else {
226                 $bestlink="$page/".lc($link);
227         }
228
229         return $link if length $bestlink && $page eq $bestlink;
230         
231         # TODO BUG: %renderedfiles may not have it, if the linked to page
232         # was also added and isn't yet rendered! Note that this bug is
233         # masked by the bug mentioned below that makes all new files
234         # be rendered twice.
235         if (! grep { $_ eq $bestlink } values %renderedfiles) {
236                 $bestlink=htmlpage($bestlink);
237         }
238         if (! grep { $_ eq $bestlink } values %renderedfiles) {
239                 return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$link"
240         }
241         
242         $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
243         
244         if (! $noimageinline && isinlinableimage($bestlink)) {
245                 return "<img src=\"$bestlink\">";
246         }
247         return "<a href=\"$bestlink\">$link</a>";
248 } #}}}
249
250 sub linkify ($$) { #{{{
251         my $content=shift;
252         my $page=shift;
253
254         $content =~ s{(\\?)$config{wiki_link_regexp}}{
255                 $1 ? "[[$2]]" : htmllink($page, $2)
256         }eg;
257         
258         return $content;
259 } #}}}
260
261 sub htmlize ($$) { #{{{
262         my $type=shift;
263         my $content=shift;
264         
265         if (! $INC{"/usr/bin/markdown"}) {
266                 no warnings 'once';
267                 $blosxom::version="is a proper perl module too much to ask?";
268                 use warnings 'all';
269                 do "/usr/bin/markdown";
270         }
271         
272         if ($type eq '.mdwn') {
273                 return Markdown::Markdown($content);
274         }
275         else {
276                 error("htmlization of $type not supported");
277         }
278 } #}}}
279
280 sub backlinks ($) { #{{{
281         my $page=shift;
282
283         my @links;
284         foreach my $p (keys %links) {
285                 next if bestlink($page, $p) eq $page;
286                 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
287                         my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
288                         
289                         # Trim common dir prefixes from both pages.
290                         my $p_trimmed=$p;
291                         my $page_trimmed=$page;
292                         my $dir;
293                         1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
294                                 defined $dir &&
295                                 $p_trimmed=~s/^\Q$dir\E// &&
296                                 $page_trimmed=~s/^\Q$dir\E//;
297                                        
298                         push @links, { url => $href, page => $p_trimmed };
299                 }
300         }
301
302         return sort { $a->{page} cmp $b->{page} } @links;
303 } #}}}
304         
305 sub parentlinks ($) { #{{{
306         my $page=shift;
307         
308         my @ret;
309         my $pagelink="";
310         my $path="";
311         my $skip=1;
312         foreach my $dir (reverse split("/", $page)) {
313                 if (! $skip) {
314                         $path.="../";
315                         unshift @ret, { url => "$path$dir.html", page => $dir };
316                 }
317                 else {
318                         $skip=0;
319                 }
320         }
321         unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
322         return @ret;
323 } #}}}
324
325 sub indexlink () { #{{{
326         return "<a href=\"$config{url}\">$config{wikiname}</a>";
327 } #}}}
328
329 sub finalize ($$$) { #{{{
330         my $content=shift;
331         my $page=shift;
332         my $mtime=shift;
333
334         my $title=basename($page);
335         $title=~s/_/ /g;
336         
337         my $template=HTML::Template->new(blind_cache => 1,
338                 filename => "$config{templatedir}/page.tmpl");
339         
340         if (length $config{cgiurl}) {
341                 $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
342                 if ($config{svn}) {
343                         $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
344                 }
345         }
346
347         if (length $config{historyurl}) {
348                 my $u=$config{historyurl};
349                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
350                 $template->param(historyurl => $u);
351         }
352         
353         $template->param(
354                 title => $title,
355                 wikiname => $config{wikiname},
356                 parentlinks => [parentlinks($page)],
357                 content => $content,
358                 backlinks => [backlinks($page)],
359                 discussionlink => htmllink($page, "Discussion", 1, 1),
360                 mtime => scalar(gmtime($mtime)),
361         );
362         
363         return $template->output;
364 } #}}}
365
366 sub check_overwrite ($$) { #{{{
367         # Important security check. Make sure to call this before saving
368         # any files to the source directory.
369         my $dest=shift;
370         my $src=shift;
371         
372         if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
373                 error("$dest already exists and was rendered from ".
374                         join(" ",(grep { $renderedfiles{$_} eq $dest } keys
375                                 %renderedfiles)).
376                         ", before, so not rendering from $src");
377         }
378 } #}}}
379
380 sub render ($) { #{{{
381         my $file=shift;
382         
383         my $type=pagetype($file);
384         my $content=readfile("$config{srcdir}/$file");
385         if ($type ne 'unknown') {
386                 my $page=pagename($file);
387                 
388                 $links{$page}=[findlinks($content, $page)];
389                 
390                 $content=linkify($content, $page);
391                 $content=htmlize($type, $content);
392                 $content=finalize($content, $page,
393                         mtime("$config{srcdir}/$file"));
394                 
395                 check_overwrite("$config{destdir}/".htmlpage($page), $page);
396                 writefile("$config{destdir}/".htmlpage($page), $content);
397                 $oldpagemtime{$page}=time;
398                 $renderedfiles{$page}=htmlpage($page);
399         }
400         else {
401                 $links{$file}=[];
402                 check_overwrite("$config{destdir}/$file", $file);
403                 writefile("$config{destdir}/$file", $content);
404                 $oldpagemtime{$file}=time;
405                 $renderedfiles{$file}=$file;
406         }
407 } #}}}
408
409 sub lockwiki () { #{{{
410         # Take an exclusive lock on the wiki to prevent multiple concurrent
411         # run issues. The lock will be dropped on program exit.
412         if (! -d "$config{srcdir}/.ikiwiki") {
413                 mkdir("$config{srcdir}/.ikiwiki");
414         }
415         open(WIKILOCK, ">$config{srcdir}/.ikiwiki/lockfile") || error ("cannot write to lockfile: $!");
416         if (! flock(WIKILOCK, 2 | 4)) {
417                 debug("wiki seems to be locked, waiting for lock");
418                 my $wait=600; # arbitrary, but don't hang forever to 
419                               # prevent process pileup
420                 for (1..600) {
421                         return if flock(WIKILOCK, 2 | 4);
422                         sleep 1;
423                 }
424                 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
425         }
426 } #}}}
427
428 sub unlockwiki () { #{{{
429         close WIKILOCK;
430 } #}}}
431
432 sub loadindex () { #{{{
433         open (IN, "$config{srcdir}/.ikiwiki/index") || return;
434         while (<IN>) {
435                 $_=possibly_foolish_untaint($_);
436                 chomp;
437                 my ($mtime, $file, $rendered, @links)=split(' ', $_);
438                 my $page=pagename($file);
439                 $pagesources{$page}=$file;
440                 $oldpagemtime{$page}=$mtime;
441                 $oldlinks{$page}=[@links];
442                 $links{$page}=[@links];
443                 $renderedfiles{$page}=$rendered;
444         }
445         close IN;
446 } #}}}
447
448 sub saveindex () { #{{{
449         if (! -d "$config{srcdir}/.ikiwiki") {
450                 mkdir("$config{srcdir}/.ikiwiki");
451         }
452         open (OUT, ">$config{srcdir}/.ikiwiki/index") || error("cannot write to index: $!");
453         foreach my $page (keys %oldpagemtime) {
454                 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
455                         join(" ", @{$links{$page}})."\n"
456                                 if $oldpagemtime{$page};
457         }
458         close OUT;
459 } #}}}
460
461 sub rcs_update () { #{{{
462         if (-d "$config{srcdir}/.svn") {
463                 if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
464                         warn("svn update failed\n");
465                 }
466         }
467 } #}}}
468
469 sub rcs_prepedit ($) { #{{{
470         # Prepares to edit a file under revision control. Returns a token
471         # that must be passed into rcs_commit when the file is ready
472         # for committing.
473         # The file is relative to the srcdir.
474         my $file=shift;
475         
476         if (-d "$config{srcdir}/.svn") {
477                 # For subversion, return the revision of the file when
478                 # editing begins.
479                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
480                 return defined $rev ? $rev : "";
481         }
482 } #}}}
483
484 sub rcs_commit ($$$) { #{{{
485         # Tries to commit the page; returns undef on _success_ and
486         # a version of the page with the rcs's conflict markers on failure.
487         # The file is relative to the srcdir.
488         my $file=shift;
489         my $message=shift;
490         my $rcstoken=shift;
491
492         if (-d "$config{srcdir}/.svn") {
493                 # Check to see if the page has been changed by someone
494                 # else since rcs_prepedit was called.
495                 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
496                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
497                 if (defined $rev && defined $oldrev && $rev != $oldrev) {
498                         # Merge their changes into the file that we've
499                         # changed.
500                         chdir($config{srcdir}); # svn merge wants to be here
501                         if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
502                                    "$config{srcdir}/$file") != 0) {
503                                 warn("svn merge -r$oldrev:$rev failed\n");
504                         }
505                 }
506
507                 if (system("svn", "commit", "--quiet", "-m",
508                            possibly_foolish_untaint($message),
509                            "$config{srcdir}") != 0) {
510                         my $conflict=readfile("$config{srcdir}/$file");
511                         if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
512                                 warn("svn revert failed\n");
513                         }
514                         return $conflict;
515                 }
516         }
517         return undef # success
518 } #}}}
519
520 sub rcs_add ($) { #{{{
521         # filename is relative to the root of the srcdir
522         my $file=shift;
523
524         if (-d "$config{srcdir}/.svn") {
525                 my $parent=dirname($file);
526                 while (! -d "$config{srcdir}/$parent/.svn") {
527                         $file=$parent;
528                         $parent=dirname($file);
529                 }
530                 
531                 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
532                         warn("svn add failed\n");
533                 }
534         }
535 } #}}}
536
537 sub svn_info ($$) { #{{{
538         my $field=shift;
539         my $file=shift;
540
541         my $info=`LANG=C svn info $file`;
542         my ($ret)=$info=~/^$field: (.*)$/m;
543         return $ret;
544 } #}}}
545
546 sub rcs_recentchanges ($) { #{{{
547         my $num=shift;
548         my @ret;
549         
550         eval q{use CGI 'escapeHTML'};
551         eval q{use Date::Parse};
552         eval q{use Time::Duration};
553         
554         if (-d "$config{srcdir}/.svn") {
555                 my $svn_url=svn_info("URL", $config{srcdir});
556
557                 # FIXME: currently assumes that the wiki is somewhere
558                 # under trunk in svn, doesn't support other layouts.
559                 my ($svn_base)=$svn_url=~m!(/trunk(?:/.*)?)$!;
560                 
561                 my $div=qr/^--------------------+$/;
562                 my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
563                 my $state='start';
564                 my ($rev, $user, $when, @pages, @message);
565                 foreach (`LANG=C svn log --limit $num -v '$svn_url'`) {
566                         chomp;
567                         if ($state eq 'start' && /$div/) {
568                                 $state='header';
569                         }
570                         elsif ($state eq 'header' && /$infoline/) {
571                                 $rev=$1;
572                                 $user=$2;
573                                 $when=concise(ago(time - str2time($3)));
574                         }
575                         elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/([^ ]+)(?:$|\s)/) {
576                                 my $file=$1;
577                                 my $diffurl=$config{diffurl};
578                                 $diffurl=~s/\[\[file\]\]/$file/g;
579                                 $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
580                                 $diffurl=~s/\[\[r2\]\]/$rev/g;
581                                 push @pages, {
582                                         link => htmllink("", pagename($file), 1),
583                                         diffurl => $diffurl,
584                                 } if length $file;
585                         }
586                         elsif ($state eq 'header' && /^$/) {
587                                 $state='body';
588                         }
589                         elsif ($state eq 'body' && /$div/) {
590                                 my $committype="web";
591                                 if (defined $message[0] &&
592                                     $message[0]->{line}=~/^web commit by (\w+):?(.*)/) {
593                                         $user="$1";
594                                         $message[0]->{line}=$2;
595                                 }
596                                 else {
597                                         $committype="svn";
598                                 }
599                                 
600                                 push @ret, { rev => $rev,
601                                         user => htmllink("", $user, 1),
602                                         committype => $committype,
603                                         when => $when, message => [@message],
604                                         pages => [@pages],
605                                 } if @pages;
606                                 return @ret if @ret >= $num;
607                                 
608                                 $state='header';
609                                 $rev=$user=$when=undef;
610                                 @pages=@message=();
611                         }
612                         elsif ($state eq 'body') {
613                                 push @message, {line => escapeHTML($_)},
614                         }
615                 }
616         }
617
618         return @ret;
619 } #}}}
620
621 sub prune ($) { #{{{
622         my $file=shift;
623
624         unlink($file);
625         my $dir=dirname($file);
626         while (rmdir($dir)) {
627                 $dir=dirname($dir);
628         }
629 } #}}}
630
631 sub refresh () { #{{{
632         # find existing pages
633         my %exists;
634         my @files;
635         eval q{use File::Find};
636         find({
637                 no_chdir => 1,
638                 wanted => sub {
639                         if (/$config{wiki_file_prune_regexp}/) {
640                                 no warnings 'once';
641                                 $File::Find::prune=1;
642                                 use warnings "all";
643                         }
644                         elsif (! -d $_ && ! -l $_) {
645                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
646                                 if (! defined $f) {
647                                         warn("skipping bad filename $_\n");
648                                 }
649                                 else {
650                                         $f=~s/^\Q$config{srcdir}\E\/?//;
651                                         push @files, $f;
652                                         $exists{pagename($f)}=1;
653                                 }
654                         }
655                 },
656         }, $config{srcdir});
657
658         my %rendered;
659
660         # check for added or removed pages
661         my @add;
662         foreach my $file (@files) {
663                 my $page=pagename($file);
664                 if (! $oldpagemtime{$page}) {
665                         debug("new page $page");
666                         push @add, $file;
667                         $links{$page}=[];
668                         $pagesources{$page}=$file;
669                 }
670         }
671         my @del;
672         foreach my $page (keys %oldpagemtime) {
673                 if (! $exists{$page}) {
674                         debug("removing old page $page");
675                         push @del, $pagesources{$page};
676                         prune($config{destdir}."/".$renderedfiles{$page});
677                         delete $renderedfiles{$page};
678                         $oldpagemtime{$page}=0;
679                         delete $pagesources{$page};
680                 }
681         }
682         
683         # render any updated files
684         foreach my $file (@files) {
685                 my $page=pagename($file);
686                 
687                 if (! exists $oldpagemtime{$page} ||
688                     mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
689                         debug("rendering changed file $file");
690                         render($file);
691                         $rendered{$file}=1;
692                 }
693         }
694         
695         # if any files were added or removed, check to see if each page
696         # needs an update due to linking to them
697         # TODO: inefficient; pages may get rendered above and again here;
698         # problem is the bestlink may have changed and we won't know until
699         # now
700         if (@add || @del) {
701 FILE:           foreach my $file (@files) {
702                         my $page=pagename($file);
703                         foreach my $f (@add, @del) {
704                                 my $p=pagename($f);
705                                 foreach my $link (@{$links{$page}}) {
706                                         if (bestlink($page, $link) eq $p) {
707                                                 debug("rendering $file, which links to $p");
708                                                 render($file);
709                                                 $rendered{$file}=1;
710                                                 next FILE;
711                                         }
712                                 }
713                         }
714                 }
715         }
716
717         # handle backlinks; if a page has added/removed links, update the
718         # pages it links to
719         # TODO: inefficient; pages may get rendered above and again here;
720         # problem is the backlinks could be wrong in the first pass render
721         # above
722         if (%rendered) {
723                 my %linkchanged;
724                 foreach my $file (keys %rendered, @del) {
725                         my $page=pagename($file);
726                         if (exists $links{$page}) {
727                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
728                                         if (length $link &&
729                                             ! exists $oldlinks{$page} ||
730                                             ! grep { $_ eq $link } @{$oldlinks{$page}}) {
731                                                 $linkchanged{$link}=1;
732                                         }
733                                 }
734                         }
735                         if (exists $oldlinks{$page}) {
736                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
737                                         if (length $link &&
738                                             ! exists $links{$page} ||
739                                             ! grep { $_ eq $link } @{$links{$page}}) {
740                                                 $linkchanged{$link}=1;
741                                         }
742                                 }
743                         }
744                 }
745                 foreach my $link (keys %linkchanged) {
746                         my $linkfile=$pagesources{$link};
747                         if (defined $linkfile) {
748                                 debug("rendering $linkfile, to update its backlinks");
749                                 render($linkfile);
750                         }
751                 }
752         }
753 } #}}}
754
755 sub gen_wrapper (@) { #{{{
756         my %config=(@_);
757         eval q{use Cwd 'abs_path'};
758         $config{srcdir}=abs_path($config{srcdir});
759         $config{destdir}=abs_path($config{destdir});
760         my $this=abs_path($0);
761         if (! -x $this) {
762                 error("$this doesn't seem to be executable");
763         }
764
765         if ($config{setup}) {
766                 error("cannot create a wrapper that uses a setup file");
767         }
768         
769         my @params=($config{srcdir}, $config{templatedir}, $config{destdir},
770                 "--wikiname=$config{wikiname}");
771         push @params, "--verbose" if $config{verbose};
772         push @params, "--rebuild" if $config{rebuild};
773         push @params, "--nosvn" if !$config{svn};
774         push @params, "--cgi" if $config{cgi};
775         push @params, "--url=$config{url}" if length $config{url};
776         push @params, "--cgiurl=$config{cgiurl}" if length $config{cgiurl};
777         push @params, "--historyurl=$config{historyurl}" if length $config{historyurl};
778         push @params, "--diffurl=$config{diffurl}" if length $config{diffurl};
779         push @params, "--anonok" if $config{anonok};
780         my $params=join(" ", @params);
781         my $call='';
782         foreach my $p ($this, $this, @params) {
783                 $call.=qq{"$p", };
784         }
785         $call.="NULL";
786         
787         my @envsave;
788         push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
789                        CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
790                        HTTP_COOKIE} if $config{cgi};
791         my $envsave="";
792         foreach my $var (@envsave) {
793                 $envsave.=<<"EOF"
794         if ((s=getenv("$var")))
795                 asprintf(&newenviron[i++], "%s=%s", "$var", s);
796 EOF
797         }
798         
799         open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
800         print OUT <<"EOF";
801 /* A wrapper for ikiwiki, can be safely made suid. */
802 #define _GNU_SOURCE
803 #include <stdio.h>
804 #include <unistd.h>
805 #include <stdlib.h>
806 #include <string.h>
807
808 extern char **environ;
809
810 int main (int argc, char **argv) {
811         /* Sanitize environment. */
812         char *s;
813         char *newenviron[$#envsave+3];
814         int i=0;
815 $envsave
816         newenviron[i++]="HOME=$ENV{HOME}";
817         newenviron[i]=NULL;
818         environ=newenviron;
819
820         if (argc == 2 && strcmp(argv[1], "--params") == 0) {
821                 printf("$params\\n");
822                 exit(0);
823         }
824         
825         execl($call);
826         perror("failed to run $this");
827         exit(1);
828 }
829 EOF
830         close OUT;
831         if (system("gcc", "ikiwiki-wrap.c", "-o", possibly_foolish_untaint($config{wrapper})) != 0) {
832                 error("failed to compile ikiwiki-wrap.c");
833         }
834         unlink("ikiwiki-wrap.c");
835         if (defined $config{wrappermode} &&
836             ! chmod(oct($config{wrappermode}), possibly_foolish_untaint($config{wrapper}))) {
837                 error("chmod $config{wrapper}: $!");
838         }
839         print "successfully generated $config{wrapper}\n";
840 } #}}}
841                 
842 sub misctemplate ($$) { #{{{
843         my $title=shift;
844         my $pagebody=shift;
845         
846         my $template=HTML::Template->new(
847                 filename => "$config{templatedir}/misc.tmpl"
848         );
849         $template->param(
850                 title => $title,
851                 indexlink => indexlink(),
852                 wikiname => $config{wikiname},
853                 pagebody => $pagebody,
854         );
855         return $template->output;
856 }#}}}
857
858 sub cgi_recentchanges ($) { #{{{
859         my $q=shift;
860         
861         my $template=HTML::Template->new(
862                 filename => "$config{templatedir}/recentchanges.tmpl"
863         );
864         $template->param(
865                 title => "RecentChanges",
866                 indexlink => indexlink(),
867                 wikiname => $config{wikiname},
868                 changelog => [rcs_recentchanges(100)],
869         );
870         print $q->header, $template->output;
871 } #}}}
872
873 sub userinfo_get ($$) { #{{{
874         my $user=shift;
875         my $field=shift;
876
877         eval q{use Storable};
878         my $userdata=eval{ Storable::lock_retrieve("$config{srcdir}/.ikiwiki/userdb") };
879         if (! defined $userdata || ! ref $userdata || 
880             ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
881                 return "";
882         }
883         return $userdata->{$user}->{$field};
884 } #}}}
885
886 sub userinfo_set ($$) { #{{{
887         my $user=shift;
888         my $info=shift;
889         
890         eval q{use Storable};
891         my $userdata=eval{ Storable::lock_retrieve("$config{srcdir}/.ikiwiki/userdb") };
892         if (! defined $userdata || ! ref $userdata) {
893                 $userdata={};
894         }
895         $userdata->{$user}=$info;
896         my $oldmask=umask(077);
897         my $ret=Storable::lock_store($userdata, "$config{srcdir}/.ikiwiki/userdb");
898         umask($oldmask);
899         return $ret;
900 } #}}}
901
902 sub cgi_signin ($$) { #{{{
903         my $q=shift;
904         my $session=shift;
905
906         eval q{use CGI::FormBuilder};
907         my $form = CGI::FormBuilder->new(
908                 title => "$config{wikiname} signin",
909                 fields => [qw(do page from name password confirm_password email)],
910                 header => 1,
911                 method => 'POST',
912                 validate => {
913                         confirm_password => {
914                                 perl => q{eq $form->field("password")},
915                         },
916                         email => 'EMAIL',
917                 },
918                 required => 'NONE',
919                 javascript => 0,
920                 params => $q,
921                 action => $q->request_uri,
922                 header => 0,
923                 template => (-e "$config{templatedir}/signin.tmpl" ?
924                               "$config{templatedir}/signin.tmpl" : "")
925         );
926         
927         $form->field(name => "name", required => 0);
928         $form->field(name => "do", type => "hidden");
929         $form->field(name => "page", type => "hidden");
930         $form->field(name => "from", type => "hidden");
931         $form->field(name => "password", type => "password", required => 0);
932         $form->field(name => "confirm_password", type => "password", required => 0);
933         $form->field(name => "email", required => 0);
934         if ($q->param("do") ne "signin") {
935                 $form->text("You need to log in before you can edit pages.");
936         }
937         
938         if ($form->submitted) {
939                 # Set required fields based on how form was submitted.
940                 my %required=(
941                         "Login" => [qw(name password)],
942                         "Register" => [qw(name password confirm_password email)],
943                         "Mail Password" => [qw(name)],
944                 );
945                 foreach my $opt (@{$required{$form->submitted}}) {
946                         $form->field(name => $opt, required => 1);
947                 }
948         
949                 # Validate password differently depending on how
950                 # form was submitted.
951                 if ($form->submitted eq 'Login') {
952                         $form->field(
953                                 name => "password",
954                                 validate => sub {
955                                         length $form->field("name") &&
956                                         shift eq userinfo_get($form->field("name"), 'password');
957                                 },
958                         );
959                         $form->field(name => "name", validate => '/^\w+$/');
960                 }
961                 else {
962                         $form->field(name => "password", validate => 'VALUE');
963                 }
964                 # And make sure the entered name exists when logging
965                 # in or sending email, and does not when registering.
966                 if ($form->submitted eq 'Register') {
967                         $form->field(
968                                 name => "name",
969                                 validate => sub {
970                                         my $name=shift;
971                                         length $name &&
972                                         ! userinfo_get($name, "regdate");
973                                 },
974                         );
975                 }
976                 else {
977                         $form->field(
978                                 name => "name",
979                                 validate => sub {
980                                         my $name=shift;
981                                         length $name &&
982                                         userinfo_get($name, "regdate");
983                                 },
984                         );
985                 }
986         }
987         else {
988                 # First time settings.
989                 $form->field(name => "name", comment => "use FirstnameLastName");
990                 $form->field(name => "confirm_password", comment => "(only needed");
991                 $form->field(name => "email",            comment => "for registration)");
992                 if ($session->param("name")) {
993                         $form->field(name => "name", value => $session->param("name"));
994                 }
995         }
996
997         if ($form->submitted && $form->validate) {
998                 if ($form->submitted eq 'Login') {
999                         $session->param("name", $form->field("name"));
1000                         if (defined $form->field("do") && 
1001                             $form->field("do") ne 'signin') {
1002                                 print $q->redirect(
1003                                         "$config{cgiurl}?do=".$form->field("do").
1004                                         "&page=".$form->field("page").
1005                                         "&from=".$form->field("from"));;
1006                         }
1007                         else {
1008                                 print $q->redirect($config{url});
1009                         }
1010                 }
1011                 elsif ($form->submitted eq 'Register') {
1012                         my $user_name=$form->field('name');
1013                         if (userinfo_set($user_name, {
1014                                            'email' => $form->field('email'),
1015                                            'password' => $form->field('password'),
1016                                            'regdate' => time
1017                                          })) {
1018                                 $form->field(name => "confirm_password", type => "hidden");
1019                                 $form->field(name => "email", type => "hidden");
1020                                 $form->text("Registration successful. Now you can Login.");
1021                                 print $session->header();
1022                                 print misctemplate($form->title, $form->render(submit => ["Login"]));
1023                         }
1024                         else {
1025                                 error("Error saving registration.");
1026                         }
1027                 }
1028                 elsif ($form->submitted eq 'Mail Password') {
1029                         my $user_name=$form->field("name");
1030                         my $template=HTML::Template->new(
1031                                 filename => "$config{templatedir}/passwordmail.tmpl"
1032                         );
1033                         $template->param(
1034                                 user_name => $user_name,
1035                                 user_password => userinfo_get($user_name, "password"),
1036                                 wikiurl => $config{url},
1037                                 wikiname => $config{wikiname},
1038                                 REMOTE_ADDR => $ENV{REMOTE_ADDR},
1039                         );
1040                         
1041                         eval q{use Mail::Sendmail};
1042                         my ($fromhost) = $config{cgiurl} =~ m!/([^/]+)!;
1043                         sendmail(
1044                                 To => userinfo_get($user_name, "email"),
1045                                 From => "$config{wikiname} admin <".(getpwuid($>))[0]."@".$fromhost.">",
1046                                 Subject => "$config{wikiname} information",
1047                                 Message => $template->output,
1048                         ) or error("Failed to send mail");
1049                         
1050                         $form->text("Your password has been emailed to you.");
1051                         $form->field(name => "name", required => 0);
1052                         print $session->header();
1053                         print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
1054                 }
1055         }
1056         else {
1057                 print $session->header();
1058                 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
1059         }
1060 } #}}}
1061
1062 sub cgi_editpage ($$) { #{{{
1063         my $q=shift;
1064         my $session=shift;
1065
1066         eval q{use CGI::FormBuilder};
1067         my $form = CGI::FormBuilder->new(
1068                 fields => [qw(do rcsinfo from page content comments)],
1069                 header => 1,
1070                 method => 'POST',
1071                 validate => {
1072                         content => '/.+/',
1073                 },
1074                 required => [qw{content}],
1075                 javascript => 0,
1076                 params => $q,
1077                 action => $q->request_uri,
1078                 table => 0,
1079                 template => "$config{templatedir}/editpage.tmpl"
1080         );
1081         my @buttons=("Save Page", "Preview", "Cancel");
1082         
1083         my ($page)=$form->param('page')=~/$config{wiki_file_regexp}/;
1084         if (! defined $page || ! length $page || $page ne $q->param('page') ||
1085             $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
1086                 error("bad page name");
1087         }
1088         $page=lc($page);
1089         
1090         my $file=$page.$config{default_pageext};
1091         my $newfile=1;
1092         if (exists $pagesources{lc($page)}) {
1093                 $file=$pagesources{lc($page)};
1094                 $newfile=0;
1095         }
1096
1097         $form->field(name => "do", type => 'hidden');
1098         $form->field(name => "from", type => 'hidden');
1099         $form->field(name => "rcsinfo", type => 'hidden');
1100         $form->field(name => "page", value => "$page", force => 1);
1101         $form->field(name => "comments", type => "text", size => 80);
1102         $form->field(name => "content", type => "textarea", rows => 20,
1103                 cols => 80);
1104         $form->tmpl_param("can_commit", $config{svn});
1105         $form->tmpl_param("indexlink", indexlink());
1106         $form->tmpl_param("helponformattinglink",
1107                 htmllink("", "HelpOnFormatting", 1));
1108         if (! $form->submitted) {
1109                 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
1110                         force => 1);
1111         }
1112         
1113         if ($form->submitted eq "Cancel") {
1114                 print $q->redirect("$config{url}/".htmlpage($page));
1115                 return;
1116         }
1117         elsif ($form->submitted eq "Preview") {
1118                 $form->tmpl_param("page_preview",
1119                         htmlize($config{default_pageext},
1120                                 linkify($form->field('content'), $page)));
1121         }
1122         else {
1123                 $form->tmpl_param("page_preview", "");
1124         }
1125         $form->tmpl_param("page_conflict", "");
1126         
1127         if (! $form->submitted || $form->submitted eq "Preview" || 
1128             ! $form->validate) {
1129                 if ($form->field("do") eq "create") {
1130                         if (exists $pagesources{lc($page)}) {
1131                                 # hmm, someone else made the page in the
1132                                 # meantime?
1133                                 print $q->redirect("$config{url}/".htmlpage($page));
1134                                 return;
1135                         }
1136                         
1137                         my @page_locs;
1138                         my $best_loc;
1139                         my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
1140                         if (! defined $from || ! length $from ||
1141                             $from ne $form->param('from') ||
1142                             $from=~/$config{wiki_file_prune_regexp}/ || $from=~/^\//) {
1143                                 @page_locs=$best_loc=$page;
1144                         }
1145                         else {
1146                                 my $dir=$from."/";
1147                                 $dir=~s![^/]+/$!!;
1148                                 
1149                                 if ($page eq 'discussion') {
1150                                         $best_loc="$from/$page";
1151                                 }
1152                                 else {
1153                                         $best_loc=$dir.$page;
1154                                 }
1155                                 
1156                                 push @page_locs, $dir.$page;
1157                                 push @page_locs, "$from/$page";
1158                                 while (length $dir) {
1159                                         $dir=~s![^/]+/$!!;
1160                                         push @page_locs, $dir.$page;
1161                                 }
1162
1163                                 @page_locs = grep { ! exists
1164                                         $pagesources{lc($_)} } @page_locs;
1165                         }
1166
1167                         $form->tmpl_param("page_select", 1);
1168                         $form->field(name => "page", type => 'select',
1169                                 options => \@page_locs, value => $best_loc);
1170                         $form->title("creating $page");
1171                 }
1172                 elsif ($form->field("do") eq "edit") {
1173                         if (! defined $form->field('content') || 
1174                             ! length $form->field('content')) {
1175                                 my $content="";
1176                                 if (exists $pagesources{lc($page)}) {
1177                                                 $content=readfile("$config{srcdir}/$pagesources{lc($page)}");
1178                                         $content=~s/\n/\r\n/g;
1179                                 }
1180                                 $form->field(name => "content", value => $content,
1181                                         force => 1);
1182                         }
1183                         $form->tmpl_param("page_select", 0);
1184                         $form->field(name => "page", type => 'hidden');
1185                         $form->title("editing $page");
1186                 }
1187                 
1188                 print $form->render(submit => \@buttons);
1189         }
1190         else {
1191                 # save page
1192                 my $content=$form->field('content');
1193                 $content=~s/\r\n/\n/g;
1194                 $content=~s/\r/\n/g;
1195                 writefile("$config{srcdir}/$file", $content);
1196                 
1197                 my $message="web commit ";
1198                 if ($session->param("name")) {
1199                         $message.="by ".$session->param("name");
1200                 }
1201                 else {
1202                         $message.="from $ENV{REMOTE_ADDR}";
1203                 }
1204                 if (defined $form->field('comments') &&
1205                     length $form->field('comments')) {
1206                         $message.=": ".$form->field('comments');
1207                 }
1208                 
1209                 if ($config{svn}) {
1210                         if ($newfile) {
1211                                 rcs_add($file);
1212                         }
1213                         # prevent deadlock with post-commit hook
1214                         unlockwiki();
1215                         # presumably the commit will trigger an update
1216                         # of the wiki
1217                         my $conflict=rcs_commit($file, $message,
1218                                 $form->field("rcsinfo"));
1219                 
1220                         if (defined $conflict) {
1221                                 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
1222                                         force => 1);
1223                                 $form->tmpl_param("page_conflict", 1);
1224                                 $form->field("content", value => $conflict, force => 1);
1225                                 $form->field("do", "edit)");
1226                                 $form->tmpl_param("page_select", 0);
1227                                 $form->field(name => "page", type => 'hidden');
1228                                 $form->title("editing $page");
1229                                 print $form->render(submit => \@buttons);
1230                                 return;
1231                         }
1232                 }
1233                 else {
1234                         loadindex();
1235                         refresh();
1236                         saveindex();
1237                 }
1238                 
1239                 # The trailing question mark tries to avoid broken
1240                 # caches and get the most recent version of the page.
1241                 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
1242         }
1243 } #}}}
1244
1245 sub cgi () { #{{{
1246         eval q{use CGI};
1247         eval q{use CGI::Session};
1248         
1249         my $q=CGI->new;
1250         
1251         my $do=$q->param('do');
1252         if (! defined $do || ! length $do) {
1253                 error("\"do\" parameter missing");
1254         }
1255         
1256         # This does not need a session.
1257         if ($do eq 'recentchanges') {
1258                 cgi_recentchanges($q);
1259                 return;
1260         }
1261         
1262         CGI::Session->name("ikiwiki_session");
1263
1264         my $oldmask=umask(077);
1265         my $session = CGI::Session->new("driver:db_file", $q,
1266                 { FileName => "$config{srcdir}/.ikiwiki/sessions.db" });
1267         umask($oldmask);
1268         
1269         # Everything below this point needs the user to be signed in.
1270         if ((! $config{anonok} && ! defined $session->param("name") ||
1271                 ! userinfo_get($session->param("name"), "regdate")) || $do eq 'signin') {
1272                 cgi_signin($q, $session);
1273         
1274                 # Force session flush with safe umask.
1275                 my $oldmask=umask(077);
1276                 $session->flush;
1277                 umask($oldmask);
1278                 
1279                 return;
1280         }
1281         
1282         if ($do eq 'create' || $do eq 'edit') {
1283                 cgi_editpage($q, $session);
1284         }
1285         else {
1286                 error("unknown do parameter");
1287         }
1288 } #}}}
1289
1290 sub setup () { # {{{
1291         my $setup=possibly_foolish_untaint($config{setup});
1292         delete $config{setup};
1293         open (IN, $setup) || error("read $setup: $!\n");
1294         local $/=undef;
1295         my $code=<IN>;
1296         ($code)=$code=~/(.*)/s;
1297         close IN;
1298
1299         eval $code;
1300         error($@) if $@;
1301         exit;
1302 } #}}}
1303
1304 # main {{{
1305 setup() if $config{setup};
1306 lockwiki();
1307 if ($config{wrapper}) {
1308         gen_wrapper(%config);
1309         exit;
1310 }
1311 memoize('pagename');
1312 memoize('bestlink');
1313 loadindex() unless $config{rebuild};
1314 if ($config{cgi}) {
1315         cgi();
1316 }
1317 else {
1318         rcs_update() if $config{svn};
1319         refresh();
1320         saveindex();
1321 }
1322 #}}}