]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki.pm
Fix support for --pingurl at the command line.
[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 sub will_render ($$;$) { #{{{
268         my $page=shift;
269         my $dest=shift;
270         my $clear=shift;
271
272         # Important security check.
273         if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
274             ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}})) {
275                 error("$config{destdir}/$dest independently created, not overwriting with version from $page");
276         }
277
278         if (! $clear) {
279                 $renderedfiles{$page}=[$dest, grep { $_ ne $dest } @{$renderedfiles{$page}}];
280         }
281         else {
282                 $renderedfiles{$page}=[$dest];
283         }
284 } #}}}
285
286 sub bestlink ($$) { #{{{
287         my $page=shift;
288         my $link=shift;
289         
290         my $cwd=$page;
291         do {
292                 my $l=$cwd;
293                 $l.="/" if length $l;
294                 $l.=$link;
295
296                 if (exists $links{$l}) {
297                         return $l;
298                 }
299                 elsif (exists $pagecase{lc $l}) {
300                         return $pagecase{lc $l};
301                 }
302         } while $cwd=~s!/?[^/]+$!!;
303
304         #print STDERR "warning: page $page, broken link: $link\n";
305         return "";
306 } #}}}
307
308 sub isinlinableimage ($) { #{{{
309         my $file=shift;
310         
311         $file=~/\.(png|gif|jpg|jpeg)$/i;
312 } #}}}
313
314 sub pagetitle ($) { #{{{
315         my $page=shift;
316         $page=~s/__(\d+)__/&#$1;/g;
317         $page=~y/_/ /;
318         return $page;
319 } #}}}
320
321 sub titlepage ($) { #{{{
322         my $title=shift;
323         $title=~y/ /_/;
324         $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
325         return $title;
326 } #}}}
327
328 sub cgiurl (@) { #{{{
329         my %params=@_;
330
331         return $config{cgiurl}."?".join("&amp;", map "$_=$params{$_}", keys %params);
332 } #}}}
333
334 sub baseurl (;$) { #{{{
335         my $page=shift;
336
337         return "$config{url}/" if ! defined $page;
338         
339         $page=~s/[^\/]+$//;
340         $page=~s/[^\/]+\//..\//g;
341         return $page;
342 } #}}}
343
344 sub abs2rel ($$) { #{{{
345         # Work around very innefficient behavior in File::Spec if abs2rel
346         # is passed two relative paths. It's much faster if paths are
347         # absolute! (Debian bug #376658)
348         my $path="/".shift;
349         my $base="/".shift;
350
351         require File::Spec;
352         my $ret=File::Spec->abs2rel($path, $base);
353         $ret=~s/^// if defined $ret;
354         return $ret;
355 } #}}}
356
357 sub displaytime ($) { #{{{
358         my $time=shift;
359
360         eval q{use POSIX};
361         # strftime doesn't know about encodings, so make sure
362         # its output is properly treated as utf8
363         return decode_utf8(POSIX::strftime(
364                         $config{timeformat}, localtime($time)));
365 } #}}}
366
367 sub htmllink ($$$;$$$) { #{{{
368         my $lpage=shift; # the page doing the linking
369         my $page=shift; # the page that will contain the link (different for inline)
370         my $link=shift;
371         my $noimageinline=shift; # don't turn links into inline html images
372         my $forcesubpage=shift; # force a link to a subpage
373         my $linktext=shift; # set to force the link text to something
374
375         my $bestlink;
376         if (! $forcesubpage) {
377                 $bestlink=bestlink($lpage, $link);
378         }
379         else {
380                 $bestlink="$lpage/".lc($link);
381         }
382
383         $linktext=pagetitle(basename($link)) unless defined $linktext;
384         
385         return "<span class=\"selflink\">$linktext</span>"
386                 if length $bestlink && $page eq $bestlink;
387         
388         # TODO BUG: %renderedfiles may not have it, if the linked to page
389         # was also added and isn't yet rendered! Note that this bug is
390         # masked by the bug that makes all new files be rendered twice.
391         if (! grep { $_ eq $bestlink } map { @{$_} } values %renderedfiles) {
392                 $bestlink=htmlpage($bestlink);
393         }
394         if (! grep { $_ eq $bestlink } map { @{$_} } values %renderedfiles) {
395                 return "<span><a href=\"".
396                         cgiurl(do => "create", page => lc($link), from => $page).
397                         "\">?</a>$linktext</span>"
398         }
399         
400         $bestlink=abs2rel($bestlink, dirname($page));
401         
402         if (! $noimageinline && isinlinableimage($bestlink)) {
403                 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
404         }
405         return "<a href=\"$bestlink\">$linktext</a>";
406 } #}}}
407
408 sub htmlize ($$$) { #{{{
409         my $page=shift;
410         my $type=shift;
411         my $content=shift;
412
413         if (exists $hooks{htmlize}{$type}) {
414                 $content=$hooks{htmlize}{$type}{call}->(
415                         page => $page,
416                         content => $content,
417                 );
418         }
419         else {
420                 error("htmlization of $type not supported");
421         }
422
423         run_hooks(sanitize => sub {
424                 $content=shift->(
425                         page => $page,
426                         content => $content,
427                 );
428         });
429
430         return $content;
431 } #}}}
432
433 sub linkify ($$$) { #{{{
434         my $lpage=shift; # the page containing the links
435         my $page=shift; # the page the link will end up on (different for inline)
436         my $content=shift;
437
438         $content =~ s{(\\?)$config{wiki_link_regexp}}{
439                 $2 ? ( $1 ? "[[$2|$3]]" : htmllink($lpage, $page, titlepage($3), 0, 0, pagetitle($2)))
440                    : ( $1 ? "[[$3]]" :    htmllink($lpage, $page, titlepage($3)))
441         }eg;
442         
443         return $content;
444 } #}}}
445
446 my %preprocessing;
447 sub preprocess ($$$) { #{{{
448         my $page=shift; # the page the data comes from
449         my $destpage=shift; # the page the data will appear in (different for inline)
450         my $content=shift;
451
452         my $handle=sub {
453                 my $escape=shift;
454                 my $command=shift;
455                 my $params=shift;
456                 if (length $escape) {
457                         return "[[$command $params]]";
458                 }
459                 elsif (exists $hooks{preprocess}{$command}) {
460                         # Note: preserve order of params, some plugins may
461                         # consider it significant.
462                         my @params;
463                         while ($params =~ /(?:(\w+)=)?(?:"""(.*?)"""|"([^"]+)"|(\S+))(?:\s+|$)/sg) {
464                                 my $key=$1;
465                                 my $val;
466                                 if (defined $2) {
467                                         $val=$2;
468                                         $val=~s/\r\n/\n/mg;
469                                         $val=~s/^\n+//g;
470                                         $val=~s/\n+$//g;
471                                 }
472                                 elsif (defined $3) {
473                                         $val=$3;
474                                 }
475                                 elsif (defined $4) {
476                                         $val=$4;
477                                 }
478
479                                 if (defined $key) {
480                                         push @params, $key, $val;
481                                 }
482                                 else {
483                                         push @params, $val, '';
484                                 }
485                         }
486                         if ($preprocessing{$page}++ > 3) {
487                                 # Avoid loops of preprocessed pages preprocessing
488                                 # other pages that preprocess them, etc.
489                                 return "[[$command preprocessing loop detected on $page at depth $preprocessing{$page}]]";
490                         }
491                         my $ret=$hooks{preprocess}{$command}{call}->(
492                                 @params,
493                                 page => $page,
494                                 destpage => $destpage,
495                         );
496                         $preprocessing{$page}--;
497                         return $ret;
498                 }
499                 else {
500                         return "[[$command $params]]";
501                 }
502         };
503         
504         $content =~ s{(\\?)\[\[(\w+)\s+((?:(?:\w+=)?(?:""".*?"""|"[^"]+"|[^\s\]]+)\s*)*)\]\]}{$handle->($1, $2, $3)}seg;
505         return $content;
506 } #}}}
507
508 sub filter ($$) {
509         my $page=shift;
510         my $content=shift;
511
512         run_hooks(filter => sub {
513                 $content=shift->(page => $page, content => $content);
514         });
515
516         return $content;
517 }
518
519 sub indexlink () { #{{{
520         return "<a href=\"$config{url}\">$config{wikiname}</a>";
521 } #}}}
522
523 sub lockwiki () { #{{{
524         # Take an exclusive lock on the wiki to prevent multiple concurrent
525         # run issues. The lock will be dropped on program exit.
526         if (! -d $config{wikistatedir}) {
527                 mkdir($config{wikistatedir});
528         }
529         open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
530                 error ("cannot write to $config{wikistatedir}/lockfile: $!");
531         if (! flock(WIKILOCK, 2 | 4)) {
532                 debug("wiki seems to be locked, waiting for lock");
533                 my $wait=600; # arbitrary, but don't hang forever to 
534                               # prevent process pileup
535                 for (1..600) {
536                         return if flock(WIKILOCK, 2 | 4);
537                         sleep 1;
538                 }
539                 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
540         }
541 } #}}}
542
543 sub unlockwiki () { #{{{
544         close WIKILOCK;
545 } #}}}
546
547 sub loadindex () { #{{{
548         open (IN, "$config{wikistatedir}/index") || return;
549         while (<IN>) {
550                 $_=possibly_foolish_untaint($_);
551                 chomp;
552                 my %items;
553                 $items{link}=[];
554                 $items{dest}=[];
555                 foreach my $i (split(/ /, $_)) {
556                         my ($item, $val)=split(/=/, $i, 2);
557                         push @{$items{$item}}, decode_entities($val);
558                 }
559
560                 next unless exists $items{src}; # skip bad lines for now
561
562                 my $page=pagename($items{src}[0]);
563                 if (! $config{rebuild}) {
564                         $pagesources{$page}=$items{src}[0];
565                         $oldpagemtime{$page}=$items{mtime}[0];
566                         $oldlinks{$page}=[@{$items{link}}];
567                         $links{$page}=[@{$items{link}}];
568                         $depends{$page}=$items{depends}[0] if exists $items{depends};
569                         $renderedfiles{$page}=[@{$items{dest}}];
570                         $oldrenderedfiles{$page}=[@{$items{dest}}];
571                         $pagecase{lc $page}=$page;
572                 }
573                 $pagectime{$page}=$items{ctime}[0];
574         }
575         close IN;
576 } #}}}
577
578 sub saveindex () { #{{{
579         run_hooks(savestate => sub { shift->() });
580
581         if (! -d $config{wikistatedir}) {
582                 mkdir($config{wikistatedir});
583         }
584         open (OUT, ">$config{wikistatedir}/index") || 
585                 error("cannot write to $config{wikistatedir}/index: $!");
586         foreach my $page (keys %oldpagemtime) {
587                 next unless $oldpagemtime{$page};
588                 my $line="mtime=$oldpagemtime{$page} ".
589                         "ctime=$pagectime{$page} ".
590                         "src=$pagesources{$page}";
591                 $line.=" dest=$_" foreach @{$renderedfiles{$page}};
592                 $line.=" link=$_" foreach @{$links{$page}};
593                 if (exists $depends{$page}) {
594                         $line.=" depends=".encode_entities($depends{$page}, " \t\n");
595                 }
596                 print OUT $line."\n";
597         }
598         close OUT;
599 } #}}}
600
601 sub template_params (@) { #{{{
602         my $filename=shift;
603         
604         require HTML::Template;
605         return filter => sub {
606                         my $text_ref = shift;
607                         $$text_ref=&Encode::decode_utf8($$text_ref);
608                 },
609                 filename => "$config{templatedir}/$filename",
610                 loop_context_vars => 1,
611                 die_on_bad_params => 0,
612                 @_;
613 } #}}}
614
615 sub template ($;@) { #{{{
616         HTML::Template->new(template_params(@_));
617 } #}}}
618
619 sub misctemplate ($$;@) { #{{{
620         my $title=shift;
621         my $pagebody=shift;
622         
623         my $template=template("misc.tmpl");
624         $template->param(
625                 title => $title,
626                 indexlink => indexlink(),
627                 wikiname => $config{wikiname},
628                 pagebody => $pagebody,
629                 baseurl => baseurl(),
630                 @_,
631         );
632         run_hooks(pagetemplate => sub {
633                 shift->(page => "", destpage => "", template => $template);
634         });
635         return $template->output;
636 }#}}}
637
638 sub hook (@) { # {{{
639         my %param=@_;
640         
641         if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
642                 error "hook requires type, call, and id parameters";
643         }
644
645         return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
646         
647         $hooks{$param{type}}{$param{id}}=\%param;
648 } # }}}
649
650 sub run_hooks ($$) { # {{{
651         # Calls the given sub for each hook of the given type,
652         # passing it the hook function to call.
653         my $type=shift;
654         my $sub=shift;
655
656         if (exists $hooks{$type}) {
657                 foreach my $id (keys %{$hooks{$type}}) {
658                         $sub->($hooks{$type}{$id}{call});
659                 }
660         }
661 } #}}}
662
663 sub globlist_to_pagespec ($) { #{{{
664         my @globlist=split(' ', shift);
665
666         my (@spec, @skip);
667         foreach my $glob (@globlist) {
668                 if ($glob=~/^!(.*)/) {
669                         push @skip, $glob;
670                 }
671                 else {
672                         push @spec, $glob;
673                 }
674         }
675
676         my $spec=join(" or ", @spec);
677         if (@skip) {
678                 my $skip=join(" and ", @skip);
679                 if (length $spec) {
680                         $spec="$skip and ($spec)";
681                 }
682                 else {
683                         $spec=$skip;
684                 }
685         }
686         return $spec;
687 } #}}}
688
689 sub is_globlist ($) { #{{{
690         my $s=shift;
691         $s=~/[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or";
692 } #}}}
693
694 sub safequote ($) { #{{{
695         my $s=shift;
696         $s=~s/[{}]//g;
697         return "q{$s}";
698 } #}}}
699
700 sub pagespec_merge ($$) { #{{{
701         my $a=shift;
702         my $b=shift;
703
704         return $a if $a eq $b;
705
706         # Support for old-style GlobLists.
707         if (is_globlist($a)) {
708                 $a=globlist_to_pagespec($a);
709         }
710         if (is_globlist($b)) {
711                 $b=globlist_to_pagespec($b);
712         }
713
714         return "($a) or ($b)";
715 } #}}}
716
717 sub pagespec_translate ($) { #{{{
718         # This assumes that $page is in scope in the function
719         # that evalulates the translated pagespec code.
720         my $spec=shift;
721
722         # Support for old-style GlobLists.
723         if (is_globlist($spec)) {
724                 $spec=globlist_to_pagespec($spec);
725         }
726
727         # Convert spec to perl code.
728         my $code="";
729         while ($spec=~m/\s*(\!|\(|\)|\w+\([^\)]+\)|[^\s()]+)\s*/ig) {
730                 my $word=$1;
731                 if (lc $word eq "and") {
732                         $code.=" &&";
733                 }
734                 elsif (lc $word eq "or") {
735                         $code.=" ||";
736                 }
737                 elsif ($word eq "(" || $word eq ")" || $word eq "!") {
738                         $code.=" ".$word;
739                 }
740                 elsif ($word =~ /^(link|backlink|created_before|created_after|creation_month|creation_year|creation_day)\((.+)\)$/) {
741                         $code.=" match_$1(\$page, ".safequote($2).")";
742                 }
743                 else {
744                         $code.=" match_glob(\$page, ".safequote($word).")";
745                 }
746         }
747
748         return $code;
749 } #}}}
750
751 sub add_depends ($$) { #{{{
752         my $page=shift;
753         my $pagespec=shift;
754         
755         if (! exists $depends{$page}) {
756                 $depends{$page}=$pagespec;
757         }
758         else {
759                 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
760         }
761 } # }}}
762
763 sub pagespec_match ($$) { #{{{
764         my $page=shift;
765         my $spec=shift;
766
767         return eval pagespec_translate($spec);
768 } #}}}
769
770 sub match_glob ($$) { #{{{
771         my $page=shift;
772         my $glob=shift;
773
774         # turn glob into safe regexp
775         $glob=quotemeta($glob);
776         $glob=~s/\\\*/.*/g;
777         $glob=~s/\\\?/./g;
778
779         return $page=~/^$glob$/i;
780 } #}}}
781
782 sub match_link ($$) { #{{{
783         my $page=shift;
784         my $link=lc(shift);
785
786         my $links = $links{$page} or return undef;
787         foreach my $p (@$links) {
788                 return 1 if lc $p eq $link;
789         }
790         return 0;
791 } #}}}
792
793 sub match_backlink ($$) { #{{{
794         match_link(pop, pop);
795 } #}}}
796
797 sub match_created_before ($$) { #{{{
798         my $page=shift;
799         my $testpage=shift;
800
801         if (exists $pagectime{$testpage}) {
802                 return $pagectime{$page} < $pagectime{$testpage};
803         }
804         else {
805                 return 0;
806         }
807 } #}}}
808
809 sub match_created_after ($$) { #{{{
810         my $page=shift;
811         my $testpage=shift;
812
813         if (exists $pagectime{$testpage}) {
814                 return $pagectime{$page} > $pagectime{$testpage};
815         }
816         else {
817                 return 0;
818         }
819 } #}}}
820
821 sub match_creation_day ($$) { #{{{
822         return ((gmtime($pagectime{shift()}))[3] == shift);
823 } #}}}
824
825 sub match_creation_month ($$) { #{{{
826         return ((gmtime($pagectime{shift()}))[4] + 1 == shift);
827 } #}}}
828
829 sub match_creation_year ($$) { #{{{
830         return ((gmtime($pagectime{shift()}))[5] + 1900 == shift);
831 } #}}}
832
833 1