9 use URI::Escape q{uri_escape_utf8};
12 use open qw{:utf8 :std};
14 use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
15 %pagestate %wikistate %renderedfiles %oldrenderedfiles
16 %pagesources %destsources %depends %depends_simple @mass_depends
17 %hooks %forcerebuild %loaded_plugins %typedlinks %oldtypedlinks
20 use Exporter q{import};
21 our @EXPORT = qw(hook debug error htmlpage template template_depends
22 deptype add_depends pagespec_match pagespec_match_list bestlink
23 htmllink readfile writefile pagetype srcfile pagename
24 displaytime will_render gettext ngettext urlto targetpage
25 add_underlay pagetitle titlepage linkpage newpagefile
26 inject add_link add_autofile
27 %config %links %pagestate %wikistate %renderedfiles
28 %pagesources %destsources %typedlinks);
29 our $VERSION = 3.00; # plugin interface version, next is ikiwiki version
30 our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
31 our $installdir='/usr'; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
33 # Page dependency types.
34 our $DEPEND_CONTENT=1;
35 our $DEPEND_PRESENCE=2;
41 memoize("sortspec_translate");
42 memoize("pagespec_translate");
43 memoize("template_file");
49 description => "name of the wiki",
56 example => 'me@example.com',
57 description => "contact email for wiki",
64 description => "users who are wiki admins",
71 description => "users who are banned from the wiki",
78 example => "$ENV{HOME}/wiki",
79 description => "where the source of the wiki is located",
86 example => "/var/www/wiki",
87 description => "where to build the wiki",
94 example => "http://example.com/wiki",
95 description => "base url to the wiki",
102 example => "http://example.com/wiki/ikiwiki.cgi",
103 description => "url to the ikiwiki.cgi",
110 example => "/var/www/wiki/ikiwiki.cgi",
111 description => "filename of cgi wrapper to generate",
118 description => "mode for cgi_wrapper (can safely be made suid)",
125 description => "rcs backend to use",
126 safe => 0, # don't allow overriding
131 default => [qw{mdwn link inline meta htmlscrubber passwordauth
132 openid signinedit lockedit conditional
133 recentchanges parentlinks editpage}],
134 description => "plugins to enable by default",
141 description => "plugins to add to the default configuration",
148 description => "plugins to disable",
154 default => "$installdir/share/ikiwiki/templates",
155 description => "additional directory to search for template files",
162 default => "$installdir/share/ikiwiki/basewiki",
163 description => "base wiki source location",
170 default => "$installdir/share/ikiwiki",
171 description => "parent directory containing additional underlays",
178 description => "wrappers to generate",
185 description => "additional underlays to use",
192 description => "display verbose messages?",
199 description => "log to syslog?",
206 description => "create output files named page/index.html?",
207 safe => 0, # changing requires manual transition
210 prefix_directives => {
213 description => "use '!'-prefixed preprocessor directives?",
214 safe => 0, # changing requires manual transition
220 description => "use page/index.mdwn source files",
227 description => "enable Discussion pages?",
233 default => gettext("Discussion"),
234 description => "name of Discussion pages",
241 description => "only send cookies over SSL connections?",
249 description => "extension to use for new pages",
250 safe => 0, # not sanitized
256 description => "extension to use for html files",
257 safe => 0, # not sanitized
263 description => "strftime format string to display date",
271 example => "en_US.UTF-8",
272 description => "UTF-8 locale to use",
281 description => "put user pages below specified page",
288 description => "how many backlinks to show before hiding excess (0 to show all)",
295 description => "attempt to hardlink source files? (optimisation for large files)",
297 safe => 0, # paranoia
303 description => "force ikiwiki to use a particular umask",
305 safe => 0, # paranoia
310 example => "ikiwiki",
311 description => "group for wrappers to run in",
313 safe => 0, # paranoia
319 example => "$ENV{HOME}/.ikiwiki/",
320 description => "extra library and plugin directory",
322 safe => 0, # directory
328 description => "environment variables",
329 safe => 0, # paranoia
335 example => '^\.htaccess$',
336 description => "regexp of normally excluded files to include",
344 example => '^(*\.private|Makefile)$',
345 description => "regexp of files that should be skipped",
350 wiki_file_prune_regexps => {
352 default => [qr/(^|\/)\.\.(\/|$)/, qr/^\//, qr/^\./, qr/\/\./,
353 qr/\.x?html?$/, qr/\.ikiwiki-new$/,
354 qr/(^|\/).svn\//, qr/.arch-ids\//, qr/{arch}\//,
355 qr/(^|\/)_MTN\//, qr/(^|\/)_darcs\//,
356 qr/(^|\/)CVS\//, qr/\.dpkg-tmp$/],
357 description => "regexps of source files to ignore",
363 description => "specifies the characters that are allowed in source filenames",
364 default => "-[:alnum:]+/.:_",
368 wiki_file_regexp => {
370 description => "regexp of legal source files",
374 web_commit_regexp => {
376 default => qr/^web commit (by (.*?(?=: |$))|from ([0-9a-fA-F:.]+[0-9a-fA-F])):?(.*)/,
377 description => "regexp to parse web commits from logs",
384 description => "run as a cgi",
388 cgi_disable_uploads => {
391 description => "whether CGI should accept file uploads",
398 description => "run as a post-commit hook",
405 description => "running in rebuild mode",
412 description => "running in setup mode",
419 description => "running in clean mode",
426 description => "running in refresh mode",
433 description => "running in receive test mode",
439 description => "running in gettime mode",
446 description => "running in w3mmode",
453 description => "path to the .ikiwiki directory holding ikiwiki state",
460 description => "path to setup file",
466 default => "Standard",
467 description => "perl class to use to dump setup file",
471 allow_symlinks_before_srcdir => {
474 description => "allow symlinks in the path leading to the srcdir (potentially insecure)",
480 sub defaultconfig () {
483 foreach my $key (keys %s) {
484 push @ret, $key, $s{$key}->{default};
491 # locale stuff; avoid LC_ALL since it overrides everything
492 if (defined $ENV{LC_ALL}) {
493 $ENV{LANG} = $ENV{LC_ALL};
496 if (defined $config{locale}) {
497 if (POSIX::setlocale(&POSIX::LC_ALL, $config{locale})) {
498 $ENV{LANG}=$config{locale};
503 if (! defined $config{wiki_file_regexp}) {
504 $config{wiki_file_regexp}=qr/(^[$config{wiki_file_chars}]+$)/;
507 if (ref $config{ENV} eq 'HASH') {
508 foreach my $val (keys %{$config{ENV}}) {
509 $ENV{$val}=$config{ENV}{$val};
513 if ($config{w3mmode}) {
514 eval q{use Cwd q{abs_path}};
516 $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir}));
517 $config{destdir}=possibly_foolish_untaint(abs_path($config{destdir}));
518 $config{cgiurl}="file:///\$LIB/ikiwiki-w3m.cgi/".$config{cgiurl}
519 unless $config{cgiurl} =~ m!file:///!;
520 $config{url}="file://".$config{destdir};
523 if ($config{cgi} && ! length $config{url}) {
524 error(gettext("Must specify url to wiki with --url when using --cgi"));
527 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
528 unless exists $config{wikistatedir} && defined $config{wikistatedir};
530 if (defined $config{umask}) {
531 umask(possibly_foolish_untaint($config{umask}));
534 run_hooks(checkconfig => sub { shift->() });
542 foreach my $dir (@INC, $config{libdir}) {
543 next unless defined $dir && length $dir;
544 foreach my $file (glob("$dir/IkiWiki/Plugin/*.pm")) {
545 my ($plugin)=$file=~/.*\/(.*)\.pm$/;
549 foreach my $dir ($config{libdir}, "$installdir/lib/ikiwiki") {
550 next unless defined $dir && length $dir;
551 foreach my $file (glob("$dir/plugins/*")) {
552 $ret{basename($file)}=1 if -x $file;
560 if (defined $config{libdir} && length $config{libdir}) {
561 unshift @INC, possibly_foolish_untaint($config{libdir});
564 foreach my $plugin (@{$config{default_plugins}}, @{$config{add_plugins}}) {
569 if (exists $hooks{rcs}) {
570 error(gettext("cannot use multiple rcs plugins"));
572 loadplugin($config{rcs});
574 if (! exists $hooks{rcs}) {
578 run_hooks(getopt => sub { shift->() });
579 if (grep /^-/, @ARGV) {
580 print STDERR "Unknown option (or missing parameter): $_\n"
581 foreach grep /^-/, @ARGV;
591 return if grep { $_ eq $plugin} @{$config{disable_plugins}};
593 foreach my $dir (defined $config{libdir} ? possibly_foolish_untaint($config{libdir}) : undef,
594 "$installdir/lib/ikiwiki") {
595 if (defined $dir && -x "$dir/plugins/$plugin") {
596 eval { require IkiWiki::Plugin::external };
599 error(sprintf(gettext("failed to load external plugin needed for %s plugin: %s"), $plugin, $reason));
601 import IkiWiki::Plugin::external "$dir/plugins/$plugin";
602 $loaded_plugins{$plugin}=1;
607 my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
610 error("Failed to load plugin $mod: $@");
612 $loaded_plugins{$plugin}=1;
619 log_message('err' => $message) if $config{syslog};
620 if (defined $cleaner) {
627 return unless $config{verbose};
628 return log_message(debug => @_);
632 sub log_message ($$) {
635 if ($config{syslog}) {
638 Sys::Syslog::setlogsock('unix');
639 Sys::Syslog::openlog('ikiwiki', '', 'user');
643 Sys::Syslog::syslog($type, "[$config{wikiname}] %s", join(" ", @_));
646 elsif (! $config{cgi}) {
650 return print STDERR "@_\n";
654 sub possibly_foolish_untaint ($) {
656 my ($untainted)=$tainted=~/(.*)/s;
676 return exists $pagesources{$page} &&
677 $pagesources{$page} =~ /\._([^.]+)$/;
683 if ($file =~ /\.([^.]+)$/) {
684 return $1 if exists $hooks{htmlize}{$1};
686 my $base=basename($file);
687 if (exists $hooks{htmlize}{$base} &&
688 $hooks{htmlize}{$base}{noextension}) {
699 if (exists $pagename_cache{$file}) {
700 return $pagename_cache{$file};
703 my $type=pagetype($file);
705 $page=~s/\Q.$type\E*$//
706 if defined $type && !$hooks{htmlize}{$type}{keepextension}
707 && !$hooks{htmlize}{$type}{noextension};
708 if ($config{indexpages} && $page=~/(.*)\/index$/) {
712 $pagename_cache{$file} = $page;
716 sub newpagefile ($$) {
720 if (! $config{indexpages} || $page eq 'index') {
721 return $page.".".$type;
724 return $page."/index.".$type;
728 sub targetpage ($$;$) {
733 if (defined $filename) {
734 return $page."/".$filename.".".$ext;
736 elsif (! $config{usedirs} || $page eq 'index') {
737 return $page.".".$ext;
740 return $page."/index.".$ext;
747 return targetpage($page, $config{htmlext});
754 return "$config{srcdir}/$file", stat(_) if -e "$config{srcdir}/$file";
755 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
756 return "$dir/$file", stat(_) if -e "$dir/$file";
758 error("internal error: $file cannot be found in $config{srcdir} or underlay") unless $nothrow;
763 return (srcfile_stat(@_))[0];
766 sub add_underlay ($) {
770 $dir="$config{underlaydirbase}/$dir";
773 if (! grep { $_ eq $dir } @{$config{underlaydirs}}) {
774 unshift @{$config{underlaydirs}}, $dir;
780 sub readfile ($;$$) {
786 error("cannot read a symlink ($file)");
790 open (my $in, "<", $file) || error("failed to read $file: $!");
791 binmode($in) if ($binary);
792 return \*$in if $wantfd;
794 # check for invalid utf-8, and toss it back to avoid crashes
795 if (! utf8::valid($ret)) {
796 $ret=encode_utf8($ret);
798 close $in || error("failed to read $file: $!");
802 sub prep_writefile ($$) {
807 while (length $test) {
808 if (-l "$destdir/$test") {
809 error("cannot write to a symlink ($test)");
811 $test=dirname($test);
814 my $dir=dirname("$destdir/$file");
817 foreach my $s (split(m!/+!, $dir)) {
820 mkdir($d) || error("failed to create directory $d: $!");
828 sub writefile ($$$;$$) {
829 my $file=shift; # can include subdirs
830 my $destdir=shift; # directory to put file in
835 prep_writefile($file, $destdir);
837 my $newfile="$destdir/$file.ikiwiki-new";
839 error("cannot write to a symlink ($newfile)");
842 my $cleanup = sub { unlink($newfile) };
843 open (my $out, '>', $newfile) || error("failed to write $newfile: $!", $cleanup);
844 binmode($out) if ($binary);
846 $writer->(\*$out, $cleanup);
849 print $out $content or error("failed writing to $newfile: $!", $cleanup);
851 close $out || error("failed saving $newfile: $!", $cleanup);
852 rename($newfile, "$destdir/$file") ||
853 error("failed renaming $newfile to $destdir/$file: $!", $cleanup);
859 sub will_render ($$;$) {
864 # Important security check.
865 if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
866 ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}}, @{$wikistate{editpage}{previews}})) {
867 error("$config{destdir}/$dest independently created, not overwriting with version from $page");
870 if (! $clear || $cleared{$page}) {
871 $renderedfiles{$page}=[$dest, grep { $_ ne $dest } @{$renderedfiles{$page}}];
874 foreach my $old (@{$renderedfiles{$page}}) {
875 delete $destsources{$old};
877 $renderedfiles{$page}=[$dest];
880 $destsources{$dest}=$page;
890 if ($link=~s/^\/+//) {
898 $l.="/" if length $l;
901 if (exists $pagesources{$l}) {
904 elsif (exists $pagecase{lc $l}) {
905 return $pagecase{lc $l};
907 } while $cwd=~s{/?[^/]+$}{};
909 if (length $config{userdir}) {
910 my $l = "$config{userdir}/".lc($link);
911 if (exists $pagesources{$l}) {
914 elsif (exists $pagecase{lc $l}) {
915 return $pagecase{lc $l};
919 #print STDERR "warning: page $page, broken link: $link\n";
923 sub isinlinableimage ($) {
926 return $file =~ /\.(png|gif|jpg|jpeg)$/i;
929 sub pagetitle ($;$) {
934 $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : chr($2)/eg;
937 $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : "&#$2;"/eg;
945 # support use w/o %config set
946 my $chars = defined $config{wiki_file_chars} ? $config{wiki_file_chars} : "-[:alnum:]+/.:_";
947 $title=~s/([^$chars]|_)/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
953 my $chars = defined $config{wiki_file_chars} ? $config{wiki_file_chars} : "-[:alnum:]+/.:_";
954 $link=~s/([^$chars])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
961 my $cgiurl=$config{cgiurl};
962 if (exists $params{cgiurl}) {
963 $cgiurl=$params{cgiurl};
964 delete $params{cgiurl};
967 join("&", map $_."=".uri_escape_utf8($params{$_}), keys %params);
973 return "$config{url}/" if ! defined $page;
975 $page=htmlpage($page);
977 $page=~s/[^\/]+\//..\//g;
982 # Work around very innefficient behavior in File::Spec if abs2rel
983 # is passed two relative paths. It's much faster if paths are
984 # absolute! (Debian bug #376658; fixed in debian unstable now)
989 my $ret=File::Spec->abs2rel($path, $base);
990 $ret=~s/^// if defined $ret;
994 sub displaytime ($;$) {
995 # Plugins can override this function to mark up the time to
997 return '<span class="date">'.formattime(@_).'</span>';
1000 sub formattime ($;$) {
1001 # Plugins can override this function to format the time.
1004 if (! defined $format) {
1005 $format=$config{timeformat};
1008 # strftime doesn't know about encodings, so make sure
1009 # its output is properly treated as utf8
1010 return decode_utf8(POSIX::strftime($format, localtime($time)));
1013 sub beautify_urlpath ($) {
1016 # Ensure url is not an empty link, and if necessary,
1017 # add ./ to avoid colon confusion.
1018 if ($url !~ /^\// && $url !~ /^\.\.?\//) {
1022 if ($config{usedirs}) {
1023 $url =~ s!/index.$config{htmlext}$!/!;
1035 return beautify_urlpath(baseurl($from)."index.$config{htmlext}");
1038 if (! $destsources{$to}) {
1043 return $config{url}.beautify_urlpath("/".$to);
1046 my $link = abs2rel($to, dirname(htmlpage($from)));
1048 return beautify_urlpath($link);
1051 sub htmllink ($$$;@) {
1052 my $lpage=shift; # the page doing the linking
1053 my $page=shift; # the page that will contain the link (different for inline)
1060 if (! $opts{forcesubpage}) {
1061 $bestlink=bestlink($lpage, $link);
1064 $bestlink="$lpage/".lc($link);
1068 if (defined $opts{linktext}) {
1069 $linktext=$opts{linktext};
1072 $linktext=pagetitle(basename($link));
1075 return "<span class=\"selflink\">$linktext</span>"
1076 if length $bestlink && $page eq $bestlink &&
1077 ! defined $opts{anchor};
1079 if (! $destsources{$bestlink}) {
1080 $bestlink=htmlpage($bestlink);
1082 if (! $destsources{$bestlink}) {
1084 if (length $config{cgiurl}) {
1085 $cgilink = "<a href=\"".
1090 )."\" rel=\"nofollow\">?</a>";
1092 return "<span class=\"createlink\">$cgilink$linktext</span>"
1096 $bestlink=abs2rel($bestlink, dirname(htmlpage($page)));
1097 $bestlink=beautify_urlpath($bestlink);
1099 if (! $opts{noimageinline} && isinlinableimage($bestlink)) {
1100 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
1103 if (defined $opts{anchor}) {
1104 $bestlink.="#".$opts{anchor};
1108 foreach my $attr (qw{rel class title}) {
1109 if (defined $opts{$attr}) {
1110 push @attrs, " $attr=\"$opts{$attr}\"";
1114 return "<a href=\"$bestlink\"@attrs>$linktext</a>";
1119 return length $config{userdir} ? "$config{userdir}/$user" : $user;
1122 sub openiduser ($) {
1125 if ($user =~ m!^https?://! &&
1126 eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
1129 if (Net::OpenID::VerifiedIdentity->can("DisplayOfURL")) {
1130 $display = Net::OpenID::VerifiedIdentity::DisplayOfURL($user);
1133 # backcompat with old version
1134 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
1135 $display=$oid->display;
1138 # Convert "user.somehost.com" to "user [somehost.com]"
1139 # (also "user.somehost.co.uk")
1140 if ($display !~ /\[/) {
1141 $display=~s/^([-a-zA-Z0-9]+?)\.([-.a-zA-Z0-9]+\.[a-z]+)$/$1 [$2]/;
1143 # Convert "http://somehost.com/user" to "user [somehost.com]".
1144 # (also "https://somehost.com/user/")
1145 if ($display !~ /\[/) {
1146 $display=~s/^https?:\/\/(.+)\/([^\/#?]+)\/?(?:[#?].*)?$/$2 [$1]/;
1148 $display=~s!^https?://!!; # make sure this is removed
1149 eval q{use CGI 'escapeHTML'};
1151 return escapeHTML($display);
1156 sub htmlize ($$$$) {
1162 my $oneline = $content !~ /\n/;
1164 if (exists $hooks{htmlize}{$type}) {
1165 $content=$hooks{htmlize}{$type}{call}->(
1167 content => $content,
1171 error("htmlization of $type not supported");
1174 run_hooks(sanitize => sub {
1177 destpage => $destpage,
1178 content => $content,
1183 # hack to get rid of enclosing junk added by markdown
1184 # and other htmlizers/sanitizers
1185 $content=~s/^<p>//i;
1186 $content=~s/<\/p>\n*$//i;
1197 run_hooks(linkify => sub {
1200 destpage => $destpage,
1201 content => $content,
1209 our $preprocess_preview=0;
1210 sub preprocess ($$$;$$) {
1211 my $page=shift; # the page the data comes from
1212 my $destpage=shift; # the page the data will appear in (different for inline)
1217 # Using local because it needs to be set within any nested calls
1219 local $preprocess_preview=$preview if defined $preview;
1226 $params="" if ! defined $params;
1228 if (length $escape) {
1229 return "[[$prefix$command $params]]";
1231 elsif (exists $hooks{preprocess}{$command}) {
1232 return "" if $scan && ! $hooks{preprocess}{$command}{scan};
1233 # Note: preserve order of params, some plugins may
1234 # consider it significant.
1236 while ($params =~ m{
1237 (?:([-\w]+)=)? # 1: named parameter key?
1239 """(.*?)""" # 2: triple-quoted value
1241 "([^"]*?)" # 3: single-quoted value
1243 (\S+) # 4: unquoted value
1245 (?:\s+|$) # delimiter to next param
1255 elsif (defined $3) {
1258 elsif (defined $4) {
1263 push @params, $key, $val;
1266 push @params, $val, '';
1269 if ($preprocessing{$page}++ > 3) {
1270 # Avoid loops of preprocessed pages preprocessing
1271 # other pages that preprocess them, etc.
1272 return "[[!$command <span class=\"error\">".
1273 sprintf(gettext("preprocessing loop detected on %s at depth %i"),
1274 $page, $preprocessing{$page}).
1280 $hooks{preprocess}{$command}{call}->(
1283 destpage => $destpage,
1284 preview => $preprocess_preview,
1290 $ret="[[!$command <span class=\"error\">".
1291 gettext("Error").": $error"."</span>]]";
1295 # use void context during scan pass
1297 $hooks{preprocess}{$command}{call}->(
1300 destpage => $destpage,
1301 preview => $preprocess_preview,
1306 $preprocessing{$page}--;
1310 return "[[$prefix$command $params]]";
1315 if ($config{prefix_directives}) {
1318 \[\[(!) # directive open; 2: prefix
1319 ([-\w]+) # 3: command
1320 ( # 4: the parameters..
1321 \s+ # Must have space if parameters present
1323 (?:[-\w]+=)? # named parameter key?
1325 """.*?""" # triple-quoted value
1327 "[^"]*?" # single-quoted value
1329 [^"\s\]]+ # unquoted value
1331 \s* # whitespace or end
1334 *)? # 0 or more parameters
1335 \]\] # directive closed
1341 \[\[(!?) # directive open; 2: optional prefix
1342 ([-\w]+) # 3: command
1344 ( # 4: the parameters..
1346 (?:[-\w]+=)? # named parameter key?
1348 """.*?""" # triple-quoted value
1350 "[^"]*?" # single-quoted value
1352 [^"\s\]]+ # unquoted value
1354 \s* # whitespace or end
1357 *) # 0 or more parameters
1358 \]\] # directive closed
1362 $content =~ s{$regex}{$handle->($1, $2, $3, $4)}eg;
1371 run_hooks(filter => sub {
1372 $content=shift->(page => $page, destpage => $destpage,
1373 content => $content);
1380 return "<a href=\"$config{url}\">$config{wikiname}</a>";
1383 sub check_canedit ($$$;$) {
1390 run_hooks(canedit => sub {
1391 return if defined $canedit;
1392 my $ret=shift->($page, $q, $session);
1397 elsif (ref $ret eq 'CODE') {
1398 $ret->() unless $nonfatal;
1401 elsif (defined $ret) {
1402 error($ret) unless $nonfatal;
1407 return defined $canedit ? $canedit : 1;
1410 sub check_content (@) {
1413 return 1 if ! exists $hooks{checkcontent}; # optimisation
1415 if (exists $pagesources{$params{page}}) {
1417 my %old=map { $_ => 1 }
1418 split("\n", readfile(srcfile($pagesources{$params{page}})));
1419 foreach my $line (split("\n", $params{content})) {
1420 push @diff, $line if ! exists $old{$line};
1422 $params{diff}=join("\n", @diff);
1426 run_hooks(checkcontent => sub {
1427 return if defined $ok;
1428 my $ret=shift->(%params);
1433 elsif (ref $ret eq 'CODE') {
1434 $ret->() unless $params{nonfatal};
1437 elsif (defined $ret) {
1438 error($ret) unless $params{nonfatal};
1444 return defined $ok ? $ok : 1;
1450 # Take an exclusive lock on the wiki to prevent multiple concurrent
1451 # run issues. The lock will be dropped on program exit.
1452 if (! -d $config{wikistatedir}) {
1453 mkdir($config{wikistatedir});
1455 open($wikilock, '>', "$config{wikistatedir}/lockfile") ||
1456 error ("cannot write to $config{wikistatedir}/lockfile: $!");
1457 if (! flock($wikilock, 2)) { # LOCK_EX
1458 error("failed to get lock");
1464 POSIX::close($ENV{IKIWIKI_CGILOCK_FD}) if exists $ENV{IKIWIKI_CGILOCK_FD};
1465 return close($wikilock) if $wikilock;
1471 sub commit_hook_enabled () {
1472 open($commitlock, '+>', "$config{wikistatedir}/commitlock") ||
1473 error("cannot write to $config{wikistatedir}/commitlock: $!");
1474 if (! flock($commitlock, 1 | 4)) { # LOCK_SH | LOCK_NB to test
1475 close($commitlock) || error("failed closing commitlock: $!");
1478 close($commitlock) || error("failed closing commitlock: $!");
1482 sub disable_commit_hook () {
1483 open($commitlock, '>', "$config{wikistatedir}/commitlock") ||
1484 error("cannot write to $config{wikistatedir}/commitlock: $!");
1485 if (! flock($commitlock, 2)) { # LOCK_EX
1486 error("failed to get commit lock");
1491 sub enable_commit_hook () {
1492 return close($commitlock) if $commitlock;
1497 %oldrenderedfiles=%pagectime=();
1498 if (! $config{rebuild}) {
1499 %pagesources=%pagemtime=%oldlinks=%links=%depends=
1500 %destsources=%renderedfiles=%pagecase=%pagestate=
1501 %depends_simple=%typedlinks=%oldtypedlinks=();
1504 if (! open ($in, "<", "$config{wikistatedir}/indexdb")) {
1505 if (-e "$config{wikistatedir}/index") {
1506 system("ikiwiki-transition", "indexdb", $config{srcdir});
1507 open ($in, "<", "$config{wikistatedir}/indexdb") || return;
1510 $config{gettime}=1; # first build
1515 my $index=Storable::fd_retrieve($in);
1516 if (! defined $index) {
1521 if (exists $index->{version} && ! ref $index->{version}) {
1522 $pages=$index->{page};
1523 %wikistate=%{$index->{state}};
1530 foreach my $src (keys %$pages) {
1531 my $d=$pages->{$src};
1532 my $page=pagename($src);
1533 $pagectime{$page}=$d->{ctime};
1534 $pagesources{$page}=$src;
1535 if (! $config{rebuild}) {
1536 $pagemtime{$page}=$d->{mtime};
1537 $renderedfiles{$page}=$d->{dest};
1538 if (exists $d->{links} && ref $d->{links}) {
1539 $links{$page}=$d->{links};
1540 $oldlinks{$page}=[@{$d->{links}}];
1542 if (ref $d->{depends_simple} eq 'ARRAY') {
1544 $depends_simple{$page}={
1545 map { $_ => 1 } @{$d->{depends_simple}}
1548 elsif (exists $d->{depends_simple}) {
1549 $depends_simple{$page}=$d->{depends_simple};
1551 if (exists $d->{dependslist}) {
1554 map { $_ => $DEPEND_CONTENT }
1555 @{$d->{dependslist}}
1558 elsif (exists $d->{depends} && ! ref $d->{depends}) {
1560 $depends{$page}={$d->{depends} => $DEPEND_CONTENT };
1562 elsif (exists $d->{depends}) {
1563 $depends{$page}=$d->{depends};
1565 if (exists $d->{state}) {
1566 $pagestate{$page}=$d->{state};
1568 if (exists $d->{typedlinks}) {
1569 $typedlinks{$page}=$d->{typedlinks};
1571 while (my ($type, $links) = each %{$typedlinks{$page}}) {
1572 next unless %$links;
1573 $oldtypedlinks{$page}{$type} = {%$links};
1577 $oldrenderedfiles{$page}=[@{$d->{dest}}];
1579 foreach my $page (keys %pagesources) {
1580 $pagecase{lc $page}=$page;
1582 foreach my $page (keys %renderedfiles) {
1583 $destsources{$_}=$page foreach @{$renderedfiles{$page}};
1589 run_hooks(savestate => sub { shift->() });
1592 foreach my $type (keys %hooks) {
1593 $hookids{$_}=1 foreach keys %{$hooks{$type}};
1595 my @hookids=keys %hookids;
1597 if (! -d $config{wikistatedir}) {
1598 mkdir($config{wikistatedir});
1600 my $newfile="$config{wikistatedir}/indexdb.new";
1601 my $cleanup = sub { unlink($newfile) };
1602 open (my $out, '>', $newfile) || error("cannot write to $newfile: $!", $cleanup);
1605 foreach my $page (keys %pagemtime) {
1606 next unless $pagemtime{$page};
1607 my $src=$pagesources{$page};
1609 $index{page}{$src}={
1610 ctime => $pagectime{$page},
1611 mtime => $pagemtime{$page},
1612 dest => $renderedfiles{$page},
1613 links => $links{$page},
1616 if (exists $depends{$page}) {
1617 $index{page}{$src}{depends} = $depends{$page};
1620 if (exists $depends_simple{$page}) {
1621 $index{page}{$src}{depends_simple} = $depends_simple{$page};
1624 if (exists $typedlinks{$page} && %{$typedlinks{$page}}) {
1625 $index{page}{$src}{typedlinks} = $typedlinks{$page};
1628 if (exists $pagestate{$page}) {
1629 foreach my $id (@hookids) {
1630 foreach my $key (keys %{$pagestate{$page}{$id}}) {
1631 $index{page}{$src}{state}{$id}{$key}=$pagestate{$page}{$id}{$key};
1638 foreach my $id (@hookids) {
1639 foreach my $key (keys %{$wikistate{$id}}) {
1640 $index{state}{$id}{$key}=$wikistate{$id}{$key};
1644 $index{version}="3";
1645 my $ret=Storable::nstore_fd(\%index, $out);
1646 return if ! defined $ret || ! $ret;
1647 close $out || error("failed saving to $newfile: $!", $cleanup);
1648 rename($newfile, "$config{wikistatedir}/indexdb") ||
1649 error("failed renaming $newfile to $config{wikistatedir}/indexdb", $cleanup);
1654 sub template_file ($) {
1657 my $tpage=($name =~ s/^\///) ? $name : "templates/$name";
1658 if ($name !~ /\.tmpl$/ && exists $pagesources{$tpage}) {
1659 $tpage=$pagesources{$tpage};
1663 my $template=srcfile($tpage, 1);
1664 if (defined $template) {
1665 return $template, $tpage, 1 if wantarray;
1669 $name=~s:/::; # avoid path traversal
1670 foreach my $dir ($config{templatedir},
1671 "$installdir/share/ikiwiki/templates") {
1672 if (-e "$dir/$name") {
1673 $template="$dir/$name";
1677 if (defined $template) {
1678 return $template, $tpage if wantarray;
1686 sub template_depends ($$;@) {
1690 my ($filename, $tpage, $untrusted)=template_file($name);
1691 if (defined $page && defined $tpage) {
1692 add_depends($page, $tpage);
1695 return unless defined $filename;
1699 my $text_ref = shift;
1700 ${$text_ref} = decode_utf8(${$text_ref});
1702 loop_context_vars => 1,
1703 die_on_bad_params => 0,
1704 filename => $filename,
1706 ($untrusted ? (no_includes => 1) : ()),
1708 return @opts if wantarray;
1710 require HTML::Template;
1711 return HTML::Template->new(@opts);
1714 sub template ($;@) {
1715 template_depends(shift, undef, @_);
1718 sub misctemplate ($$;@) {
1722 my $template=template("misc.tmpl");
1725 indexlink => indexlink(),
1726 wikiname => $config{wikiname},
1727 pagebody => $pagebody,
1728 baseurl => baseurl(),
1731 run_hooks(pagetemplate => sub {
1732 shift->(page => "", destpage => "", template => $template);
1734 return $template->output;
1740 if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
1741 error 'hook requires type, call, and id parameters';
1744 return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
1746 $hooks{$param{type}}{$param{id}}=\%param;
1750 sub run_hooks ($$) {
1751 # Calls the given sub for each hook of the given type,
1752 # passing it the hook function to call.
1756 if (exists $hooks{$type}) {
1757 my (@first, @middle, @last);
1758 foreach my $id (keys %{$hooks{$type}}) {
1759 if ($hooks{$type}{$id}{first}) {
1762 elsif ($hooks{$type}{$id}{last}) {
1769 foreach my $id (@first, @middle, @last) {
1770 $sub->($hooks{$type}{$id}{call});
1778 $hooks{rcs}{rcs_update}{call}->(@_);
1781 sub rcs_prepedit ($) {
1782 $hooks{rcs}{rcs_prepedit}{call}->(@_);
1785 sub rcs_commit ($$$;$$) {
1786 $hooks{rcs}{rcs_commit}{call}->(@_);
1789 sub rcs_commit_staged ($$$) {
1790 $hooks{rcs}{rcs_commit_staged}{call}->(@_);
1794 $hooks{rcs}{rcs_add}{call}->(@_);
1797 sub rcs_remove ($) {
1798 $hooks{rcs}{rcs_remove}{call}->(@_);
1801 sub rcs_rename ($$) {
1802 $hooks{rcs}{rcs_rename}{call}->(@_);
1805 sub rcs_recentchanges ($) {
1806 $hooks{rcs}{rcs_recentchanges}{call}->(@_);
1810 $hooks{rcs}{rcs_diff}{call}->(@_);
1813 sub rcs_getctime ($) {
1814 $hooks{rcs}{rcs_getctime}{call}->(@_);
1817 sub rcs_getmtime ($) {
1818 $hooks{rcs}{rcs_getmtime}{call}->(@_);
1821 sub rcs_receive () {
1822 $hooks{rcs}{rcs_receive}{call}->();
1825 sub add_depends ($$;$) {
1828 my $deptype=shift || $DEPEND_CONTENT;
1830 # Is the pagespec a simple page name?
1831 if ($pagespec =~ /$config{wiki_file_regexp}/ &&
1832 $pagespec !~ /[\s*?()!]/) {
1833 $depends_simple{$page}{lc $pagespec} |= $deptype;
1837 # Add explicit dependencies for influences.
1838 my $sub=pagespec_translate($pagespec);
1839 return unless defined $sub;
1840 foreach my $p (keys %pagesources) {
1841 my $r=$sub->($p, location => $page);
1842 my $i=$r->influences;
1843 my $static=$r->influences_static;
1844 foreach my $k (keys %$i) {
1845 next unless $r || $static || $k eq $page;
1846 $depends_simple{$page}{lc $k} |= $i->{$k};
1851 $depends{$page}{$pagespec} |= $deptype;
1857 foreach my $type (@_) {
1858 if ($type eq 'presence') {
1859 $deptype |= $DEPEND_PRESENCE;
1861 elsif ($type eq 'links') {
1862 $deptype |= $DEPEND_LINKS;
1864 elsif ($type eq 'content') {
1865 $deptype |= $DEPEND_CONTENT;
1871 my $file_prune_regexp;
1872 sub file_pruned ($) {
1875 if (defined $config{include} && length $config{include}) {
1876 return 0 if $file =~ m/$config{include}/;
1879 if (! defined $file_prune_regexp) {
1880 $file_prune_regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
1881 $file_prune_regexp=qr/$file_prune_regexp/;
1883 return $file =~ m/$file_prune_regexp/;
1886 sub define_gettext () {
1887 # If translation is needed, redefine the gettext function to do it.
1888 # Otherwise, it becomes a quick no-op.
1891 if ((exists $ENV{LANG} && length $ENV{LANG}) ||
1892 (exists $ENV{LC_ALL} && length $ENV{LC_ALL}) ||
1893 (exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES})) {
1895 $gettext_obj=eval q{
1896 use Locale::gettext q{textdomain};
1897 Locale::gettext->domain('ikiwiki')
1902 no warnings 'redefine';
1904 $getobj->() if $getobj;
1906 $gettext_obj->get(shift);
1913 $getobj->() if $getobj;
1915 $gettext_obj->nget(@_);
1918 return ($_[2] == 1 ? $_[0] : $_[1])
1936 return (defined $val && (lc($val) eq gettext("yes") || lc($val) eq "yes" || $val eq "1"));
1940 # Injects a new function into the symbol table to replace an
1941 # exported function.
1944 # This is deep ugly perl foo, beware.
1947 if (! defined $params{parent}) {
1948 $params{parent}='::';
1949 $params{old}=\&{$params{name}};
1950 $params{name}=~s/.*:://;
1952 my $parent=$params{parent};
1953 foreach my $ns (grep /^\w+::/, keys %{$parent}) {
1954 $ns = $params{parent} . $ns;
1955 inject(%params, parent => $ns) unless $ns eq '::main::';
1956 *{$ns . $params{name}} = $params{call}
1957 if exists ${$ns}{$params{name}} &&
1958 \&{${$ns}{$params{name}}} == $params{old};
1964 sub add_link ($$;$) {
1969 push @{$links{$page}}, $link
1970 unless grep { $_ eq $link } @{$links{$page}};
1972 if (defined $type) {
1973 $typedlinks{$page}{$type}{$link} = 1;
1977 sub add_autofile ($$$) {
1980 my $generator=shift;
1982 $autofiles{$file}{plugin}=$plugin;
1983 $autofiles{$file}{generator}=$generator;
1986 sub sortspec_translate ($$) {
1988 my $reverse = shift;
1994 (-?) # group 1: perhaps negated
1997 \w+\([^\)]*\) # command(params)
1999 [^\s]+ # or anything else
2007 if ($word =~ m/^(\w+)\((.*)\)$/) {
2008 # command with parameters
2012 elsif ($word !~ m/^\w+$/) {
2013 error(sprintf(gettext("invalid sort type %s"), $word));
2024 if (exists $IkiWiki::SortSpec::{"cmp_$word"}) {
2025 if (defined $params) {
2026 push @data, $params;
2027 $code .= "IkiWiki::SortSpec::cmp_$word(\$data[$#data])";
2030 $code .= "IkiWiki::SortSpec::cmp_$word(undef)";
2034 error(sprintf(gettext("unknown sort type %s"), $word));
2038 if (! length $code) {
2039 # undefined sorting method... sort arbitrarily
2048 return eval 'sub { '.$code.' }';
2051 sub pagespec_translate ($) {
2054 # Convert spec to perl code.
2058 \s* # ignore whitespace
2059 ( # 1: match a single word
2066 \w+\([^\)]*\) # command(params)
2068 [^\s()]+ # any other text
2070 \s* # ignore whitespace
2073 if (lc $word eq 'and') {
2076 elsif (lc $word eq 'or') {
2079 elsif ($word eq "(" || $word eq ")" || $word eq "!") {
2082 elsif ($word =~ /^(\w+)\((.*)\)$/) {
2083 if (exists $IkiWiki::PageSpec::{"match_$1"}) {
2085 $code.="IkiWiki::PageSpec::match_$1(\$page, \$data[$#data], \@_)";
2088 push @data, qq{unknown function in pagespec "$word"};
2089 $code.="IkiWiki::ErrorReason->new(\$data[$#data])";
2094 $code.=" IkiWiki::PageSpec::match_glob(\$page, \$data[$#data], \@_)";
2098 if (! length $code) {
2099 $code="IkiWiki::FailReason->new('empty pagespec')";
2103 return eval 'sub { my $page=shift; '.$code.' }';
2106 sub pagespec_match ($$;@) {
2111 # Backwards compatability with old calling convention.
2113 unshift @params, 'location';
2116 my $sub=pagespec_translate($spec);
2117 return IkiWiki::ErrorReason->new("syntax error in pagespec \"$spec\"")
2119 return $sub->($page, @params);
2122 sub pagespec_match_list ($$;@) {
2127 # Backwards compatability with old calling convention.
2129 print STDERR "warning: a plugin (".caller().") is using pagespec_match_list in an obsolete way, and needs to be updated\n";
2130 $params{list}=$page;
2131 $page=$params{location}; # ugh!
2134 my $sub=pagespec_translate($pagespec);
2135 error "syntax error in pagespec \"$pagespec\""
2137 my $sort=sortspec_translate($params{sort}, $params{reverse})
2138 if defined $params{sort};
2141 if (exists $params{list}) {
2142 @candidates=exists $params{filter}
2143 ? grep { ! $params{filter}->($_) } @{$params{list}}
2147 @candidates=exists $params{filter}
2148 ? grep { ! $params{filter}->($_) } keys %pagesources
2149 : keys %pagesources;
2152 # clear params, remainder is passed to pagespec
2153 $depends{$page}{$pagespec} |= ($params{deptype} || $DEPEND_CONTENT);
2154 my $num=$params{num};
2155 delete @params{qw{num deptype reverse sort filter list}};
2157 # when only the top matches will be returned, it's efficient to
2158 # sort before matching to pagespec,
2159 if (defined $num && defined $sort) {
2160 @candidates=IkiWiki::SortSpec::sort_pages(
2161 $sort, @candidates);
2167 my $accum=IkiWiki::SuccessReason->new();
2168 foreach my $p (@candidates) {
2169 my $r=$sub->($p, %params, location => $page);
2170 error(sprintf(gettext("cannot match pages: %s"), $r))
2171 if $r->isa("IkiWiki::ErrorReason");
2172 unless ($r || $r->influences_static) {
2173 $r->remove_influence($p);
2178 last if defined $num && ++$count == $num;
2182 # Add simple dependencies for accumulated influences.
2183 my $i=$accum->influences;
2184 foreach my $k (keys %$i) {
2185 $depends_simple{$page}{lc $k} |= $i->{$k};
2188 # when all matches will be returned, it's efficient to
2189 # sort after matching
2190 if (! defined $num && defined $sort) {
2191 return IkiWiki::SortSpec::sort_pages(
2199 sub pagespec_valid ($) {
2202 return defined pagespec_translate($spec);
2206 my $re=quotemeta(shift);
2212 package IkiWiki::FailReason;
2215 '""' => sub { $_[0][0] },
2217 '!' => sub { bless $_[0], 'IkiWiki::SuccessReason'},
2218 '&' => sub { $_[0]->merge_influences($_[1], 1); $_[0] },
2219 '|' => sub { $_[1]->merge_influences($_[0]); $_[1] },
2223 our @ISA = 'IkiWiki::SuccessReason';
2225 package IkiWiki::SuccessReason;
2228 '""' => sub { $_[0][0] },
2230 '!' => sub { bless $_[0], 'IkiWiki::FailReason'},
2231 '&' => sub { $_[1]->merge_influences($_[0], 1); $_[1] },
2232 '|' => sub { $_[0]->merge_influences($_[1]); $_[0] },
2239 return bless [$value, {@_}], $class;
2244 $this->[1]={@_} if @_;
2245 my %i=%{$this->[1]};
2250 sub influences_static {
2251 return ! $_[0][1]->{""};
2254 sub merge_influences {
2259 if (! $anded || (($this || %{$this->[1]}) &&
2260 ($other || %{$other->[1]}))) {
2261 foreach my $influence (keys %{$other->[1]}) {
2262 $this->[1]{$influence} |= $other->[1]{$influence};
2271 sub remove_influence {
2275 delete $this->[1]{$torm};
2278 package IkiWiki::ErrorReason;
2280 our @ISA = 'IkiWiki::FailReason';
2282 package IkiWiki::PageSpec;
2288 if ($path =~ m!^\./!) {
2289 $from=~s#/?[^/]+$## if defined $from;
2291 $path="$from/$path" if defined $from && length $from;
2297 sub match_glob ($$;@) {
2302 $glob=derel($glob, $params{location});
2304 my $regexp=IkiWiki::glob2re($glob);
2305 if ($page=~/^$regexp$/i) {
2306 if ($params{onlypage} &&
2307 ! defined IkiWiki::pagetype($IkiWiki::pagesources{$page})) {
2308 return IkiWiki::FailReason->new("$page is not a page");
2310 elsif (! IkiWiki::isinternal($page) || $params{internal}) {
2311 return IkiWiki::SuccessReason->new("$glob matches $page");
2314 return IkiWiki::FailReason->new("$glob matches $page, but the page is an internal page");
2318 return IkiWiki::FailReason->new("$glob does not match $page");
2322 sub match_internal ($$;@) {
2323 return match_glob($_[0], $_[1], @_, internal => 1)
2326 sub match_page ($$;@) {
2327 return match_glob($_[0], $_[1], @_, onlypage => 1)
2330 sub match_link ($$;@) {
2335 $link=derel($link, $params{location});
2336 my $from=exists $params{location} ? $params{location} : '';
2337 my $linktype=$params{linktype};
2339 if (defined $linktype) {
2340 $qualifier=" with type $linktype";
2343 my $links = $IkiWiki::links{$page};
2344 return IkiWiki::FailReason->new("$page has no links", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
2345 unless $links && @{$links};
2346 my $bestlink = IkiWiki::bestlink($from, $link);
2347 foreach my $p (@{$links}) {
2348 if (length $bestlink) {
2349 if ((!defined $linktype || exists $IkiWiki::typedlinks{$page}{$linktype}{$p}) && $bestlink eq IkiWiki::bestlink($page, $p)) {
2350 return IkiWiki::SuccessReason->new("$page links to $link$qualifier", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
2354 if ((!defined $linktype || exists $IkiWiki::typedlinks{$page}{$linktype}{$p}) && match_glob($p, $link, %params)) {
2355 return IkiWiki::SuccessReason->new("$page links to page $p$qualifier, matching $link", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
2357 my ($p_rel)=$p=~/^\/?(.*)/;
2359 if ((!defined $linktype || exists $IkiWiki::typedlinks{$page}{$linktype}{$p_rel}) && match_glob($p_rel, $link, %params)) {
2360 return IkiWiki::SuccessReason->new("$page links to page $p_rel$qualifier, matching $link", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
2364 return IkiWiki::FailReason->new("$page does not link to $link$qualifier", $page => $IkiWiki::DEPEND_LINKS, "" => 1);
2367 sub match_backlink ($$;@) {
2368 my $ret=match_link($_[1], $_[0], @_);
2369 $ret->influences($_[1] => $IkiWiki::DEPEND_LINKS);
2373 sub match_created_before ($$;@) {
2378 $testpage=derel($testpage, $params{location});
2380 if (exists $IkiWiki::pagectime{$testpage}) {
2381 if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
2382 return IkiWiki::SuccessReason->new("$page created before $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
2385 return IkiWiki::FailReason->new("$page not created before $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
2389 return IkiWiki::ErrorReason->new("$testpage does not exist", $testpage => $IkiWiki::DEPEND_PRESENCE);
2393 sub match_created_after ($$;@) {
2398 $testpage=derel($testpage, $params{location});
2400 if (exists $IkiWiki::pagectime{$testpage}) {
2401 if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
2402 return IkiWiki::SuccessReason->new("$page created after $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
2405 return IkiWiki::FailReason->new("$page not created after $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
2409 return IkiWiki::ErrorReason->new("$testpage does not exist", $testpage => $IkiWiki::DEPEND_PRESENCE);
2413 sub match_creation_day ($$;@) {
2414 if ((gmtime($IkiWiki::pagectime{shift()}))[3] == shift) {
2415 return IkiWiki::SuccessReason->new('creation_day matched');
2418 return IkiWiki::FailReason->new('creation_day did not match');
2422 sub match_creation_month ($$;@) {
2423 if ((gmtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
2424 return IkiWiki::SuccessReason->new('creation_month matched');
2427 return IkiWiki::FailReason->new('creation_month did not match');
2431 sub match_creation_year ($$;@) {
2432 if ((gmtime($IkiWiki::pagectime{shift()}))[5] + 1900 == shift) {
2433 return IkiWiki::SuccessReason->new('creation_year matched');
2436 return IkiWiki::FailReason->new('creation_year did not match');
2440 sub match_user ($$;@) {
2445 my $regexp=IkiWiki::glob2re($user);
2447 if (! exists $params{user}) {
2448 return IkiWiki::ErrorReason->new("no user specified");
2451 if (defined $params{user} && $params{user}=~/^$regexp$/i) {
2452 return IkiWiki::SuccessReason->new("user is $user");
2454 elsif (! defined $params{user}) {
2455 return IkiWiki::FailReason->new("not logged in");
2458 return IkiWiki::FailReason->new("user is $params{user}, not $user");
2462 sub match_admin ($$;@) {
2467 if (! exists $params{user}) {
2468 return IkiWiki::ErrorReason->new("no user specified");
2471 if (defined $params{user} && IkiWiki::is_admin($params{user})) {
2472 return IkiWiki::SuccessReason->new("user is an admin");
2474 elsif (! defined $params{user}) {
2475 return IkiWiki::FailReason->new("not logged in");
2478 return IkiWiki::FailReason->new("user is not an admin");
2482 sub match_ip ($$;@) {
2487 if (! exists $params{ip}) {
2488 return IkiWiki::ErrorReason->new("no IP specified");
2491 if (defined $params{ip} && lc $params{ip} eq lc $ip) {
2492 return IkiWiki::SuccessReason->new("IP is $ip");
2495 return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");
2499 package IkiWiki::SortSpec;
2501 # This is in the SortSpec namespace so that the $a and $b that sort() uses
2502 # are easily available in this namespace, for cmp functions to use them.
2509 IkiWiki::pagetitle(IkiWiki::basename($a))
2511 IkiWiki::pagetitle(IkiWiki::basename($b))
2514 sub cmp_mtime { $IkiWiki::pagemtime{$b} <=> $IkiWiki::pagemtime{$a} }
2515 sub cmp_age { $IkiWiki::pagectime{$b} <=> $IkiWiki::pagectime{$a} }