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 () { #{{{
32 wiki_file_prune_regexps => [qr/\.\./, qr/^\./, qr/\/\./,
33 qr/\.x?html?$/, qr/\.ikiwiki-new$/,
34 qr/(^|\/).svn\//, qr/.arch-ids\//, qr/{arch}\//,
36 wiki_link_regexp => qr{
37 \[\[ # beginning of link
39 ([^\]\|]+) # 1: link text
43 ([^\s\]#]+) # 2: page to link to
45 \# # '#', beginning of anchor
46 ([^\s\]]+) # 3: anchor text
51 wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
52 web_commit_regexp => qr/^web commit (by (.*?(?=: |$))|from (\d+\.\d+\.\d+\.\d+)):?(.*)/,
56 default_pageext => "mdwn",
77 gitorigin_branch => "origin",
78 gitmaster_branch => "master",
82 templatedir => "$installdir/share/ikiwiki/templates",
83 underlaydir => "$installdir/share/ikiwiki/basewiki",
87 plugin => [qw{mdwn inline htmlscrubber passwordauth openid signinedit
88 lockedit conditional}],
97 account_creation_password => "",
100 sub checkconfig () { #{{{
101 # locale stuff; avoid LC_ALL since it overrides everything
102 if (defined $ENV{LC_ALL}) {
103 $ENV{LANG} = $ENV{LC_ALL};
106 if (defined $config{locale}) {
107 if (POSIX::setlocale(&POSIX::LC_ALL, $config{locale})) {
108 $ENV{LANG}=$config{locale};
113 if ($config{w3mmode}) {
114 eval q{use Cwd q{abs_path}};
116 $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir}));
117 $config{destdir}=possibly_foolish_untaint(abs_path($config{destdir}));
118 $config{cgiurl}="file:///\$LIB/ikiwiki-w3m.cgi/".$config{cgiurl}
119 unless $config{cgiurl} =~ m!file:///!;
120 $config{url}="file://".$config{destdir};
123 if ($config{cgi} && ! length $config{url}) {
124 error(gettext("Must specify url to wiki with --url when using --cgi"));
127 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
128 unless exists $config{wikistatedir};
131 eval qq{use IkiWiki::Rcs::$config{rcs}};
133 error("Failed to load RCS module IkiWiki::Rcs::$config{rcs}: $@");
137 require IkiWiki::Rcs::Stub;
140 run_hooks(checkconfig => sub { shift->() });
143 sub loadplugins () { #{{{
144 if (defined $config{libdir}) {
145 unshift @INC, $config{libdir};
148 loadplugin($_) foreach @{$config{plugin}};
150 run_hooks(getopt => sub { shift->() });
151 if (grep /^-/, @ARGV) {
152 print STDERR "Unknown option: $_\n"
153 foreach grep /^-/, @ARGV;
158 sub loadplugin ($) { #{{{
161 return if grep { $_ eq $plugin} @{$config{disable_plugins}};
163 my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
166 error("Failed to load plugin $mod: $@");
170 sub error ($;$) { #{{{
174 print "Content-type: text/html\n\n";
175 print misctemplate(gettext("Error"),
176 "<p>".gettext("Error").": $message</p>");
178 log_message('err' => $message) if $config{syslog};
179 if (defined $cleaner) {
186 return unless $config{verbose};
187 log_message(debug => @_);
191 sub log_message ($$) { #{{{
194 if ($config{syslog}) {
197 Sys::Syslog::setlogsock('unix');
198 Sys::Syslog::openlog('ikiwiki', '', 'user');
202 Sys::Syslog::syslog($type, "[$config{wikiname}] %s", join(" ", @_));
205 elsif (! $config{cgi}) {
213 sub possibly_foolish_untaint ($) { #{{{
215 my ($untainted)=$tainted=~/(.*)/s;
219 sub basename ($) { #{{{
226 sub dirname ($) { #{{{
233 sub pagetype ($) { #{{{
236 if ($page =~ /\.([^.]+)$/) {
237 return $1 if exists $hooks{htmlize}{$1};
242 sub pagename ($) { #{{{
245 my $type=pagetype($file);
247 $page=~s/\Q.$type\E*$// if defined $type;
251 sub targetpage ($$) { #{{{
255 if (! $config{usedirs} || $page =~ /^index$/ ) {
256 return $page.".".$ext;
258 return $page."/index.".$ext;
262 sub htmlpage ($) { #{{{
265 return targetpage($page, $config{htmlext});
268 sub srcfile ($) { #{{{
271 return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
272 return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
273 error("internal error: $file cannot be found in $config{srcdir} or $config{underlaydir}");
276 sub readfile ($;$$) { #{{{
282 error("cannot read a symlink ($file)");
286 open (IN, $file) || error("failed to read $file: $!");
287 binmode(IN) if ($binary);
288 return \*IN if $wantfd;
290 close IN || error("failed to read $file: $!");
294 sub writefile ($$$;$$) { #{{{
295 my $file=shift; # can include subdirs
296 my $destdir=shift; # directory to put file in
302 while (length $test) {
303 if (-l "$destdir/$test") {
304 error("cannot write to a symlink ($test)");
306 $test=dirname($test);
308 my $newfile="$destdir/$file.ikiwiki-new";
310 error("cannot write to a symlink ($newfile)");
313 my $dir=dirname($newfile);
316 foreach my $s (split(m!/+!, $dir)) {
319 mkdir($d) || error("failed to create directory $d: $!");
324 my $cleanup = sub { unlink($newfile) };
325 open (OUT, ">$newfile") || error("failed to write $newfile: $!", $cleanup);
326 binmode(OUT) if ($binary);
328 $writer->(\*OUT, $cleanup);
331 print OUT $content or error("failed writing to $newfile: $!", $cleanup);
333 close OUT || error("failed saving $newfile: $!", $cleanup);
334 rename($newfile, "$destdir/$file") ||
335 error("failed renaming $newfile to $destdir/$file: $!", $cleanup);
339 sub will_render ($$;$) { #{{{
344 # Important security check.
345 if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
346 ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}})) {
347 error("$config{destdir}/$dest independently created, not overwriting with version from $page");
350 if (! $clear || $cleared{$page}) {
351 $renderedfiles{$page}=[$dest, grep { $_ ne $dest } @{$renderedfiles{$page}}];
354 foreach my $old (@{$renderedfiles{$page}}) {
355 delete $destsources{$old};
357 $renderedfiles{$page}=[$dest];
360 $destsources{$dest}=$page;
363 sub bestlink ($$) { #{{{
368 if ($link=~s/^\/+//) {
375 $l.="/" if length $l;
378 if (exists $links{$l}) {
381 elsif (exists $pagecase{lc $l}) {
382 return $pagecase{lc $l};
384 } while $cwd=~s!/?[^/]+$!!;
386 if (length $config{userdir}) {
387 my $l = "$config{userdir}/".lc($link);
388 if (exists $links{$l}) {
391 elsif (exists $pagecase{lc $l}) {
392 return $pagecase{lc $l};
396 #print STDERR "warning: page $page, broken link: $link\n";
400 sub isinlinableimage ($) { #{{{
403 $file=~/\.(png|gif|jpg|jpeg)$/i;
406 sub pagetitle ($;$) { #{{{
411 $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : chr($2)/eg;
414 $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : "&#$2;"/eg;
420 sub titlepage ($) { #{{{
422 $title=~s/([^-[:alnum:]:+\/.])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
426 sub linkpage ($) { #{{{
428 $link=~s/([^-[:alnum:]:+\/._])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
432 sub cgiurl (@) { #{{{
435 return $config{cgiurl}."?".
436 join("&", map $_."=".uri_escape_utf8($params{$_}), keys %params);
439 sub baseurl (;$) { #{{{
442 return "$config{url}/" if ! defined $page;
444 $page=htmlpage($page);
446 $page=~s/[^\/]+\//..\//g;
450 sub abs2rel ($$) { #{{{
451 # Work around very innefficient behavior in File::Spec if abs2rel
452 # is passed two relative paths. It's much faster if paths are
453 # absolute! (Debian bug #376658; fixed in debian unstable now)
458 my $ret=File::Spec->abs2rel($path, $base);
459 $ret=~s/^// if defined $ret;
463 sub displaytime ($) { #{{{
466 # strftime doesn't know about encodings, so make sure
467 # its output is properly treated as utf8
468 return decode_utf8(POSIX::strftime(
469 $config{timeformat}, localtime($time)));
472 sub beautify_url ($) { #{{{
475 $url =~ s!/index.$config{htmlext}$!/!;
476 $url =~ s!^$!./!; # Browsers don't like empty links...
481 sub urlto ($$) { #{{{
486 return beautify_url(baseurl($from));
489 if (! $destsources{$to}) {
493 my $link = abs2rel($to, dirname(htmlpage($from)));
495 return beautify_url($link);
498 sub htmllink ($$$;@) { #{{{
499 my $lpage=shift; # the page doing the linking
500 my $page=shift; # the page that will contain the link (different for inline)
505 if (! $opts{forcesubpage}) {
506 $bestlink=bestlink($lpage, $link);
509 $bestlink="$lpage/".lc($link);
513 if (defined $opts{linktext}) {
514 $linktext=$opts{linktext};
517 $linktext=pagetitle(basename($link));
520 return "<span class=\"selflink\">$linktext</span>"
521 if length $bestlink && $page eq $bestlink;
523 if (! $destsources{$bestlink}) {
524 $bestlink=htmlpage($bestlink);
526 if (! $destsources{$bestlink}) {
527 return $linktext unless length $config{cgiurl};
528 return "<span><a href=\"".
531 page => pagetitle(lc($link), 1),
534 "\">?</a>$linktext</span>"
538 $bestlink=abs2rel($bestlink, dirname(htmlpage($page)));
539 $bestlink=beautify_url($bestlink);
541 if (! $opts{noimageinline} && isinlinableimage($bestlink)) {
542 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
545 if (defined $opts{anchor}) {
546 $bestlink.="#".$opts{anchor};
550 if (defined $opts{rel}) {
551 push @attrs, ' rel="'.$opts{rel}.'"';
554 return "<a href=\"$bestlink\"@attrs>$linktext</a>";
557 sub htmlize ($$$) { #{{{
562 if (exists $hooks{htmlize}{$type}) {
563 $content=$hooks{htmlize}{$type}{call}->(
569 error("htmlization of $type not supported");
572 run_hooks(sanitize => sub {
582 sub linkify ($$$) { #{{{
583 my $lpage=shift; # the page containing the links
584 my $page=shift; # the page the link will end up on (different for inline)
587 $content =~ s{(\\?)$config{wiki_link_regexp}}{
590 ? "[[$2|$3".($4 ? "#$4" : "")."]]"
591 : htmllink($lpage, $page, linkpage($3),
592 anchor => $4, linktext => pagetitle($2)))
594 ? "[[$3".($4 ? "#$4" : "")."]]"
595 : htmllink($lpage, $page, linkpage($3),
603 our $preprocess_preview=0;
604 sub preprocess ($$$;$$) { #{{{
605 my $page=shift; # the page the data comes from
606 my $destpage=shift; # the page the data will appear in (different for inline)
611 # Using local because it needs to be set within any nested calls
613 local $preprocess_preview=$preview if defined $preview;
619 if (length $escape) {
620 return "\\[[$command $params]]";
622 elsif (exists $hooks{preprocess}{$command}) {
623 return "" if $scan && ! $hooks{preprocess}{$command}{scan};
624 # Note: preserve order of params, some plugins may
625 # consider it significant.
628 (?:(\w+)=)? # 1: named parameter key?
630 """(.*?)""" # 2: triple-quoted value
632 "([^"]+)" # 3: single-quoted value
634 (\S+) # 4: unquoted value
636 (?:\s+|$) # delimiter to next param
654 push @params, $key, $val;
657 push @params, $val, '';
660 if ($preprocessing{$page}++ > 3) {
661 # Avoid loops of preprocessed pages preprocessing
662 # other pages that preprocess them, etc.
663 #translators: The first parameter is a
664 #translators: preprocessor directive name,
665 #translators: the second a page name, the
666 #translators: third a number.
667 return "[[".sprintf(gettext("%s preprocessing loop detected on %s at depth %i"),
668 $command, $page, $preprocessing{$page}).
671 my $ret=$hooks{preprocess}{$command}{call}->(
674 destpage => $destpage,
675 preview => $preprocess_preview,
677 $preprocessing{$page}--;
681 return "\\[[$command $params]]";
687 \[\[ # directive open
690 ( # 3: the parameters..
692 (?:\w+=)? # named parameter key?
694 """.*?""" # triple-quoted value
696 "[^"]+" # single-quoted value
698 [^\s\]]+ # unquoted value
700 \s* # whitespace or end
703 *) # 0 or more parameters
704 \]\] # directive closed
705 }{$handle->($1, $2, $3)}sexg;
709 sub filter ($$$) { #{{{
714 run_hooks(filter => sub {
715 $content=shift->(page => $page, destpage => $destpage,
716 content => $content);
722 sub indexlink () { #{{{
723 return "<a href=\"$config{url}\">$config{wikiname}</a>";
726 sub lockwiki (;$) { #{{{
727 my $wait=@_ ? shift : 1;
728 # Take an exclusive lock on the wiki to prevent multiple concurrent
729 # run issues. The lock will be dropped on program exit.
730 if (! -d $config{wikistatedir}) {
731 mkdir($config{wikistatedir});
733 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
734 error ("cannot write to $config{wikistatedir}/lockfile: $!");
735 if (! flock(WIKILOCK, 2 | 4)) { # LOCK_EX | LOCK_NB
737 debug("wiki seems to be locked, waiting for lock");
738 my $wait=600; # arbitrary, but don't hang forever to
739 # prevent process pileup
741 return if flock(WIKILOCK, 2 | 4);
744 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
753 sub unlockwiki () { #{{{
757 sub commit_hook_enabled () { #{{{
758 open(COMMITLOCK, "+>$config{wikistatedir}/commitlock") ||
759 error ("cannot write to $config{wikistatedir}/commitlock: $!");
760 if (! flock(COMMITLOCK, 1 | 4)) { # LOCK_SH | LOCK_NB to test
768 sub disable_commit_hook () { #{{{
769 open(COMMITLOCK, ">$config{wikistatedir}/commitlock") ||
770 error ("cannot write to $config{wikistatedir}/commitlock: $!");
771 if (! flock(COMMITLOCK, 2)) { # LOCK_EX
772 error("failed to get commit lock");
776 sub enable_commit_hook () { #{{{
780 sub loadindex () { #{{{
781 open (IN, "$config{wikistatedir}/index") || return;
783 $_=possibly_foolish_untaint($_);
788 foreach my $i (split(/ /, $_)) {
789 my ($item, $val)=split(/=/, $i, 2);
790 push @{$items{$item}}, decode_entities($val);
793 next unless exists $items{src}; # skip bad lines for now
795 my $page=pagename($items{src}[0]);
796 if (! $config{rebuild}) {
797 $pagesources{$page}=$items{src}[0];
798 $pagemtime{$page}=$items{mtime}[0];
799 $oldlinks{$page}=[@{$items{link}}];
800 $links{$page}=[@{$items{link}}];
801 $depends{$page}=$items{depends}[0] if exists $items{depends};
802 $destsources{$_}=$page foreach @{$items{dest}};
803 $renderedfiles{$page}=[@{$items{dest}}];
804 $pagecase{lc $page}=$page;
806 $oldrenderedfiles{$page}=[@{$items{dest}}];
807 $pagectime{$page}=$items{ctime}[0];
812 sub saveindex () { #{{{
813 run_hooks(savestate => sub { shift->() });
815 if (! -d $config{wikistatedir}) {
816 mkdir($config{wikistatedir});
818 my $newfile="$config{wikistatedir}/index.new";
819 my $cleanup = sub { unlink($newfile) };
820 open (OUT, ">$newfile") || error("cannot write to $newfile: $!", $cleanup);
821 foreach my $page (keys %pagemtime) {
822 next unless $pagemtime{$page};
823 my $line="mtime=$pagemtime{$page} ".
824 "ctime=$pagectime{$page} ".
825 "src=$pagesources{$page}";
826 $line.=" dest=$_" foreach @{$renderedfiles{$page}};
828 $line.=" link=$_" foreach grep { ++$count{$_} == 1 } @{$links{$page}};
829 if (exists $depends{$page}) {
830 $line.=" depends=".encode_entities($depends{$page}, " \t\n");
832 print OUT $line."\n" || error("failed writing to $newfile: $!", $cleanup);
834 close OUT || error("failed saving to $newfile: $!", $cleanup);
835 rename($newfile, "$config{wikistatedir}/index") ||
836 error("failed renaming $newfile to $config{wikistatedir}/index", $cleanup);
839 sub template_file ($) { #{{{
842 foreach my $dir ($config{templatedir}, "$installdir/share/ikiwiki/templates") {
843 return "$dir/$template" if -e "$dir/$template";
848 sub template_params (@) { #{{{
849 my $filename=template_file(shift);
851 if (! defined $filename) {
858 my $text_ref = shift;
859 $$text_ref=&Encode::decode_utf8($$text_ref);
861 filename => $filename,
862 loop_context_vars => 1,
863 die_on_bad_params => 0,
866 return wantarray ? @ret : {@ret};
869 sub template ($;@) { #{{{
870 require HTML::Template;
871 HTML::Template->new(template_params(@_));
874 sub misctemplate ($$;@) { #{{{
878 my $template=template("misc.tmpl");
881 indexlink => indexlink(),
882 wikiname => $config{wikiname},
883 pagebody => $pagebody,
884 baseurl => baseurl(),
887 run_hooks(pagetemplate => sub {
888 shift->(page => "", destpage => "", template => $template);
890 return $template->output;
896 if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
897 error "hook requires type, call, and id parameters";
900 return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
902 $hooks{$param{type}}{$param{id}}=\%param;
905 sub run_hooks ($$) { # {{{
906 # Calls the given sub for each hook of the given type,
907 # passing it the hook function to call.
911 if (exists $hooks{$type}) {
913 foreach my $id (keys %{$hooks{$type}}) {
914 if ($hooks{$type}{$id}{last}) {
918 $sub->($hooks{$type}{$id}{call});
920 foreach my $id (@deferred) {
921 $sub->($hooks{$type}{$id}{call});
926 sub globlist_to_pagespec ($) { #{{{
927 my @globlist=split(' ', shift);
930 foreach my $glob (@globlist) {
931 if ($glob=~/^!(.*)/) {
939 my $spec=join(" or ", @spec);
941 my $skip=join(" and ", @skip);
943 $spec="$skip and ($spec)";
952 sub is_globlist ($) { #{{{
954 $s=~/[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or";
957 sub safequote ($) { #{{{
963 sub add_depends ($$) { #{{{
967 if (! exists $depends{$page}) {
968 $depends{$page}=$pagespec;
971 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
975 sub file_pruned ($$) { #{{{
977 my $file=File::Spec->canonpath(shift);
978 my $base=File::Spec->canonpath(shift);
979 $file=~s#^\Q$base\E/*##;
981 my $regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
986 # Only use gettext in the rare cases it's needed.
987 if (exists $ENV{LANG} || exists $ENV{LC_ALL} || exists $ENV{LC_MESSAGES}) {
988 if (! $gettext_obj) {
990 use Locale::gettext q{textdomain};
991 Locale::gettext->domain('ikiwiki')
999 return $gettext_obj->get(shift);
1006 sub pagespec_merge ($$) { #{{{
1010 return $a if $a eq $b;
1012 # Support for old-style GlobLists.
1013 if (is_globlist($a)) {
1014 $a=globlist_to_pagespec($a);
1016 if (is_globlist($b)) {
1017 $b=globlist_to_pagespec($b);
1020 return "($a) or ($b)";
1023 sub pagespec_translate ($) { #{{{
1024 # This assumes that $page is in scope in the function
1025 # that evalulates the translated pagespec code.
1028 # Support for old-style GlobLists.
1029 if (is_globlist($spec)) {
1030 $spec=globlist_to_pagespec($spec);
1033 # Convert spec to perl code.
1036 \s* # ignore whitespace
1037 ( # 1: match a single word
1044 \w+\([^\)]*\) # command(params)
1046 [^\s()]+ # any other text
1048 \s* # ignore whitespace
1051 if (lc $word eq "and") {
1054 elsif (lc $word eq "or") {
1057 elsif ($word eq "(" || $word eq ")" || $word eq "!") {
1060 elsif ($word =~ /^(\w+)\((.*)\)$/) {
1061 if (exists $IkiWiki::PageSpec::{"match_$1"}) {
1062 $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \@params)";
1069 $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \@params)";
1076 sub pagespec_match ($$;@) { #{{{
1081 # Backwards compatability with old calling convention.
1083 unshift @params, "location";
1086 my $ret=eval pagespec_translate($spec);
1087 return IkiWiki::FailReason->new("syntax error") if $@;
1091 package IkiWiki::FailReason;
1094 '""' => sub { ${$_[0]} },
1096 '!' => sub { bless $_[0], 'IkiWiki::SuccessReason'},
1101 bless \$_[1], $_[0];
1104 package IkiWiki::SuccessReason;
1107 '""' => sub { ${$_[0]} },
1109 '!' => sub { bless $_[0], 'IkiWiki::FailReason'},
1114 bless \$_[1], $_[0];
1117 package IkiWiki::PageSpec;
1119 sub match_glob ($$;@) { #{{{
1124 my $from=exists $params{location} ? $params{location} : "";
1127 if ($glob =~ m!^\./!) {
1128 $from=~s#/?[^/]+$##;
1130 $glob="$from/$glob" if length $from;
1133 # turn glob into safe regexp
1134 $glob=quotemeta($glob);
1138 if ($page=~/^$glob$/i) {
1139 return IkiWiki::SuccessReason->new("$glob matches $page");
1142 return IkiWiki::FailReason->new("$glob does not match $page");
1146 sub match_link ($$;@) { #{{{
1151 my $from=exists $params{location} ? $params{location} : "";
1154 if ($link =~ m!^\.! && defined $from) {
1155 $from=~s#/?[^/]+$##;
1157 $link="$from/$link" if length $from;
1160 my $links = $IkiWiki::links{$page} or return undef;
1161 return IkiWiki::FailReason->new("$page has no links") unless @$links;
1162 my $bestlink = IkiWiki::bestlink($from, $link);
1163 foreach my $p (@$links) {
1164 if (length $bestlink) {
1165 return IkiWiki::SuccessReason->new("$page links to $link")
1166 if $bestlink eq IkiWiki::bestlink($page, $p);
1169 return IkiWiki::SuccessReason->new("$page links to page matching $link")
1170 if match_glob($p, $link, %params);
1173 return IkiWiki::FailReason->new("$page does not link to $link");
1176 sub match_backlink ($$;@) { #{{{
1177 match_link($_[1], $_[0], @_);
1180 sub match_created_before ($$;@) { #{{{
1184 if (exists $IkiWiki::pagectime{$testpage}) {
1185 if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
1186 IkiWiki::SuccessReason->new("$page created before $testpage");
1189 IkiWiki::FailReason->new("$page not created before $testpage");
1193 return IkiWiki::FailReason->new("$testpage has no ctime");
1197 sub match_created_after ($$;@) { #{{{
1201 if (exists $IkiWiki::pagectime{$testpage}) {
1202 if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
1203 IkiWiki::SuccessReason->new("$page created after $testpage");
1206 IkiWiki::FailReason->new("$page not created after $testpage");
1210 return IkiWiki::FailReason->new("$testpage has no ctime");
1214 sub match_creation_day ($$;@) { #{{{
1215 if ((gmtime($IkiWiki::pagectime{shift()}))[3] == shift) {
1216 return IkiWiki::SuccessReason->new("creation_day matched");
1219 return IkiWiki::FailReason->new("creation_day did not match");
1223 sub match_creation_month ($$;@) { #{{{
1224 if ((gmtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
1225 return IkiWiki::SuccessReason->new("creation_month matched");
1228 return IkiWiki::FailReason->new("creation_month did not match");
1232 sub match_creation_year ($$;@) { #{{{
1233 if ((gmtime($IkiWiki::pagectime{shift()}))[5] + 1900 == shift) {
1234 return IkiWiki::SuccessReason->new("creation_year matched");
1237 return IkiWiki::FailReason->new("creation_year did not match");
1241 sub match_user ($$;@) { #{{{
1246 return IkiWiki::FailReason->new("cannot match user") unless exists $params{user};
1247 if ($user eq $params{user}) {
1248 return IkiWiki::SuccessReason->new("user is $user")
1251 return IkiWiki::FailReason->new("user is not $user");