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 foreach my $dir ($config{libdir}, "$installdir/lib/ikiwiki") {
164 if (defined $dir && -x "$dir/plugins/$plugin") {
165 require IkiWiki::Plugin::external;
166 import IkiWiki::Plugin::external "$dir/plugins/$plugin";
171 my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
174 error("Failed to load plugin $mod: $@");
179 sub error ($;$) { #{{{
183 print "Content-type: text/html\n\n";
184 print misctemplate(gettext("Error"),
185 "<p>".gettext("Error").": $message</p>");
187 log_message('err' => $message) if $config{syslog};
188 if (defined $cleaner) {
195 return unless $config{verbose};
196 log_message(debug => @_);
200 sub log_message ($$) { #{{{
203 if ($config{syslog}) {
206 Sys::Syslog::setlogsock('unix');
207 Sys::Syslog::openlog('ikiwiki', '', 'user');
211 Sys::Syslog::syslog($type, "[$config{wikiname}] %s", join(" ", @_));
214 elsif (! $config{cgi}) {
222 sub possibly_foolish_untaint ($) { #{{{
224 my ($untainted)=$tainted=~/(.*)/s;
228 sub basename ($) { #{{{
235 sub dirname ($) { #{{{
242 sub pagetype ($) { #{{{
245 if ($page =~ /\.([^.]+)$/) {
246 return $1 if exists $hooks{htmlize}{$1};
251 sub pagename ($) { #{{{
254 my $type=pagetype($file);
256 $page=~s/\Q.$type\E*$// if defined $type;
260 sub targetpage ($$) { #{{{
264 if (! $config{usedirs} || $page =~ /^index$/ ) {
265 return $page.".".$ext;
267 return $page."/index.".$ext;
271 sub htmlpage ($) { #{{{
274 return targetpage($page, $config{htmlext});
277 sub srcfile ($) { #{{{
280 return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
281 return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
282 error("internal error: $file cannot be found in $config{srcdir} or $config{underlaydir}");
285 sub readfile ($;$$) { #{{{
291 error("cannot read a symlink ($file)");
295 open (IN, $file) || error("failed to read $file: $!");
296 binmode(IN) if ($binary);
297 return \*IN if $wantfd;
299 close IN || error("failed to read $file: $!");
303 sub writefile ($$$;$$) { #{{{
304 my $file=shift; # can include subdirs
305 my $destdir=shift; # directory to put file in
311 while (length $test) {
312 if (-l "$destdir/$test") {
313 error("cannot write to a symlink ($test)");
315 $test=dirname($test);
317 my $newfile="$destdir/$file.ikiwiki-new";
319 error("cannot write to a symlink ($newfile)");
322 my $dir=dirname($newfile);
325 foreach my $s (split(m!/+!, $dir)) {
328 mkdir($d) || error("failed to create directory $d: $!");
333 my $cleanup = sub { unlink($newfile) };
334 open (OUT, ">$newfile") || error("failed to write $newfile: $!", $cleanup);
335 binmode(OUT) if ($binary);
337 $writer->(\*OUT, $cleanup);
340 print OUT $content or error("failed writing to $newfile: $!", $cleanup);
342 close OUT || error("failed saving $newfile: $!", $cleanup);
343 rename($newfile, "$destdir/$file") ||
344 error("failed renaming $newfile to $destdir/$file: $!", $cleanup);
348 sub will_render ($$;$) { #{{{
353 # Important security check.
354 if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
355 ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}})) {
356 error("$config{destdir}/$dest independently created, not overwriting with version from $page");
359 if (! $clear || $cleared{$page}) {
360 $renderedfiles{$page}=[$dest, grep { $_ ne $dest } @{$renderedfiles{$page}}];
363 foreach my $old (@{$renderedfiles{$page}}) {
364 delete $destsources{$old};
366 $renderedfiles{$page}=[$dest];
369 $destsources{$dest}=$page;
372 sub bestlink ($$) { #{{{
377 if ($link=~s/^\/+//) {
384 $l.="/" if length $l;
387 if (exists $links{$l}) {
390 elsif (exists $pagecase{lc $l}) {
391 return $pagecase{lc $l};
393 } while $cwd=~s!/?[^/]+$!!;
395 if (length $config{userdir}) {
396 my $l = "$config{userdir}/".lc($link);
397 if (exists $links{$l}) {
400 elsif (exists $pagecase{lc $l}) {
401 return $pagecase{lc $l};
405 #print STDERR "warning: page $page, broken link: $link\n";
409 sub isinlinableimage ($) { #{{{
412 $file=~/\.(png|gif|jpg|jpeg)$/i;
415 sub pagetitle ($;$) { #{{{
420 $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : chr($2)/eg;
423 $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : "&#$2;"/eg;
429 sub titlepage ($) { #{{{
431 $title=~s/([^-[:alnum:]:+\/.])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
435 sub linkpage ($) { #{{{
437 $link=~s/([^-[:alnum:]:+\/._])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
441 sub cgiurl (@) { #{{{
444 return $config{cgiurl}."?".
445 join("&", map $_."=".uri_escape_utf8($params{$_}), keys %params);
448 sub baseurl (;$) { #{{{
451 return "$config{url}/" if ! defined $page;
453 $page=htmlpage($page);
455 $page=~s/[^\/]+\//..\//g;
459 sub abs2rel ($$) { #{{{
460 # Work around very innefficient behavior in File::Spec if abs2rel
461 # is passed two relative paths. It's much faster if paths are
462 # absolute! (Debian bug #376658; fixed in debian unstable now)
467 my $ret=File::Spec->abs2rel($path, $base);
468 $ret=~s/^// if defined $ret;
472 sub displaytime ($) { #{{{
475 # strftime doesn't know about encodings, so make sure
476 # its output is properly treated as utf8
477 return decode_utf8(POSIX::strftime(
478 $config{timeformat}, localtime($time)));
481 sub beautify_url ($) { #{{{
484 $url =~ s!/index.$config{htmlext}$!/!;
485 $url =~ s!^$!./!; # Browsers don't like empty links...
490 sub urlto ($$) { #{{{
495 return beautify_url(baseurl($from));
498 if (! $destsources{$to}) {
502 my $link = abs2rel($to, dirname(htmlpage($from)));
504 return beautify_url($link);
507 sub htmllink ($$$;@) { #{{{
508 my $lpage=shift; # the page doing the linking
509 my $page=shift; # the page that will contain the link (different for inline)
514 if (! $opts{forcesubpage}) {
515 $bestlink=bestlink($lpage, $link);
518 $bestlink="$lpage/".lc($link);
522 if (defined $opts{linktext}) {
523 $linktext=$opts{linktext};
526 $linktext=pagetitle(basename($link));
529 return "<span class=\"selflink\">$linktext</span>"
530 if length $bestlink && $page eq $bestlink;
532 if (! $destsources{$bestlink}) {
533 $bestlink=htmlpage($bestlink);
535 if (! $destsources{$bestlink}) {
536 return $linktext unless length $config{cgiurl};
537 return "<span><a href=\"".
540 page => pagetitle(lc($link), 1),
543 "\">?</a>$linktext</span>"
547 $bestlink=abs2rel($bestlink, dirname(htmlpage($page)));
548 $bestlink=beautify_url($bestlink);
550 if (! $opts{noimageinline} && isinlinableimage($bestlink)) {
551 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
554 if (defined $opts{anchor}) {
555 $bestlink.="#".$opts{anchor};
559 if (defined $opts{rel}) {
560 push @attrs, ' rel="'.$opts{rel}.'"';
563 return "<a href=\"$bestlink\"@attrs>$linktext</a>";
566 sub htmlize ($$$) { #{{{
571 if (exists $hooks{htmlize}{$type}) {
572 $content=$hooks{htmlize}{$type}{call}->(
578 error("htmlization of $type not supported");
581 run_hooks(sanitize => sub {
591 sub linkify ($$$) { #{{{
592 my $lpage=shift; # the page containing the links
593 my $page=shift; # the page the link will end up on (different for inline)
596 $content =~ s{(\\?)$config{wiki_link_regexp}}{
599 ? "[[$2|$3".($4 ? "#$4" : "")."]]"
600 : htmllink($lpage, $page, linkpage($3),
601 anchor => $4, linktext => pagetitle($2)))
603 ? "[[$3".($4 ? "#$4" : "")."]]"
604 : htmllink($lpage, $page, linkpage($3),
612 our $preprocess_preview=0;
613 sub preprocess ($$$;$$) { #{{{
614 my $page=shift; # the page the data comes from
615 my $destpage=shift; # the page the data will appear in (different for inline)
620 # Using local because it needs to be set within any nested calls
622 local $preprocess_preview=$preview if defined $preview;
628 if (length $escape) {
629 return "\\[[$command $params]]";
631 elsif (exists $hooks{preprocess}{$command}) {
632 return "" if $scan && ! $hooks{preprocess}{$command}{scan};
633 # Note: preserve order of params, some plugins may
634 # consider it significant.
637 (?:(\w+)=)? # 1: named parameter key?
639 """(.*?)""" # 2: triple-quoted value
641 "([^"]+)" # 3: single-quoted value
643 (\S+) # 4: unquoted value
645 (?:\s+|$) # delimiter to next param
663 push @params, $key, $val;
666 push @params, $val, '';
669 if ($preprocessing{$page}++ > 3) {
670 # Avoid loops of preprocessed pages preprocessing
671 # other pages that preprocess them, etc.
672 #translators: The first parameter is a
673 #translators: preprocessor directive name,
674 #translators: the second a page name, the
675 #translators: third a number.
676 return "[[".sprintf(gettext("%s preprocessing loop detected on %s at depth %i"),
677 $command, $page, $preprocessing{$page}).
680 my $ret=$hooks{preprocess}{$command}{call}->(
683 destpage => $destpage,
684 preview => $preprocess_preview,
686 $preprocessing{$page}--;
690 return "\\[[$command $params]]";
696 \[\[ # directive open
699 ( # 3: the parameters..
701 (?:\w+=)? # named parameter key?
703 """.*?""" # triple-quoted value
705 "[^"]+" # single-quoted value
707 [^\s\]]+ # unquoted value
709 \s* # whitespace or end
712 *) # 0 or more parameters
713 \]\] # directive closed
714 }{$handle->($1, $2, $3)}sexg;
718 sub filter ($$$) { #{{{
723 run_hooks(filter => sub {
724 $content=shift->(page => $page, destpage => $destpage,
725 content => $content);
731 sub indexlink () { #{{{
732 return "<a href=\"$config{url}\">$config{wikiname}</a>";
735 sub lockwiki (;$) { #{{{
736 my $wait=@_ ? shift : 1;
737 # Take an exclusive lock on the wiki to prevent multiple concurrent
738 # run issues. The lock will be dropped on program exit.
739 if (! -d $config{wikistatedir}) {
740 mkdir($config{wikistatedir});
742 open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
743 error ("cannot write to $config{wikistatedir}/lockfile: $!");
744 if (! flock(WIKILOCK, 2 | 4)) { # LOCK_EX | LOCK_NB
746 debug("wiki seems to be locked, waiting for lock");
747 my $wait=600; # arbitrary, but don't hang forever to
748 # prevent process pileup
750 return if flock(WIKILOCK, 2 | 4);
753 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
762 sub unlockwiki () { #{{{
766 sub commit_hook_enabled () { #{{{
767 open(COMMITLOCK, "+>$config{wikistatedir}/commitlock") ||
768 error ("cannot write to $config{wikistatedir}/commitlock: $!");
769 if (! flock(COMMITLOCK, 1 | 4)) { # LOCK_SH | LOCK_NB to test
777 sub disable_commit_hook () { #{{{
778 open(COMMITLOCK, ">$config{wikistatedir}/commitlock") ||
779 error ("cannot write to $config{wikistatedir}/commitlock: $!");
780 if (! flock(COMMITLOCK, 2)) { # LOCK_EX
781 error("failed to get commit lock");
785 sub enable_commit_hook () { #{{{
789 sub loadindex () { #{{{
790 open (IN, "$config{wikistatedir}/index") || return;
792 $_=possibly_foolish_untaint($_);
797 foreach my $i (split(/ /, $_)) {
798 my ($item, $val)=split(/=/, $i, 2);
799 push @{$items{$item}}, decode_entities($val);
802 next unless exists $items{src}; # skip bad lines for now
804 my $page=pagename($items{src}[0]);
805 if (! $config{rebuild}) {
806 $pagesources{$page}=$items{src}[0];
807 $pagemtime{$page}=$items{mtime}[0];
808 $oldlinks{$page}=[@{$items{link}}];
809 $links{$page}=[@{$items{link}}];
810 $depends{$page}=$items{depends}[0] if exists $items{depends};
811 $destsources{$_}=$page foreach @{$items{dest}};
812 $renderedfiles{$page}=[@{$items{dest}}];
813 $pagecase{lc $page}=$page;
815 $oldrenderedfiles{$page}=[@{$items{dest}}];
816 $pagectime{$page}=$items{ctime}[0];
821 sub saveindex () { #{{{
822 run_hooks(savestate => sub { shift->() });
824 if (! -d $config{wikistatedir}) {
825 mkdir($config{wikistatedir});
827 my $newfile="$config{wikistatedir}/index.new";
828 my $cleanup = sub { unlink($newfile) };
829 open (OUT, ">$newfile") || error("cannot write to $newfile: $!", $cleanup);
830 foreach my $page (keys %pagemtime) {
831 next unless $pagemtime{$page};
832 my $line="mtime=$pagemtime{$page} ".
833 "ctime=$pagectime{$page} ".
834 "src=$pagesources{$page}";
835 $line.=" dest=$_" foreach @{$renderedfiles{$page}};
837 $line.=" link=$_" foreach grep { ++$count{$_} == 1 } @{$links{$page}};
838 if (exists $depends{$page}) {
839 $line.=" depends=".encode_entities($depends{$page}, " \t\n");
841 print OUT $line."\n" || error("failed writing to $newfile: $!", $cleanup);
843 close OUT || error("failed saving to $newfile: $!", $cleanup);
844 rename($newfile, "$config{wikistatedir}/index") ||
845 error("failed renaming $newfile to $config{wikistatedir}/index", $cleanup);
848 sub template_file ($) { #{{{
851 foreach my $dir ($config{templatedir}, "$installdir/share/ikiwiki/templates") {
852 return "$dir/$template" if -e "$dir/$template";
857 sub template_params (@) { #{{{
858 my $filename=template_file(shift);
860 if (! defined $filename) {
867 my $text_ref = shift;
868 $$text_ref=&Encode::decode_utf8($$text_ref);
870 filename => $filename,
871 loop_context_vars => 1,
872 die_on_bad_params => 0,
875 return wantarray ? @ret : {@ret};
878 sub template ($;@) { #{{{
879 require HTML::Template;
880 HTML::Template->new(template_params(@_));
883 sub misctemplate ($$;@) { #{{{
887 my $template=template("misc.tmpl");
890 indexlink => indexlink(),
891 wikiname => $config{wikiname},
892 pagebody => $pagebody,
893 baseurl => baseurl(),
896 run_hooks(pagetemplate => sub {
897 shift->(page => "", destpage => "", template => $template);
899 return $template->output;
905 if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
906 error "hook requires type, call, and id parameters";
909 return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
911 $hooks{$param{type}}{$param{id}}=\%param;
914 sub run_hooks ($$) { # {{{
915 # Calls the given sub for each hook of the given type,
916 # passing it the hook function to call.
920 if (exists $hooks{$type}) {
922 foreach my $id (keys %{$hooks{$type}}) {
923 if ($hooks{$type}{$id}{last}) {
927 $sub->($hooks{$type}{$id}{call});
929 foreach my $id (@deferred) {
930 $sub->($hooks{$type}{$id}{call});
935 sub globlist_to_pagespec ($) { #{{{
936 my @globlist=split(' ', shift);
939 foreach my $glob (@globlist) {
940 if ($glob=~/^!(.*)/) {
948 my $spec=join(" or ", @spec);
950 my $skip=join(" and ", @skip);
952 $spec="$skip and ($spec)";
961 sub is_globlist ($) { #{{{
963 $s=~/[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or";
966 sub safequote ($) { #{{{
972 sub add_depends ($$) { #{{{
976 if (! exists $depends{$page}) {
977 $depends{$page}=$pagespec;
980 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
984 sub file_pruned ($$) { #{{{
986 my $file=File::Spec->canonpath(shift);
987 my $base=File::Spec->canonpath(shift);
988 $file=~s#^\Q$base\E/*##;
990 my $regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
995 # Only use gettext in the rare cases it's needed.
996 if (exists $ENV{LANG} || exists $ENV{LC_ALL} || exists $ENV{LC_MESSAGES}) {
997 if (! $gettext_obj) {
999 use Locale::gettext q{textdomain};
1000 Locale::gettext->domain('ikiwiki')
1008 return $gettext_obj->get(shift);
1015 sub pagespec_merge ($$) { #{{{
1019 return $a if $a eq $b;
1021 # Support for old-style GlobLists.
1022 if (is_globlist($a)) {
1023 $a=globlist_to_pagespec($a);
1025 if (is_globlist($b)) {
1026 $b=globlist_to_pagespec($b);
1029 return "($a) or ($b)";
1032 sub pagespec_translate ($) { #{{{
1033 # This assumes that $page is in scope in the function
1034 # that evalulates the translated pagespec code.
1037 # Support for old-style GlobLists.
1038 if (is_globlist($spec)) {
1039 $spec=globlist_to_pagespec($spec);
1042 # Convert spec to perl code.
1045 \s* # ignore whitespace
1046 ( # 1: match a single word
1053 \w+\([^\)]*\) # command(params)
1055 [^\s()]+ # any other text
1057 \s* # ignore whitespace
1060 if (lc $word eq "and") {
1063 elsif (lc $word eq "or") {
1066 elsif ($word eq "(" || $word eq ")" || $word eq "!") {
1069 elsif ($word =~ /^(\w+)\((.*)\)$/) {
1070 if (exists $IkiWiki::PageSpec::{"match_$1"}) {
1071 $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \@params)";
1078 $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \@params)";
1085 sub pagespec_match ($$;@) { #{{{
1090 # Backwards compatability with old calling convention.
1092 unshift @params, "location";
1095 my $ret=eval pagespec_translate($spec);
1096 return IkiWiki::FailReason->new("syntax error") if $@;
1100 package IkiWiki::FailReason;
1103 '""' => sub { ${$_[0]} },
1105 '!' => sub { bless $_[0], 'IkiWiki::SuccessReason'},
1110 bless \$_[1], $_[0];
1113 package IkiWiki::SuccessReason;
1116 '""' => sub { ${$_[0]} },
1118 '!' => sub { bless $_[0], 'IkiWiki::FailReason'},
1123 bless \$_[1], $_[0];
1126 package IkiWiki::PageSpec;
1128 sub match_glob ($$;@) { #{{{
1133 my $from=exists $params{location} ? $params{location} : "";
1136 if ($glob =~ m!^\./!) {
1137 $from=~s#/?[^/]+$##;
1139 $glob="$from/$glob" if length $from;
1142 # turn glob into safe regexp
1143 $glob=quotemeta($glob);
1147 if ($page=~/^$glob$/i) {
1148 return IkiWiki::SuccessReason->new("$glob matches $page");
1151 return IkiWiki::FailReason->new("$glob does not match $page");
1155 sub match_link ($$;@) { #{{{
1160 my $from=exists $params{location} ? $params{location} : "";
1163 if ($link =~ m!^\.! && defined $from) {
1164 $from=~s#/?[^/]+$##;
1166 $link="$from/$link" if length $from;
1169 my $links = $IkiWiki::links{$page} or return undef;
1170 return IkiWiki::FailReason->new("$page has no links") unless @$links;
1171 my $bestlink = IkiWiki::bestlink($from, $link);
1172 foreach my $p (@$links) {
1173 if (length $bestlink) {
1174 return IkiWiki::SuccessReason->new("$page links to $link")
1175 if $bestlink eq IkiWiki::bestlink($page, $p);
1178 return IkiWiki::SuccessReason->new("$page links to page matching $link")
1179 if match_glob($p, $link, %params);
1182 return IkiWiki::FailReason->new("$page does not link to $link");
1185 sub match_backlink ($$;@) { #{{{
1186 match_link($_[1], $_[0], @_);
1189 sub match_created_before ($$;@) { #{{{
1193 if (exists $IkiWiki::pagectime{$testpage}) {
1194 if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
1195 IkiWiki::SuccessReason->new("$page created before $testpage");
1198 IkiWiki::FailReason->new("$page not created before $testpage");
1202 return IkiWiki::FailReason->new("$testpage has no ctime");
1206 sub match_created_after ($$;@) { #{{{
1210 if (exists $IkiWiki::pagectime{$testpage}) {
1211 if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
1212 IkiWiki::SuccessReason->new("$page created after $testpage");
1215 IkiWiki::FailReason->new("$page not created after $testpage");
1219 return IkiWiki::FailReason->new("$testpage has no ctime");
1223 sub match_creation_day ($$;@) { #{{{
1224 if ((gmtime($IkiWiki::pagectime{shift()}))[3] == shift) {
1225 return IkiWiki::SuccessReason->new("creation_day matched");
1228 return IkiWiki::FailReason->new("creation_day did not match");
1232 sub match_creation_month ($$;@) { #{{{
1233 if ((gmtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
1234 return IkiWiki::SuccessReason->new("creation_month matched");
1237 return IkiWiki::FailReason->new("creation_month did not match");
1241 sub match_creation_year ($$;@) { #{{{
1242 if ((gmtime($IkiWiki::pagectime{shift()}))[5] + 1900 == shift) {
1243 return IkiWiki::SuccessReason->new("creation_year matched");
1246 return IkiWiki::FailReason->new("creation_year did not match");
1250 sub match_user ($$;@) { #{{{
1255 return IkiWiki::FailReason->new("cannot match user") unless exists $params{user};
1256 if ($user eq $params{user}) {
1257 return IkiWiki::SuccessReason->new("user is $user")
1260 return IkiWiki::FailReason->new("user is not $user");