]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki.pm
remove
[ikiwiki.git] / IkiWiki.pm
1 #!/usr/bin/perl
2
3 package IkiWiki;
4 use warnings;
5 use strict;
6 use Encode;
7 use HTML::Entities;
8 use open qw{:utf8 :std};
9
10 use vars qw{%config %links %oldlinks %oldpagemtime %pagectime %pagecase
11             %renderedfiles %oldrenderedfiles %pagesources %depends %hooks
12             %forcerebuild};
13
14 use Exporter q{import};
15 our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
16                  bestlink htmllink readfile writefile pagetype srcfile pagename
17                  displaytime will_render
18                  %config %links %renderedfiles %pagesources);
19 our $VERSION = 1.01; # plugin interface version
20
21 # Optimisation.
22 use Memoize;
23 memoize("abs2rel");
24 memoize("pagespec_translate");
25
26 my $installdir=''; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
27 our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
28
29 sub defaultconfig () { #{{{
30         wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.x?html?$|\.rss$|\.atom$|.arch-ids/|{arch}/)},
31         wiki_link_regexp => qr/\[\[(?:([^\]\|]+)\|)?([^\s\]]+)\]\]/,
32         wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
33         verbose => 0,
34         syslog => 0,
35         wikiname => "wiki",
36         default_pageext => "mdwn",
37         cgi => 0,
38         rcs => 'svn',
39         notify => 0,
40         url => '',
41         cgiurl => '',
42         historyurl => '',
43         diffurl => '',
44         anonok => 0,
45         rss => 0,
46         atom => 0,
47         discussion => 1,
48         rebuild => 0,
49         refresh => 0,
50         getctime => 0,
51         w3mmode => 0,
52         wrapper => undef,
53         wrappermode => undef,
54         svnrepo => undef,
55         svnpath => "trunk",
56         srcdir => undef,
57         destdir => undef,
58         pingurl => [],
59         templatedir => "$installdir/share/ikiwiki/templates",
60         underlaydir => "$installdir/share/ikiwiki/basewiki",
61         setup => undef,
62         adminuser => undef,
63         adminemail => undef,
64         plugin => [qw{mdwn inline htmlscrubber}],
65         timeformat => '%c',
66         locale => undef,
67         sslcookie => 0,
68         httpauth => 0,
69 } #}}}
70    
71 sub checkconfig () { #{{{
72         # locale stuff; avoid LC_ALL since it overrides everything
73         if (defined $ENV{LC_ALL}) {
74                 $ENV{LANG} = $ENV{LC_ALL};
75                 delete $ENV{LC_ALL};
76         }
77         if (defined $config{locale}) {
78                 eval q{use POSIX};
79                 $ENV{LANG} = $config{locale}
80                         if POSIX::setlocale(&POSIX::LC_TIME, $config{locale});
81         }
82
83         if ($config{w3mmode}) {
84                 eval q{use Cwd q{abs_path}};
85                 $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir}));
86                 $config{destdir}=possibly_foolish_untaint(abs_path($config{destdir}));
87                 $config{cgiurl}="file:///\$LIB/ikiwiki-w3m.cgi/".$config{cgiurl}
88                         unless $config{cgiurl} =~ m!file:///!;
89                 $config{url}="file://".$config{destdir};
90         }
91
92         if ($config{cgi} && ! length $config{url}) {
93                 error("Must specify url to wiki with --url when using --cgi\n");
94         }
95         if (($config{rss} || $config{atom}) && ! length $config{url}) {
96                 error("Must specify url to wiki with --url when using --rss or --atom\n");
97         }
98         
99         $config{wikistatedir}="$config{srcdir}/.ikiwiki"
100                 unless exists $config{wikistatedir};
101         
102         if ($config{rcs}) {
103                 eval qq{require IkiWiki::Rcs::$config{rcs}};
104                 if ($@) {
105                         error("Failed to load RCS module IkiWiki::Rcs::$config{rcs}: $@");
106                 }
107         }
108         else {
109                 require IkiWiki::Rcs::Stub;
110         }
111
112         run_hooks(checkconfig => sub { shift->() });
113 } #}}}
114
115 sub loadplugins () { #{{{
116         foreach my $plugin (@{$config{plugin}}) {
117                 my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
118                 eval qq{use $mod};
119                 if ($@) {
120                         error("Failed to load plugin $mod: $@");
121                 }
122         }
123         run_hooks(getopt => sub { shift->() });
124         if (grep /^-/, @ARGV) {
125                 print STDERR "Unknown option: $_\n"
126                         foreach grep /^-/, @ARGV;
127                 usage();
128         }
129 } #}}}
130
131 sub error ($) { #{{{
132         if ($config{cgi}) {
133                 print "Content-type: text/html\n\n";
134                 print misctemplate("Error", "<p>Error: @_</p>");
135         }
136         log_message(error => @_);
137         exit(1);
138 } #}}}
139
140 sub debug ($) { #{{{
141         return unless $config{verbose};
142         log_message(debug => @_);
143 } #}}}
144
145 my $log_open=0;
146 sub log_message ($$) { #{{{
147         my $type=shift;
148
149         if ($config{syslog}) {
150                 require Sys::Syslog;
151                 unless ($log_open) {
152                         Sys::Syslog::setlogsock('unix');
153                         Sys::Syslog::openlog('ikiwiki', '', 'user');
154                         $log_open=1;
155                 }
156                 eval {
157                         Sys::Syslog::syslog($type, join(" ", @_));
158                 }
159         }
160         elsif (! $config{cgi}) {
161                 print "@_\n";
162         }
163         else {
164                 print STDERR "@_\n";
165         }
166 } #}}}
167
168 sub possibly_foolish_untaint ($) { #{{{
169         my $tainted=shift;
170         my ($untainted)=$tainted=~/(.*)/;
171         return $untainted;
172 } #}}}
173
174 sub basename ($) { #{{{
175         my $file=shift;
176
177         $file=~s!.*/+!!;
178         return $file;
179 } #}}}
180
181 sub dirname ($) { #{{{
182         my $file=shift;
183
184         $file=~s!/*[^/]+$!!;
185         return $file;
186 } #}}}
187
188 sub pagetype ($) { #{{{
189         my $page=shift;
190         
191         if ($page =~ /\.([^.]+)$/) {
192                 return $1 if exists $hooks{htmlize}{$1};
193         }
194         return undef;
195 } #}}}
196
197 sub pagename ($) { #{{{
198         my $file=shift;
199
200         my $type=pagetype($file);
201         my $page=$file;
202         $page=~s/\Q.$type\E*$// if defined $type;
203         return $page;
204 } #}}}
205
206 sub htmlpage ($) { #{{{
207         my $page=shift;
208
209         return $page.".html";
210 } #}}}
211
212 sub srcfile ($) { #{{{
213         my $file=shift;
214
215         return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
216         return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
217         error("internal error: $file cannot be found");
218 } #}}}
219
220 sub readfile ($;$) { #{{{
221         my $file=shift;
222         my $binary=shift;
223
224         if (-l $file) {
225                 error("cannot read a symlink ($file)");
226         }
227         
228         local $/=undef;
229         open (IN, $file) || error("failed to read $file: $!");
230         binmode(IN) if ($binary);
231         my $ret=<IN>;
232         close IN;
233         return $ret;
234 } #}}}
235
236 sub writefile ($$$;$) { #{{{
237         my $file=shift; # can include subdirs
238         my $destdir=shift; # directory to put file in
239         my $content=shift;
240         my $binary=shift;
241         
242         my $test=$file;
243         while (length $test) {
244                 if (-l "$destdir/$test") {
245                         error("cannot write to a symlink ($test)");
246                 }
247                 $test=dirname($test);
248         }
249
250         my $dir=dirname("$destdir/$file");
251         if (! -d $dir) {
252                 my $d="";
253                 foreach my $s (split(m!/+!, $dir)) {
254                         $d.="$s/";
255                         if (! -d $d) {
256                                 mkdir($d) || error("failed to create directory $d: $!");
257                         }
258                 }
259         }
260         
261         open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
262         binmode(OUT) if ($binary);
263         print OUT $content;
264         close OUT;
265 } #}}}
266
267 my %cleared;
268 sub will_render ($$;$) { #{{{
269         my $page=shift;
270         my $dest=shift;
271         my $clear=shift;
272
273         # Important security check.
274         if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
275             ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}})) {
276                 error("$config{destdir}/$dest independently created, not overwriting with version from $page");
277         }
278
279         if (! $clear || $cleared{$page}) {
280                 $renderedfiles{$page}=[$dest, grep { $_ ne $dest } @{$renderedfiles{$page}}];
281         }
282         else {
283                 $renderedfiles{$page}=[$dest];
284                 $cleared{$page}=1;
285         }
286 } #}}}
287
288 sub bestlink ($$) { #{{{
289         my $page=shift;
290         my $link=shift;
291         
292         my $cwd=$page;
293         do {
294                 my $l=$cwd;
295                 $l.="/" if length $l;
296                 $l.=$link;
297
298                 if (exists $links{$l}) {
299                         return $l;
300                 }
301                 elsif (exists $pagecase{lc $l}) {
302                         return $pagecase{lc $l};
303                 }
304         } while $cwd=~s!/?[^/]+$!!;
305
306         #print STDERR "warning: page $page, broken link: $link\n";
307         return "";
308 } #}}}
309
310 sub isinlinableimage ($) { #{{{
311         my $file=shift;
312         
313         $file=~/\.(png|gif|jpg|jpeg)$/i;
314 } #}}}
315
316 sub pagetitle ($) { #{{{
317         my $page=shift;
318         $page=~s/__(\d+)__/&#$1;/g;
319         $page=~y/_/ /;
320         return $page;
321 } #}}}
322
323 sub titlepage ($) { #{{{
324         my $title=shift;
325         $title=~y/ /_/;
326         $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
327         return $title;
328 } #}}}
329
330 sub cgiurl (@) { #{{{
331         my %params=@_;
332
333         return $config{cgiurl}."?".join("&amp;", map "$_=$params{$_}", keys %params);
334 } #}}}
335
336 sub baseurl (;$) { #{{{
337         my $page=shift;
338
339         return "$config{url}/" if ! defined $page;
340         
341         $page=~s/[^\/]+$//;
342         $page=~s/[^\/]+\//..\//g;
343         return $page;
344 } #}}}
345
346 sub abs2rel ($$) { #{{{
347         # Work around very innefficient behavior in File::Spec if abs2rel
348         # is passed two relative paths. It's much faster if paths are
349         # absolute! (Debian bug #376658)
350         my $path="/".shift;
351         my $base="/".shift;
352
353         require File::Spec;
354         my $ret=File::Spec->abs2rel($path, $base);
355         $ret=~s/^// if defined $ret;
356         return $ret;
357 } #}}}
358
359 sub displaytime ($) { #{{{
360         my $time=shift;
361
362         eval q{use POSIX};
363         # strftime doesn't know about encodings, so make sure
364         # its output is properly treated as utf8
365         return decode_utf8(POSIX::strftime(
366                         $config{timeformat}, localtime($time)));
367 } #}}}
368
369 sub htmllink ($$$;$$$) { #{{{
370         my $lpage=shift; # the page doing the linking
371         my $page=shift; # the page that will contain the link (different for inline)
372         my $link=shift;
373         my $noimageinline=shift; # don't turn links into inline html images
374         my $forcesubpage=shift; # force a link to a subpage
375         my $linktext=shift; # set to force the link text to something
376
377         my $bestlink;
378         if (! $forcesubpage) {
379                 $bestlink=bestlink($lpage, $link);
380         }
381         else {
382                 $bestlink="$lpage/".lc($link);
383         }
384
385         $linktext=pagetitle(basename($link)) unless defined $linktext;
386         
387         return "<span class=\"selflink\">$linktext</span>"
388                 if length $bestlink && $page eq $bestlink;
389         
390         # TODO BUG: %renderedfiles may not have it, if the linked to page
391         # was also added and isn't yet rendered! Note that this bug is
392         # masked by the bug that makes all new files be rendered twice.
393         if (! grep { $_ eq $bestlink } map { @{$_} } values %renderedfiles) {
394                 $bestlink=htmlpage($bestlink);
395         }
396         if (! grep { $_ eq $bestlink } map { @{$_} } values %renderedfiles) {
397                 return "<span><a href=\"".
398                         cgiurl(do => "create", page => lc($link), from => $page).
399                         "\">?</a>$linktext</span>"
400         }
401         
402         $bestlink=abs2rel($bestlink, dirname($page));
403         
404         if (! $noimageinline && isinlinableimage($bestlink)) {
405                 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
406         }
407         return "<a href=\"$bestlink\">$linktext</a>";
408 } #}}}
409
410 sub htmlize ($$$) { #{{{
411         my $page=shift;
412         my $type=shift;
413         my $content=shift;
414
415         if (exists $hooks{htmlize}{$type}) {
416                 $content=$hooks{htmlize}{$type}{call}->(
417                         page => $page,
418                         content => $content,
419                 );
420         }
421         else {
422                 error("htmlization of $type not supported");
423         }
424
425         run_hooks(sanitize => sub {
426                 $content=shift->(
427                         page => $page,
428                         content => $content,
429                 );
430         });
431
432         return $content;
433 } #}}}
434
435 sub linkify ($$$) { #{{{
436         my $lpage=shift; # the page containing the links
437         my $page=shift; # the page the link will end up on (different for inline)
438         my $content=shift;
439
440         $content =~ s{(\\?)$config{wiki_link_regexp}}{
441                 $2 ? ( $1 ? "[[$2|$3]]" : htmllink($lpage, $page, titlepage($3), 0, 0, pagetitle($2)))
442                    : ( $1 ? "[[$3]]" :    htmllink($lpage, $page, titlepage($3)))
443         }eg;
444         
445         return $content;
446 } #}}}
447
448 my %preprocessing;
449 sub preprocess ($$$) { #{{{
450         my $page=shift; # the page the data comes from
451         my $destpage=shift; # the page the data will appear in (different for inline)
452         my $content=shift;
453
454         my $handle=sub {
455                 my $escape=shift;
456                 my $command=shift;
457                 my $params=shift;
458                 if (length $escape) {
459                         return "[[$command $params]]";
460                 }
461                 elsif (exists $hooks{preprocess}{$command}) {
462                         # Note: preserve order of params, some plugins may
463                         # consider it significant.
464                         my @params;
465                         while ($params =~ /(?:(\w+)=)?(?:"""(.*?)"""|"([^"]+)"|(\S+))(?:\s+|$)/sg) {
466                                 my $key=$1;
467                                 my $val;
468                                 if (defined $2) {
469                                         $val=$2;
470                                         $val=~s/\r\n/\n/mg;
471                                         $val=~s/^\n+//g;
472                                         $val=~s/\n+$//g;
473                                 }
474                                 elsif (defined $3) {
475                                         $val=$3;
476                                 }
477                                 elsif (defined $4) {
478                                         $val=$4;
479                                 }
480
481                                 if (defined $key) {
482                                         push @params, $key, $val;
483                                 }
484                                 else {
485                                         push @params, $val, '';
486                                 }
487                         }
488                         if ($preprocessing{$page}++ > 3) {
489                                 # Avoid loops of preprocessed pages preprocessing
490                                 # other pages that preprocess them, etc.
491                                 return "[[$command preprocessing loop detected on $page at depth $preprocessing{$page}]]";
492                         }
493                         my $ret=$hooks{preprocess}{$command}{call}->(
494                                 @params,
495                                 page => $page,
496                                 destpage => $destpage,
497                         );
498                         $preprocessing{$page}--;
499                         return $ret;
500                 }
501                 else {
502                         return "[[$command $params]]";
503                 }
504         };
505         
506         $content =~ s{(\\?)\[\[(\w+)\s+((?:(?:\w+=)?(?:""".*?"""|"[^"]+"|[^\s\]]+)\s*)*)\]\]}{$handle->($1, $2, $3)}seg;
507         return $content;
508 } #}}}
509
510 sub filter ($$) {
511         my $page=shift;
512         my $content=shift;
513
514         run_hooks(filter => sub {
515                 $content=shift->(page => $page, content => $content);
516         });
517
518         return $content;
519 }
520
521 sub indexlink () { #{{{
522         return "<a href=\"$config{url}\">$config{wikiname}</a>";
523 } #}}}
524
525 sub lockwiki () { #{{{
526         # Take an exclusive lock on the wiki to prevent multiple concurrent
527         # run issues. The lock will be dropped on program exit.
528         if (! -d $config{wikistatedir}) {
529                 mkdir($config{wikistatedir});
530         }
531         open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
532                 error ("cannot write to $config{wikistatedir}/lockfile: $!");
533         if (! flock(WIKILOCK, 2 | 4)) {
534                 debug("wiki seems to be locked, waiting for lock");
535                 my $wait=600; # arbitrary, but don't hang forever to 
536                               # prevent process pileup
537                 for (1..600) {
538                         return if flock(WIKILOCK, 2 | 4);
539                         sleep 1;
540                 }
541                 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
542         }
543 } #}}}
544
545 sub unlockwiki () { #{{{
546         close WIKILOCK;
547 } #}}}
548
549 sub loadindex () { #{{{
550         open (IN, "$config{wikistatedir}/index") || return;
551         while (<IN>) {
552                 $_=possibly_foolish_untaint($_);
553                 chomp;
554                 my %items;
555                 $items{link}=[];
556                 $items{dest}=[];
557                 foreach my $i (split(/ /, $_)) {
558                         my ($item, $val)=split(/=/, $i, 2);
559                         push @{$items{$item}}, decode_entities($val);
560                 }
561
562                 next unless exists $items{src}; # skip bad lines for now
563
564                 my $page=pagename($items{src}[0]);
565                 if (! $config{rebuild}) {
566                         $pagesources{$page}=$items{src}[0];
567                         $oldpagemtime{$page}=$items{mtime}[0];
568                         $oldlinks{$page}=[@{$items{link}}];
569                         $links{$page}=[@{$items{link}}];
570                         $depends{$page}=$items{depends}[0] if exists $items{depends};
571                         $renderedfiles{$page}=[@{$items{dest}}];
572                         $oldrenderedfiles{$page}=[@{$items{dest}}];
573                         $pagecase{lc $page}=$page;
574                 }
575                 $pagectime{$page}=$items{ctime}[0];
576         }
577         close IN;
578 } #}}}
579
580 sub saveindex () { #{{{
581         run_hooks(savestate => sub { shift->() });
582
583         if (! -d $config{wikistatedir}) {
584                 mkdir($config{wikistatedir});
585         }
586         open (OUT, ">$config{wikistatedir}/index") || 
587                 error("cannot write to $config{wikistatedir}/index: $!");
588         foreach my $page (keys %oldpagemtime) {
589                 next unless $oldpagemtime{$page};
590                 my $line="mtime=$oldpagemtime{$page} ".
591                         "ctime=$pagectime{$page} ".
592                         "src=$pagesources{$page}";
593                 $line.=" dest=$_" foreach @{$renderedfiles{$page}};
594                 $line.=" link=$_" foreach @{$links{$page}};
595                 if (exists $depends{$page}) {
596                         $line.=" depends=".encode_entities($depends{$page}, " \t\n");
597                 }
598                 print OUT $line."\n";
599         }
600         close OUT;
601 } #}}}
602
603 sub template_params (@) { #{{{
604         my $filename=shift;
605         
606         require HTML::Template;
607         return filter => sub {
608                         my $text_ref = shift;
609                         $$text_ref=&Encode::decode_utf8($$text_ref);
610                 },
611                 filename => "$config{templatedir}/$filename",
612                 loop_context_vars => 1,
613                 die_on_bad_params => 0,
614                 @_;
615 } #}}}
616
617 sub template ($;@) { #{{{
618         HTML::Template->new(template_params(@_));
619 } #}}}
620
621 sub misctemplate ($$;@) { #{{{
622         my $title=shift;
623         my $pagebody=shift;
624         
625         my $template=template("misc.tmpl");
626         $template->param(
627                 title => $title,
628                 indexlink => indexlink(),
629                 wikiname => $config{wikiname},
630                 pagebody => $pagebody,
631                 baseurl => baseurl(),
632                 @_,
633         );
634         run_hooks(pagetemplate => sub {
635                 shift->(page => "", destpage => "", template => $template);
636         });
637         return $template->output;
638 }#}}}
639
640 sub hook (@) { # {{{
641         my %param=@_;
642         
643         if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
644                 error "hook requires type, call, and id parameters";
645         }
646
647         return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
648         
649         $hooks{$param{type}}{$param{id}}=\%param;
650 } # }}}
651
652 sub run_hooks ($$) { # {{{
653         # Calls the given sub for each hook of the given type,
654         # passing it the hook function to call.
655         my $type=shift;
656         my $sub=shift;
657
658         if (exists $hooks{$type}) {
659                 foreach my $id (keys %{$hooks{$type}}) {
660                         $sub->($hooks{$type}{$id}{call});
661                 }
662         }
663 } #}}}
664
665 sub globlist_to_pagespec ($) { #{{{
666         my @globlist=split(' ', shift);
667
668         my (@spec, @skip);
669         foreach my $glob (@globlist) {
670                 if ($glob=~/^!(.*)/) {
671                         push @skip, $glob;
672                 }
673                 else {
674                         push @spec, $glob;
675                 }
676         }
677
678         my $spec=join(" or ", @spec);
679         if (@skip) {
680                 my $skip=join(" and ", @skip);
681                 if (length $spec) {
682                         $spec="$skip and ($spec)";
683                 }
684                 else {
685                         $spec=$skip;
686                 }
687         }
688         return $spec;
689 } #}}}
690
691 sub is_globlist ($) { #{{{
692         my $s=shift;
693         $s=~/[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or";
694 } #}}}
695
696 sub safequote ($) { #{{{
697         my $s=shift;
698         $s=~s/[{}]//g;
699         return "q{$s}";
700 } #}}}
701
702 sub pagespec_merge ($$) { #{{{
703         my $a=shift;
704         my $b=shift;
705
706         return $a if $a eq $b;
707
708         # Support for old-style GlobLists.
709         if (is_globlist($a)) {
710                 $a=globlist_to_pagespec($a);
711         }
712         if (is_globlist($b)) {
713                 $b=globlist_to_pagespec($b);
714         }
715
716         return "($a) or ($b)";
717 } #}}}
718
719 sub pagespec_translate ($) { #{{{
720         # This assumes that $page is in scope in the function
721         # that evalulates the translated pagespec code.
722         my $spec=shift;
723
724         # Support for old-style GlobLists.
725         if (is_globlist($spec)) {
726                 $spec=globlist_to_pagespec($spec);
727         }
728
729         # Convert spec to perl code.
730         my $code="";
731         while ($spec=~m/\s*(\!|\(|\)|\w+\([^\)]+\)|[^\s()]+)\s*/ig) {
732                 my $word=$1;
733                 if (lc $word eq "and") {
734                         $code.=" &&";
735                 }
736                 elsif (lc $word eq "or") {
737                         $code.=" ||";
738                 }
739                 elsif ($word eq "(" || $word eq ")" || $word eq "!") {
740                         $code.=" ".$word;
741                 }
742                 elsif ($word =~ /^(link|backlink|created_before|created_after|creation_month|creation_year|creation_day)\((.+)\)$/) {
743                         $code.=" match_$1(\$page, ".safequote($2).")";
744                 }
745                 else {
746                         $code.=" match_glob(\$page, ".safequote($word).")";
747                 }
748         }
749
750         return $code;
751 } #}}}
752
753 sub add_depends ($$) { #{{{
754         my $page=shift;
755         my $pagespec=shift;
756         
757         if (! exists $depends{$page}) {
758                 $depends{$page}=$pagespec;
759         }
760         else {
761                 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
762         }
763 } # }}}
764
765 sub pagespec_match ($$) { #{{{
766         my $page=shift;
767         my $spec=shift;
768
769         return eval pagespec_translate($spec);
770 } #}}}
771
772 sub match_glob ($$) { #{{{
773         my $page=shift;
774         my $glob=shift;
775
776         # turn glob into safe regexp
777         $glob=quotemeta($glob);
778         $glob=~s/\\\*/.*/g;
779         $glob=~s/\\\?/./g;
780
781         return $page=~/^$glob$/i;
782 } #}}}
783
784 sub match_link ($$) { #{{{
785         my $page=shift;
786         my $link=lc(shift);
787
788         my $links = $links{$page} or return undef;
789         foreach my $p (@$links) {
790                 return 1 if lc $p eq $link;
791         }
792         return 0;
793 } #}}}
794
795 sub match_backlink ($$) { #{{{
796         match_link(pop, pop);
797 } #}}}
798
799 sub match_created_before ($$) { #{{{
800         my $page=shift;
801         my $testpage=shift;
802
803         if (exists $pagectime{$testpage}) {
804                 return $pagectime{$page} < $pagectime{$testpage};
805         }
806         else {
807                 return 0;
808         }
809 } #}}}
810
811 sub match_created_after ($$) { #{{{
812         my $page=shift;
813         my $testpage=shift;
814
815         if (exists $pagectime{$testpage}) {
816                 return $pagectime{$page} > $pagectime{$testpage};
817         }
818         else {
819                 return 0;
820         }
821 } #}}}
822
823 sub match_creation_day ($$) { #{{{
824         return ((gmtime($pagectime{shift()}))[3] == shift);
825 } #}}}
826
827 sub match_creation_month ($$) { #{{{
828         return ((gmtime($pagectime{shift()}))[4] + 1 == shift);
829 } #}}}
830
831 sub match_creation_year ($$) { #{{{
832         return ((gmtime($pagectime{shift()}))[5] + 1900 == shift);
833 } #}}}
834
835 1