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