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