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}\//,
38 wiki_link_regexp => qr{
39 \[\[ # beginning of link
41 ([^\]\|]+) # 1: link text
45 ([^\s\]#]+) # 2: page to link to
47 \# # '#', beginning of anchor
48 ([^\s\]]+) # 3: anchor text
53 wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
54 web_commit_regexp => qr/^web commit (by (.*?(?=: |$))|from (\d+\.\d+\.\d+\.\d+)):?(.*)/,
58 default_pageext => "mdwn",
79 gitorigin_branch => "origin",
80 gitmaster_branch => "master",
84 templatedir => "$installdir/share/ikiwiki/templates",
85 underlaydir => "$installdir/share/ikiwiki/basewiki",
89 plugin => [qw{mdwn inline htmlscrubber passwordauth openid signinedit
90 lockedit conditional}],
99 account_creation_password => "",
102 sub checkconfig () { #{{{
103 # locale stuff; avoid LC_ALL since it overrides everything
104 if (defined $ENV{LC_ALL}) {
105 $ENV{LANG} = $ENV{LC_ALL};
108 if (defined $config{locale}) {
109 if (POSIX::setlocale(&POSIX::LC_ALL, $config{locale})) {
110 $ENV{LANG}=$config{locale};
115 if ($config{w3mmode}) {
116 eval q{use Cwd q{abs_path}};
118 $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir}));
119 $config{destdir}=possibly_foolish_untaint(abs_path($config{destdir}));
120 $config{cgiurl}="file:///\$LIB/ikiwiki-w3m.cgi/".$config{cgiurl}
121 unless $config{cgiurl} =~ m!file:///!;
122 $config{url}="file://".$config{destdir};
125 if ($config{cgi} && ! length $config{url}) {
126 error(gettext("Must specify url to wiki with --url when using --cgi"));
129 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
130 unless exists $config{wikistatedir};
133 eval qq{use IkiWiki::Rcs::$config{rcs}};
135 error("Failed to load RCS module IkiWiki::Rcs::$config{rcs}: $@");
139 require IkiWiki::Rcs::Stub;
142 run_hooks(checkconfig => sub { shift->() });
147 sub loadplugins () { #{{{
148 if (defined $config{libdir}) {
149 unshift @INC, $config{libdir};
152 loadplugin($_) foreach @{$config{plugin}};
154 run_hooks(getopt => sub { shift->() });
155 if (grep /^-/, @ARGV) {
156 print STDERR "Unknown option: $_\n"
157 foreach grep /^-/, @ARGV;
164 sub loadplugin ($) { #{{{
167 return if grep { $_ eq $plugin} @{$config{disable_plugins}};
169 foreach my $dir ($config{libdir}, "$installdir/lib/ikiwiki") {
170 if (defined $dir && -x "$dir/plugins/$plugin") {
171 require IkiWiki::Plugin::external;
172 import IkiWiki::Plugin::external "$dir/plugins/$plugin";
177 my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
180 error("Failed to load plugin $mod: $@");
185 sub error ($;$) { #{{{
189 print "Content-type: text/html\n\n";
190 print misctemplate(gettext("Error"),
191 "<p>".gettext("Error").": $message</p>");
193 log_message('err' => $message) if $config{syslog};
194 if (defined $cleaner) {
201 return unless $config{verbose};
202 return log_message(debug => @_);
206 sub log_message ($$) { #{{{
209 if ($config{syslog}) {
212 Sys::Syslog::setlogsock('unix');
213 Sys::Syslog::openlog('ikiwiki', '', 'user');
217 Sys::Syslog::syslog($type, "[$config{wikiname}] %s", join(" ", @_));
220 elsif (! $config{cgi}) {
224 return print STDERR "@_\n";
228 sub possibly_foolish_untaint ($) { #{{{
230 my ($untainted)=$tainted=~/(.*)/s;
234 sub basename ($) { #{{{
241 sub dirname ($) { #{{{
248 sub pagetype ($) { #{{{
251 if ($page =~ /\.([^.]+)$/) {
252 return $1 if exists $hooks{htmlize}{$1};
257 sub pagename ($) { #{{{
260 my $type=pagetype($file);
262 $page=~s/\Q.$type\E*$// if defined $type;
266 sub targetpage ($$) { #{{{
270 if (! $config{usedirs} || $page =~ /^index$/ ) {
271 return $page.".".$ext;
273 return $page."/index.".$ext;
277 sub htmlpage ($) { #{{{
280 return targetpage($page, $config{htmlext});
283 sub srcfile ($) { #{{{
286 return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
287 return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
288 error("internal error: $file cannot be found in $config{srcdir} or $config{underlaydir}");
292 sub readfile ($;$$) { #{{{
298 error("cannot read a symlink ($file)");
302 open (my $in, "<", $file) || error("failed to read $file: $!");
303 binmode($in) if ($binary);
304 return \*$in if $wantfd;
306 close $in || error("failed to read $file: $!");
310 sub writefile ($$$;$$) { #{{{
311 my $file=shift; # can include subdirs
312 my $destdir=shift; # directory to put file in
318 while (length $test) {
319 if (-l "$destdir/$test") {
320 error("cannot write to a symlink ($test)");
322 $test=dirname($test);
324 my $newfile="$destdir/$file.ikiwiki-new";
326 error("cannot write to a symlink ($newfile)");
329 my $dir=dirname($newfile);
332 foreach my $s (split(m!/+!, $dir)) {
335 mkdir($d) || error("failed to create directory $d: $!");
340 my $cleanup = sub { unlink($newfile) };
341 open (my $out, '>', $newfile) || error("failed to write $newfile: $!", $cleanup);
342 binmode($out) if ($binary);
344 $writer->(\*$out, $cleanup);
347 print $out $content or error("failed writing to $newfile: $!", $cleanup);
349 close $out || error("failed saving $newfile: $!", $cleanup);
350 rename($newfile, "$destdir/$file") ||
351 error("failed renaming $newfile to $destdir/$file: $!", $cleanup);
357 sub will_render ($$;$) { #{{{
362 # Important security check.
363 if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
364 ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}})) {
365 error("$config{destdir}/$dest independently created, not overwriting with version from $page");
368 if (! $clear || $cleared{$page}) {
369 $renderedfiles{$page}=[$dest, grep { $_ ne $dest } @{$renderedfiles{$page}}];
372 foreach my $old (@{$renderedfiles{$page}}) {
373 delete $destsources{$old};
375 $renderedfiles{$page}=[$dest];
378 $destsources{$dest}=$page;
383 sub bestlink ($$) { #{{{
388 if ($link=~s/^\/+//) {
395 $l.="/" if length $l;
398 if (exists $links{$l}) {
401 elsif (exists $pagecase{lc $l}) {
402 return $pagecase{lc $l};
404 } while $cwd=~s!/?[^/]+$!!;
406 if (length $config{userdir}) {
407 my $l = "$config{userdir}/".lc($link);
408 if (exists $links{$l}) {
411 elsif (exists $pagecase{lc $l}) {
412 return $pagecase{lc $l};
416 #print STDERR "warning: page $page, broken link: $link\n";
420 sub isinlinableimage ($) { #{{{
423 return $file =~ /\.(png|gif|jpg|jpeg)$/i;
426 sub pagetitle ($;$) { #{{{
431 $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : chr($2)/eg;
434 $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : "&#$2;"/eg;
440 sub titlepage ($) { #{{{
442 $title=~s/([^-[:alnum:]:+\/.])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
446 sub linkpage ($) { #{{{
448 $link=~s/([^-[:alnum:]:+\/._])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
452 sub cgiurl (@) { #{{{
455 return $config{cgiurl}."?".
456 join("&", map $_."=".uri_escape_utf8($params{$_}), keys %params);
459 sub baseurl (;$) { #{{{
462 return "$config{url}/" if ! defined $page;
464 $page=htmlpage($page);
466 $page=~s/[^\/]+\//..\//g;
470 sub abs2rel ($$) { #{{{
471 # Work around very innefficient behavior in File::Spec if abs2rel
472 # is passed two relative paths. It's much faster if paths are
473 # absolute! (Debian bug #376658; fixed in debian unstable now)
478 my $ret=File::Spec->abs2rel($path, $base);
479 $ret=~s/^// if defined $ret;
483 sub displaytime ($) { #{{{
486 # strftime doesn't know about encodings, so make sure
487 # its output is properly treated as utf8
488 return decode_utf8(POSIX::strftime(
489 $config{timeformat}, localtime($time)));
492 sub beautify_url ($) { #{{{
495 $url =~ s!/index.$config{htmlext}$!/!;
496 $url =~ s!^$!./!; # Browsers don't like empty links...
501 sub urlto ($$) { #{{{
506 return beautify_url(baseurl($from));
509 if (! $destsources{$to}) {
513 my $link = abs2rel($to, dirname(htmlpage($from)));
515 return beautify_url($link);
518 sub htmllink ($$$;@) { #{{{
519 my $lpage=shift; # the page doing the linking
520 my $page=shift; # the page that will contain the link (different for inline)
525 if (! $opts{forcesubpage}) {
526 $bestlink=bestlink($lpage, $link);
529 $bestlink="$lpage/".lc($link);
533 if (defined $opts{linktext}) {
534 $linktext=$opts{linktext};
537 $linktext=pagetitle(basename($link));
540 return "<span class=\"selflink\">$linktext</span>"
541 if length $bestlink && $page eq $bestlink;
543 if (! $destsources{$bestlink}) {
544 $bestlink=htmlpage($bestlink);
546 if (! $destsources{$bestlink}) {
547 return $linktext unless length $config{cgiurl};
548 return "<span><a href=\"".
551 page => pagetitle(lc($link), 1),
554 "\">?</a>$linktext</span>"
558 $bestlink=abs2rel($bestlink, dirname(htmlpage($page)));
559 $bestlink=beautify_url($bestlink);
561 if (! $opts{noimageinline} && isinlinableimage($bestlink)) {
562 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
565 if (defined $opts{anchor}) {
566 $bestlink.="#".$opts{anchor};
570 if (defined $opts{rel}) {
571 push @attrs, ' rel="'.$opts{rel}.'"';
574 return "<a href=\"$bestlink\"@attrs>$linktext</a>";
577 sub htmlize ($$$) { #{{{
582 if (exists $hooks{htmlize}{$type}) {
583 $content=$hooks{htmlize}{$type}{call}->(
589 error("htmlization of $type not supported");
592 run_hooks(sanitize => sub {
602 sub linkify ($$$) { #{{{
603 my $lpage=shift; # the page containing the links
604 my $page=shift; # the page the link will end up on (different for inline)
607 $content =~ s{(\\?)$config{wiki_link_regexp}}{
610 ? "[[$2|$3".($4 ? "#$4" : "")."]]"
611 : htmllink($lpage, $page, linkpage($3),
612 anchor => $4, linktext => pagetitle($2)))
614 ? "[[$3".($4 ? "#$4" : "")."]]"
615 : htmllink($lpage, $page, linkpage($3),
623 our $preprocess_preview=0;
624 sub preprocess ($$$;$$) { #{{{
625 my $page=shift; # the page the data comes from
626 my $destpage=shift; # the page the data will appear in (different for inline)
631 # Using local because it needs to be set within any nested calls
633 local $preprocess_preview=$preview if defined $preview;
639 if (length $escape) {
640 return "[[$command $params]]";
642 elsif (exists $hooks{preprocess}{$command}) {
643 return "" if $scan && ! $hooks{preprocess}{$command}{scan};
644 # Note: preserve order of params, some plugins may
645 # consider it significant.
648 (?:(\w+)=)? # 1: named parameter key?
650 """(.*?)""" # 2: triple-quoted value
652 "([^"]+)" # 3: single-quoted value
654 (\S+) # 4: unquoted value
656 (?:\s+|$) # delimiter to next param
674 push @params, $key, $val;
677 push @params, $val, '';
680 if ($preprocessing{$page}++ > 3) {
681 # Avoid loops of preprocessed pages preprocessing
682 # other pages that preprocess them, etc.
683 #translators: The first parameter is a
684 #translators: preprocessor directive name,
685 #translators: the second a page name, the
686 #translators: third a number.
687 return "[[".sprintf(gettext("%s preprocessing loop detected on %s at depth %i"),
688 $command, $page, $preprocessing{$page}).
691 my $ret=$hooks{preprocess}{$command}{call}->(
694 destpage => $destpage,
695 preview => $preprocess_preview,
697 $preprocessing{$page}--;
701 return "[[$command $params]]";
707 \[\[ # directive open
710 ( # 3: the parameters..
712 (?:\w+=)? # named parameter key?
714 """.*?""" # triple-quoted value
716 "[^"]+" # single-quoted value
718 [^\s\]]+ # unquoted value
720 \s* # whitespace or end
723 *) # 0 or more parameters
724 \]\] # directive closed
725 }{$handle->($1, $2, $3)}sexg;
729 sub filter ($$$) { #{{{
734 run_hooks(filter => sub {
735 $content=shift->(page => $page, destpage => $destpage,
736 content => $content);
742 sub indexlink () { #{{{
743 return "<a href=\"$config{url}\">$config{wikiname}</a>";
748 sub lockwiki (;$) { #{{{
749 my $wait=@_ ? shift : 1;
750 # Take an exclusive lock on the wiki to prevent multiple concurrent
751 # run issues. The lock will be dropped on program exit.
752 if (! -d $config{wikistatedir}) {
753 mkdir($config{wikistatedir});
755 open($wikilock, '>', "$config{wikistatedir}/lockfile") ||
756 error ("cannot write to $config{wikistatedir}/lockfile: $!");
757 if (! flock($wikilock, 2 | 4)) { # LOCK_EX | LOCK_NB
759 debug("wiki seems to be locked, waiting for lock");
760 my $wait=600; # arbitrary, but don't hang forever to
761 # prevent process pileup
763 return if flock($wikilock, 2 | 4);
766 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
775 sub unlockwiki () { #{{{
776 return close($wikilock);
781 sub commit_hook_enabled () { #{{{
782 open($commitlock, '+>', "$config{wikistatedir}/commitlock") ||
783 error("cannot write to $config{wikistatedir}/commitlock: $!");
784 if (! flock($commitlock, 1 | 4)) { # LOCK_SH | LOCK_NB to test
785 close($commitlock) || error("failed closing commitlock: $!");
788 close($commitlock) || error("failed closing commitlock: $!");
792 sub disable_commit_hook () { #{{{
793 open($commitlock, '>', "$config{wikistatedir}/commitlock") ||
794 error("cannot write to $config{wikistatedir}/commitlock: $!");
795 if (! flock($commitlock, 2)) { # LOCK_EX
796 error("failed to get commit lock");
801 sub enable_commit_hook () { #{{{
802 return close($commitlock);
805 sub loadindex () { #{{{
806 open (my $in, "<", "$config{wikistatedir}/index") || return;
808 $_=possibly_foolish_untaint($_);
813 foreach my $i (split(/ /, $_)) {
814 my ($item, $val)=split(/=/, $i, 2);
815 push @{$items{$item}}, decode_entities($val);
818 next unless exists $items{src}; # skip bad lines for now
820 my $page=pagename($items{src}[0]);
821 if (! $config{rebuild}) {
822 $pagesources{$page}=$items{src}[0];
823 $pagemtime{$page}=$items{mtime}[0];
824 $oldlinks{$page}=[@{$items{link}}];
825 $links{$page}=[@{$items{link}}];
826 $depends{$page}=$items{depends}[0] if exists $items{depends};
827 $destsources{$_}=$page foreach @{$items{dest}};
828 $renderedfiles{$page}=[@{$items{dest}}];
829 $pagecase{lc $page}=$page;
831 $oldrenderedfiles{$page}=[@{$items{dest}}];
832 $pagectime{$page}=$items{ctime}[0];
837 sub saveindex () { #{{{
838 run_hooks(savestate => sub { shift->() });
840 if (! -d $config{wikistatedir}) {
841 mkdir($config{wikistatedir});
843 my $newfile="$config{wikistatedir}/index.new";
844 my $cleanup = sub { unlink($newfile) };
845 open (my $out, '>', $newfile) || error("cannot write to $newfile: $!", $cleanup);
846 foreach my $page (keys %pagemtime) {
847 next unless $pagemtime{$page};
848 my $line="mtime=$pagemtime{$page} ".
849 "ctime=$pagectime{$page} ".
850 "src=$pagesources{$page}";
851 $line.=" dest=$_" foreach @{$renderedfiles{$page}};
853 $line.=" link=$_" foreach grep { ++$count{$_} == 1 } @{$links{$page}};
854 if (exists $depends{$page}) {
855 $line.=" depends=".encode_entities($depends{$page}, " \t\n");
857 print $out $line."\n" || error("failed writing to $newfile: $!", $cleanup);
859 close $out || error("failed saving to $newfile: $!", $cleanup);
860 rename($newfile, "$config{wikistatedir}/index") ||
861 error("failed renaming $newfile to $config{wikistatedir}/index", $cleanup);
866 sub template_file ($) { #{{{
869 foreach my $dir ($config{templatedir}, "$installdir/share/ikiwiki/templates") {
870 return "$dir/$template" if -e "$dir/$template";
875 sub template_params (@) { #{{{
876 my $filename=template_file(shift);
878 if (! defined $filename) {
885 my $text_ref = shift;
886 ${$text_ref} = Encode::decode_utf8(${$text_ref});
888 filename => $filename,
889 loop_context_vars => 1,
890 die_on_bad_params => 0,
893 return wantarray ? @ret : {@ret};
896 sub template ($;@) { #{{{
897 require HTML::Template;
898 return HTML::Template->new(template_params(@_));
901 sub misctemplate ($$;@) { #{{{
905 my $template=template("misc.tmpl");
908 indexlink => indexlink(),
909 wikiname => $config{wikiname},
910 pagebody => $pagebody,
911 baseurl => baseurl(),
914 run_hooks(pagetemplate => sub {
915 shift->(page => "", destpage => "", template => $template);
917 return $template->output;
923 if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
924 error 'hook requires type, call, and id parameters';
927 return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
929 $hooks{$param{type}}{$param{id}}=\%param;
933 sub run_hooks ($$) { # {{{
934 # Calls the given sub for each hook of the given type,
935 # passing it the hook function to call.
939 if (exists $hooks{$type}) {
941 foreach my $id (keys %{$hooks{$type}}) {
942 if ($hooks{$type}{$id}{last}) {
946 $sub->($hooks{$type}{$id}{call});
948 foreach my $id (@deferred) {
949 $sub->($hooks{$type}{$id}{call});
956 sub globlist_to_pagespec ($) { #{{{
957 my @globlist=split(' ', shift);
960 foreach my $glob (@globlist) {
961 if ($glob=~/^!(.*)/) {
969 my $spec=join(' or ', @spec);
971 my $skip=join(' and ', @skip);
973 $spec="$skip and ($spec)";
982 sub is_globlist ($) { #{{{
984 return ( $s =~ /[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or" );
987 sub safequote ($) { #{{{
993 sub add_depends ($$) { #{{{
997 if (! exists $depends{$page}) {
998 $depends{$page}=$pagespec;
1001 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
1007 sub file_pruned ($$) { #{{{
1009 my $file=File::Spec->canonpath(shift);
1010 my $base=File::Spec->canonpath(shift);
1011 $file =~ s#^\Q$base\E/*##;
1013 my $regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
1014 return $file =~ m/$regexp/;
1018 # Only use gettext in the rare cases it's needed.
1019 if (exists $ENV{LANG} || exists $ENV{LC_ALL} || exists $ENV{LC_MESSAGES}) {
1020 if (! $gettext_obj) {
1021 $gettext_obj=eval q{
1022 use Locale::gettext q{textdomain};
1023 Locale::gettext->domain('ikiwiki')
1031 return $gettext_obj->get(shift);
1038 sub pagespec_merge ($$) { #{{{
1042 return $a if $a eq $b;
1044 # Support for old-style GlobLists.
1045 if (is_globlist($a)) {
1046 $a=globlist_to_pagespec($a);
1048 if (is_globlist($b)) {
1049 $b=globlist_to_pagespec($b);
1052 return "($a) or ($b)";
1055 sub pagespec_translate ($) { #{{{
1056 # This assumes that $page is in scope in the function
1057 # that evalulates the translated pagespec code.
1060 # Support for old-style GlobLists.
1061 if (is_globlist($spec)) {
1062 $spec=globlist_to_pagespec($spec);
1065 # Convert spec to perl code.
1068 \s* # ignore whitespace
1069 ( # 1: match a single word
1076 \w+\([^\)]*\) # command(params)
1078 [^\s()]+ # any other text
1080 \s* # ignore whitespace
1083 if (lc $word eq 'and') {
1086 elsif (lc $word eq 'or') {
1089 elsif ($word eq "(" || $word eq ")" || $word eq "!") {
1092 elsif ($word =~ /^(\w+)\((.*)\)$/) {
1093 if (exists $IkiWiki::PageSpec::{"match_$1"}) {
1094 $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \@params)";
1101 $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \@params)";
1108 sub pagespec_match ($$;@) { #{{{
1113 # Backwards compatability with old calling convention.
1115 unshift @params, 'location';
1118 my $ret=eval pagespec_translate($spec);
1119 return IkiWiki::FailReason->new('syntax error') if $@;
1123 package IkiWiki::FailReason;
1126 '""' => sub { ${$_[0]} },
1128 '!' => sub { bless $_[0], 'IkiWiki::SuccessReason'},
1133 return bless \$_[1], $_[0];
1136 package IkiWiki::SuccessReason;
1139 '""' => sub { ${$_[0]} },
1141 '!' => sub { bless $_[0], 'IkiWiki::FailReason'},
1146 return bless \$_[1], $_[0];
1149 package IkiWiki::PageSpec;
1151 sub match_glob ($$;@) { #{{{
1156 my $from=exists $params{location} ? $params{location} : '';
1159 if ($glob =~ m!^\./!) {
1160 $from=~s#/?[^/]+$##;
1162 $glob="$from/$glob" if length $from;
1165 # turn glob into safe regexp
1166 $glob=quotemeta($glob);
1170 if ($page=~/^$glob$/i) {
1171 return IkiWiki::SuccessReason->new("$glob matches $page");
1174 return IkiWiki::FailReason->new("$glob does not match $page");
1178 sub match_link ($$;@) { #{{{
1183 my $from=exists $params{location} ? $params{location} : '';
1186 if ($link =~ m!^\.! && defined $from) {
1187 $from=~s#/?[^/]+$##;
1189 $link="$from/$link" if length $from;
1192 my $links = $IkiWiki::links{$page};
1193 return IkiWiki::FailReason->new("$page has no links") unless $links && @{$links};
1194 my $bestlink = IkiWiki::bestlink($from, $link);
1195 foreach my $p (@{$links}) {
1196 if (length $bestlink) {
1197 return IkiWiki::SuccessReason->new("$page links to $link")
1198 if $bestlink eq IkiWiki::bestlink($page, $p);
1201 return IkiWiki::SuccessReason->new("$page links to page matching $link")
1202 if match_glob($p, $link, %params);
1205 return IkiWiki::FailReason->new("$page does not link to $link");
1208 sub match_backlink ($$;@) { #{{{
1209 return match_link($_[1], $_[0], @_);
1212 sub match_created_before ($$;@) { #{{{
1216 if (exists $IkiWiki::pagectime{$testpage}) {
1217 if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
1218 return IkiWiki::SuccessReason->new("$page created before $testpage");
1221 return IkiWiki::FailReason->new("$page not created before $testpage");
1225 return IkiWiki::FailReason->new("$testpage has no ctime");
1229 sub match_created_after ($$;@) { #{{{
1233 if (exists $IkiWiki::pagectime{$testpage}) {
1234 if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
1235 return IkiWiki::SuccessReason->new("$page created after $testpage");
1238 return IkiWiki::FailReason->new("$page not created after $testpage");
1242 return IkiWiki::FailReason->new("$testpage has no ctime");
1246 sub match_creation_day ($$;@) { #{{{
1247 if ((gmtime($IkiWiki::pagectime{shift()}))[3] == shift) {
1248 return IkiWiki::SuccessReason->new('creation_day matched');
1251 return IkiWiki::FailReason->new('creation_day did not match');
1255 sub match_creation_month ($$;@) { #{{{
1256 if ((gmtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
1257 return IkiWiki::SuccessReason->new('creation_month matched');
1260 return IkiWiki::FailReason->new('creation_month did not match');
1264 sub match_creation_year ($$;@) { #{{{
1265 if ((gmtime($IkiWiki::pagectime{shift()}))[5] + 1900 == shift) {
1266 return IkiWiki::SuccessReason->new('creation_year matched');
1269 return IkiWiki::FailReason->new('creation_year did not match');
1273 sub match_user ($$;@) { #{{{
1278 return IkiWiki::FailReason->new('cannot match user')
1279 unless exists $params{user};
1280 if ($user eq $params{user}) {
1281 return IkiWiki::SuccessReason->new("user is $user")
1284 return IkiWiki::FailReason->new("user is not $user");