* htmlscrubber security fix: Block javascript in uris.
[ikiwiki.git] / IkiWiki.pm
1 #!/usr/bin/perl
2
3 package IkiWiki;
4 use warnings;
5 use strict;
6 use Encode;
7 use HTML::Entities;
8 use URI::Escape q{uri_escape_utf8};
9 use POSIX;
10 use open qw{:utf8 :std};
11
12 use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
13             %pagestate %renderedfiles %oldrenderedfiles %pagesources
14             %destsources %depends %hooks %forcerebuild $gettext_obj};
15
16 use Exporter q{import};
17 our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
18                  bestlink htmllink readfile writefile pagetype srcfile pagename
19                  displaytime will_render gettext urlto targetpage
20                  add_underlay
21                  %config %links %pagestate %renderedfiles
22                  %pagesources %destsources);
23 our $VERSION = 2.00; # plugin interface version, next is ikiwiki version
24 our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
25 my $installdir=''; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
26
27 # Optimisation.
28 use Memoize;
29 memoize("abs2rel");
30 memoize("pagespec_translate");
31 memoize("file_pruned");
32
33 sub defaultconfig () { #{{{
34         return
35         wiki_file_prune_regexps => [qr/(^|\/)\.\.(\/|$)/, qr/^\./, qr/\/\./,
36                 qr/\.x?html?$/, qr/\.ikiwiki-new$/,
37                 qr/(^|\/).svn\//, qr/.arch-ids\//, qr/{arch}\//,
38                 qr/(^|\/)_MTN\//,
39                 qr/\.dpkg-tmp$/],
40         wiki_link_regexp => qr{
41                 \[\[(?=[^!])            # beginning of link
42                 (?:
43                         ([^\]\|]+)      # 1: link text
44                         \|              # followed by '|'
45                 )?                      # optional
46                 
47                 ([^\n\r\]#]+)           # 2: page to link to
48                 (?:
49                         \#              # '#', beginning of anchor
50                         ([^\s\]]+)      # 3: anchor text
51                 )?                      # optional
52                 
53                 \]\]                    # end of link
54         }x,
55         wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
56         web_commit_regexp => qr/^web commit (by (.*?(?=: |$))|from (\d+\.\d+\.\d+\.\d+)):?(.*)/,
57         verbose => 0,
58         syslog => 0,
59         wikiname => "wiki",
60         default_pageext => "mdwn",
61         htmlext => "html",
62         cgi => 0,
63         post_commit => 0,
64         rcs => '',
65         url => '',
66         cgiurl => '',
67         historyurl => '',
68         diffurl => '',
69         rss => 0,
70         atom => 0,
71         allowrss => 0,
72         allowatom => 0,
73         discussion => 1,
74         rebuild => 0,
75         refresh => 0,
76         getctime => 0,
77         w3mmode => 0,
78         wrapper => undef,
79         wrappermode => undef,
80         svnpath => "trunk",
81         gitorigin_branch => "origin",
82         gitmaster_branch => "master",
83         srcdir => undef,
84         destdir => undef,
85         pingurl => [],
86         templatedir => "$installdir/share/ikiwiki/templates",
87         underlaydir => "$installdir/share/ikiwiki/basewiki",
88         underlaydirs => [],
89         setup => undef,
90         adminuser => undef,
91         adminemail => undef,
92         plugin => [qw{mdwn inline htmlscrubber passwordauth openid signinedit
93                       lockedit conditional recentchanges}],
94         libdir => undef,
95         timeformat => '%c',
96         locale => undef,
97         sslcookie => 0,
98         httpauth => 0,
99         userdir => "",
100         usedirs => 1,
101         numbacklinks => 10,
102         account_creation_password => "",
103         prefix_directives => 0,
104 } #}}}
105
106 sub checkconfig () { #{{{
107         # locale stuff; avoid LC_ALL since it overrides everything
108         if (defined $ENV{LC_ALL}) {
109                 $ENV{LANG} = $ENV{LC_ALL};
110                 delete $ENV{LC_ALL};
111         }
112         if (defined $config{locale}) {
113                 if (POSIX::setlocale(&POSIX::LC_ALL, $config{locale})) {
114                         $ENV{LANG}=$config{locale};
115                         $gettext_obj=undef;
116                 }
117         }
118
119         if ($config{w3mmode}) {
120                 eval q{use Cwd q{abs_path}};
121                 error($@) if $@;
122                 $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir}));
123                 $config{destdir}=possibly_foolish_untaint(abs_path($config{destdir}));
124                 $config{cgiurl}="file:///\$LIB/ikiwiki-w3m.cgi/".$config{cgiurl}
125                         unless $config{cgiurl} =~ m!file:///!;
126                 $config{url}="file://".$config{destdir};
127         }
128
129         if ($config{cgi} && ! length $config{url}) {
130                 error(gettext("Must specify url to wiki with --url when using --cgi"));
131         }
132         
133         $config{wikistatedir}="$config{srcdir}/.ikiwiki"
134                 unless exists $config{wikistatedir};
135         
136         if ($config{rcs}) {
137                 eval qq{use IkiWiki::Rcs::$config{rcs}};
138                 if ($@) {
139                         error("Failed to load RCS module IkiWiki::Rcs::$config{rcs}: $@");
140                 }
141         }
142         else {
143                 require IkiWiki::Rcs::Stub;
144         }
145
146         if (exists $config{umask}) {
147                 umask(possibly_foolish_untaint($config{umask}));
148         }
149
150         if (!$config{prefix_directives}) {
151                 $config{wiki_link_regexp} = qr{
152                         \[\[                    # beginning of link
153                         (?:
154                                 ([^\]\|\n\s]+)  # 1: link text
155                                 \|              # followed by '|'
156                         )?                      # optional
157
158                         ([^\s\]#]+)             # 2: page to link to
159                         (?:
160                                 \#              # '#', beginning of anchor
161                                 ([^\s\]]+)      # 3: anchor text
162                         )?                      # optional
163
164                         \]\]                    # end of link
165                 }x,
166         }
167
168         run_hooks(checkconfig => sub { shift->() });
169
170         return 1;
171 } #}}}
172
173 sub loadplugins () { #{{{
174         if (defined $config{libdir}) {
175                 unshift @INC, possibly_foolish_untaint($config{libdir});
176         }
177
178         loadplugin($_) foreach @{$config{plugin}};
179
180         run_hooks(getopt => sub { shift->() });
181         if (grep /^-/, @ARGV) {
182                 print STDERR "Unknown option: $_\n"
183                         foreach grep /^-/, @ARGV;
184                 usage();
185         }
186
187         return 1;
188 } #}}}
189
190 sub loadplugin ($) { #{{{
191         my $plugin=shift;
192
193         return if grep { $_ eq $plugin} @{$config{disable_plugins}};
194
195         foreach my $dir (defined $config{libdir} ? possibly_foolish_untaint($config{libdir}) : undef,
196                          "$installdir/lib/ikiwiki") {
197                 if (defined $dir && -x "$dir/plugins/$plugin") {
198                         require IkiWiki::Plugin::external;
199                         import IkiWiki::Plugin::external "$dir/plugins/$plugin";
200                         return 1;
201                 }
202         }
203
204         my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
205         eval qq{use $mod};
206         if ($@) {
207                 error("Failed to load plugin $mod: $@");
208         }
209         return 1;
210 } #}}}
211
212 sub error ($;$) { #{{{
213         my $message=shift;
214         my $cleaner=shift;
215         if ($config{cgi}) {
216                 print "Content-type: text/html\n\n";
217                 print misctemplate(gettext("Error"),
218                         "<p>".gettext("Error").": $message</p>");
219         }
220         log_message('err' => $message) if $config{syslog};
221         if (defined $cleaner) {
222                 $cleaner->();
223         }
224         die $message."\n";
225 } #}}}
226
227 sub debug ($) { #{{{
228         return unless $config{verbose};
229         return log_message(debug => @_);
230 } #}}}
231
232 my $log_open=0;
233 sub log_message ($$) { #{{{
234         my $type=shift;
235
236         if ($config{syslog}) {
237                 require Sys::Syslog;
238                 if (! $log_open) {
239                         Sys::Syslog::setlogsock('unix');
240                         Sys::Syslog::openlog('ikiwiki', '', 'user');
241                         $log_open=1;
242                 }
243                 return eval {
244                         Sys::Syslog::syslog($type, "[$config{wikiname}] %s", join(" ", @_));
245                 };
246         }
247         elsif (! $config{cgi}) {
248                 return print "@_\n";
249         }
250         else {
251                 return print STDERR "@_\n";
252         }
253 } #}}}
254
255 sub possibly_foolish_untaint ($) { #{{{
256         my $tainted=shift;
257         my ($untainted)=$tainted=~/(.*)/s;
258         return $untainted;
259 } #}}}
260
261 sub basename ($) { #{{{
262         my $file=shift;
263
264         $file=~s!.*/+!!;
265         return $file;
266 } #}}}
267
268 sub dirname ($) { #{{{
269         my $file=shift;
270
271         $file=~s!/*[^/]+$!!;
272         return $file;
273 } #}}}
274
275 sub pagetype ($) { #{{{
276         my $page=shift;
277         
278         if ($page =~ /\.([^.]+)$/) {
279                 return $1 if exists $hooks{htmlize}{$1};
280         }
281         return;
282 } #}}}
283
284 sub isinternal ($) { #{{{
285         my $page=shift;
286         return exists $pagesources{$page} &&
287                 $pagesources{$page} =~ /\._([^.]+)$/;
288 } #}}}
289
290 sub pagename ($) { #{{{
291         my $file=shift;
292
293         my $type=pagetype($file);
294         my $page=$file;
295         $page=~s/\Q.$type\E*$// if defined $type;
296         return $page;
297 } #}}}
298
299 sub targetpage ($$) { #{{{
300         my $page=shift;
301         my $ext=shift;
302         
303         if (! $config{usedirs} || $page =~ /^index$/ ) {
304                 return $page.".".$ext;
305         } else {
306                 return $page."/index.".$ext;
307         }
308 } #}}}
309
310 sub htmlpage ($) { #{{{
311         my $page=shift;
312         
313         return targetpage($page, $config{htmlext});
314 } #}}}
315
316 sub srcfile ($) { #{{{
317         my $file=shift;
318
319         return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
320         foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
321                 return "$dir/$file" if -e "$dir/$file";
322         }
323         error("internal error: $file cannot be found in $config{srcdir} or underlay");
324         return;
325 } #}}}
326
327 sub add_underlay ($) { #{{{
328         my $dir=shift;
329
330         if ($dir=~/^\//) {
331                 unshift @{$config{underlaydirs}}, $dir;
332         }
333         else {
334                 unshift @{$config{underlaydirs}}, "$config{underlaydir}/../$dir";
335         }
336
337         return 1;
338 } #}}}
339
340 sub readfile ($;$$) { #{{{
341         my $file=shift;
342         my $binary=shift;
343         my $wantfd=shift;
344
345         if (-l $file) {
346                 error("cannot read a symlink ($file)");
347         }
348         
349         local $/=undef;
350         open (my $in, "<", $file) || error("failed to read $file: $!");
351         binmode($in) if ($binary);
352         return \*$in if $wantfd;
353         my $ret=<$in>;
354         close $in || error("failed to read $file: $!");
355         return $ret;
356 } #}}}
357
358 sub writefile ($$$;$$) { #{{{
359         my $file=shift; # can include subdirs
360         my $destdir=shift; # directory to put file in
361         my $content=shift;
362         my $binary=shift;
363         my $writer=shift;
364         
365         my $test=$file;
366         while (length $test) {
367                 if (-l "$destdir/$test") {
368                         error("cannot write to a symlink ($test)");
369                 }
370                 $test=dirname($test);
371         }
372         my $newfile="$destdir/$file.ikiwiki-new";
373         if (-l $newfile) {
374                 error("cannot write to a symlink ($newfile)");
375         }
376
377         my $dir=dirname($newfile);
378         if (! -d $dir) {
379                 my $d="";
380                 foreach my $s (split(m!/+!, $dir)) {
381                         $d.="$s/";
382                         if (! -d $d) {
383                                 mkdir($d) || error("failed to create directory $d: $!");
384                         }
385                 }
386         }
387
388         my $cleanup = sub { unlink($newfile) };
389         open (my $out, '>', $newfile) || error("failed to write $newfile: $!", $cleanup);
390         binmode($out) if ($binary);
391         if ($writer) {
392                 $writer->(\*$out, $cleanup);
393         }
394         else {
395                 print $out $content or error("failed writing to $newfile: $!", $cleanup);
396         }
397         close $out || error("failed saving $newfile: $!", $cleanup);
398         rename($newfile, "$destdir/$file") || 
399                 error("failed renaming $newfile to $destdir/$file: $!", $cleanup);
400
401         return 1;
402 } #}}}
403
404 my %cleared;
405 sub will_render ($$;$) { #{{{
406         my $page=shift;
407         my $dest=shift;
408         my $clear=shift;
409
410         # Important security check.
411         if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
412             ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}})) {
413                 error("$config{destdir}/$dest independently created, not overwriting with version from $page");
414         }
415
416         if (! $clear || $cleared{$page}) {
417                 $renderedfiles{$page}=[$dest, grep { $_ ne $dest } @{$renderedfiles{$page}}];
418         }
419         else {
420                 foreach my $old (@{$renderedfiles{$page}}) {
421                         delete $destsources{$old};
422                 }
423                 $renderedfiles{$page}=[$dest];
424                 $cleared{$page}=1;
425         }
426         $destsources{$dest}=$page;
427
428         return 1;
429 } #}}}
430
431 sub bestlink ($$) { #{{{
432         my $page=shift;
433         my $link=shift;
434         
435         my $cwd=$page;
436         if ($link=~s/^\/+//) {
437                 # absolute links
438                 $cwd="";
439         }
440         $link=~s/\/$//;
441
442         do {
443                 my $l=$cwd;
444                 $l.="/" if length $l;
445                 $l.=$link;
446
447                 if (exists $links{$l}) {
448                         return $l;
449                 }
450                 elsif (exists $pagecase{lc $l}) {
451                         return $pagecase{lc $l};
452                 }
453         } while $cwd=~s!/?[^/]+$!!;
454
455         if (length $config{userdir}) {
456                 my $l = "$config{userdir}/".lc($link);
457                 if (exists $links{$l}) {
458                         return $l;
459                 }
460                 elsif (exists $pagecase{lc $l}) {
461                         return $pagecase{lc $l};
462                 }
463         }
464
465         #print STDERR "warning: page $page, broken link: $link\n";
466         return "";
467 } #}}}
468
469 sub isinlinableimage ($) { #{{{
470         my $file=shift;
471         
472         return $file =~ /\.(png|gif|jpg|jpeg)$/i;
473 } #}}}
474
475 sub pagetitle ($;$) { #{{{
476         my $page=shift;
477         my $unescaped=shift;
478
479         if ($unescaped) {
480                 $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : chr($2)/eg;
481         }
482         else {
483                 $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : "&#$2;"/eg;
484         }
485
486         return $page;
487 } #}}}
488
489 sub titlepage ($) { #{{{
490         my $title=shift;
491         $title=~s/([^-[:alnum:]:+\/.])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
492         return $title;
493 } #}}}
494
495 sub linkpage ($) { #{{{
496         my $link=shift;
497         $link=~s/([^-[:alnum:]:+\/._])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
498         return $link;
499 } #}}}
500
501 sub cgiurl (@) { #{{{
502         my %params=@_;
503
504         return $config{cgiurl}."?".
505                 join("&amp;", map $_."=".uri_escape_utf8($params{$_}), keys %params);
506 } #}}}
507
508 sub baseurl (;$) { #{{{
509         my $page=shift;
510
511         return "$config{url}/" if ! defined $page;
512         
513         $page=htmlpage($page);
514         $page=~s/[^\/]+$//;
515         $page=~s/[^\/]+\//..\//g;
516         return $page;
517 } #}}}
518
519 sub abs2rel ($$) { #{{{
520         # Work around very innefficient behavior in File::Spec if abs2rel
521         # is passed two relative paths. It's much faster if paths are
522         # absolute! (Debian bug #376658; fixed in debian unstable now)
523         my $path="/".shift;
524         my $base="/".shift;
525
526         require File::Spec;
527         my $ret=File::Spec->abs2rel($path, $base);
528         $ret=~s/^// if defined $ret;
529         return $ret;
530 } #}}}
531
532 sub displaytime ($;$) { #{{{
533         my $time=shift;
534         my $format=shift;
535         if (! defined $format) {
536                 $format=$config{timeformat};
537         }
538
539         # strftime doesn't know about encodings, so make sure
540         # its output is properly treated as utf8
541         return decode_utf8(POSIX::strftime($format, localtime($time)));
542 } #}}}
543
544 sub beautify_url ($) { #{{{
545         my $url=shift;
546
547         if ($config{usedirs}) {
548                 $url =~ s!/index.$config{htmlext}$!/!;
549         }
550         $url =~ s!^$!./!; # Browsers don't like empty links...
551
552         return $url;
553 } #}}}
554
555 sub urlto ($$) { #{{{
556         my $to=shift;
557         my $from=shift;
558
559         if (! length $to) {
560                 return beautify_url(baseurl($from));
561         }
562
563         if (! $destsources{$to}) {
564                 $to=htmlpage($to);
565         }
566
567         my $link = abs2rel($to, dirname(htmlpage($from)));
568
569         return beautify_url($link);
570 } #}}}
571
572 sub htmllink ($$$;@) { #{{{
573         my $lpage=shift; # the page doing the linking
574         my $page=shift; # the page that will contain the link (different for inline)
575         my $link=shift;
576         my %opts=@_;
577
578         $link=~s/\/$//;
579
580         my $bestlink;
581         if (! $opts{forcesubpage}) {
582                 $bestlink=bestlink($lpage, $link);
583         }
584         else {
585                 $bestlink="$lpage/".lc($link);
586         }
587
588         my $linktext;
589         if (defined $opts{linktext}) {
590                 $linktext=$opts{linktext};
591         }
592         else {
593                 $linktext=pagetitle(basename($link));
594         }
595         
596         return "<span class=\"selflink\">$linktext</span>"
597                 if length $bestlink && $page eq $bestlink &&
598                    ! defined $opts{anchor};
599         
600         if (! $destsources{$bestlink}) {
601                 $bestlink=htmlpage($bestlink);
602
603                 if (! $destsources{$bestlink}) {
604                         return $linktext unless length $config{cgiurl};
605                         return "<span class=\"createlink\"><a href=\"".
606                                 cgiurl(
607                                         do => "create",
608                                         page => pagetitle(lc($link), 1),
609                                         from => $lpage
610                                 ).
611                                 "\">?</a>$linktext</span>"
612                 }
613         }
614         
615         $bestlink=abs2rel($bestlink, dirname(htmlpage($page)));
616         $bestlink=beautify_url($bestlink);
617         
618         if (! $opts{noimageinline} && isinlinableimage($bestlink)) {
619                 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
620         }
621
622         if (defined $opts{anchor}) {
623                 $bestlink.="#".$opts{anchor};
624         }
625
626         my @attrs;
627         if (defined $opts{rel}) {
628                 push @attrs, ' rel="'.$opts{rel}.'"';
629         }
630         if (defined $opts{class}) {
631                 push @attrs, ' class="'.$opts{class}.'"';
632         }
633
634         return "<a href=\"$bestlink\"@attrs>$linktext</a>";
635 } #}}}
636
637 sub userlink ($) { #{{{
638         my $user=shift;
639
640         my $oiduser=eval { openiduser($user) };
641         if (defined $oiduser) {
642                 return "<a href=\"$user\">$oiduser</a>";
643         }
644         else {
645                 return htmllink("", "", escapeHTML(
646                         length $config{userdir} ? $config{userdir}."/".$user : $user
647                 ), noimageinline => 1);
648         }
649 } #}}}
650
651 sub htmlize ($$$) { #{{{
652         my $page=shift;
653         my $type=shift;
654         my $content=shift;
655         
656         my $oneline = $content !~ /\n/;
657
658         if (exists $hooks{htmlize}{$type}) {
659                 $content=$hooks{htmlize}{$type}{call}->(
660                         page => $page,
661                         content => $content,
662                 );
663         }
664         else {
665                 error("htmlization of $type not supported");
666         }
667
668         run_hooks(sanitize => sub {
669                 $content=shift->(
670                         page => $page,
671                         content => $content,
672                 );
673         });
674         
675         if ($oneline) {
676                 # hack to get rid of enclosing junk added by markdown
677                 # and other htmlizers
678                 $content=~s/^<p>//i;
679                 $content=~s/<\/p>$//i;
680                 chomp $content;
681         }
682
683         return $content;
684 } #}}}
685
686 sub linkify ($$$) { #{{{
687         my $lpage=shift; # the page containing the links
688         my $page=shift; # the page the link will end up on (different for inline)
689         my $content=shift;
690
691         $content =~ s{(\\?)$config{wiki_link_regexp}}{
692                 defined $2
693                         ? ( $1 
694                                 ? "[[$2|$3".($4 ? "#$4" : "")."]]" 
695                                 : htmllink($lpage, $page, linkpage($3),
696                                         anchor => $4, linktext => pagetitle($2)))
697                         : ( $1 
698                                 ? "[[$3".($4 ? "#$4" : "")."]]"
699                                 : htmllink($lpage, $page, linkpage($3),
700                                         anchor => $4))
701         }eg;
702         
703         return $content;
704 } #}}}
705
706 my %preprocessing;
707 our $preprocess_preview=0;
708 sub preprocess ($$$;$$) { #{{{
709         my $page=shift; # the page the data comes from
710         my $destpage=shift; # the page the data will appear in (different for inline)
711         my $content=shift;
712         my $scan=shift;
713         my $preview=shift;
714
715         # Using local because it needs to be set within any nested calls
716         # of this function.
717         local $preprocess_preview=$preview if defined $preview;
718
719         my $handle=sub {
720                 my $escape=shift;
721                 my $prefix=shift;
722                 my $command=shift;
723                 my $params=shift;
724                 if (length $escape) {
725                         return "[[$prefix$command $params]]";
726                 }
727                 elsif (exists $hooks{preprocess}{$command}) {
728                         return "" if $scan && ! $hooks{preprocess}{$command}{scan};
729                         # Note: preserve order of params, some plugins may
730                         # consider it significant.
731                         my @params;
732                         while ($params =~ m{
733                                 (?:([-\w]+)=)?          # 1: named parameter key?
734                                 (?:
735                                         """(.*?)"""     # 2: triple-quoted value
736                                 |
737                                         "([^"]+)"       # 3: single-quoted value
738                                 |
739                                         (\S+)           # 4: unquoted value
740                                 )
741                                 (?:\s+|$)               # delimiter to next param
742                         }sgx) {
743                                 my $key=$1;
744                                 my $val;
745                                 if (defined $2) {
746                                         $val=$2;
747                                         $val=~s/\r\n/\n/mg;
748                                         $val=~s/^\n+//g;
749                                         $val=~s/\n+$//g;
750                                 }
751                                 elsif (defined $3) {
752                                         $val=$3;
753                                 }
754                                 elsif (defined $4) {
755                                         $val=$4;
756                                 }
757
758                                 if (defined $key) {
759                                         push @params, $key, $val;
760                                 }
761                                 else {
762                                         push @params, $val, '';
763                                 }
764                         }
765                         if ($preprocessing{$page}++ > 3) {
766                                 # Avoid loops of preprocessed pages preprocessing
767                                 # other pages that preprocess them, etc.
768                                 #translators: The first parameter is a
769                                 #translators: preprocessor directive name,
770                                 #translators: the second a page name, the
771                                 #translators: third a number.
772                                 return "[[".sprintf(gettext("%s preprocessing loop detected on %s at depth %i"),
773                                         $command, $page, $preprocessing{$page}).
774                                 "]]";
775                         }
776                         my $ret;
777                         if (! $scan) {
778                                 $ret=$hooks{preprocess}{$command}{call}->(
779                                         @params,
780                                         page => $page,
781                                         destpage => $destpage,
782                                         preview => $preprocess_preview,
783                                 );
784                         }
785                         else {
786                                 # use void context during scan pass
787                                 $hooks{preprocess}{$command}{call}->(
788                                         @params,
789                                         page => $page,
790                                         destpage => $destpage,
791                                         preview => $preprocess_preview,
792                                 );
793                                 $ret="";
794                         }
795                         $preprocessing{$page}--;
796                         return $ret;
797                 }
798                 else {
799                         return "[[$prefix$command $params]]";
800                 }
801         };
802         
803         my $regex;
804         if ($config{prefix_directives}) {
805                 $regex = qr{
806                         (\\?)           # 1: escape?
807                         \[\[(!)         # directive open; 2: prefix
808                         ([-\w]+)        # 3: command
809                         (               # 4: the parameters..
810                                 \s+     # Must have space if parameters present
811                                 (?:
812                                         (?:[-\w]+=)?            # named parameter key?
813                                         (?:
814                                                 """.*?"""       # triple-quoted value
815                                                 |
816                                                 "[^"]+"         # single-quoted value
817                                                 |
818                                                 [^\s\]]+        # unquoted value
819                                         )
820                                         \s*                     # whitespace or end
821                                                                 # of directive
822                                 )
823                         *)?             # 0 or more parameters
824                         \]\]            # directive closed
825                 }sx;
826         } else {
827                 $regex = qr{
828                         (\\?)           # 1: escape?
829                         \[\[(!?)        # directive open; 2: optional prefix
830                         ([-\w]+)        # 3: command
831                         \s+
832                         (               # 4: the parameters..
833                                 (?:
834                                         (?:[-\w]+=)?            # named parameter key?
835                                         (?:
836                                                 """.*?"""       # triple-quoted value
837                                                 |
838                                                 "[^"]+"         # single-quoted value
839                                                 |
840                                                 [^\s\]]+        # unquoted value
841                                         )
842                                         \s*                     # whitespace or end
843                                                                 # of directive
844                                 )
845                         *)              # 0 or more parameters
846                         \]\]            # directive closed
847                 }sx;
848         }
849
850         $content =~ s{$regex}{$handle->($1, $2, $3, $4)}eg;
851         return $content;
852 } #}}}
853
854 sub filter ($$$) { #{{{
855         my $page=shift;
856         my $destpage=shift;
857         my $content=shift;
858
859         run_hooks(filter => sub {
860                 $content=shift->(page => $page, destpage => $destpage, 
861                         content => $content);
862         });
863
864         return $content;
865 } #}}}
866
867 sub indexlink () { #{{{
868         return "<a href=\"$config{url}\">$config{wikiname}</a>";
869 } #}}}
870
871 my $wikilock;
872
873 sub lockwiki (;$) { #{{{
874         my $wait=@_ ? shift : 1;
875         # Take an exclusive lock on the wiki to prevent multiple concurrent
876         # run issues. The lock will be dropped on program exit.
877         if (! -d $config{wikistatedir}) {
878                 mkdir($config{wikistatedir});
879         }
880         open($wikilock, '>', "$config{wikistatedir}/lockfile") ||
881                 error ("cannot write to $config{wikistatedir}/lockfile: $!");
882         if (! flock($wikilock, 2 | 4)) { # LOCK_EX | LOCK_NB
883                 if ($wait) {
884                         debug("wiki seems to be locked, waiting for lock");
885                         my $wait=600; # arbitrary, but don't hang forever to 
886                                       # prevent process pileup
887                         for (1..$wait) {
888                                 return if flock($wikilock, 2 | 4);
889                                 sleep 1;
890                         }
891                         error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
892                 }
893                 else {
894                         return 0;
895                 }
896         }
897         return 1;
898 } #}}}
899
900 sub unlockwiki () { #{{{
901         return close($wikilock) if $wikilock;
902         return;
903 } #}}}
904
905 my $commitlock;
906
907 sub commit_hook_enabled () { #{{{
908         open($commitlock, '+>', "$config{wikistatedir}/commitlock") ||
909                 error("cannot write to $config{wikistatedir}/commitlock: $!");
910         if (! flock($commitlock, 1 | 4)) { # LOCK_SH | LOCK_NB to test
911                 close($commitlock) || error("failed closing commitlock: $!");
912                 return 0;
913         }
914         close($commitlock) || error("failed closing commitlock: $!");
915         return 1;
916 } #}}}
917
918 sub disable_commit_hook () { #{{{
919         open($commitlock, '>', "$config{wikistatedir}/commitlock") ||
920                 error("cannot write to $config{wikistatedir}/commitlock: $!");
921         if (! flock($commitlock, 2)) { # LOCK_EX
922                 error("failed to get commit lock");
923         }
924         return 1;
925 } #}}}
926
927 sub enable_commit_hook () { #{{{
928         return close($commitlock) if $commitlock;
929         return;
930 } #}}}
931
932 sub loadindex () { #{{{
933         %oldrenderedfiles=%pagectime=();
934         if (! $config{rebuild}) {
935                 %pagesources=%pagemtime=%oldlinks=%links=%depends=
936                         %destsources=%renderedfiles=%pagecase=%pagestate=();
937         }
938         open (my $in, "<", "$config{wikistatedir}/index") || return;
939         while (<$in>) {
940                 $_=possibly_foolish_untaint($_);
941                 chomp;
942                 my %items;
943                 $items{link}=[];
944                 $items{dest}=[];
945                 foreach my $i (split(/ /, $_)) {
946                         my ($item, $val)=split(/=/, $i, 2);
947                         push @{$items{$item}}, decode_entities($val);
948                 }
949
950                 next unless exists $items{src}; # skip bad lines for now
951
952                 my $page=pagename($items{src}[0]);
953                 if (! $config{rebuild}) {
954                         $pagesources{$page}=$items{src}[0];
955                         $pagemtime{$page}=$items{mtime}[0];
956                         $oldlinks{$page}=[@{$items{link}}];
957                         $links{$page}=[@{$items{link}}];
958                         $depends{$page}=$items{depends}[0] if exists $items{depends};
959                         $destsources{$_}=$page foreach @{$items{dest}};
960                         $renderedfiles{$page}=[@{$items{dest}}];
961                         $pagecase{lc $page}=$page;
962                         foreach my $k (grep /_/, keys %items) {
963                                 my ($id, $key)=split(/_/, $k, 2);
964                                 $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
965                         }
966                 }
967                 $oldrenderedfiles{$page}=[@{$items{dest}}];
968                 $pagectime{$page}=$items{ctime}[0];
969         }
970         return close($in);
971 } #}}}
972
973 sub saveindex () { #{{{
974         run_hooks(savestate => sub { shift->() });
975
976         my %hookids;
977         foreach my $type (keys %hooks) {
978                 $hookids{encode_entities($_)}=1 foreach keys %{$hooks{$type}};
979         }
980         my @hookids=sort keys %hookids;
981
982         if (! -d $config{wikistatedir}) {
983                 mkdir($config{wikistatedir});
984         }
985         my $newfile="$config{wikistatedir}/index.new";
986         my $cleanup = sub { unlink($newfile) };
987         open (my $out, '>', $newfile) || error("cannot write to $newfile: $!", $cleanup);
988         foreach my $page (keys %pagemtime) {
989                 next unless $pagemtime{$page};
990                 my $line="mtime=$pagemtime{$page} ".
991                         "ctime=$pagectime{$page} ".
992                         "src=$pagesources{$page}";
993                 $line.=" dest=$_" foreach @{$renderedfiles{$page}};
994                 my %count;
995                 $line.=" link=$_" foreach grep { ++$count{$_} == 1 } @{$links{$page}};
996                 if (exists $depends{$page}) {
997                         $line.=" depends=".encode_entities($depends{$page}, " \t\n");
998                 }
999                 if (exists $pagestate{$page}) {
1000                         foreach my $id (@hookids) {
1001                                 foreach my $key (keys %{$pagestate{$page}{$id}}) {
1002                                         $line.=' '.$id.'_'.encode_entities($key)."=".encode_entities($pagestate{$page}{$id}{$key}, " \t\n");
1003                                 }
1004                         }
1005                 }
1006                 print $out $line."\n" || error("failed writing to $newfile: $!", $cleanup);
1007         }
1008         close $out || error("failed saving to $newfile: $!", $cleanup);
1009         rename($newfile, "$config{wikistatedir}/index") ||
1010                 error("failed renaming $newfile to $config{wikistatedir}/index", $cleanup);
1011         
1012         return 1;
1013 } #}}}
1014
1015 sub template_file ($) { #{{{
1016         my $template=shift;
1017
1018         foreach my $dir ($config{templatedir}, "$installdir/share/ikiwiki/templates") {
1019                 return "$dir/$template" if -e "$dir/$template";
1020         }
1021         return;
1022 } #}}}
1023
1024 sub template_params (@) { #{{{
1025         my $filename=template_file(shift);
1026
1027         if (! defined $filename) {
1028                 return if wantarray;
1029                 return "";
1030         }
1031
1032         my @ret=(
1033                 filter => sub {
1034                         my $text_ref = shift;
1035                         ${$text_ref} = decode_utf8(${$text_ref});
1036                 },
1037                 filename => $filename,
1038                 loop_context_vars => 1,
1039                 die_on_bad_params => 0,
1040                 @_
1041         );
1042         return wantarray ? @ret : {@ret};
1043 } #}}}
1044
1045 sub template ($;@) { #{{{
1046         require HTML::Template;
1047         return HTML::Template->new(template_params(@_));
1048 } #}}}
1049
1050 sub misctemplate ($$;@) { #{{{
1051         my $title=shift;
1052         my $pagebody=shift;
1053         
1054         my $template=template("misc.tmpl");
1055         $template->param(
1056                 title => $title,
1057                 indexlink => indexlink(),
1058                 wikiname => $config{wikiname},
1059                 pagebody => $pagebody,
1060                 baseurl => baseurl(),
1061                 @_,
1062         );
1063         run_hooks(pagetemplate => sub {
1064                 shift->(page => "", destpage => "", template => $template);
1065         });
1066         return $template->output;
1067 }#}}}
1068
1069 sub hook (@) { # {{{
1070         my %param=@_;
1071         
1072         if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
1073                 error 'hook requires type, call, and id parameters';
1074         }
1075
1076         return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
1077         
1078         $hooks{$param{type}}{$param{id}}=\%param;
1079         return 1;
1080 } # }}}
1081
1082 sub run_hooks ($$) { # {{{
1083         # Calls the given sub for each hook of the given type,
1084         # passing it the hook function to call.
1085         my $type=shift;
1086         my $sub=shift;
1087
1088         if (exists $hooks{$type}) {
1089                 my @deferred;
1090                 foreach my $id (keys %{$hooks{$type}}) {
1091                         if ($hooks{$type}{$id}{last}) {
1092                                 push @deferred, $id;
1093                                 next;
1094                         }
1095                         $sub->($hooks{$type}{$id}{call});
1096                 }
1097                 foreach my $id (@deferred) {
1098                         $sub->($hooks{$type}{$id}{call});
1099                 }
1100         }
1101
1102         return 1;
1103 } #}}}
1104
1105 sub globlist_to_pagespec ($) { #{{{
1106         my @globlist=split(' ', shift);
1107
1108         my (@spec, @skip);
1109         foreach my $glob (@globlist) {
1110                 if ($glob=~/^!(.*)/) {
1111                         push @skip, $glob;
1112                 }
1113                 else {
1114                         push @spec, $glob;
1115                 }
1116         }
1117
1118         my $spec=join(' or ', @spec);
1119         if (@skip) {
1120                 my $skip=join(' and ', @skip);
1121                 if (length $spec) {
1122                         $spec="$skip and ($spec)";
1123                 }
1124                 else {
1125                         $spec=$skip;
1126                 }
1127         }
1128         return $spec;
1129 } #}}}
1130
1131 sub is_globlist ($) { #{{{
1132         my $s=shift;
1133         return ( $s =~ /[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or" );
1134 } #}}}
1135
1136 sub safequote ($) { #{{{
1137         my $s=shift;
1138         $s=~s/[{}]//g;
1139         return "q{$s}";
1140 } #}}}
1141
1142 sub add_depends ($$) { #{{{
1143         my $page=shift;
1144         my $pagespec=shift;
1145         
1146         if (! exists $depends{$page}) {
1147                 $depends{$page}=$pagespec;
1148         }
1149         else {
1150                 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
1151         }
1152
1153         return 1;
1154 } # }}}
1155
1156 sub file_pruned ($$) { #{{{
1157         require File::Spec;
1158         my $file=File::Spec->canonpath(shift);
1159         my $base=File::Spec->canonpath(shift);
1160         $file =~ s#^\Q$base\E/+##;
1161
1162         my $regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
1163         return $file =~ m/$regexp/ && $file ne $base;
1164 } #}}}
1165
1166 sub gettext { #{{{
1167         # Only use gettext in the rare cases it's needed.
1168         if ((exists $ENV{LANG} && length $ENV{LANG}) ||
1169             (exists $ENV{LC_ALL} && length $ENV{LC_ALL}) ||
1170             (exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES})) {
1171                 if (! $gettext_obj) {
1172                         $gettext_obj=eval q{
1173                                 use Locale::gettext q{textdomain};
1174                                 Locale::gettext->domain('ikiwiki')
1175                         };
1176                         if ($@) {
1177                                 print STDERR "$@";
1178                                 $gettext_obj=undef;
1179                                 return shift;
1180                         }
1181                 }
1182                 return $gettext_obj->get(shift);
1183         }
1184         else {
1185                 return shift;
1186         }
1187 } #}}}
1188
1189 sub pagespec_merge ($$) { #{{{
1190         my $a=shift;
1191         my $b=shift;
1192
1193         return $a if $a eq $b;
1194
1195         # Support for old-style GlobLists.
1196         if (is_globlist($a)) {
1197                 $a=globlist_to_pagespec($a);
1198         }
1199         if (is_globlist($b)) {
1200                 $b=globlist_to_pagespec($b);
1201         }
1202
1203         return "($a) or ($b)";
1204 } #}}}
1205
1206 sub pagespec_translate ($) { #{{{
1207         # This assumes that $page is in scope in the function
1208         # that evalulates the translated pagespec code.
1209         my $spec=shift;
1210
1211         # Support for old-style GlobLists.
1212         if (is_globlist($spec)) {
1213                 $spec=globlist_to_pagespec($spec);
1214         }
1215
1216         # Convert spec to perl code.
1217         my $code="";
1218         while ($spec=~m{
1219                 \s*             # ignore whitespace
1220                 (               # 1: match a single word
1221                         \!              # !
1222                 |
1223                         \(              # (
1224                 |
1225                         \)              # )
1226                 |
1227                         \w+\([^\)]*\)   # command(params)
1228                 |
1229                         [^\s()]+        # any other text
1230                 )
1231                 \s*             # ignore whitespace
1232         }igx) {
1233                 my $word=$1;
1234                 if (lc $word eq 'and') {
1235                         $code.=' &&';
1236                 }
1237                 elsif (lc $word eq 'or') {
1238                         $code.=' ||';
1239                 }
1240                 elsif ($word eq "(" || $word eq ")" || $word eq "!") {
1241                         $code.=' '.$word;
1242                 }
1243                 elsif ($word =~ /^(\w+)\((.*)\)$/) {
1244                         if (exists $IkiWiki::PageSpec::{"match_$1"}) {
1245                                 $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \@params)";
1246                         }
1247                         else {
1248                                 $code.=' 0';
1249                         }
1250                 }
1251                 else {
1252                         $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \@params)";
1253                 }
1254         }
1255
1256         return $code;
1257 } #}}}
1258
1259 sub pagespec_match ($$;@) { #{{{
1260         my $page=shift;
1261         my $spec=shift;
1262         my @params=@_;
1263
1264         # Backwards compatability with old calling convention.
1265         if (@params == 1) {
1266                 unshift @params, 'location';
1267         }
1268
1269         my $ret=eval pagespec_translate($spec);
1270         return IkiWiki::FailReason->new('syntax error') if $@;
1271         return $ret;
1272 } #}}}
1273
1274 package IkiWiki::FailReason;
1275
1276 use overload ( #{{{
1277         '""'    => sub { ${$_[0]} },
1278         '0+'    => sub { 0 },
1279         '!'     => sub { bless $_[0], 'IkiWiki::SuccessReason'},
1280         fallback => 1,
1281 ); #}}}
1282
1283 sub new { #{{{
1284         return bless \$_[1], $_[0];
1285 } #}}}
1286
1287 package IkiWiki::SuccessReason;
1288
1289 use overload ( #{{{
1290         '""'    => sub { ${$_[0]} },
1291         '0+'    => sub { 1 },
1292         '!'     => sub { bless $_[0], 'IkiWiki::FailReason'},
1293         fallback => 1,
1294 ); #}}}
1295
1296 sub new { #{{{
1297         return bless \$_[1], $_[0];
1298 }; #}}}
1299
1300 package IkiWiki::PageSpec;
1301
1302 sub match_glob ($$;@) { #{{{
1303         my $page=shift;
1304         my $glob=shift;
1305         my %params=@_;
1306         
1307         my $from=exists $params{location} ? $params{location} : '';
1308         
1309         # relative matching
1310         if ($glob =~ m!^\./!) {
1311                 $from=~s#/?[^/]+$##;
1312                 $glob=~s#^\./##;
1313                 $glob="$from/$glob" if length $from;
1314         }
1315
1316         # turn glob into safe regexp
1317         $glob=quotemeta($glob);
1318         $glob=~s/\\\*/.*/g;
1319         $glob=~s/\\\?/./g;
1320
1321         if ($page=~/^$glob$/i) {
1322                 if (! IkiWiki::isinternal($page) || $params{internal}) {
1323                         return IkiWiki::SuccessReason->new("$glob matches $page");
1324                 }
1325                 else {
1326                         return IkiWiki::FailReason->new("$glob matches $page, but the page is an internal page");
1327                 }
1328         }
1329         else {
1330                 return IkiWiki::FailReason->new("$glob does not match $page");
1331         }
1332 } #}}}
1333
1334 sub match_internal ($$;@) { #{{{
1335         return match_glob($_[0], $_[1], @_, internal => 1)
1336 } #}}}
1337
1338 sub match_link ($$;@) { #{{{
1339         my $page=shift;
1340         my $link=lc(shift);
1341         my %params=@_;
1342
1343         my $from=exists $params{location} ? $params{location} : '';
1344
1345         # relative matching
1346         if ($link =~ m!^\.! && defined $from) {
1347                 $from=~s#/?[^/]+$##;
1348                 $link=~s#^\./##;
1349                 $link="$from/$link" if length $from;
1350         }
1351
1352         my $links = $IkiWiki::links{$page};
1353         return IkiWiki::FailReason->new("$page has no links") unless $links && @{$links};
1354         my $bestlink = IkiWiki::bestlink($from, $link);
1355         foreach my $p (@{$links}) {
1356                 if (length $bestlink) {
1357                         return IkiWiki::SuccessReason->new("$page links to $link")
1358                                 if $bestlink eq IkiWiki::bestlink($page, $p);
1359                 }
1360                 else {
1361                         return IkiWiki::SuccessReason->new("$page links to page $p matching $link")
1362                                 if match_glob($p, $link, %params);
1363                 }
1364         }
1365         return IkiWiki::FailReason->new("$page does not link to $link");
1366 } #}}}
1367
1368 sub match_backlink ($$;@) { #{{{
1369         return match_link($_[1], $_[0], @_);
1370 } #}}}
1371
1372 sub match_created_before ($$;@) { #{{{
1373         my $page=shift;
1374         my $testpage=shift;
1375
1376         if (exists $IkiWiki::pagectime{$testpage}) {
1377                 if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
1378                         return IkiWiki::SuccessReason->new("$page created before $testpage");
1379                 }
1380                 else {
1381                         return IkiWiki::FailReason->new("$page not created before $testpage");
1382                 }
1383         }
1384         else {
1385                 return IkiWiki::FailReason->new("$testpage has no ctime");
1386         }
1387 } #}}}
1388
1389 sub match_created_after ($$;@) { #{{{
1390         my $page=shift;
1391         my $testpage=shift;
1392
1393         if (exists $IkiWiki::pagectime{$testpage}) {
1394                 if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
1395                         return IkiWiki::SuccessReason->new("$page created after $testpage");
1396                 }
1397                 else {
1398                         return IkiWiki::FailReason->new("$page not created after $testpage");
1399                 }
1400         }
1401         else {
1402                 return IkiWiki::FailReason->new("$testpage has no ctime");
1403         }
1404 } #}}}
1405
1406 sub match_creation_day ($$;@) { #{{{
1407         if ((gmtime($IkiWiki::pagectime{shift()}))[3] == shift) {
1408                 return IkiWiki::SuccessReason->new('creation_day matched');
1409         }
1410         else {
1411                 return IkiWiki::FailReason->new('creation_day did not match');
1412         }
1413 } #}}}
1414
1415 sub match_creation_month ($$;@) { #{{{
1416         if ((gmtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
1417                 return IkiWiki::SuccessReason->new('creation_month matched');
1418         }
1419         else {
1420                 return IkiWiki::FailReason->new('creation_month did not match');
1421         }
1422 } #}}}
1423
1424 sub match_creation_year ($$;@) { #{{{
1425         if ((gmtime($IkiWiki::pagectime{shift()}))[5] + 1900 == shift) {
1426                 return IkiWiki::SuccessReason->new('creation_year matched');
1427         }
1428         else {
1429                 return IkiWiki::FailReason->new('creation_year did not match');
1430         }
1431 } #}}}
1432
1433 1