8 use URI::Escape q{uri_escape_utf8};
10 use open qw{:utf8 :std};
12 use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
13 %renderedfiles %oldrenderedfiles %pagesources %destsources
14 %depends %hooks %forcerebuild $gettext_obj};
16 use Exporter q{import};
17 our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
18 bestlink htmllink readfile writefile pagetype srcfile pagename
19 displaytime will_render gettext urlto targetpage
20 %config %links %renderedfiles %pagesources %destsources);
21 our $VERSION = 2.00; # plugin interface version, next is ikiwiki version
22 our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
23 my $installdir=''; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
28 memoize("pagespec_translate");
29 memoize("file_pruned");
31 sub defaultconfig () { #{{{
33 wiki_file_prune_regexps => [qr/\.\./, qr/^\./, qr/\/\./,
34 qr/\.x?html?$/, qr/\.ikiwiki-new$/,
35 qr/(^|\/).svn\//, qr/.arch-ids\//, qr/{arch}\//,
37 wiki_link_regexp => qr{
38 \[\[ # beginning of link
40 ([^\]\|]+) # 1: link text
44 ([^\s\]#]+) # 2: page to link to
46 \# # '#', beginning of anchor
47 ([^\s\]]+) # 3: anchor text
52 wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
53 web_commit_regexp => qr/^web commit (by (.*?(?=: |$))|from (\d+\.\d+\.\d+\.\d+)):?(.*)/,
57 default_pageext => "mdwn",
78 gitorigin_branch => "origin",
79 gitmaster_branch => "master",
83 templatedir => "$installdir/share/ikiwiki/templates",
84 underlaydir => "$installdir/share/ikiwiki/basewiki",
88 plugin => [qw{mdwn inline htmlscrubber passwordauth openid signinedit
89 lockedit conditional}],
98 account_creation_password => "",
101 sub checkconfig () { #{{{
102 # locale stuff; avoid LC_ALL since it overrides everything
103 if (defined $ENV{LC_ALL}) {
104 $ENV{LANG} = $ENV{LC_ALL};
107 if (defined $config{locale}) {
108 if (POSIX::setlocale(&POSIX::LC_ALL, $config{locale})) {
109 $ENV{LANG}=$config{locale};
114 if ($config{w3mmode}) {
115 eval q{use Cwd q{abs_path}};
117 $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir}));
118 $config{destdir}=possibly_foolish_untaint(abs_path($config{destdir}));
119 $config{cgiurl}="file:///\$LIB/ikiwiki-w3m.cgi/".$config{cgiurl}
120 unless $config{cgiurl} =~ m!file:///!;
121 $config{url}="file://".$config{destdir};
124 if ($config{cgi} && ! length $config{url}) {
125 error(gettext("Must specify url to wiki with --url when using --cgi"));
128 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
129 unless exists $config{wikistatedir};
132 eval qq{use IkiWiki::Rcs::$config{rcs}};
134 error("Failed to load RCS module IkiWiki::Rcs::$config{rcs}: $@");
138 require IkiWiki::Rcs::Stub;
141 run_hooks(checkconfig => sub { shift->() });
146 sub loadplugins () { #{{{
147 if (defined $config{libdir}) {
148 unshift @INC, $config{libdir};
151 loadplugin($_) foreach @{$config{plugin}};
153 run_hooks(getopt => sub { shift->() });
154 if (grep /^-/, @ARGV) {
155 print STDERR "Unknown option: $_\n"
156 foreach grep /^-/, @ARGV;
163 sub loadplugin ($) { #{{{
166 return if grep { $_ eq $plugin} @{$config{disable_plugins}};
168 foreach my $dir ($config{libdir}, "$installdir/lib/ikiwiki") {
169 if (defined $dir && -x "$dir/plugins/$plugin") {
170 require IkiWiki::Plugin::external;
171 import IkiWiki::Plugin::external "$dir/plugins/$plugin";
176 my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
179 error("Failed to load plugin $mod: $@");
184 sub error ($;$) { #{{{
188 print "Content-type: text/html\n\n";
189 print misctemplate(gettext("Error"),
190 "<p>".gettext("Error").": $message</p>");
192 log_message('err' => $message) if $config{syslog};
193 if (defined $cleaner) {
200 return unless $config{verbose};
201 return log_message(debug => @_);
205 sub log_message ($$) { #{{{
208 if ($config{syslog}) {
211 Sys::Syslog::setlogsock('unix');
212 Sys::Syslog::openlog('ikiwiki', '', 'user');
216 Sys::Syslog::syslog($type, "[$config{wikiname}] %s", join(" ", @_));
219 elsif (! $config{cgi}) {
223 return print STDERR "@_\n";
227 sub possibly_foolish_untaint ($) { #{{{
229 my ($untainted)=$tainted=~/(.*)/s;
233 sub basename ($) { #{{{
240 sub dirname ($) { #{{{
247 sub pagetype ($) { #{{{
250 if ($page =~ /\.([^.]+)$/) {
251 return $1 if exists $hooks{htmlize}{$1};
256 sub pagename ($) { #{{{
259 my $type=pagetype($file);
261 $page=~s/\Q.$type\E*$// if defined $type;
265 sub targetpage ($$) { #{{{
269 if (! $config{usedirs} || $page =~ /^index$/ ) {
270 return $page.".".$ext;
272 return $page."/index.".$ext;
276 sub htmlpage ($) { #{{{
279 return targetpage($page, $config{htmlext});
282 sub srcfile ($) { #{{{
285 return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
286 return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
287 error("internal error: $file cannot be found in $config{srcdir} or $config{underlaydir}");
291 sub readfile ($;$$) { #{{{
297 error("cannot read a symlink ($file)");
301 open (my $in, "<", $file) || error("failed to read $file: $!");
302 binmode($in) if ($binary);
303 return \*$in if $wantfd;
305 close $in || error("failed to read $file: $!");
309 sub writefile ($$$;$$) { #{{{
310 my $file=shift; # can include subdirs
311 my $destdir=shift; # directory to put file in
317 while (length $test) {
318 if (-l "$destdir/$test") {
319 error("cannot write to a symlink ($test)");
321 $test=dirname($test);
323 my $newfile="$destdir/$file.ikiwiki-new";
325 error("cannot write to a symlink ($newfile)");
328 my $dir=dirname($newfile);
331 foreach my $s (split(m!/+!, $dir)) {
334 mkdir($d) || error("failed to create directory $d: $!");
339 my $cleanup = sub { unlink($newfile) };
340 open (my $out, '>', $newfile) || error("failed to write $newfile: $!", $cleanup);
341 binmode($out) if ($binary);
343 $writer->(\*$out, $cleanup);
346 print $out $content or error("failed writing to $newfile: $!", $cleanup);
348 close $out || error("failed saving $newfile: $!", $cleanup);
349 rename($newfile, "$destdir/$file") ||
350 error("failed renaming $newfile to $destdir/$file: $!", $cleanup);
356 sub will_render ($$;$) { #{{{
361 # Important security check.
362 if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
363 ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}})) {
364 error("$config{destdir}/$dest independently created, not overwriting with version from $page");
367 if (! $clear || $cleared{$page}) {
368 $renderedfiles{$page}=[$dest, grep { $_ ne $dest } @{$renderedfiles{$page}}];
371 foreach my $old (@{$renderedfiles{$page}}) {
372 delete $destsources{$old};
374 $renderedfiles{$page}=[$dest];
377 $destsources{$dest}=$page;
382 sub bestlink ($$) { #{{{
387 if ($link=~s/^\/+//) {
394 $l.="/" if length $l;
397 if (exists $links{$l}) {
400 elsif (exists $pagecase{lc $l}) {
401 return $pagecase{lc $l};
403 } while $cwd=~s!/?[^/]+$!!;
405 if (length $config{userdir}) {
406 my $l = "$config{userdir}/".lc($link);
407 if (exists $links{$l}) {
410 elsif (exists $pagecase{lc $l}) {
411 return $pagecase{lc $l};
415 #print STDERR "warning: page $page, broken link: $link\n";
419 sub isinlinableimage ($) { #{{{
422 return $file =~ /\.(png|gif|jpg|jpeg)$/i;
425 sub pagetitle ($;$) { #{{{
430 $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : chr($2)/eg;
433 $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : "&#$2;"/eg;
439 sub titlepage ($) { #{{{
441 $title=~s/([^-[:alnum:]:+\/.])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
445 sub linkpage ($) { #{{{
447 $link=~s/([^-[:alnum:]:+\/._])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
451 sub cgiurl (@) { #{{{
454 return $config{cgiurl}."?".
455 join("&", map $_."=".uri_escape_utf8($params{$_}), keys %params);
458 sub baseurl (;$) { #{{{
461 return "$config{url}/" if ! defined $page;
463 $page=htmlpage($page);
465 $page=~s/[^\/]+\//..\//g;
469 sub abs2rel ($$) { #{{{
470 # Work around very innefficient behavior in File::Spec if abs2rel
471 # is passed two relative paths. It's much faster if paths are
472 # absolute! (Debian bug #376658; fixed in debian unstable now)
477 my $ret=File::Spec->abs2rel($path, $base);
478 $ret=~s/^// if defined $ret;
482 sub displaytime ($) { #{{{
485 # strftime doesn't know about encodings, so make sure
486 # its output is properly treated as utf8
487 return decode_utf8(POSIX::strftime(
488 $config{timeformat}, localtime($time)));
491 sub beautify_url ($) { #{{{
494 $url =~ s!/index.$config{htmlext}$!/!;
495 $url =~ s!^$!./!; # Browsers don't like empty links...
500 sub urlto ($$) { #{{{
505 return beautify_url(baseurl($from));
508 if (! $destsources{$to}) {
512 my $link = abs2rel($to, dirname(htmlpage($from)));
514 return beautify_url($link);
517 sub htmllink ($$$;@) { #{{{
518 my $lpage=shift; # the page doing the linking
519 my $page=shift; # the page that will contain the link (different for inline)
524 if (! $opts{forcesubpage}) {
525 $bestlink=bestlink($lpage, $link);
528 $bestlink="$lpage/".lc($link);
532 if (defined $opts{linktext}) {
533 $linktext=$opts{linktext};
536 $linktext=pagetitle(basename($link));
539 return "<span class=\"selflink\">$linktext</span>"
540 if length $bestlink && $page eq $bestlink;
542 if (! $destsources{$bestlink}) {
543 $bestlink=htmlpage($bestlink);
545 if (! $destsources{$bestlink}) {
546 return $linktext unless length $config{cgiurl};
547 return "<span><a href=\"".
550 page => pagetitle(lc($link), 1),
553 "\">?</a>$linktext</span>"
557 $bestlink=abs2rel($bestlink, dirname(htmlpage($page)));
558 $bestlink=beautify_url($bestlink);
560 if (! $opts{noimageinline} && isinlinableimage($bestlink)) {
561 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
564 if (defined $opts{anchor}) {
565 $bestlink.="#".$opts{anchor};
569 if (defined $opts{rel}) {
570 push @attrs, ' rel="'.$opts{rel}.'"';
573 return "<a href=\"$bestlink\"@attrs>$linktext</a>";
576 sub htmlize ($$$) { #{{{
581 if (exists $hooks{htmlize}{$type}) {
582 $content=$hooks{htmlize}{$type}{call}->(
588 error("htmlization of $type not supported");
591 run_hooks(sanitize => sub {
601 sub linkify ($$$) { #{{{
602 my $lpage=shift; # the page containing the links
603 my $page=shift; # the page the link will end up on (different for inline)
606 $content =~ s{(\\?)$config{wiki_link_regexp}}{
609 ? "[[$2|$3".($4 ? "#$4" : "")."]]"
610 : htmllink($lpage, $page, linkpage($3),
611 anchor => $4, linktext => pagetitle($2)))
613 ? "[[$3".($4 ? "#$4" : "")."]]"
614 : htmllink($lpage, $page, linkpage($3),
622 our $preprocess_preview=0;
623 sub preprocess ($$$;$$) { #{{{
624 my $page=shift; # the page the data comes from
625 my $destpage=shift; # the page the data will appear in (different for inline)
630 # Using local because it needs to be set within any nested calls
632 local $preprocess_preview=$preview if defined $preview;
638 if (length $escape) {
639 return "[[$command $params]]";
641 elsif (exists $hooks{preprocess}{$command}) {
642 return "" if $scan && ! $hooks{preprocess}{$command}{scan};
643 # Note: preserve order of params, some plugins may
644 # consider it significant.
647 (?:(\w+)=)? # 1: named parameter key?
649 """(.*?)""" # 2: triple-quoted value
651 "([^"]+)" # 3: single-quoted value
653 (\S+) # 4: unquoted value
655 (?:\s+|$) # delimiter to next param
673 push @params, $key, $val;
676 push @params, $val, '';
679 if ($preprocessing{$page}++ > 3) {
680 # Avoid loops of preprocessed pages preprocessing
681 # other pages that preprocess them, etc.
682 #translators: The first parameter is a
683 #translators: preprocessor directive name,
684 #translators: the second a page name, the
685 #translators: third a number.
686 return "[[".sprintf(gettext("%s preprocessing loop detected on %s at depth %i"),
687 $command, $page, $preprocessing{$page}).
690 my $ret=$hooks{preprocess}{$command}{call}->(
693 destpage => $destpage,
694 preview => $preprocess_preview,
696 $preprocessing{$page}--;
700 return "[[$command $params]]";
706 \[\[ # directive open
709 ( # 3: the parameters..
711 (?:\w+=)? # named parameter key?
713 """.*?""" # triple-quoted value
715 "[^"]+" # single-quoted value
717 [^\s\]]+ # unquoted value
719 \s* # whitespace or end
722 *) # 0 or more parameters
723 \]\] # directive closed
724 }{$handle->($1, $2, $3)}sexg;
728 sub filter ($$$) { #{{{
733 run_hooks(filter => sub {
734 $content=shift->(page => $page, destpage => $destpage,
735 content => $content);
741 sub indexlink () { #{{{
742 return "<a href=\"$config{url}\">$config{wikiname}</a>";
747 sub lockwiki (;$) { #{{{
748 my $wait=@_ ? shift : 1;
749 # Take an exclusive lock on the wiki to prevent multiple concurrent
750 # run issues. The lock will be dropped on program exit.
751 if (! -d $config{wikistatedir}) {
752 mkdir($config{wikistatedir});
754 open($wikilock, '>', "$config{wikistatedir}/lockfile") ||
755 error ("cannot write to $config{wikistatedir}/lockfile: $!");
756 if (! flock($wikilock, 2 | 4)) { # LOCK_EX | LOCK_NB
758 debug("wiki seems to be locked, waiting for lock");
759 my $wait=600; # arbitrary, but don't hang forever to
760 # prevent process pileup
762 return if flock($wikilock, 2 | 4);
765 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
774 sub unlockwiki () { #{{{
775 return close($wikilock);
780 sub commit_hook_enabled () { #{{{
781 open($commitlock, '+>', "$config{wikistatedir}/commitlock") ||
782 error("cannot write to $config{wikistatedir}/commitlock: $!");
783 if (! flock($commitlock, 1 | 4)) { # LOCK_SH | LOCK_NB to test
784 close($commitlock) || error("failed closing commitlock: $!");
787 close($commitlock) || error("failed closing commitlock: $!");
791 sub disable_commit_hook () { #{{{
792 open($commitlock, '>', "$config{wikistatedir}/commitlock") ||
793 error("cannot write to $config{wikistatedir}/commitlock: $!");
794 if (! flock($commitlock, 2)) { # LOCK_EX
795 error("failed to get commit lock");
800 sub enable_commit_hook () { #{{{
801 return close($commitlock);
804 sub loadindex () { #{{{
805 open (my $in, "<", "$config{wikistatedir}/index") || return;
807 $_=possibly_foolish_untaint($_);
812 foreach my $i (split(/ /, $_)) {
813 my ($item, $val)=split(/=/, $i, 2);
814 push @{$items{$item}}, decode_entities($val);
817 next unless exists $items{src}; # skip bad lines for now
819 my $page=pagename($items{src}[0]);
820 if (! $config{rebuild}) {
821 $pagesources{$page}=$items{src}[0];
822 $pagemtime{$page}=$items{mtime}[0];
823 $oldlinks{$page}=[@{$items{link}}];
824 $links{$page}=[@{$items{link}}];
825 $depends{$page}=$items{depends}[0] if exists $items{depends};
826 $destsources{$_}=$page foreach @{$items{dest}};
827 $renderedfiles{$page}=[@{$items{dest}}];
828 $pagecase{lc $page}=$page;
830 $oldrenderedfiles{$page}=[@{$items{dest}}];
831 $pagectime{$page}=$items{ctime}[0];
836 sub saveindex () { #{{{
837 run_hooks(savestate => sub { shift->() });
839 if (! -d $config{wikistatedir}) {
840 mkdir($config{wikistatedir});
842 my $newfile="$config{wikistatedir}/index.new";
843 my $cleanup = sub { unlink($newfile) };
844 open (my $out, '>', $newfile) || error("cannot write to $newfile: $!", $cleanup);
845 foreach my $page (keys %pagemtime) {
846 next unless $pagemtime{$page};
847 my $line="mtime=$pagemtime{$page} ".
848 "ctime=$pagectime{$page} ".
849 "src=$pagesources{$page}";
850 $line.=" dest=$_" foreach @{$renderedfiles{$page}};
852 $line.=" link=$_" foreach grep { ++$count{$_} == 1 } @{$links{$page}};
853 if (exists $depends{$page}) {
854 $line.=" depends=".encode_entities($depends{$page}, " \t\n");
856 print $out $line."\n" || error("failed writing to $newfile: $!", $cleanup);
858 close $out || error("failed saving to $newfile: $!", $cleanup);
859 rename($newfile, "$config{wikistatedir}/index") ||
860 error("failed renaming $newfile to $config{wikistatedir}/index", $cleanup);
865 sub template_file ($) { #{{{
868 foreach my $dir ($config{templatedir}, "$installdir/share/ikiwiki/templates") {
869 return "$dir/$template" if -e "$dir/$template";
874 sub template_params (@) { #{{{
875 my $filename=template_file(shift);
877 if (! defined $filename) {
884 my $text_ref = shift;
885 ${$text_ref} = Encode::decode_utf8(${$text_ref});
887 filename => $filename,
888 loop_context_vars => 1,
889 die_on_bad_params => 0,
892 return wantarray ? @ret : {@ret};
895 sub template ($;@) { #{{{
896 require HTML::Template;
897 return HTML::Template->new(template_params(@_));
900 sub misctemplate ($$;@) { #{{{
904 my $template=template("misc.tmpl");
907 indexlink => indexlink(),
908 wikiname => $config{wikiname},
909 pagebody => $pagebody,
910 baseurl => baseurl(),
913 run_hooks(pagetemplate => sub {
914 shift->(page => "", destpage => "", template => $template);
916 return $template->output;
922 if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
923 error 'hook requires type, call, and id parameters';
926 return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
928 $hooks{$param{type}}{$param{id}}=\%param;
932 sub run_hooks ($$) { # {{{
933 # Calls the given sub for each hook of the given type,
934 # passing it the hook function to call.
938 if (exists $hooks{$type}) {
940 foreach my $id (keys %{$hooks{$type}}) {
941 if ($hooks{$type}{$id}{last}) {
945 $sub->($hooks{$type}{$id}{call});
947 foreach my $id (@deferred) {
948 $sub->($hooks{$type}{$id}{call});
955 sub globlist_to_pagespec ($) { #{{{
956 my @globlist=split(' ', shift);
959 foreach my $glob (@globlist) {
960 if ($glob=~/^!(.*)/) {
968 my $spec=join(' or ', @spec);
970 my $skip=join(' and ', @skip);
972 $spec="$skip and ($spec)";
981 sub is_globlist ($) { #{{{
983 return ( $s =~ /[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or" );
986 sub safequote ($) { #{{{
992 sub add_depends ($$) { #{{{
996 if (! exists $depends{$page}) {
997 $depends{$page}=$pagespec;
1000 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
1006 sub file_pruned ($$) { #{{{
1008 my $file=File::Spec->canonpath(shift);
1009 my $base=File::Spec->canonpath(shift);
1010 $file =~ s#^\Q$base\E/*##;
1012 my $regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
1013 return $file =~ m/$regexp/;
1017 # Only use gettext in the rare cases it's needed.
1018 if (exists $ENV{LANG} || exists $ENV{LC_ALL} || exists $ENV{LC_MESSAGES}) {
1019 if (! $gettext_obj) {
1020 $gettext_obj=eval q{
1021 use Locale::gettext q{textdomain};
1022 Locale::gettext->domain('ikiwiki')
1030 return $gettext_obj->get(shift);
1037 sub pagespec_merge ($$) { #{{{
1041 return $a if $a eq $b;
1043 # Support for old-style GlobLists.
1044 if (is_globlist($a)) {
1045 $a=globlist_to_pagespec($a);
1047 if (is_globlist($b)) {
1048 $b=globlist_to_pagespec($b);
1051 return "($a) or ($b)";
1054 sub pagespec_translate ($) { #{{{
1055 # This assumes that $page is in scope in the function
1056 # that evalulates the translated pagespec code.
1059 # Support for old-style GlobLists.
1060 if (is_globlist($spec)) {
1061 $spec=globlist_to_pagespec($spec);
1064 # Convert spec to perl code.
1067 \s* # ignore whitespace
1068 ( # 1: match a single word
1075 \w+\([^\)]*\) # command(params)
1077 [^\s()]+ # any other text
1079 \s* # ignore whitespace
1082 if (lc $word eq 'and') {
1085 elsif (lc $word eq 'or') {
1088 elsif ($word eq "(" || $word eq ")" || $word eq "!") {
1091 elsif ($word =~ /^(\w+)\((.*)\)$/) {
1092 if (exists $IkiWiki::PageSpec::{"match_$1"}) {
1093 $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \@params)";
1100 $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \@params)";
1107 sub pagespec_match ($$;@) { #{{{
1112 # Backwards compatability with old calling convention.
1114 unshift @params, 'location';
1117 my $ret=eval pagespec_translate($spec);
1118 return IkiWiki::FailReason->new('syntax error') if $@;
1122 package IkiWiki::FailReason;
1125 '""' => sub { ${$_[0]} },
1127 '!' => sub { bless $_[0], 'IkiWiki::SuccessReason'},
1132 return bless \$_[1], $_[0];
1135 package IkiWiki::SuccessReason;
1138 '""' => sub { ${$_[0]} },
1140 '!' => sub { bless $_[0], 'IkiWiki::FailReason'},
1145 return bless \$_[1], $_[0];
1148 package IkiWiki::PageSpec;
1150 sub match_glob ($$;@) { #{{{
1155 my $from=exists $params{location} ? $params{location} : '';
1158 if ($glob =~ m!^\./!) {
1159 $from=~s#/?[^/]+$##;
1161 $glob="$from/$glob" if length $from;
1164 # turn glob into safe regexp
1165 $glob=quotemeta($glob);
1169 if ($page=~/^$glob$/i) {
1170 return IkiWiki::SuccessReason->new("$glob matches $page");
1173 return IkiWiki::FailReason->new("$glob does not match $page");
1177 sub match_link ($$;@) { #{{{
1182 my $from=exists $params{location} ? $params{location} : '';
1185 if ($link =~ m!^\.! && defined $from) {
1186 $from=~s#/?[^/]+$##;
1188 $link="$from/$link" if length $from;
1191 my $links = $IkiWiki::links{$page};
1192 return IkiWiki::FailReason->new("$page has no links") unless $links && @{$links};
1193 my $bestlink = IkiWiki::bestlink($from, $link);
1194 foreach my $p (@{$links}) {
1195 if (length $bestlink) {
1196 return IkiWiki::SuccessReason->new("$page links to $link")
1197 if $bestlink eq IkiWiki::bestlink($page, $p);
1200 return IkiWiki::SuccessReason->new("$page links to page matching $link")
1201 if match_glob($p, $link, %params);
1204 return IkiWiki::FailReason->new("$page does not link to $link");
1207 sub match_backlink ($$;@) { #{{{
1208 return match_link($_[1], $_[0], @_);
1211 sub match_created_before ($$;@) { #{{{
1215 if (exists $IkiWiki::pagectime{$testpage}) {
1216 if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
1217 return IkiWiki::SuccessReason->new("$page created before $testpage");
1220 return IkiWiki::FailReason->new("$page not created before $testpage");
1224 return IkiWiki::FailReason->new("$testpage has no ctime");
1228 sub match_created_after ($$;@) { #{{{
1232 if (exists $IkiWiki::pagectime{$testpage}) {
1233 if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
1234 return IkiWiki::SuccessReason->new("$page created after $testpage");
1237 return IkiWiki::FailReason->new("$page not created after $testpage");
1241 return IkiWiki::FailReason->new("$testpage has no ctime");
1245 sub match_creation_day ($$;@) { #{{{
1246 if ((gmtime($IkiWiki::pagectime{shift()}))[3] == shift) {
1247 return IkiWiki::SuccessReason->new('creation_day matched');
1250 return IkiWiki::FailReason->new('creation_day did not match');
1254 sub match_creation_month ($$;@) { #{{{
1255 if ((gmtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
1256 return IkiWiki::SuccessReason->new('creation_month matched');
1259 return IkiWiki::FailReason->new('creation_month did not match');
1263 sub match_creation_year ($$;@) { #{{{
1264 if ((gmtime($IkiWiki::pagectime{shift()}))[5] + 1900 == shift) {
1265 return IkiWiki::SuccessReason->new('creation_year matched');
1268 return IkiWiki::FailReason->new('creation_year did not match');
1272 sub match_user ($$;@) { #{{{
1277 return IkiWiki::FailReason->new('cannot match user')
1278 unless exists $params{user};
1279 if ($user eq $params{user}) {
1280 return IkiWiki::SuccessReason->new("user is $user")
1283 return IkiWiki::FailReason->new("user is not $user");