response
[ikiwiki.git] / IkiWiki.pm
1 #!/usr/bin/perl
2
3 package IkiWiki;
4
5 use warnings;
6 use strict;
7 use Encode;
8 use HTML::Entities;
9 use URI::Escape q{uri_escape_utf8};
10 use POSIX;
11 use Storable;
12 use open qw{:utf8 :std};
13
14 use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
15             %pagestate %renderedfiles %oldrenderedfiles %pagesources
16             %destsources %depends %hooks %forcerebuild $gettext_obj};
17
18 use Exporter q{import};
19 our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
20                  bestlink htmllink readfile writefile pagetype srcfile pagename
21                  displaytime will_render gettext urlto targetpage
22                  add_underlay
23                  %config %links %pagestate %renderedfiles
24                  %pagesources %destsources);
25 our $VERSION = 2.00; # plugin interface version, next is ikiwiki version
26 our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
27 my $installdir=''; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
28
29 # Optimisation.
30 use Memoize;
31 memoize("abs2rel");
32 memoize("pagespec_translate");
33 memoize("file_pruned");
34
35 sub getsetup () { #{{{
36         wikiname => {
37                 type => "string",
38                 default => "wiki",
39                 description => "name of the wiki",
40                 safe => 1,
41                 rebuild => 1,
42         },
43         srcdir => {
44                 type => "string",
45                 default => undef,
46                 example => "$ENV{HOME}/wiki",
47                 description => "where the source of the wiki is located",
48                 safe => 0, # path
49                 rebuild => 1,
50         },
51         destdir => {
52                 type => "string",
53                 default => undef,
54                 example => "/var/www/wiki",
55                 description => "where to build the wiki",
56                 safe => 0, # path
57                 rebuild => 1,
58         },
59         adminuser => {
60                 type => "string",
61                 default => [],
62                 description => "user names of wiki admins",
63                 safe => 1,
64                 rebuild => 0,
65         },
66         adminemail => {
67                 type => "string",
68                 default => undef,
69                 example => 'me@example.com',
70                 description => "contact email for wiki",
71                 safe => 1,
72                 rebuild => 0,
73         },
74         url => {
75                 type => "string",
76                 default => '',
77                 example => "http://example.com/wiki",
78                 description => "base url to the wiki",
79                 safe => 1,
80                 rebuild => 1,
81         },
82         cgiurl => {
83                 type => "string",
84                 default => '',
85                 example => "http://example.com/wiki/ikiwiki.cgi",
86                 description => "url to the ikiwiki.cgi",
87                 safe => 1,
88                 rebuild => 1,
89         },
90         cgi_wrapper => {
91                 type => "string",
92                 default => '',
93                 example => "/var/www/wiki/ikiwiki.cgi",
94                 description => "cgi executable to generate",
95                 safe => 0, # file
96                 rebuild => 0,
97         },
98         cgi_wrappermode => {
99                 type => "string",
100                 default => '06755',
101                 description => "mode for cgi_wrapper (can safely be made suid)",
102                 safe => 0,
103                 rebuild => 0,
104         },
105         rcs => {
106                 type => "string",
107                 default => '',
108                 description => "rcs backend to use",
109                 safe => 0, # don't allow overriding
110                 rebuild => 0,
111         },
112         default_plugins => {
113                 type => "internal",
114                 default => [qw{mdwn link inline htmlscrubber passwordauth
115                                 openid signinedit lockedit conditional
116                                 recentchanges parentlinks}],
117                 description => "plugins to enable by default",
118                 safe => 0,
119                 rebuild => 1,
120         },
121         add_plugins => {
122                 type => "string",
123                 default => [],
124                 description => "plugins to add to the default configuration",
125                 safe => 1,
126                 rebuild => 1,
127         },
128         disable_plugins => {
129                 type => "string",
130                 default => [],
131                 description => "plugins to disable",
132                 safe => 1,
133                 rebuild => 1,
134         },
135         templatedir => {
136                 type => "string",
137                 default => "$installdir/share/ikiwiki/templates",
138                 description => "location of template files",
139                 safe => 0, # path
140                 rebuild => 1,
141         },
142         underlaydir => {
143                 type => "string",
144                 default => "$installdir/share/ikiwiki/basewiki",
145                 description => "base wiki source location",
146                 safe => 0, # path
147                 rebuild => 0,
148         },
149         wrappers => {
150                 type => "internal",
151                 default => [],
152                 description => "wrappers to generate",
153                 safe => 0,
154                 rebuild => 0,
155         },
156         underlaydirs => {
157                 type => "internal",
158                 default => [],
159                 description => "additional underlays to use",
160                 safe => 0,
161                 rebuild => 0,
162         },
163         verbose => {
164                 type => "boolean",
165                 default => 0,
166                 description => "display verbose messages when building?",
167                 safe => 1,
168                 rebuild => 0,
169         },
170         syslog => {
171                 type => "boolean",
172                 default => 0,
173                 description => "log to syslog?",
174                 safe => 1,
175                 rebuild => 0,
176         },
177         usedirs => {
178                 type => "boolean",
179                 default => 1,
180                 description => "create output files named page/index.html?",
181                 safe => 0, # changing requires manual transition
182                 rebuild => 1,
183         },
184         prefix_directives => {
185                 type => "boolean",
186                 default => 0,
187                 description => "use '!'-prefixed preprocessor directives?",
188                 safe => 0, # changing requires manual transition
189                 rebuild => 1,
190         },
191         discussion => {
192                 type => "boolean",
193                 default => 1,
194                 description => "enable Discussion pages?",
195                 safe => 1,
196                 rebuild => 1,
197         },
198         default_pageext => {
199                 type => "string",
200                 default => "mdwn",
201                 description => "extension to use for new pages",
202                 safe => 0, # not sanitized
203                 rebuild => 0,
204         },
205         htmlext => {
206                 type => "string",
207                 default => "html",
208                 description => "extension to use for html files",
209                 safe => 0, # not sanitized
210                 rebuild => 1,
211         },
212         timeformat => {
213                 type => "string",
214                 default => '%c',
215                 description => "strftime format string to display date",
216                 safe => 1,
217                 rebuild => 1,
218         },
219         locale => {
220                 type => "string",
221                 default => undef,
222                 example => "en_US.UTF-8",
223                 description => "UTF-8 locale to use",
224                 safe => 0,
225                 rebuild => 1,
226         },
227         sslcookie => {
228                 type => "boolean",
229                 default => 0,
230                 description => "only send cookies over SSL connections?",
231                 safe => 1,
232                 rebuild => 0,
233         },
234         userdir => {
235                 type => "string",
236                 default => "",
237                 example => "users",
238                 description => "put user pages below specified page",
239                 safe => 1,
240                 rebuild => 1,
241         },
242         numbacklinks => {
243                 type => "integer",
244                 default => 10,
245                 description => "how many backlinks to show before hiding excess (0 to show all)",
246                 safe => 1,
247                 rebuild => 1,
248         },
249         hardlink => {
250                 type => "boolean",
251                 default => 0,
252                 description => "attempt to hardlink source files? (optimisation for large files)",
253                 safe => 0, # paranoia
254                 rebuild => 0,
255         },
256         umask => {
257                 type => "integer",
258                 description => "",
259                 example => "022",
260                 description => "force ikiwiki to use a particular umask",
261                 safe => 0, # paranoia
262                 rebuild => 0,
263         },
264         libdir => {
265                 type => "string",
266                 default => "",
267                 example => "$ENV{HOME}/.ikiwiki/",
268                 description => "extra library and plugin directory",
269                 safe => 0, # directory
270                 rebuild => 0,
271         },
272         ENV => {
273                 type => "string", 
274                 default => {},
275                 description => "environment variables",
276                 safe => 0, # paranoia
277                 rebuild => 0,
278         },
279         exclude => {
280                 type => "string",
281                 default => undef,
282                 example => '\.wav$',
283                 description => "regexp of source files to ignore",
284                 safe => 0, # regexp
285                 rebuild => 1,
286         },
287         wiki_file_prune_regexps => {
288                 type => "internal",
289                 default => [qr/(^|\/)\.\.(\/|$)/, qr/^\./, qr/\/\./,
290                         qr/\.x?html?$/, qr/\.ikiwiki-new$/,
291                         qr/(^|\/).svn\//, qr/.arch-ids\//, qr/{arch}\//,
292                         qr/(^|\/)_MTN\//,
293                         qr/\.dpkg-tmp$/],
294                 description => "regexps of source files to ignore",
295                 safe => 0,
296                 rebuild => 1,
297         },
298         wiki_file_regexp => {
299                 type => "internal",
300                 default => qr/(^[-[:alnum:]_.:\/+]+$)/,
301                 description => "regexp of legal source files",
302                 safe => 0,
303                 rebuild => 1,
304         },
305         web_commit_regexp => {
306                 type => "internal",
307                 default => qr/^web commit (by (.*?(?=: |$))|from (\d+\.\d+\.\d+\.\d+)):?(.*)/,
308                 description => "regexp to parse web commits from logs",
309                 safe => 0,
310                 rebuild => 0,
311         },
312         cgi => {
313                 type => "internal",
314                 default => 0,
315                 description => "run as a cgi",
316                 safe => 0,
317                 rebuild => 0,
318         },
319         cgi_disable_uploads => {
320                 type => "internal",
321                 default => 1,
322                 description => "whether CGI should accept file uploads",
323                 safe => 0,
324                 rebuild => 0,
325         },
326         post_commit => {
327                 type => "internal",
328                 default => 0,
329                 description => "run as a post-commit hook",
330                 safe => 0,
331                 rebuild => 0,
332         },
333         rebuild => {
334                 type => "internal",
335                 default => 0,
336                 description => "running in rebuild mode",
337                 safe => 0,
338                 rebuild => 0,
339         },
340         refresh => {
341                 type => "internal",
342                 default => 0,
343                 description => "running in refresh mode",
344                 safe => 0,
345                 rebuild => 0,
346         },
347         getctime => {
348                 type => "internal",
349                 default => 0,
350                 description => "running in getctime mode",
351                 safe => 0,
352                 rebuild => 0,
353         },
354         w3mmode => {
355                 type => "internal",
356                 default => 0,
357                 description => "running in w3mmode",
358                 safe => 0,
359                 rebuild => 0,
360         },
361         setup => {
362                 type => "internal",
363                 default => undef,
364                 description => "setup file to read",
365                 safe => 0,
366                 rebuild => 0,
367         },
368 } #}}}
369
370 sub defaultconfig () { #{{{
371         my %s=getsetup();
372         my @ret;
373         foreach my $key (keys %s) {
374                 push @ret, $key, $s{$key}->{default};
375         }
376         use Data::Dumper;
377         return @ret;
378 } #}}}
379
380 sub checkconfig () { #{{{
381         # locale stuff; avoid LC_ALL since it overrides everything
382         if (defined $ENV{LC_ALL}) {
383                 $ENV{LANG} = $ENV{LC_ALL};
384                 delete $ENV{LC_ALL};
385         }
386         if (defined $config{locale}) {
387                 if (POSIX::setlocale(&POSIX::LC_ALL, $config{locale})) {
388                         $ENV{LANG}=$config{locale};
389                         $gettext_obj=undef;
390                 }
391         }
392
393         if (ref $config{ENV} eq 'HASH') {
394                 foreach my $val (keys %{$config{ENV}}) {
395                         $ENV{$val}=$config{ENV}{$val};
396                 }
397         }
398
399         if ($config{w3mmode}) {
400                 eval q{use Cwd q{abs_path}};
401                 error($@) if $@;
402                 $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir}));
403                 $config{destdir}=possibly_foolish_untaint(abs_path($config{destdir}));
404                 $config{cgiurl}="file:///\$LIB/ikiwiki-w3m.cgi/".$config{cgiurl}
405                         unless $config{cgiurl} =~ m!file:///!;
406                 $config{url}="file://".$config{destdir};
407         }
408
409         if ($config{cgi} && ! length $config{url}) {
410                 error(gettext("Must specify url to wiki with --url when using --cgi"));
411         }
412         
413         $config{wikistatedir}="$config{srcdir}/.ikiwiki"
414                 unless exists $config{wikistatedir};
415
416         if (defined $config{umask}) {
417                 umask(possibly_foolish_untaint($config{umask}));
418         }
419
420         run_hooks(checkconfig => sub { shift->() });
421
422         return 1;
423 } #}}}
424
425 sub listplugins () { #{{{
426         my %ret;
427
428         foreach my $dir (@INC, $config{libdir}) {
429                 next unless defined $dir && length $dir;
430                 foreach my $file (glob("$dir/IkiWiki/Plugin/*.pm")) {
431                         my ($plugin)=$file=~/.*\/(.*)\.pm$/;
432                         $ret{$plugin}=1;
433                 }
434         }
435         foreach my $dir ($config{libdir}, "$installdir/lib/ikiwiki") {
436                 next unless defined $dir && length $dir;
437                 foreach my $file (glob("$dir/plugins/*")) {
438                         $ret{basename($file)}=1 if -x $file;
439                 }
440         }
441
442         return keys %ret;
443 } #}}}
444
445 sub loadplugins () { #{{{
446         if (defined $config{libdir} && length $config{libdir}) {
447                 unshift @INC, possibly_foolish_untaint($config{libdir});
448         }
449
450         loadplugin($_) foreach @{$config{default_plugins}}, @{$config{add_plugins}};
451         
452         if ($config{rcs}) {
453                 if (exists $IkiWiki::hooks{rcs}) {
454                         error(gettext("cannot use multiple rcs plugins"));
455                 }
456                 loadplugin($config{rcs});
457         }
458         if (! exists $IkiWiki::hooks{rcs}) {
459                 loadplugin("norcs");
460         }
461
462         run_hooks(getopt => sub { shift->() });
463         if (grep /^-/, @ARGV) {
464                 print STDERR "Unknown option: $_\n"
465                         foreach grep /^-/, @ARGV;
466                 usage();
467         }
468
469         return 1;
470 } #}}}
471
472 sub loadplugin ($) { #{{{
473         my $plugin=shift;
474
475         return if grep { $_ eq $plugin} @{$config{disable_plugins}};
476
477         foreach my $dir (defined $config{libdir} ? possibly_foolish_untaint($config{libdir}) : undef,
478                          "$installdir/lib/ikiwiki") {
479                 if (defined $dir && -x "$dir/plugins/$plugin") {
480                         require IkiWiki::Plugin::external;
481                         import IkiWiki::Plugin::external "$dir/plugins/$plugin";
482                         return 1;
483                 }
484         }
485
486         my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
487         eval qq{use $mod};
488         if ($@) {
489                 error("Failed to load plugin $mod: $@");
490         }
491         return 1;
492 } #}}}
493
494 sub error ($;$) { #{{{
495         my $message=shift;
496         my $cleaner=shift;
497         log_message('err' => $message) if $config{syslog};
498         if (defined $cleaner) {
499                 $cleaner->();
500         }
501         die $message."\n";
502 } #}}}
503
504 sub debug ($) { #{{{
505         return unless $config{verbose};
506         return log_message(debug => @_);
507 } #}}}
508
509 my $log_open=0;
510 sub log_message ($$) { #{{{
511         my $type=shift;
512
513         if ($config{syslog}) {
514                 require Sys::Syslog;
515                 if (! $log_open) {
516                         Sys::Syslog::setlogsock('unix');
517                         Sys::Syslog::openlog('ikiwiki', '', 'user');
518                         $log_open=1;
519                 }
520                 return eval {
521                         Sys::Syslog::syslog($type, "[$config{wikiname}] %s", join(" ", @_));
522                 };
523         }
524         elsif (! $config{cgi}) {
525                 return print "@_\n";
526         }
527         else {
528                 return print STDERR "@_\n";
529         }
530 } #}}}
531
532 sub possibly_foolish_untaint ($) { #{{{
533         my $tainted=shift;
534         my ($untainted)=$tainted=~/(.*)/s;
535         return $untainted;
536 } #}}}
537
538 sub basename ($) { #{{{
539         my $file=shift;
540
541         $file=~s!.*/+!!;
542         return $file;
543 } #}}}
544
545 sub dirname ($) { #{{{
546         my $file=shift;
547
548         $file=~s!/*[^/]+$!!;
549         return $file;
550 } #}}}
551
552 sub pagetype ($) { #{{{
553         my $page=shift;
554         
555         if ($page =~ /\.([^.]+)$/) {
556                 return $1 if exists $hooks{htmlize}{$1};
557         }
558         return;
559 } #}}}
560
561 sub isinternal ($) { #{{{
562         my $page=shift;
563         return exists $pagesources{$page} &&
564                 $pagesources{$page} =~ /\._([^.]+)$/;
565 } #}}}
566
567 sub pagename ($) { #{{{
568         my $file=shift;
569
570         my $type=pagetype($file);
571         my $page=$file;
572         $page=~s/\Q.$type\E*$// if defined $type;
573         return $page;
574 } #}}}
575
576 sub targetpage ($$) { #{{{
577         my $page=shift;
578         my $ext=shift;
579         
580         if (! $config{usedirs} || $page =~ /^index$/ ) {
581                 return $page.".".$ext;
582         } else {
583                 return $page."/index.".$ext;
584         }
585 } #}}}
586
587 sub htmlpage ($) { #{{{
588         my $page=shift;
589         
590         return targetpage($page, $config{htmlext});
591 } #}}}
592
593 sub srcfile_stat { #{{{
594         my $file=shift;
595         my $nothrow=shift;
596
597         return "$config{srcdir}/$file", stat(_) if -e "$config{srcdir}/$file";
598         foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
599                 return "$dir/$file", stat(_) if -e "$dir/$file";
600         }
601         error("internal error: $file cannot be found in $config{srcdir} or underlay") unless $nothrow;
602         return;
603 } #}}}
604
605 sub srcfile ($;$) { #{{{
606         return (srcfile_stat(@_))[0];
607 } #}}}
608
609 sub add_underlay ($) { #{{{
610         my $dir=shift;
611
612         if ($dir=~/^\//) {
613                 unshift @{$config{underlaydirs}}, $dir;
614         }
615         else {
616                 unshift @{$config{underlaydirs}}, "$config{underlaydir}/../$dir";
617         }
618
619         return 1;
620 } #}}}
621
622 sub readfile ($;$$) { #{{{
623         my $file=shift;
624         my $binary=shift;
625         my $wantfd=shift;
626
627         if (-l $file) {
628                 error("cannot read a symlink ($file)");
629         }
630         
631         local $/=undef;
632         open (my $in, "<", $file) || error("failed to read $file: $!");
633         binmode($in) if ($binary);
634         return \*$in if $wantfd;
635         my $ret=<$in>;
636         close $in || error("failed to read $file: $!");
637         return $ret;
638 } #}}}
639
640 sub prep_writefile ($$) { #{{{
641         my $file=shift;
642         my $destdir=shift;
643         
644         my $test=$file;
645         while (length $test) {
646                 if (-l "$destdir/$test") {
647                         error("cannot write to a symlink ($test)");
648                 }
649                 $test=dirname($test);
650         }
651
652         my $dir=dirname("$destdir/$file");
653         if (! -d $dir) {
654                 my $d="";
655                 foreach my $s (split(m!/+!, $dir)) {
656                         $d.="$s/";
657                         if (! -d $d) {
658                                 mkdir($d) || error("failed to create directory $d: $!");
659                         }
660                 }
661         }
662
663         return 1;
664 } #}}}
665
666 sub writefile ($$$;$$) { #{{{
667         my $file=shift; # can include subdirs
668         my $destdir=shift; # directory to put file in
669         my $content=shift;
670         my $binary=shift;
671         my $writer=shift;
672         
673         prep_writefile($file, $destdir);
674         
675         my $newfile="$destdir/$file.ikiwiki-new";
676         if (-l $newfile) {
677                 error("cannot write to a symlink ($newfile)");
678         }
679         
680         my $cleanup = sub { unlink($newfile) };
681         open (my $out, '>', $newfile) || error("failed to write $newfile: $!", $cleanup);
682         binmode($out) if ($binary);
683         if ($writer) {
684                 $writer->(\*$out, $cleanup);
685         }
686         else {
687                 print $out $content or error("failed writing to $newfile: $!", $cleanup);
688         }
689         close $out || error("failed saving $newfile: $!", $cleanup);
690         rename($newfile, "$destdir/$file") || 
691                 error("failed renaming $newfile to $destdir/$file: $!", $cleanup);
692
693         return 1;
694 } #}}}
695
696 my %cleared;
697 sub will_render ($$;$) { #{{{
698         my $page=shift;
699         my $dest=shift;
700         my $clear=shift;
701
702         # Important security check.
703         if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
704             ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}})) {
705                 error("$config{destdir}/$dest independently created, not overwriting with version from $page");
706         }
707
708         if (! $clear || $cleared{$page}) {
709                 $renderedfiles{$page}=[$dest, grep { $_ ne $dest } @{$renderedfiles{$page}}];
710         }
711         else {
712                 foreach my $old (@{$renderedfiles{$page}}) {
713                         delete $destsources{$old};
714                 }
715                 $renderedfiles{$page}=[$dest];
716                 $cleared{$page}=1;
717         }
718         $destsources{$dest}=$page;
719
720         return 1;
721 } #}}}
722
723 sub bestlink ($$) { #{{{
724         my $page=shift;
725         my $link=shift;
726         
727         my $cwd=$page;
728         if ($link=~s/^\/+//) {
729                 # absolute links
730                 $cwd="";
731         }
732         $link=~s/\/$//;
733
734         do {
735                 my $l=$cwd;
736                 $l.="/" if length $l;
737                 $l.=$link;
738
739                 if (exists $links{$l}) {
740                         return $l;
741                 }
742                 elsif (exists $pagecase{lc $l}) {
743                         return $pagecase{lc $l};
744                 }
745         } while $cwd=~s!/?[^/]+$!!;
746
747         if (length $config{userdir}) {
748                 my $l = "$config{userdir}/".lc($link);
749                 if (exists $links{$l}) {
750                         return $l;
751                 }
752                 elsif (exists $pagecase{lc $l}) {
753                         return $pagecase{lc $l};
754                 }
755         }
756
757         #print STDERR "warning: page $page, broken link: $link\n";
758         return "";
759 } #}}}
760
761 sub isinlinableimage ($) { #{{{
762         my $file=shift;
763         
764         return $file =~ /\.(png|gif|jpg|jpeg)$/i;
765 } #}}}
766
767 sub pagetitle ($;$) { #{{{
768         my $page=shift;
769         my $unescaped=shift;
770
771         if ($unescaped) {
772                 $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : chr($2)/eg;
773         }
774         else {
775                 $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : "&#$2;"/eg;
776         }
777
778         return $page;
779 } #}}}
780
781 sub titlepage ($) { #{{{
782         my $title=shift;
783         $title=~s/([^-[:alnum:]:+\/.])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
784         return $title;
785 } #}}}
786
787 sub linkpage ($) { #{{{
788         my $link=shift;
789         $link=~s/([^-[:alnum:]:+\/._])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
790         return $link;
791 } #}}}
792
793 sub cgiurl (@) { #{{{
794         my %params=@_;
795
796         return $config{cgiurl}."?".
797                 join("&amp;", map $_."=".uri_escape_utf8($params{$_}), keys %params);
798 } #}}}
799
800 sub baseurl (;$) { #{{{
801         my $page=shift;
802
803         return "$config{url}/" if ! defined $page;
804         
805         $page=htmlpage($page);
806         $page=~s/[^\/]+$//;
807         $page=~s/[^\/]+\//..\//g;
808         return $page;
809 } #}}}
810
811 sub abs2rel ($$) { #{{{
812         # Work around very innefficient behavior in File::Spec if abs2rel
813         # is passed two relative paths. It's much faster if paths are
814         # absolute! (Debian bug #376658; fixed in debian unstable now)
815         my $path="/".shift;
816         my $base="/".shift;
817
818         require File::Spec;
819         my $ret=File::Spec->abs2rel($path, $base);
820         $ret=~s/^// if defined $ret;
821         return $ret;
822 } #}}}
823
824 sub displaytime ($;$) { #{{{
825         my $time=shift;
826         my $format=shift;
827         if (! defined $format) {
828                 $format=$config{timeformat};
829         }
830
831         # strftime doesn't know about encodings, so make sure
832         # its output is properly treated as utf8
833         return decode_utf8(POSIX::strftime($format, localtime($time)));
834 } #}}}
835
836 sub beautify_urlpath ($) { #{{{
837         my $url=shift;
838
839         if ($config{usedirs}) {
840                 $url =~ s!/index.$config{htmlext}$!/!;
841         }
842
843         # Ensure url is not an empty link, and
844         # if it's relative, make that explicit to avoid colon confusion.
845         if ($url !~ /^\//) {
846                 $url="./$url";
847         }
848
849         return $url;
850 } #}}}
851
852 sub urlto ($$;$) { #{{{
853         my $to=shift;
854         my $from=shift;
855         my $absolute=shift;
856         
857         if (! length $to) {
858                 return beautify_urlpath(baseurl($from)."index.$config{htmlext}");
859         }
860
861         if (! $destsources{$to}) {
862                 $to=htmlpage($to);
863         }
864
865         if ($absolute) {
866                 return $config{url}.beautify_urlpath("/".$to);
867         }
868
869         my $link = abs2rel($to, dirname(htmlpage($from)));
870
871         return beautify_urlpath($link);
872 } #}}}
873
874 sub htmllink ($$$;@) { #{{{
875         my $lpage=shift; # the page doing the linking
876         my $page=shift; # the page that will contain the link (different for inline)
877         my $link=shift;
878         my %opts=@_;
879
880         $link=~s/\/$//;
881
882         my $bestlink;
883         if (! $opts{forcesubpage}) {
884                 $bestlink=bestlink($lpage, $link);
885         }
886         else {
887                 $bestlink="$lpage/".lc($link);
888         }
889
890         my $linktext;
891         if (defined $opts{linktext}) {
892                 $linktext=$opts{linktext};
893         }
894         else {
895                 $linktext=pagetitle(basename($link));
896         }
897         
898         return "<span class=\"selflink\">$linktext</span>"
899                 if length $bestlink && $page eq $bestlink &&
900                    ! defined $opts{anchor};
901         
902         if (! $destsources{$bestlink}) {
903                 $bestlink=htmlpage($bestlink);
904
905                 if (! $destsources{$bestlink}) {
906                         return $linktext unless length $config{cgiurl};
907                         return "<span class=\"createlink\"><a href=\"".
908                                 cgiurl(
909                                         do => "create",
910                                         page => lc($link),
911                                         from => $lpage
912                                 ).
913                                 "\" rel=\"nofollow\">?</a>$linktext</span>"
914                 }
915         }
916         
917         $bestlink=abs2rel($bestlink, dirname(htmlpage($page)));
918         $bestlink=beautify_urlpath($bestlink);
919         
920         if (! $opts{noimageinline} && isinlinableimage($bestlink)) {
921                 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
922         }
923
924         if (defined $opts{anchor}) {
925                 $bestlink.="#".$opts{anchor};
926         }
927
928         my @attrs;
929         if (defined $opts{rel}) {
930                 push @attrs, ' rel="'.$opts{rel}.'"';
931         }
932         if (defined $opts{class}) {
933                 push @attrs, ' class="'.$opts{class}.'"';
934         }
935
936         return "<a href=\"$bestlink\"@attrs>$linktext</a>";
937 } #}}}
938
939 sub userlink ($) { #{{{
940         my $user=shift;
941
942         my $oiduser=eval { openiduser($user) };
943         if (defined $oiduser) {
944                 return "<a href=\"$user\">$oiduser</a>";
945         }
946         else {
947                 eval q{use CGI 'escapeHTML'};
948                 error($@) if $@;
949
950                 return htmllink("", "", escapeHTML(
951                         length $config{userdir} ? $config{userdir}."/".$user : $user
952                 ), noimageinline => 1);
953         }
954 } #}}}
955
956 sub htmlize ($$$$) { #{{{
957         my $page=shift;
958         my $destpage=shift;
959         my $type=shift;
960         my $content=shift;
961         
962         my $oneline = $content !~ /\n/;
963
964         if (exists $hooks{htmlize}{$type}) {
965                 $content=$hooks{htmlize}{$type}{call}->(
966                         page => $page,
967                         content => $content,
968                 );
969         }
970         else {
971                 error("htmlization of $type not supported");
972         }
973
974         run_hooks(sanitize => sub {
975                 $content=shift->(
976                         page => $page,
977                         destpage => $destpage,
978                         content => $content,
979                 );
980         });
981         
982         if ($oneline) {
983                 # hack to get rid of enclosing junk added by markdown
984                 # and other htmlizers
985                 $content=~s/^<p>//i;
986                 $content=~s/<\/p>$//i;
987                 chomp $content;
988         }
989
990         return $content;
991 } #}}}
992
993 sub linkify ($$$) { #{{{
994         my $page=shift;
995         my $destpage=shift;
996         my $content=shift;
997
998         run_hooks(linkify => sub {
999                 $content=shift->(
1000                         page => $page,
1001                         destpage => $destpage,
1002                         content => $content,
1003                 );
1004         });
1005         
1006         return $content;
1007 } #}}}
1008
1009 our %preprocessing;
1010 our $preprocess_preview=0;
1011 sub preprocess ($$$;$$) { #{{{
1012         my $page=shift; # the page the data comes from
1013         my $destpage=shift; # the page the data will appear in (different for inline)
1014         my $content=shift;
1015         my $scan=shift;
1016         my $preview=shift;
1017
1018         # Using local because it needs to be set within any nested calls
1019         # of this function.
1020         local $preprocess_preview=$preview if defined $preview;
1021
1022         my $handle=sub {
1023                 my $escape=shift;
1024                 my $prefix=shift;
1025                 my $command=shift;
1026                 my $params=shift;
1027                 if (length $escape) {
1028                         return "[[$prefix$command $params]]";
1029                 }
1030                 elsif (exists $hooks{preprocess}{$command}) {
1031                         return "" if $scan && ! $hooks{preprocess}{$command}{scan};
1032                         # Note: preserve order of params, some plugins may
1033                         # consider it significant.
1034                         my @params;
1035                         while ($params =~ m{
1036                                 (?:([-\w]+)=)?          # 1: named parameter key?
1037                                 (?:
1038                                         """(.*?)"""     # 2: triple-quoted value
1039                                 |
1040                                         "([^"]+)"       # 3: single-quoted value
1041                                 |
1042                                         (\S+)           # 4: unquoted value
1043                                 )
1044                                 (?:\s+|$)               # delimiter to next param
1045                         }sgx) {
1046                                 my $key=$1;
1047                                 my $val;
1048                                 if (defined $2) {
1049                                         $val=$2;
1050                                         $val=~s/\r\n/\n/mg;
1051                                         $val=~s/^\n+//g;
1052                                         $val=~s/\n+$//g;
1053                                 }
1054                                 elsif (defined $3) {
1055                                         $val=$3;
1056                                 }
1057                                 elsif (defined $4) {
1058                                         $val=$4;
1059                                 }
1060
1061                                 if (defined $key) {
1062                                         push @params, $key, $val;
1063                                 }
1064                                 else {
1065                                         push @params, $val, '';
1066                                 }
1067                         }
1068                         if ($preprocessing{$page}++ > 3) {
1069                                 # Avoid loops of preprocessed pages preprocessing
1070                                 # other pages that preprocess them, etc.
1071                                 return "[[!$command <span class=\"error\">".
1072                                         sprintf(gettext("preprocessing loop detected on %s at depth %i"),
1073                                                 $page, $preprocessing{$page}).
1074                                         "</span>]]";
1075                         }
1076                         my $ret;
1077                         if (! $scan) {
1078                                 $ret=eval {
1079                                         $hooks{preprocess}{$command}{call}->(
1080                                                 @params,
1081                                                 page => $page,
1082                                                 destpage => $destpage,
1083                                                 preview => $preprocess_preview,
1084                                         );
1085                                 };
1086                                 if ($@) {
1087                                         chomp $@;
1088                                         $ret="[[!$command <span class=\"error\">".
1089                                                 gettext("Error").": $@"."</span>]]";
1090                                 }
1091                         }
1092                         else {
1093                                 # use void context during scan pass
1094                                 eval {
1095                                         $hooks{preprocess}{$command}{call}->(
1096                                                 @params,
1097                                                 page => $page,
1098                                                 destpage => $destpage,
1099                                                 preview => $preprocess_preview,
1100                                         );
1101                                 };
1102                                 $ret="";
1103                         }
1104                         $preprocessing{$page}--;
1105                         return $ret;
1106                 }
1107                 else {
1108                         return "[[$prefix$command $params]]";
1109                 }
1110         };
1111         
1112         my $regex;
1113         if ($config{prefix_directives}) {
1114                 $regex = qr{
1115                         (\\?)           # 1: escape?
1116                         \[\[(!)         # directive open; 2: prefix
1117                         ([-\w]+)        # 3: command
1118                         (               # 4: the parameters..
1119                                 \s+     # Must have space if parameters present
1120                                 (?:
1121                                         (?:[-\w]+=)?            # named parameter key?
1122                                         (?:
1123                                                 """.*?"""       # triple-quoted value
1124                                                 |
1125                                                 "[^"]+"         # single-quoted value
1126                                                 |
1127                                                 [^\s\]]+        # unquoted value
1128                                         )
1129                                         \s*                     # whitespace or end
1130                                                                 # of directive
1131                                 )
1132                         *)?             # 0 or more parameters
1133                         \]\]            # directive closed
1134                 }sx;
1135         }
1136         else {
1137                 $regex = qr{
1138                         (\\?)           # 1: escape?
1139                         \[\[(!?)        # directive open; 2: optional prefix
1140                         ([-\w]+)        # 3: command
1141                         \s+
1142                         (               # 4: the parameters..
1143                                 (?:
1144                                         (?:[-\w]+=)?            # named parameter key?
1145                                         (?:
1146                                                 """.*?"""       # triple-quoted value
1147                                                 |
1148                                                 "[^"]+"         # single-quoted value
1149                                                 |
1150                                                 [^\s\]]+        # unquoted value
1151                                         )
1152                                         \s*                     # whitespace or end
1153                                                                 # of directive
1154                                 )
1155                         *)              # 0 or more parameters
1156                         \]\]            # directive closed
1157                 }sx;
1158         }
1159
1160         $content =~ s{$regex}{$handle->($1, $2, $3, $4)}eg;
1161         return $content;
1162 } #}}}
1163
1164 sub filter ($$$) { #{{{
1165         my $page=shift;
1166         my $destpage=shift;
1167         my $content=shift;
1168
1169         run_hooks(filter => sub {
1170                 $content=shift->(page => $page, destpage => $destpage, 
1171                         content => $content);
1172         });
1173
1174         return $content;
1175 } #}}}
1176
1177 sub indexlink () { #{{{
1178         return "<a href=\"$config{url}\">$config{wikiname}</a>";
1179 } #}}}
1180
1181 my $wikilock;
1182
1183 sub lockwiki (;$) { #{{{
1184         my $wait=@_ ? shift : 1;
1185         # Take an exclusive lock on the wiki to prevent multiple concurrent
1186         # run issues. The lock will be dropped on program exit.
1187         if (! -d $config{wikistatedir}) {
1188                 mkdir($config{wikistatedir});
1189         }
1190         open($wikilock, '>', "$config{wikistatedir}/lockfile") ||
1191                 error ("cannot write to $config{wikistatedir}/lockfile: $!");
1192         if (! flock($wikilock, 2 | 4)) { # LOCK_EX | LOCK_NB
1193                 if ($wait) {
1194                         debug("wiki seems to be locked, waiting for lock");
1195                         my $wait=600; # arbitrary, but don't hang forever to 
1196                                       # prevent process pileup
1197                         for (1..$wait) {
1198                                 return if flock($wikilock, 2 | 4);
1199                                 sleep 1;
1200                         }
1201                         error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
1202                 }
1203                 else {
1204                         return 0;
1205                 }
1206         }
1207         return 1;
1208 } #}}}
1209
1210 sub unlockwiki () { #{{{
1211         return close($wikilock) if $wikilock;
1212         return;
1213 } #}}}
1214
1215 my $commitlock;
1216
1217 sub commit_hook_enabled () { #{{{
1218         open($commitlock, '+>', "$config{wikistatedir}/commitlock") ||
1219                 error("cannot write to $config{wikistatedir}/commitlock: $!");
1220         if (! flock($commitlock, 1 | 4)) { # LOCK_SH | LOCK_NB to test
1221                 close($commitlock) || error("failed closing commitlock: $!");
1222                 return 0;
1223         }
1224         close($commitlock) || error("failed closing commitlock: $!");
1225         return 1;
1226 } #}}}
1227
1228 sub disable_commit_hook () { #{{{
1229         open($commitlock, '>', "$config{wikistatedir}/commitlock") ||
1230                 error("cannot write to $config{wikistatedir}/commitlock: $!");
1231         if (! flock($commitlock, 2)) { # LOCK_EX
1232                 error("failed to get commit lock");
1233         }
1234         return 1;
1235 } #}}}
1236
1237 sub enable_commit_hook () { #{{{
1238         return close($commitlock) if $commitlock;
1239         return;
1240 } #}}}
1241
1242 sub loadindex () { #{{{
1243         %oldrenderedfiles=%pagectime=();
1244         if (! $config{rebuild}) {
1245                 %pagesources=%pagemtime=%oldlinks=%links=%depends=
1246                 %destsources=%renderedfiles=%pagecase=%pagestate=();
1247         }
1248         my $in;
1249         if (! open ($in, "<", "$config{wikistatedir}/indexdb")) {
1250                 if (-e "$config{wikistatedir}/index") {
1251                         system("ikiwiki-transition", "indexdb", $config{srcdir});
1252                         open ($in, "<", "$config{wikistatedir}/indexdb") || return;
1253                 }
1254                 else {
1255                         return;
1256                 }
1257         }
1258         my $ret=Storable::fd_retrieve($in);
1259         if (! defined $ret) {
1260                 return 0;
1261         }
1262         my %index=%$ret;
1263         foreach my $src (keys %index) {
1264                 my %d=%{$index{$src}};
1265                 my $page=pagename($src);
1266                 $pagectime{$page}=$d{ctime};
1267                 if (! $config{rebuild}) {
1268                         $pagesources{$page}=$src;
1269                         $pagemtime{$page}=$d{mtime};
1270                         $renderedfiles{$page}=$d{dest};
1271                         if (exists $d{links} && ref $d{links}) {
1272                                 $links{$page}=$d{links};
1273                                 $oldlinks{$page}=[@{$d{links}}];
1274                         }
1275                         if (exists $d{depends}) {
1276                                 $depends{$page}=$d{depends};
1277                         }
1278                         if (exists $d{state}) {
1279                                 $pagestate{$page}=$d{state};
1280                         }
1281                 }
1282                 $oldrenderedfiles{$page}=[@{$d{dest}}];
1283         }
1284         foreach my $page (keys %pagesources) {
1285                 $pagecase{lc $page}=$page;
1286         }
1287         foreach my $page (keys %renderedfiles) {
1288                 $destsources{$_}=$page foreach @{$renderedfiles{$page}};
1289         }
1290         return close($in);
1291 } #}}}
1292
1293 sub saveindex () { #{{{
1294         run_hooks(savestate => sub { shift->() });
1295
1296         my %hookids;
1297         foreach my $type (keys %hooks) {
1298                 $hookids{$_}=1 foreach keys %{$hooks{$type}};
1299         }
1300         my @hookids=keys %hookids;
1301
1302         if (! -d $config{wikistatedir}) {
1303                 mkdir($config{wikistatedir});
1304         }
1305         my $newfile="$config{wikistatedir}/indexdb.new";
1306         my $cleanup = sub { unlink($newfile) };
1307         open (my $out, '>', $newfile) || error("cannot write to $newfile: $!", $cleanup);
1308         my %index;
1309         foreach my $page (keys %pagemtime) {
1310                 next unless $pagemtime{$page};
1311                 my $src=$pagesources{$page};
1312
1313                 $index{$src}={
1314                         ctime => $pagectime{$page},
1315                         mtime => $pagemtime{$page},
1316                         dest => $renderedfiles{$page},
1317                         links => $links{$page},
1318                 };
1319
1320                 if (exists $depends{$page}) {
1321                         $index{$src}{depends} = $depends{$page};
1322                 }
1323
1324                 if (exists $pagestate{$page}) {
1325                         foreach my $id (@hookids) {
1326                                 foreach my $key (keys %{$pagestate{$page}{$id}}) {
1327                                         $index{$src}{state}{$id}{$key}=$pagestate{$page}{$id}{$key};
1328                                 }
1329                         }
1330                 }
1331         }
1332         my $ret=Storable::nstore_fd(\%index, $out);
1333         return if ! defined $ret || ! $ret;
1334         close $out || error("failed saving to $newfile: $!", $cleanup);
1335         rename($newfile, "$config{wikistatedir}/indexdb") ||
1336                 error("failed renaming $newfile to $config{wikistatedir}/indexdb", $cleanup);
1337         
1338         return 1;
1339 } #}}}
1340
1341 sub template_file ($) { #{{{
1342         my $template=shift;
1343
1344         foreach my $dir ($config{templatedir}, "$installdir/share/ikiwiki/templates") {
1345                 return "$dir/$template" if -e "$dir/$template";
1346         }
1347         return;
1348 } #}}}
1349
1350 sub template_params (@) { #{{{
1351         my $filename=template_file(shift);
1352
1353         if (! defined $filename) {
1354                 return if wantarray;
1355                 return "";
1356         }
1357
1358         my @ret=(
1359                 filter => sub {
1360                         my $text_ref = shift;
1361                         ${$text_ref} = decode_utf8(${$text_ref});
1362                 },
1363                 filename => $filename,
1364                 loop_context_vars => 1,
1365                 die_on_bad_params => 0,
1366                 @_
1367         );
1368         return wantarray ? @ret : {@ret};
1369 } #}}}
1370
1371 sub template ($;@) { #{{{
1372         require HTML::Template;
1373         return HTML::Template->new(template_params(@_));
1374 } #}}}
1375
1376 sub misctemplate ($$;@) { #{{{
1377         my $title=shift;
1378         my $pagebody=shift;
1379         
1380         my $template=template("misc.tmpl");
1381         $template->param(
1382                 title => $title,
1383                 indexlink => indexlink(),
1384                 wikiname => $config{wikiname},
1385                 pagebody => $pagebody,
1386                 baseurl => baseurl(),
1387                 @_,
1388         );
1389         run_hooks(pagetemplate => sub {
1390                 shift->(page => "", destpage => "", template => $template);
1391         });
1392         return $template->output;
1393 }#}}}
1394
1395 sub hook (@) { # {{{
1396         my %param=@_;
1397         
1398         if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
1399                 error 'hook requires type, call, and id parameters';
1400         }
1401
1402         return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
1403         
1404         $hooks{$param{type}}{$param{id}}=\%param;
1405         return 1;
1406 } # }}}
1407
1408 sub run_hooks ($$) { # {{{
1409         # Calls the given sub for each hook of the given type,
1410         # passing it the hook function to call.
1411         my $type=shift;
1412         my $sub=shift;
1413
1414         if (exists $hooks{$type}) {
1415                 my @deferred;
1416                 foreach my $id (keys %{$hooks{$type}}) {
1417                         if ($hooks{$type}{$id}{last}) {
1418                                 push @deferred, $id;
1419                                 next;
1420                         }
1421                         $sub->($hooks{$type}{$id}{call});
1422                 }
1423                 foreach my $id (@deferred) {
1424                         $sub->($hooks{$type}{$id}{call});
1425                 }
1426         }
1427
1428         return 1;
1429 } #}}}
1430
1431 sub rcs_update () { #{{{
1432         $hooks{rcs}{rcs_update}{call}->(@_);
1433 } #}}}
1434
1435 sub rcs_prepedit ($) { #{{{
1436         $hooks{rcs}{rcs_prepedit}{call}->(@_);
1437 } #}}}
1438
1439 sub rcs_commit ($$$;$$) { #{{{
1440         $hooks{rcs}{rcs_commit}{call}->(@_);
1441 } #}}}
1442
1443 sub rcs_commit_staged ($$$) { #{{{
1444         $hooks{rcs}{rcs_commit_staged}{call}->(@_);
1445 } #}}}
1446
1447 sub rcs_add ($) { #{{{
1448         $hooks{rcs}{rcs_add}{call}->(@_);
1449 } #}}}
1450
1451 sub rcs_remove ($) { #{{{
1452         $hooks{rcs}{rcs_remove}{call}->(@_);
1453 } #}}}
1454
1455 sub rcs_rename ($$) { #{{{
1456         $hooks{rcs}{rcs_rename}{call}->(@_);
1457 } #}}}
1458
1459 sub rcs_recentchanges ($) { #{{{
1460         $hooks{rcs}{rcs_recentchanges}{call}->(@_);
1461 } #}}}
1462
1463 sub rcs_diff ($) { #{{{
1464         $hooks{rcs}{rcs_diff}{call}->(@_);
1465 } #}}}
1466
1467 sub rcs_getctime ($) { #{{{
1468         $hooks{rcs}{rcs_getctime}{call}->(@_);
1469 } #}}}
1470
1471 sub globlist_to_pagespec ($) { #{{{
1472         my @globlist=split(' ', shift);
1473
1474         my (@spec, @skip);
1475         foreach my $glob (@globlist) {
1476                 if ($glob=~/^!(.*)/) {
1477                         push @skip, $glob;
1478                 }
1479                 else {
1480                         push @spec, $glob;
1481                 }
1482         }
1483
1484         my $spec=join(' or ', @spec);
1485         if (@skip) {
1486                 my $skip=join(' and ', @skip);
1487                 if (length $spec) {
1488                         $spec="$skip and ($spec)";
1489                 }
1490                 else {
1491                         $spec=$skip;
1492                 }
1493         }
1494         return $spec;
1495 } #}}}
1496
1497 sub is_globlist ($) { #{{{
1498         my $s=shift;
1499         return ( $s =~ /[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or" );
1500 } #}}}
1501
1502 sub safequote ($) { #{{{
1503         my $s=shift;
1504         $s=~s/[{}]//g;
1505         return "q{$s}";
1506 } #}}}
1507
1508 sub add_depends ($$) { #{{{
1509         my $page=shift;
1510         my $pagespec=shift;
1511         
1512         return unless pagespec_valid($pagespec);
1513
1514         if (! exists $depends{$page}) {
1515                 $depends{$page}=$pagespec;
1516         }
1517         else {
1518                 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
1519         }
1520
1521         return 1;
1522 } # }}}
1523
1524 sub file_pruned ($$) { #{{{
1525         require File::Spec;
1526         my $file=File::Spec->canonpath(shift);
1527         my $base=File::Spec->canonpath(shift);
1528         $file =~ s#^\Q$base\E/+##;
1529
1530         my $regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
1531         return $file =~ m/$regexp/ && $file ne $base;
1532 } #}}}
1533
1534 sub gettext { #{{{
1535         # Only use gettext in the rare cases it's needed.
1536         if ((exists $ENV{LANG} && length $ENV{LANG}) ||
1537             (exists $ENV{LC_ALL} && length $ENV{LC_ALL}) ||
1538             (exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES})) {
1539                 if (! $gettext_obj) {
1540                         $gettext_obj=eval q{
1541                                 use Locale::gettext q{textdomain};
1542                                 Locale::gettext->domain('ikiwiki')
1543                         };
1544                         if ($@) {
1545                                 print STDERR "$@";
1546                                 $gettext_obj=undef;
1547                                 return shift;
1548                         }
1549                 }
1550                 return $gettext_obj->get(shift);
1551         }
1552         else {
1553                 return shift;
1554         }
1555 } #}}}
1556
1557 sub yesno ($) { #{{{
1558         my $val=shift;
1559
1560         return (defined $val && lc($val) eq gettext("yes"));
1561 } #}}}
1562
1563 sub pagespec_merge ($$) { #{{{
1564         my $a=shift;
1565         my $b=shift;
1566
1567         return $a if $a eq $b;
1568
1569         # Support for old-style GlobLists.
1570         if (is_globlist($a)) {
1571                 $a=globlist_to_pagespec($a);
1572         }
1573         if (is_globlist($b)) {
1574                 $b=globlist_to_pagespec($b);
1575         }
1576
1577         return "($a) or ($b)";
1578 } #}}}
1579
1580 sub pagespec_translate ($) { #{{{
1581         my $spec=shift;
1582
1583         # Support for old-style GlobLists.
1584         if (is_globlist($spec)) {
1585                 $spec=globlist_to_pagespec($spec);
1586         }
1587
1588         # Convert spec to perl code.
1589         my $code="";
1590         while ($spec=~m{
1591                 \s*             # ignore whitespace
1592                 (               # 1: match a single word
1593                         \!              # !
1594                 |
1595                         \(              # (
1596                 |
1597                         \)              # )
1598                 |
1599                         \w+\([^\)]*\)   # command(params)
1600                 |
1601                         [^\s()]+        # any other text
1602                 )
1603                 \s*             # ignore whitespace
1604         }igx) {
1605                 my $word=$1;
1606                 if (lc $word eq 'and') {
1607                         $code.=' &&';
1608                 }
1609                 elsif (lc $word eq 'or') {
1610                         $code.=' ||';
1611                 }
1612                 elsif ($word eq "(" || $word eq ")" || $word eq "!") {
1613                         $code.=' '.$word;
1614                 }
1615                 elsif ($word =~ /^(\w+)\((.*)\)$/) {
1616                         if (exists $IkiWiki::PageSpec::{"match_$1"}) {
1617                                 $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \@_)";
1618                         }
1619                         else {
1620                                 $code.=' 0';
1621                         }
1622                 }
1623                 else {
1624                         $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \@_)";
1625                 }
1626         }
1627
1628         if (! length $code) {
1629                 $code=0;
1630         }
1631
1632         no warnings;
1633         return eval 'sub { my $page=shift; '.$code.' }';
1634 } #}}}
1635
1636 sub pagespec_match ($$;@) { #{{{
1637         my $page=shift;
1638         my $spec=shift;
1639         my @params=@_;
1640
1641         # Backwards compatability with old calling convention.
1642         if (@params == 1) {
1643                 unshift @params, 'location';
1644         }
1645
1646         my $sub=pagespec_translate($spec);
1647         return IkiWiki::FailReason->new("syntax error in pagespec \"$spec\"") if $@;
1648         return $sub->($page, @params);
1649 } #}}}
1650
1651 sub pagespec_valid ($) { #{{{
1652         my $spec=shift;
1653
1654         my $sub=pagespec_translate($spec);
1655         return ! $@;
1656 } #}}}
1657         
1658 sub glob2re ($) { #{{{
1659         my $re=quotemeta(shift);
1660         $re=~s/\\\*/.*/g;
1661         $re=~s/\\\?/./g;
1662         return $re;
1663 } #}}}
1664
1665 package IkiWiki::FailReason;
1666
1667 use overload ( #{{{
1668         '""'    => sub { ${$_[0]} },
1669         '0+'    => sub { 0 },
1670         '!'     => sub { bless $_[0], 'IkiWiki::SuccessReason'},
1671         fallback => 1,
1672 ); #}}}
1673
1674 sub new { #{{{
1675         my $class = shift;
1676         my $value = shift;
1677         return bless \$value, $class;
1678 } #}}}
1679
1680 package IkiWiki::SuccessReason;
1681
1682 use overload ( #{{{
1683         '""'    => sub { ${$_[0]} },
1684         '0+'    => sub { 1 },
1685         '!'     => sub { bless $_[0], 'IkiWiki::FailReason'},
1686         fallback => 1,
1687 ); #}}}
1688
1689 sub new { #{{{
1690         my $class = shift;
1691         my $value = shift;
1692         return bless \$value, $class;
1693 }; #}}}
1694
1695 package IkiWiki::PageSpec;
1696
1697 sub match_glob ($$;@) { #{{{
1698         my $page=shift;
1699         my $glob=shift;
1700         my %params=@_;
1701         
1702         my $from=exists $params{location} ? $params{location} : '';
1703         
1704         # relative matching
1705         if ($glob =~ m!^\./!) {
1706                 $from=~s#/?[^/]+$##;
1707                 $glob=~s#^\./##;
1708                 $glob="$from/$glob" if length $from;
1709         }
1710
1711         my $regexp=IkiWiki::glob2re($glob);
1712         if ($page=~/^$regexp$/i) {
1713                 if (! IkiWiki::isinternal($page) || $params{internal}) {
1714                         return IkiWiki::SuccessReason->new("$glob matches $page");
1715                 }
1716                 else {
1717                         return IkiWiki::FailReason->new("$glob matches $page, but the page is an internal page");
1718                 }
1719         }
1720         else {
1721                 return IkiWiki::FailReason->new("$glob does not match $page");
1722         }
1723 } #}}}
1724
1725 sub match_internal ($$;@) { #{{{
1726         return match_glob($_[0], $_[1], @_, internal => 1)
1727 } #}}}
1728
1729 sub match_link ($$;@) { #{{{
1730         my $page=shift;
1731         my $link=lc(shift);
1732         my %params=@_;
1733
1734         my $from=exists $params{location} ? $params{location} : '';
1735
1736         # relative matching
1737         if ($link =~ m!^\.! && defined $from) {
1738                 $from=~s#/?[^/]+$##;
1739                 $link=~s#^\./##;
1740                 $link="$from/$link" if length $from;
1741         }
1742
1743         my $links = $IkiWiki::links{$page};
1744         return IkiWiki::FailReason->new("$page has no links") unless $links && @{$links};
1745         my $bestlink = IkiWiki::bestlink($from, $link);
1746         foreach my $p (@{$links}) {
1747                 if (length $bestlink) {
1748                         return IkiWiki::SuccessReason->new("$page links to $link")
1749                                 if $bestlink eq IkiWiki::bestlink($page, $p);
1750                 }
1751                 else {
1752                         return IkiWiki::SuccessReason->new("$page links to page $p matching $link")
1753                                 if match_glob($p, $link, %params);
1754                 }
1755         }
1756         return IkiWiki::FailReason->new("$page does not link to $link");
1757 } #}}}
1758
1759 sub match_backlink ($$;@) { #{{{
1760         return match_link($_[1], $_[0], @_);
1761 } #}}}
1762
1763 sub match_created_before ($$;@) { #{{{
1764         my $page=shift;
1765         my $testpage=shift;
1766
1767         if (exists $IkiWiki::pagectime{$testpage}) {
1768                 if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
1769                         return IkiWiki::SuccessReason->new("$page created before $testpage");
1770                 }
1771                 else {
1772                         return IkiWiki::FailReason->new("$page not created before $testpage");
1773                 }
1774         }
1775         else {
1776                 return IkiWiki::FailReason->new("$testpage has no ctime");
1777         }
1778 } #}}}
1779
1780 sub match_created_after ($$;@) { #{{{
1781         my $page=shift;
1782         my $testpage=shift;
1783
1784         if (exists $IkiWiki::pagectime{$testpage}) {
1785                 if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
1786                         return IkiWiki::SuccessReason->new("$page created after $testpage");
1787                 }
1788                 else {
1789                         return IkiWiki::FailReason->new("$page not created after $testpage");
1790                 }
1791         }
1792         else {
1793                 return IkiWiki::FailReason->new("$testpage has no ctime");
1794         }
1795 } #}}}
1796
1797 sub match_creation_day ($$;@) { #{{{
1798         if ((gmtime($IkiWiki::pagectime{shift()}))[3] == shift) {
1799                 return IkiWiki::SuccessReason->new('creation_day matched');
1800         }
1801         else {
1802                 return IkiWiki::FailReason->new('creation_day did not match');
1803         }
1804 } #}}}
1805
1806 sub match_creation_month ($$;@) { #{{{
1807         if ((gmtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
1808                 return IkiWiki::SuccessReason->new('creation_month matched');
1809         }
1810         else {
1811                 return IkiWiki::FailReason->new('creation_month did not match');
1812         }
1813 } #}}}
1814
1815 sub match_creation_year ($$;@) { #{{{
1816         if ((gmtime($IkiWiki::pagectime{shift()}))[5] + 1900 == shift) {
1817                 return IkiWiki::SuccessReason->new('creation_year matched');
1818         }
1819         else {
1820                 return IkiWiki::FailReason->new('creation_year did not match');
1821         }
1822 } #}}}
1823
1824 1