instead of over and over. Typical speedup is ~4x. Max possible speedup:
[ikiwiki] / 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         my $scan=shift;
454
455         my $handle=sub {
456                 my $escape=shift;
457                 my $command=shift;
458                 my $params=shift;
459                 if (length $escape) {
460                         return "[[$command $params]]";
461                 }
462                 elsif (exists $hooks{preprocess}{$command}) {
463                         return "" if $scan && ! $hooks{preprocess}{$command}{scan};
464                         # Note: preserve order of params, some plugins may
465                         # consider it significant.
466                         my @params;
467                         while ($params =~ /(?:(\w+)=)?(?:"""(.*?)"""|"([^"]+)"|(\S+))(?:\s+|$)/sg) {
468                                 my $key=$1;
469                                 my $val;
470                                 if (defined $2) {
471                                         $val=$2;
472                                         $val=~s/\r\n/\n/mg;
473                                         $val=~s/^\n+//g;
474                                         $val=~s/\n+$//g;
475                                 }
476                                 elsif (defined $3) {
477                                         $val=$3;
478                                 }
479                                 elsif (defined $4) {
480                                         $val=$4;
481                                 }
482
483                                 if (defined $key) {
484                                         push @params, $key, $val;
485                                 }
486                                 else {
487                                         push @params, $val, '';
488                                 }
489                         }
490                         if ($preprocessing{$page}++ > 3) {
491                                 # Avoid loops of preprocessed pages preprocessing
492                                 # other pages that preprocess them, etc.
493                                 return "[[$command preprocessing loop detected on $page at depth $preprocessing{$page}]]";
494                         }
495                         my $ret=$hooks{preprocess}{$command}{call}->(
496                                 @params,
497                                 page => $page,
498                                 destpage => $destpage,
499                         );
500                         $preprocessing{$page}--;
501                         return $ret;
502                 }
503                 else {
504                         return "[[$command $params]]";
505                 }
506         };
507         
508         $content =~ s{(\\?)\[\[(\w+)\s+((?:(?:\w+=)?(?:""".*?"""|"[^"]+"|[^\s\]]+)\s*)*)\]\]}{$handle->($1, $2, $3)}seg;
509         return $content;
510 } #}}}
511
512 sub filter ($$) {
513         my $page=shift;
514         my $content=shift;
515
516         run_hooks(filter => sub {
517                 $content=shift->(page => $page, content => $content);
518         });
519
520         return $content;
521 }
522
523 sub indexlink () { #{{{
524         return "<a href=\"$config{url}\">$config{wikiname}</a>";
525 } #}}}
526
527 sub lockwiki () { #{{{
528         # Take an exclusive lock on the wiki to prevent multiple concurrent
529         # run issues. The lock will be dropped on program exit.
530         if (! -d $config{wikistatedir}) {
531                 mkdir($config{wikistatedir});
532         }
533         open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
534                 error ("cannot write to $config{wikistatedir}/lockfile: $!");
535         if (! flock(WIKILOCK, 2 | 4)) {
536                 debug("wiki seems to be locked, waiting for lock");
537                 my $wait=600; # arbitrary, but don't hang forever to 
538                               # prevent process pileup
539                 for (1..600) {
540                         return if flock(WIKILOCK, 2 | 4);
541                         sleep 1;
542                 }
543                 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
544         }
545 } #}}}
546
547 sub unlockwiki () { #{{{
548         close WIKILOCK;
549 } #}}}
550
551 sub loadindex () { #{{{
552         open (IN, "$config{wikistatedir}/index") || return;
553         while (<IN>) {
554                 $_=possibly_foolish_untaint($_);
555                 chomp;
556                 my %items;
557                 $items{link}=[];
558                 $items{dest}=[];
559                 foreach my $i (split(/ /, $_)) {
560                         my ($item, $val)=split(/=/, $i, 2);
561                         push @{$items{$item}}, decode_entities($val);
562                 }
563
564                 next unless exists $items{src}; # skip bad lines for now
565
566                 my $page=pagename($items{src}[0]);
567                 if (! $config{rebuild}) {
568                         $pagesources{$page}=$items{src}[0];
569                         $oldpagemtime{$page}=$items{mtime}[0];
570                         $oldlinks{$page}=[@{$items{link}}];
571                         $links{$page}=[@{$items{link}}];
572                         $depends{$page}=$items{depends}[0] if exists $items{depends};
573                         $renderedfiles{$page}=[@{$items{dest}}];
574                         $oldrenderedfiles{$page}=[@{$items{dest}}];
575                         $pagecase{lc $page}=$page;
576                 }
577                 $pagectime{$page}=$items{ctime}[0];
578         }
579         close IN;
580 } #}}}
581
582 sub saveindex () { #{{{
583         run_hooks(savestate => sub { shift->() });
584
585         if (! -d $config{wikistatedir}) {
586                 mkdir($config{wikistatedir});
587         }
588         open (OUT, ">$config{wikistatedir}/index") || 
589                 error("cannot write to $config{wikistatedir}/index: $!");
590         foreach my $page (keys %oldpagemtime) {
591                 next unless $oldpagemtime{$page};
592                 my $line="mtime=$oldpagemtime{$page} ".
593                         "ctime=$pagectime{$page} ".
594                         "src=$pagesources{$page}";
595                 $line.=" dest=$_" foreach @{$renderedfiles{$page}};
596                 $line.=" link=$_" foreach @{$links{$page}};
597                 if (exists $depends{$page}) {
598                         $line.=" depends=".encode_entities($depends{$page}, " \t\n");
599                 }
600                 print OUT $line."\n";
601         }
602         close OUT;
603 } #}}}
604
605 sub template_params (@) { #{{{
606         my $filename=shift;
607         
608         require HTML::Template;
609         return filter => sub {
610                         my $text_ref = shift;
611                         $$text_ref=&Encode::decode_utf8($$text_ref);
612                 },
613                 filename => "$config{templatedir}/$filename",
614                 loop_context_vars => 1,
615                 die_on_bad_params => 0,
616                 @_;
617 } #}}}
618
619 sub template ($;@) { #{{{
620         HTML::Template->new(template_params(@_));
621 } #}}}
622
623 sub misctemplate ($$;@) { #{{{
624         my $title=shift;
625         my $pagebody=shift;
626         
627         my $template=template("misc.tmpl");
628         $template->param(
629                 title => $title,
630                 indexlink => indexlink(),
631                 wikiname => $config{wikiname},
632                 pagebody => $pagebody,
633                 baseurl => baseurl(),
634                 @_,
635         );
636         run_hooks(pagetemplate => sub {
637                 shift->(page => "", destpage => "", template => $template);
638         });
639         return $template->output;
640 }#}}}
641
642 sub hook (@) { # {{{
643         my %param=@_;
644         
645         if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
646                 error "hook requires type, call, and id parameters";
647         }
648
649         return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
650         
651         $hooks{$param{type}}{$param{id}}=\%param;
652 } # }}}
653
654 sub run_hooks ($$) { # {{{
655         # Calls the given sub for each hook of the given type,
656         # passing it the hook function to call.
657         my $type=shift;
658         my $sub=shift;
659
660         if (exists $hooks{$type}) {
661                 foreach my $id (keys %{$hooks{$type}}) {
662                         $sub->($hooks{$type}{$id}{call});
663                 }
664         }
665 } #}}}
666
667 sub globlist_to_pagespec ($) { #{{{
668         my @globlist=split(' ', shift);
669
670         my (@spec, @skip);
671         foreach my $glob (@globlist) {
672                 if ($glob=~/^!(.*)/) {
673                         push @skip, $glob;
674                 }
675                 else {
676                         push @spec, $glob;
677                 }
678         }
679
680         my $spec=join(" or ", @spec);
681         if (@skip) {
682                 my $skip=join(" and ", @skip);
683                 if (length $spec) {
684                         $spec="$skip and ($spec)";
685                 }
686                 else {
687                         $spec=$skip;
688                 }
689         }
690         return $spec;
691 } #}}}
692
693 sub is_globlist ($) { #{{{
694         my $s=shift;
695         $s=~/[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or";
696 } #}}}
697
698 sub safequote ($) { #{{{
699         my $s=shift;
700         $s=~s/[{}]//g;
701         return "q{$s}";
702 } #}}}
703
704 sub pagespec_merge ($$) { #{{{
705         my $a=shift;
706         my $b=shift;
707
708         return $a if $a eq $b;
709
710         # Support for old-style GlobLists.
711         if (is_globlist($a)) {
712                 $a=globlist_to_pagespec($a);
713         }
714         if (is_globlist($b)) {
715                 $b=globlist_to_pagespec($b);
716         }
717
718         return "($a) or ($b)";
719 } #}}}
720
721 sub pagespec_translate ($) { #{{{
722         # This assumes that $page is in scope in the function
723         # that evalulates the translated pagespec code.
724         my $spec=shift;
725
726         # Support for old-style GlobLists.
727         if (is_globlist($spec)) {
728                 $spec=globlist_to_pagespec($spec);
729         }
730
731         # Convert spec to perl code.
732         my $code="";
733         while ($spec=~m/\s*(\!|\(|\)|\w+\([^\)]+\)|[^\s()]+)\s*/ig) {
734                 my $word=$1;
735                 if (lc $word eq "and") {
736                         $code.=" &&";
737                 }
738                 elsif (lc $word eq "or") {
739                         $code.=" ||";
740                 }
741                 elsif ($word eq "(" || $word eq ")" || $word eq "!") {
742                         $code.=" ".$word;
743                 }
744                 elsif ($word =~ /^(link|backlink|created_before|created_after|creation_month|creation_year|creation_day)\((.+)\)$/) {
745                         $code.=" match_$1(\$page, ".safequote($2).")";
746                 }
747                 else {
748                         $code.=" match_glob(\$page, ".safequote($word).")";
749                 }
750         }
751
752         return $code;
753 } #}}}
754
755 sub add_depends ($$) { #{{{
756         my $page=shift;
757         my $pagespec=shift;
758         
759         if (! exists $depends{$page}) {
760                 $depends{$page}=$pagespec;
761         }
762         else {
763                 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
764         }
765 } # }}}
766
767 sub pagespec_match ($$) { #{{{
768         my $page=shift;
769         my $spec=shift;
770
771         return eval pagespec_translate($spec);
772 } #}}}
773
774 sub match_glob ($$) { #{{{
775         my $page=shift;
776         my $glob=shift;
777
778         # turn glob into safe regexp
779         $glob=quotemeta($glob);
780         $glob=~s/\\\*/.*/g;
781         $glob=~s/\\\?/./g;
782
783         return $page=~/^$glob$/i;
784 } #}}}
785
786 sub match_link ($$) { #{{{
787         my $page=shift;
788         my $link=lc(shift);
789
790         my $links = $links{$page} or return undef;
791         foreach my $p (@$links) {
792                 return 1 if lc $p eq $link;
793         }
794         return 0;
795 } #}}}
796
797 sub match_backlink ($$) { #{{{
798         match_link(pop, pop);
799 } #}}}
800
801 sub match_created_before ($$) { #{{{
802         my $page=shift;
803         my $testpage=shift;
804
805         if (exists $pagectime{$testpage}) {
806                 return $pagectime{$page} < $pagectime{$testpage};
807         }
808         else {
809                 return 0;
810         }
811 } #}}}
812
813 sub match_created_after ($$) { #{{{
814         my $page=shift;
815         my $testpage=shift;
816
817         if (exists $pagectime{$testpage}) {
818                 return $pagectime{$page} > $pagectime{$testpage};
819         }
820         else {
821                 return 0;
822         }
823 } #}}}
824
825 sub match_creation_day ($$) { #{{{
826         return ((gmtime($pagectime{shift()}))[3] == shift);
827 } #}}}
828
829 sub match_creation_month ($$) { #{{{
830         return ((gmtime($pagectime{shift()}))[4] + 1 == shift);
831 } #}}}
832
833 sub match_creation_year ($$) { #{{{
834         return ((gmtime($pagectime{shift()}))[5] + 1900 == shift);
835 } #}}}
836
837 1