]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki.pm
fixups
[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 => 1,
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 ($config{rcs}) {
417                 eval qq{use IkiWiki::Rcs::$config{rcs}};
418                 if ($@) {
419                         error("Failed to load RCS module IkiWiki::Rcs::$config{rcs}: $@");
420                 }
421         }
422         else {
423                 require IkiWiki::Rcs::Stub;
424         }
425
426         if (defined $config{umask}) {
427                 umask(possibly_foolish_untaint($config{umask}));
428         }
429
430         run_hooks(checkconfig => sub { shift->() });
431
432         return 1;
433 } #}}}
434
435 sub listplugins () { #{{{
436         my %ret;
437
438         foreach my $dir (@INC, $config{libdir}) {
439                 next unless defined $dir;
440                 foreach my $file (glob("$dir/IkiWiki/Plugin/*.pm")) {
441                         my ($plugin)=$file=~/.*\/(.*)\.pm$/;
442                         $ret{$plugin}=1;
443                 }
444         }
445         foreach my $dir ($config{libdir}, "$installdir/lib/ikiwiki") {
446                 next unless defined $dir;
447                 foreach my $file (glob("$dir/plugins/*")) {
448                         $ret{basename($file)}=1 if -x $file;
449                 }
450         }
451
452         return keys %ret;
453 } #}}}
454
455 sub loadplugins () { #{{{
456         if (defined $config{libdir}) {
457                 unshift @INC, possibly_foolish_untaint($config{libdir});
458         }
459
460         loadplugin($_) foreach @{$config{default_plugins}}, @{$config{add_plugins}};
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                                 #translators: The first parameter is a
1072                                 #translators: preprocessor directive name,
1073                                 #translators: the second a page name, the
1074                                 #translators: third a number.
1075                                 return "[[".sprintf(gettext("%s preprocessing loop detected on %s at depth %i"),
1076                                         $command, $page, $preprocessing{$page}).
1077                                 "]]";
1078                         }
1079                         my $ret;
1080                         if (! $scan) {
1081                                 $ret=eval {
1082                                         $hooks{preprocess}{$command}{call}->(
1083                                                 @params,
1084                                                 page => $page,
1085                                                 destpage => $destpage,
1086                                                 preview => $preprocess_preview,
1087                                         );
1088                                 };
1089                                 if ($@) {
1090                                         chomp $@;
1091                                         $ret="[[!$command <span class=\"error\">".
1092                                                 gettext("Error").": $@"."</span>]]";
1093                                 }
1094                         }
1095                         else {
1096                                 # use void context during scan pass
1097                                 eval {
1098                                         $hooks{preprocess}{$command}{call}->(
1099                                                 @params,
1100                                                 page => $page,
1101                                                 destpage => $destpage,
1102                                                 preview => $preprocess_preview,
1103                                         );
1104                                 };
1105                                 $ret="";
1106                         }
1107                         $preprocessing{$page}--;
1108                         return $ret;
1109                 }
1110                 else {
1111                         return "[[$prefix$command $params]]";
1112                 }
1113         };
1114         
1115         my $regex;
1116         if ($config{prefix_directives}) {
1117                 $regex = qr{
1118                         (\\?)           # 1: escape?
1119                         \[\[(!)         # directive open; 2: prefix
1120                         ([-\w]+)        # 3: command
1121                         (               # 4: the parameters..
1122                                 \s+     # Must have space if parameters present
1123                                 (?:
1124                                         (?:[-\w]+=)?            # named parameter key?
1125                                         (?:
1126                                                 """.*?"""       # triple-quoted value
1127                                                 |
1128                                                 "[^"]+"         # single-quoted value
1129                                                 |
1130                                                 [^\s\]]+        # unquoted value
1131                                         )
1132                                         \s*                     # whitespace or end
1133                                                                 # of directive
1134                                 )
1135                         *)?             # 0 or more parameters
1136                         \]\]            # directive closed
1137                 }sx;
1138         }
1139         else {
1140                 $regex = qr{
1141                         (\\?)           # 1: escape?
1142                         \[\[(!?)        # directive open; 2: optional prefix
1143                         ([-\w]+)        # 3: command
1144                         \s+
1145                         (               # 4: the parameters..
1146                                 (?:
1147                                         (?:[-\w]+=)?            # named parameter key?
1148                                         (?:
1149                                                 """.*?"""       # triple-quoted value
1150                                                 |
1151                                                 "[^"]+"         # single-quoted value
1152                                                 |
1153                                                 [^\s\]]+        # unquoted value
1154                                         )
1155                                         \s*                     # whitespace or end
1156                                                                 # of directive
1157                                 )
1158                         *)              # 0 or more parameters
1159                         \]\]            # directive closed
1160                 }sx;
1161         }
1162
1163         $content =~ s{$regex}{$handle->($1, $2, $3, $4)}eg;
1164         return $content;
1165 } #}}}
1166
1167 sub filter ($$$) { #{{{
1168         my $page=shift;
1169         my $destpage=shift;
1170         my $content=shift;
1171
1172         run_hooks(filter => sub {
1173                 $content=shift->(page => $page, destpage => $destpage, 
1174                         content => $content);
1175         });
1176
1177         return $content;
1178 } #}}}
1179
1180 sub indexlink () { #{{{
1181         return "<a href=\"$config{url}\">$config{wikiname}</a>";
1182 } #}}}
1183
1184 my $wikilock;
1185
1186 sub lockwiki (;$) { #{{{
1187         my $wait=@_ ? shift : 1;
1188         # Take an exclusive lock on the wiki to prevent multiple concurrent
1189         # run issues. The lock will be dropped on program exit.
1190         if (! -d $config{wikistatedir}) {
1191                 mkdir($config{wikistatedir});
1192         }
1193         open($wikilock, '>', "$config{wikistatedir}/lockfile") ||
1194                 error ("cannot write to $config{wikistatedir}/lockfile: $!");
1195         if (! flock($wikilock, 2 | 4)) { # LOCK_EX | LOCK_NB
1196                 if ($wait) {
1197                         debug("wiki seems to be locked, waiting for lock");
1198                         my $wait=600; # arbitrary, but don't hang forever to 
1199                                       # prevent process pileup
1200                         for (1..$wait) {
1201                                 return if flock($wikilock, 2 | 4);
1202                                 sleep 1;
1203                         }
1204                         error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
1205                 }
1206                 else {
1207                         return 0;
1208                 }
1209         }
1210         return 1;
1211 } #}}}
1212
1213 sub unlockwiki () { #{{{
1214         return close($wikilock) if $wikilock;
1215         return;
1216 } #}}}
1217
1218 my $commitlock;
1219
1220 sub commit_hook_enabled () { #{{{
1221         open($commitlock, '+>', "$config{wikistatedir}/commitlock") ||
1222                 error("cannot write to $config{wikistatedir}/commitlock: $!");
1223         if (! flock($commitlock, 1 | 4)) { # LOCK_SH | LOCK_NB to test
1224                 close($commitlock) || error("failed closing commitlock: $!");
1225                 return 0;
1226         }
1227         close($commitlock) || error("failed closing commitlock: $!");
1228         return 1;
1229 } #}}}
1230
1231 sub disable_commit_hook () { #{{{
1232         open($commitlock, '>', "$config{wikistatedir}/commitlock") ||
1233                 error("cannot write to $config{wikistatedir}/commitlock: $!");
1234         if (! flock($commitlock, 2)) { # LOCK_EX
1235                 error("failed to get commit lock");
1236         }
1237         return 1;
1238 } #}}}
1239
1240 sub enable_commit_hook () { #{{{
1241         return close($commitlock) if $commitlock;
1242         return;
1243 } #}}}
1244
1245 sub loadindex () { #{{{
1246         %oldrenderedfiles=%pagectime=();
1247         if (! $config{rebuild}) {
1248                 %pagesources=%pagemtime=%oldlinks=%links=%depends=
1249                 %destsources=%renderedfiles=%pagecase=%pagestate=();
1250         }
1251         my $in;
1252         if (! open ($in, "<", "$config{wikistatedir}/indexdb")) {
1253                 if (-e "$config{wikistatedir}/index") {
1254                         system("ikiwiki-transition", "indexdb", $config{srcdir});
1255                         open ($in, "<", "$config{wikistatedir}/indexdb") || return;
1256                 }
1257                 else {
1258                         return;
1259                 }
1260         }
1261         my $ret=Storable::fd_retrieve($in);
1262         if (! defined $ret) {
1263                 return 0;
1264         }
1265         my %index=%$ret;
1266         foreach my $src (keys %index) {
1267                 my %d=%{$index{$src}};
1268                 my $page=pagename($src);
1269                 $pagectime{$page}=$d{ctime};
1270                 if (! $config{rebuild}) {
1271                         $pagesources{$page}=$src;
1272                         $pagemtime{$page}=$d{mtime};
1273                         $renderedfiles{$page}=$d{dest};
1274                         if (exists $d{links} && ref $d{links}) {
1275                                 $links{$page}=$d{links};
1276                                 $oldlinks{$page}=[@{$d{links}}];
1277                         }
1278                         if (exists $d{depends}) {
1279                                 $depends{$page}=$d{depends};
1280                         }
1281                         if (exists $d{state}) {
1282                                 $pagestate{$page}=$d{state};
1283                         }
1284                 }
1285                 $oldrenderedfiles{$page}=[@{$d{dest}}];
1286         }
1287         foreach my $page (keys %pagesources) {
1288                 $pagecase{lc $page}=$page;
1289         }
1290         foreach my $page (keys %renderedfiles) {
1291                 $destsources{$_}=$page foreach @{$renderedfiles{$page}};
1292         }
1293         return close($in);
1294 } #}}}
1295
1296 sub saveindex () { #{{{
1297         run_hooks(savestate => sub { shift->() });
1298
1299         my %hookids;
1300         foreach my $type (keys %hooks) {
1301                 $hookids{$_}=1 foreach keys %{$hooks{$type}};
1302         }
1303         my @hookids=keys %hookids;
1304
1305         if (! -d $config{wikistatedir}) {
1306                 mkdir($config{wikistatedir});
1307         }
1308         my $newfile="$config{wikistatedir}/indexdb.new";
1309         my $cleanup = sub { unlink($newfile) };
1310         open (my $out, '>', $newfile) || error("cannot write to $newfile: $!", $cleanup);
1311         my %index;
1312         foreach my $page (keys %pagemtime) {
1313                 next unless $pagemtime{$page};
1314                 my $src=$pagesources{$page};
1315
1316                 $index{$src}={
1317                         ctime => $pagectime{$page},
1318                         mtime => $pagemtime{$page},
1319                         dest => $renderedfiles{$page},
1320                         links => $links{$page},
1321                 };
1322
1323                 if (exists $depends{$page}) {
1324                         $index{$src}{depends} = $depends{$page};
1325                 }
1326
1327                 if (exists $pagestate{$page}) {
1328                         foreach my $id (@hookids) {
1329                                 foreach my $key (keys %{$pagestate{$page}{$id}}) {
1330                                         $index{$src}{state}{$id}{$key}=$pagestate{$page}{$id}{$key};
1331                                 }
1332                         }
1333                 }
1334         }
1335         my $ret=Storable::nstore_fd(\%index, $out);
1336         return if ! defined $ret || ! $ret;
1337         close $out || error("failed saving to $newfile: $!", $cleanup);
1338         rename($newfile, "$config{wikistatedir}/indexdb") ||
1339                 error("failed renaming $newfile to $config{wikistatedir}/indexdb", $cleanup);
1340         
1341         return 1;
1342 } #}}}
1343
1344 sub template_file ($) { #{{{
1345         my $template=shift;
1346
1347         foreach my $dir ($config{templatedir}, "$installdir/share/ikiwiki/templates") {
1348                 return "$dir/$template" if -e "$dir/$template";
1349         }
1350         return;
1351 } #}}}
1352
1353 sub template_params (@) { #{{{
1354         my $filename=template_file(shift);
1355
1356         if (! defined $filename) {
1357                 return if wantarray;
1358                 return "";
1359         }
1360
1361         my @ret=(
1362                 filter => sub {
1363                         my $text_ref = shift;
1364                         ${$text_ref} = decode_utf8(${$text_ref});
1365                 },
1366                 filename => $filename,
1367                 loop_context_vars => 1,
1368                 die_on_bad_params => 0,
1369                 @_
1370         );
1371         return wantarray ? @ret : {@ret};
1372 } #}}}
1373
1374 sub template ($;@) { #{{{
1375         require HTML::Template;
1376         return HTML::Template->new(template_params(@_));
1377 } #}}}
1378
1379 sub misctemplate ($$;@) { #{{{
1380         my $title=shift;
1381         my $pagebody=shift;
1382         
1383         my $template=template("misc.tmpl");
1384         $template->param(
1385                 title => $title,
1386                 indexlink => indexlink(),
1387                 wikiname => $config{wikiname},
1388                 pagebody => $pagebody,
1389                 baseurl => baseurl(),
1390                 @_,
1391         );
1392         run_hooks(pagetemplate => sub {
1393                 shift->(page => "", destpage => "", template => $template);
1394         });
1395         return $template->output;
1396 }#}}}
1397
1398 sub hook (@) { # {{{
1399         my %param=@_;
1400         
1401         if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
1402                 error 'hook requires type, call, and id parameters';
1403         }
1404
1405         return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
1406         
1407         $hooks{$param{type}}{$param{id}}=\%param;
1408         return 1;
1409 } # }}}
1410
1411 sub run_hooks ($$) { # {{{
1412         # Calls the given sub for each hook of the given type,
1413         # passing it the hook function to call.
1414         my $type=shift;
1415         my $sub=shift;
1416
1417         if (exists $hooks{$type}) {
1418                 my @deferred;
1419                 foreach my $id (keys %{$hooks{$type}}) {
1420                         if ($hooks{$type}{$id}{last}) {
1421                                 push @deferred, $id;
1422                                 next;
1423                         }
1424                         $sub->($hooks{$type}{$id}{call});
1425                 }
1426                 foreach my $id (@deferred) {
1427                         $sub->($hooks{$type}{$id}{call});
1428                 }
1429         }
1430
1431         return 1;
1432 } #}}}
1433
1434 sub globlist_to_pagespec ($) { #{{{
1435         my @globlist=split(' ', shift);
1436
1437         my (@spec, @skip);
1438         foreach my $glob (@globlist) {
1439                 if ($glob=~/^!(.*)/) {
1440                         push @skip, $glob;
1441                 }
1442                 else {
1443                         push @spec, $glob;
1444                 }
1445         }
1446
1447         my $spec=join(' or ', @spec);
1448         if (@skip) {
1449                 my $skip=join(' and ', @skip);
1450                 if (length $spec) {
1451                         $spec="$skip and ($spec)";
1452                 }
1453                 else {
1454                         $spec=$skip;
1455                 }
1456         }
1457         return $spec;
1458 } #}}}
1459
1460 sub is_globlist ($) { #{{{
1461         my $s=shift;
1462         return ( $s =~ /[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or" );
1463 } #}}}
1464
1465 sub safequote ($) { #{{{
1466         my $s=shift;
1467         $s=~s/[{}]//g;
1468         return "q{$s}";
1469 } #}}}
1470
1471 sub add_depends ($$) { #{{{
1472         my $page=shift;
1473         my $pagespec=shift;
1474         
1475         return unless pagespec_valid($pagespec);
1476
1477         if (! exists $depends{$page}) {
1478                 $depends{$page}=$pagespec;
1479         }
1480         else {
1481                 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
1482         }
1483
1484         return 1;
1485 } # }}}
1486
1487 sub file_pruned ($$) { #{{{
1488         require File::Spec;
1489         my $file=File::Spec->canonpath(shift);
1490         my $base=File::Spec->canonpath(shift);
1491         $file =~ s#^\Q$base\E/+##;
1492
1493         my $regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
1494         return $file =~ m/$regexp/ && $file ne $base;
1495 } #}}}
1496
1497 sub gettext { #{{{
1498         # Only use gettext in the rare cases it's needed.
1499         if ((exists $ENV{LANG} && length $ENV{LANG}) ||
1500             (exists $ENV{LC_ALL} && length $ENV{LC_ALL}) ||
1501             (exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES})) {
1502                 if (! $gettext_obj) {
1503                         $gettext_obj=eval q{
1504                                 use Locale::gettext q{textdomain};
1505                                 Locale::gettext->domain('ikiwiki')
1506                         };
1507                         if ($@) {
1508                                 print STDERR "$@";
1509                                 $gettext_obj=undef;
1510                                 return shift;
1511                         }
1512                 }
1513                 return $gettext_obj->get(shift);
1514         }
1515         else {
1516                 return shift;
1517         }
1518 } #}}}
1519
1520 sub yesno ($) { #{{{
1521         my $val=shift;
1522
1523         return (defined $val && lc($val) eq gettext("yes"));
1524 } #}}}
1525
1526 sub pagespec_merge ($$) { #{{{
1527         my $a=shift;
1528         my $b=shift;
1529
1530         return $a if $a eq $b;
1531
1532         # Support for old-style GlobLists.
1533         if (is_globlist($a)) {
1534                 $a=globlist_to_pagespec($a);
1535         }
1536         if (is_globlist($b)) {
1537                 $b=globlist_to_pagespec($b);
1538         }
1539
1540         return "($a) or ($b)";
1541 } #}}}
1542
1543 sub pagespec_translate ($) { #{{{
1544         my $spec=shift;
1545
1546         # Support for old-style GlobLists.
1547         if (is_globlist($spec)) {
1548                 $spec=globlist_to_pagespec($spec);
1549         }
1550
1551         # Convert spec to perl code.
1552         my $code="";
1553         while ($spec=~m{
1554                 \s*             # ignore whitespace
1555                 (               # 1: match a single word
1556                         \!              # !
1557                 |
1558                         \(              # (
1559                 |
1560                         \)              # )
1561                 |
1562                         \w+\([^\)]*\)   # command(params)
1563                 |
1564                         [^\s()]+        # any other text
1565                 )
1566                 \s*             # ignore whitespace
1567         }igx) {
1568                 my $word=$1;
1569                 if (lc $word eq 'and') {
1570                         $code.=' &&';
1571                 }
1572                 elsif (lc $word eq 'or') {
1573                         $code.=' ||';
1574                 }
1575                 elsif ($word eq "(" || $word eq ")" || $word eq "!") {
1576                         $code.=' '.$word;
1577                 }
1578                 elsif ($word =~ /^(\w+)\((.*)\)$/) {
1579                         if (exists $IkiWiki::PageSpec::{"match_$1"}) {
1580                                 $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \@_)";
1581                         }
1582                         else {
1583                                 $code.=' 0';
1584                         }
1585                 }
1586                 else {
1587                         $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \@_)";
1588                 }
1589         }
1590
1591         if (! length $code) {
1592                 $code=0;
1593         }
1594
1595         no warnings;
1596         return eval 'sub { my $page=shift; '.$code.' }';
1597 } #}}}
1598
1599 sub pagespec_match ($$;@) { #{{{
1600         my $page=shift;
1601         my $spec=shift;
1602         my @params=@_;
1603
1604         # Backwards compatability with old calling convention.
1605         if (@params == 1) {
1606                 unshift @params, 'location';
1607         }
1608
1609         my $sub=pagespec_translate($spec);
1610         return IkiWiki::FailReason->new("syntax error in pagespec \"$spec\"") if $@;
1611         return $sub->($page, @params);
1612 } #}}}
1613
1614 sub pagespec_valid ($) { #{{{
1615         my $spec=shift;
1616
1617         my $sub=pagespec_translate($spec);
1618         return ! $@;
1619 } #}}}
1620         
1621 sub glob2re ($) { #{{{
1622         my $re=quotemeta(shift);
1623         $re=~s/\\\*/.*/g;
1624         $re=~s/\\\?/./g;
1625         return $re;
1626 } #}}}
1627
1628 package IkiWiki::FailReason;
1629
1630 use overload ( #{{{
1631         '""'    => sub { ${$_[0]} },
1632         '0+'    => sub { 0 },
1633         '!'     => sub { bless $_[0], 'IkiWiki::SuccessReason'},
1634         fallback => 1,
1635 ); #}}}
1636
1637 sub new { #{{{
1638         my $class = shift;
1639         my $value = shift;
1640         return bless \$value, $class;
1641 } #}}}
1642
1643 package IkiWiki::SuccessReason;
1644
1645 use overload ( #{{{
1646         '""'    => sub { ${$_[0]} },
1647         '0+'    => sub { 1 },
1648         '!'     => sub { bless $_[0], 'IkiWiki::FailReason'},
1649         fallback => 1,
1650 ); #}}}
1651
1652 sub new { #{{{
1653         my $class = shift;
1654         my $value = shift;
1655         return bless \$value, $class;
1656 }; #}}}
1657
1658 package IkiWiki::PageSpec;
1659
1660 sub match_glob ($$;@) { #{{{
1661         my $page=shift;
1662         my $glob=shift;
1663         my %params=@_;
1664         
1665         my $from=exists $params{location} ? $params{location} : '';
1666         
1667         # relative matching
1668         if ($glob =~ m!^\./!) {
1669                 $from=~s#/?[^/]+$##;
1670                 $glob=~s#^\./##;
1671                 $glob="$from/$glob" if length $from;
1672         }
1673
1674         my $regexp=IkiWiki::glob2re($glob);
1675         if ($page=~/^$regexp$/i) {
1676                 if (! IkiWiki::isinternal($page) || $params{internal}) {
1677                         return IkiWiki::SuccessReason->new("$glob matches $page");
1678                 }
1679                 else {
1680                         return IkiWiki::FailReason->new("$glob matches $page, but the page is an internal page");
1681                 }
1682         }
1683         else {
1684                 return IkiWiki::FailReason->new("$glob does not match $page");
1685         }
1686 } #}}}
1687
1688 sub match_internal ($$;@) { #{{{
1689         return match_glob($_[0], $_[1], @_, internal => 1)
1690 } #}}}
1691
1692 sub match_link ($$;@) { #{{{
1693         my $page=shift;
1694         my $link=lc(shift);
1695         my %params=@_;
1696
1697         my $from=exists $params{location} ? $params{location} : '';
1698
1699         # relative matching
1700         if ($link =~ m!^\.! && defined $from) {
1701                 $from=~s#/?[^/]+$##;
1702                 $link=~s#^\./##;
1703                 $link="$from/$link" if length $from;
1704         }
1705
1706         my $links = $IkiWiki::links{$page};
1707         return IkiWiki::FailReason->new("$page has no links") unless $links && @{$links};
1708         my $bestlink = IkiWiki::bestlink($from, $link);
1709         foreach my $p (@{$links}) {
1710                 if (length $bestlink) {
1711                         return IkiWiki::SuccessReason->new("$page links to $link")
1712                                 if $bestlink eq IkiWiki::bestlink($page, $p);
1713                 }
1714                 else {
1715                         return IkiWiki::SuccessReason->new("$page links to page $p matching $link")
1716                                 if match_glob($p, $link, %params);
1717                 }
1718         }
1719         return IkiWiki::FailReason->new("$page does not link to $link");
1720 } #}}}
1721
1722 sub match_backlink ($$;@) { #{{{
1723         return match_link($_[1], $_[0], @_);
1724 } #}}}
1725
1726 sub match_created_before ($$;@) { #{{{
1727         my $page=shift;
1728         my $testpage=shift;
1729
1730         if (exists $IkiWiki::pagectime{$testpage}) {
1731                 if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
1732                         return IkiWiki::SuccessReason->new("$page created before $testpage");
1733                 }
1734                 else {
1735                         return IkiWiki::FailReason->new("$page not created before $testpage");
1736                 }
1737         }
1738         else {
1739                 return IkiWiki::FailReason->new("$testpage has no ctime");
1740         }
1741 } #}}}
1742
1743 sub match_created_after ($$;@) { #{{{
1744         my $page=shift;
1745         my $testpage=shift;
1746
1747         if (exists $IkiWiki::pagectime{$testpage}) {
1748                 if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
1749                         return IkiWiki::SuccessReason->new("$page created after $testpage");
1750                 }
1751                 else {
1752                         return IkiWiki::FailReason->new("$page not created after $testpage");
1753                 }
1754         }
1755         else {
1756                 return IkiWiki::FailReason->new("$testpage has no ctime");
1757         }
1758 } #}}}
1759
1760 sub match_creation_day ($$;@) { #{{{
1761         if ((gmtime($IkiWiki::pagectime{shift()}))[3] == shift) {
1762                 return IkiWiki::SuccessReason->new('creation_day matched');
1763         }
1764         else {
1765                 return IkiWiki::FailReason->new('creation_day did not match');
1766         }
1767 } #}}}
1768
1769 sub match_creation_month ($$;@) { #{{{
1770         if ((gmtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
1771                 return IkiWiki::SuccessReason->new('creation_month matched');
1772         }
1773         else {
1774                 return IkiWiki::FailReason->new('creation_month did not match');
1775         }
1776 } #}}}
1777
1778 sub match_creation_year ($$;@) { #{{{
1779         if ((gmtime($IkiWiki::pagectime{shift()}))[5] + 1900 == shift) {
1780                 return IkiWiki::SuccessReason->new('creation_year matched');
1781         }
1782         else {
1783                 return IkiWiki::FailReason->new('creation_year did not match');
1784         }
1785 } #}}}
1786
1787 1