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 %hooks
17 %forcerebuild %loaded_plugins %typedlinks %oldtypedlinks
20 use Exporter q{import};
21 our @EXPORT = qw(hook debug error template htmlpage deptype
22 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 => "location of template files",
163 description => "additional directories containing template files",
169 default => "$installdir/share/ikiwiki/basewiki",
170 description => "base wiki source location",
177 default => "$installdir/share/ikiwiki",
178 description => "parent directory containing additional underlays",
185 description => "wrappers to generate",
192 description => "additional underlays to use",
199 description => "display verbose messages?",
206 description => "log to syslog?",
213 description => "create output files named page/index.html?",
214 safe => 0, # changing requires manual transition
217 prefix_directives => {
220 description => "use '!'-prefixed preprocessor directives?",
221 safe => 0, # changing requires manual transition
227 description => "use page/index.mdwn source files",
234 description => "enable Discussion pages?",
240 default => gettext("Discussion"),
241 description => "name of Discussion pages",
248 description => "only send cookies over SSL connections?",
256 description => "extension to use for new pages",
257 safe => 0, # not sanitized
263 description => "extension to use for html files",
264 safe => 0, # not sanitized
270 description => "strftime format string to display date",
278 example => "en_US.UTF-8",
279 description => "UTF-8 locale to use",
288 description => "put user pages below specified page",
295 description => "how many backlinks to show before hiding excess (0 to show all)",
302 description => "attempt to hardlink source files? (optimisation for large files)",
304 safe => 0, # paranoia
310 description => "force ikiwiki to use a particular umask",
312 safe => 0, # paranoia
317 example => "ikiwiki",
318 description => "group for wrappers to run in",
320 safe => 0, # paranoia
326 example => "$ENV{HOME}/.ikiwiki/",
327 description => "extra library and plugin directory",
329 safe => 0, # directory
335 description => "environment variables",
336 safe => 0, # paranoia
342 example => '^\.htaccess$',
343 description => "regexp of normally excluded files to include",
351 example => '^(*\.private|Makefile)$',
352 description => "regexp of files that should be skipped",
357 wiki_file_prune_regexps => {
359 default => [qr/(^|\/)\.\.(\/|$)/, qr/^\//, qr/^\./, qr/\/\./,
360 qr/\.x?html?$/, qr/\.ikiwiki-new$/,
361 qr/(^|\/).svn\//, qr/.arch-ids\//, qr/{arch}\//,
362 qr/(^|\/)_MTN\//, qr/(^|\/)_darcs\//,
363 qr/(^|\/)CVS\//, qr/\.dpkg-tmp$/],
364 description => "regexps of source files to ignore",
370 description => "specifies the characters that are allowed in source filenames",
371 default => "-[:alnum:]+/.:_",
375 wiki_file_regexp => {
377 description => "regexp of legal source files",
381 web_commit_regexp => {
383 default => qr/^web commit (by (.*?(?=: |$))|from ([0-9a-fA-F:.]+[0-9a-fA-F])):?(.*)/,
384 description => "regexp to parse web commits from logs",
391 description => "run as a cgi",
395 cgi_disable_uploads => {
398 description => "whether CGI should accept file uploads",
405 description => "run as a post-commit hook",
412 description => "running in rebuild mode",
419 description => "running in setup mode",
426 description => "running in clean mode",
433 description => "running in refresh mode",
440 description => "running in receive test mode",
446 description => "running in gettime mode",
453 description => "running in w3mmode",
460 description => "path to the .ikiwiki directory holding ikiwiki state",
467 description => "path to setup file",
473 default => "Standard",
474 description => "perl class to use to dump setup file",
478 allow_symlinks_before_srcdir => {
481 description => "allow symlinks in the path leading to the srcdir (potentially insecure)",
487 sub defaultconfig () {
490 foreach my $key (keys %s) {
491 push @ret, $key, $s{$key}->{default};
498 # locale stuff; avoid LC_ALL since it overrides everything
499 if (defined $ENV{LC_ALL}) {
500 $ENV{LANG} = $ENV{LC_ALL};
503 if (defined $config{locale}) {
504 if (POSIX::setlocale(&POSIX::LC_ALL, $config{locale})) {
505 $ENV{LANG}=$config{locale};
510 if (! defined $config{wiki_file_regexp}) {
511 $config{wiki_file_regexp}=qr/(^[$config{wiki_file_chars}]+$)/;
514 if (ref $config{ENV} eq 'HASH') {
515 foreach my $val (keys %{$config{ENV}}) {
516 $ENV{$val}=$config{ENV}{$val};
520 if ($config{w3mmode}) {
521 eval q{use Cwd q{abs_path}};
523 $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir}));
524 $config{destdir}=possibly_foolish_untaint(abs_path($config{destdir}));
525 $config{cgiurl}="file:///\$LIB/ikiwiki-w3m.cgi/".$config{cgiurl}
526 unless $config{cgiurl} =~ m!file:///!;
527 $config{url}="file://".$config{destdir};
530 if ($config{cgi} && ! length $config{url}) {
531 error(gettext("Must specify url to wiki with --url when using --cgi"));
534 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
535 unless exists $config{wikistatedir} && defined $config{wikistatedir};
537 if (defined $config{umask}) {
538 umask(possibly_foolish_untaint($config{umask}));
541 run_hooks(checkconfig => sub { shift->() });
549 foreach my $dir (@INC, $config{libdir}) {
550 next unless defined $dir && length $dir;
551 foreach my $file (glob("$dir/IkiWiki/Plugin/*.pm")) {
552 my ($plugin)=$file=~/.*\/(.*)\.pm$/;
556 foreach my $dir ($config{libdir}, "$installdir/lib/ikiwiki") {
557 next unless defined $dir && length $dir;
558 foreach my $file (glob("$dir/plugins/*")) {
559 $ret{basename($file)}=1 if -x $file;
567 if (defined $config{libdir} && length $config{libdir}) {
568 unshift @INC, possibly_foolish_untaint($config{libdir});
571 foreach my $plugin (@{$config{default_plugins}}, @{$config{add_plugins}}) {
576 if (exists $hooks{rcs}) {
577 error(gettext("cannot use multiple rcs plugins"));
579 loadplugin($config{rcs});
581 if (! exists $hooks{rcs}) {
585 run_hooks(getopt => sub { shift->() });
586 if (grep /^-/, @ARGV) {
587 print STDERR "Unknown option (or missing parameter): $_\n"
588 foreach grep /^-/, @ARGV;
598 return if grep { $_ eq $plugin} @{$config{disable_plugins}};
600 foreach my $dir (defined $config{libdir} ? possibly_foolish_untaint($config{libdir}) : undef,
601 "$installdir/lib/ikiwiki") {
602 if (defined $dir && -x "$dir/plugins/$plugin") {
603 eval { require IkiWiki::Plugin::external };
606 error(sprintf(gettext("failed to load external plugin needed for %s plugin: %s"), $plugin, $reason));
608 import IkiWiki::Plugin::external "$dir/plugins/$plugin";
609 $loaded_plugins{$plugin}=1;
614 my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
617 error("Failed to load plugin $mod: $@");
619 $loaded_plugins{$plugin}=1;
626 log_message('err' => $message) if $config{syslog};
627 if (defined $cleaner) {
634 return unless $config{verbose};
635 return log_message(debug => @_);
639 sub log_message ($$) {
642 if ($config{syslog}) {
645 Sys::Syslog::setlogsock('unix');
646 Sys::Syslog::openlog('ikiwiki', '', 'user');
650 Sys::Syslog::syslog($type, "[$config{wikiname}] %s", join(" ", @_));
653 elsif (! $config{cgi}) {
657 return print STDERR "@_\n";
661 sub possibly_foolish_untaint ($) {
663 my ($untainted)=$tainted=~/(.*)/s;
683 return exists $pagesources{$page} &&
684 $pagesources{$page} =~ /\._([^.]+)$/;
690 if ($file =~ /\.([^.]+)$/) {
691 return $1 if exists $hooks{htmlize}{$1};
693 my $base=basename($file);
694 if (exists $hooks{htmlize}{$base} &&
695 $hooks{htmlize}{$base}{noextension}) {
706 if (exists $pagename_cache{$file}) {
707 return $pagename_cache{$file};
710 my $type=pagetype($file);
712 $page=~s/\Q.$type\E*$//
713 if defined $type && !$hooks{htmlize}{$type}{keepextension}
714 && !$hooks{htmlize}{$type}{noextension};
715 if ($config{indexpages} && $page=~/(.*)\/index$/) {
719 $pagename_cache{$file} = $page;
723 sub newpagefile ($$) {
727 if (! $config{indexpages} || $page eq 'index') {
728 return $page.".".$type;
731 return $page."/index.".$type;
735 sub targetpage ($$;$) {
740 if (defined $filename) {
741 return $page."/".$filename.".".$ext;
743 elsif (! $config{usedirs} || $page eq 'index') {
744 return $page.".".$ext;
747 return $page."/index.".$ext;
754 return targetpage($page, $config{htmlext});
761 return "$config{srcdir}/$file", stat(_) if -e "$config{srcdir}/$file";
762 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
763 return "$dir/$file", stat(_) if -e "$dir/$file";
765 error("internal error: $file cannot be found in $config{srcdir} or underlay") unless $nothrow;
770 return (srcfile_stat(@_))[0];
773 sub add_underlay ($) {
777 $dir="$config{underlaydirbase}/$dir";
780 if (! grep { $_ eq $dir } @{$config{underlaydirs}}) {
781 unshift @{$config{underlaydirs}}, $dir;
787 sub readfile ($;$$) {
793 error("cannot read a symlink ($file)");
797 open (my $in, "<", $file) || error("failed to read $file: $!");
798 binmode($in) if ($binary);
799 return \*$in if $wantfd;
801 # check for invalid utf-8, and toss it back to avoid crashes
802 if (! utf8::valid($ret)) {
803 $ret=encode_utf8($ret);
805 close $in || error("failed to read $file: $!");
809 sub prep_writefile ($$) {
814 while (length $test) {
815 if (-l "$destdir/$test") {
816 error("cannot write to a symlink ($test)");
818 $test=dirname($test);
821 my $dir=dirname("$destdir/$file");
824 foreach my $s (split(m!/+!, $dir)) {
827 mkdir($d) || error("failed to create directory $d: $!");
835 sub writefile ($$$;$$) {
836 my $file=shift; # can include subdirs
837 my $destdir=shift; # directory to put file in
842 prep_writefile($file, $destdir);
844 my $newfile="$destdir/$file.ikiwiki-new";
846 error("cannot write to a symlink ($newfile)");
849 my $cleanup = sub { unlink($newfile) };
850 open (my $out, '>', $newfile) || error("failed to write $newfile: $!", $cleanup);
851 binmode($out) if ($binary);
853 $writer->(\*$out, $cleanup);
856 print $out $content or error("failed writing to $newfile: $!", $cleanup);
858 close $out || error("failed saving $newfile: $!", $cleanup);
859 rename($newfile, "$destdir/$file") ||
860 error("failed renaming $newfile to $destdir/$file: $!", $cleanup);
866 sub will_render ($$;$) {
871 # Important security check.
872 if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
873 ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}}, @{$wikistate{editpage}{previews}})) {
874 error("$config{destdir}/$dest independently created, not overwriting with version from $page");
877 if (! $clear || $cleared{$page}) {
878 $renderedfiles{$page}=[$dest, grep { $_ ne $dest } @{$renderedfiles{$page}}];
881 foreach my $old (@{$renderedfiles{$page}}) {
882 delete $destsources{$old};
884 $renderedfiles{$page}=[$dest];
887 $destsources{$dest}=$page;
897 if ($link=~s/^\/+//) {
905 $l.="/" if length $l;
908 if (exists $pagesources{$l}) {
911 elsif (exists $pagecase{lc $l}) {
912 return $pagecase{lc $l};
914 } while $cwd=~s{/?[^/]+$}{};
916 if (length $config{userdir}) {
917 my $l = "$config{userdir}/".lc($link);
918 if (exists $pagesources{$l}) {
921 elsif (exists $pagecase{lc $l}) {
922 return $pagecase{lc $l};
926 #print STDERR "warning: page $page, broken link: $link\n";
930 sub isinlinableimage ($) {
933 return $file =~ /\.(png|gif|jpg|jpeg)$/i;
936 sub pagetitle ($;$) {
941 $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : chr($2)/eg;
944 $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : "&#$2;"/eg;
952 # support use w/o %config set
953 my $chars = defined $config{wiki_file_chars} ? $config{wiki_file_chars} : "-[:alnum:]+/.:_";
954 $title=~s/([^$chars]|_)/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
960 my $chars = defined $config{wiki_file_chars} ? $config{wiki_file_chars} : "-[:alnum:]+/.:_";
961 $link=~s/([^$chars])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
968 my $cgiurl=$config{cgiurl};
969 if (exists $params{cgiurl}) {
970 $cgiurl=$params{cgiurl};
971 delete $params{cgiurl};
974 join("&", map $_."=".uri_escape_utf8($params{$_}), keys %params);
980 return "$config{url}/" if ! defined $page;
982 $page=htmlpage($page);
984 $page=~s/[^\/]+\//..\//g;
989 # Work around very innefficient behavior in File::Spec if abs2rel
990 # is passed two relative paths. It's much faster if paths are
991 # absolute! (Debian bug #376658; fixed in debian unstable now)
996 my $ret=File::Spec->abs2rel($path, $base);
997 $ret=~s/^// if defined $ret;
1001 sub displaytime ($;$) {
1002 # Plugins can override this function to mark up the time to
1004 return '<span class="date">'.formattime(@_).'</span>';
1007 sub formattime ($;$) {
1008 # Plugins can override this function to format the time.
1011 if (! defined $format) {
1012 $format=$config{timeformat};
1015 # strftime doesn't know about encodings, so make sure
1016 # its output is properly treated as utf8
1017 return decode_utf8(POSIX::strftime($format, localtime($time)));
1020 sub beautify_urlpath ($) {
1023 # Ensure url is not an empty link, and if necessary,
1024 # add ./ to avoid colon confusion.
1025 if ($url !~ /^\// && $url !~ /^\.\.?\//) {
1029 if ($config{usedirs}) {
1030 $url =~ s!/index.$config{htmlext}$!/!;
1042 return beautify_urlpath(baseurl($from)."index.$config{htmlext}");
1045 if (! $destsources{$to}) {
1050 return $config{url}.beautify_urlpath("/".$to);
1053 my $link = abs2rel($to, dirname(htmlpage($from)));
1055 return beautify_urlpath($link);
1058 sub htmllink ($$$;@) {
1059 my $lpage=shift; # the page doing the linking
1060 my $page=shift; # the page that will contain the link (different for inline)
1067 if (! $opts{forcesubpage}) {
1068 $bestlink=bestlink($lpage, $link);
1071 $bestlink="$lpage/".lc($link);
1075 if (defined $opts{linktext}) {
1076 $linktext=$opts{linktext};
1079 $linktext=pagetitle(basename($link));
1082 return "<span class=\"selflink\">$linktext</span>"
1083 if length $bestlink && $page eq $bestlink &&
1084 ! defined $opts{anchor};
1086 if (! $destsources{$bestlink}) {
1087 $bestlink=htmlpage($bestlink);
1089 if (! $destsources{$bestlink}) {
1091 if (length $config{cgiurl}) {
1092 $cgilink = "<a href=\"".
1097 )."\" rel=\"nofollow\">?</a>";
1099 return "<span class=\"createlink\">$cgilink$linktext</span>"
1103 $bestlink=abs2rel($bestlink, dirname(htmlpage($page)));
1104 $bestlink=beautify_urlpath($bestlink);
1106 if (! $opts{noimageinline} && isinlinableimage($bestlink)) {
1107 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
1110 if (defined $opts{anchor}) {
1111 $bestlink.="#".$opts{anchor};
1115 foreach my $attr (qw{rel class title}) {
1116 if (defined $opts{$attr}) {
1117 push @attrs, " $attr=\"$opts{$attr}\"";
1121 return "<a href=\"$bestlink\"@attrs>$linktext</a>";
1126 return length $config{userdir} ? "$config{userdir}/$user" : $user;
1129 sub openiduser ($) {
1132 if ($user =~ m!^https?://! &&
1133 eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
1136 if (Net::OpenID::VerifiedIdentity->can("DisplayOfURL")) {
1137 $display = Net::OpenID::VerifiedIdentity::DisplayOfURL($user);
1140 # backcompat with old version
1141 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
1142 $display=$oid->display;
1145 # Convert "user.somehost.com" to "user [somehost.com]"
1146 # (also "user.somehost.co.uk")
1147 if ($display !~ /\[/) {
1148 $display=~s/^([-a-zA-Z0-9]+?)\.([-.a-zA-Z0-9]+\.[a-z]+)$/$1 [$2]/;
1150 # Convert "http://somehost.com/user" to "user [somehost.com]".
1151 # (also "https://somehost.com/user/")
1152 if ($display !~ /\[/) {
1153 $display=~s/^https?:\/\/(.+)\/([^\/#?]+)\/?(?:[#?].*)?$/$2 [$1]/;
1155 $display=~s!^https?://!!; # make sure this is removed
1156 eval q{use CGI 'escapeHTML'};
1158 return escapeHTML($display);
1163 sub htmlize ($$$$) {
1169 my $oneline = $content !~ /\n/;
1171 if (exists $hooks{htmlize}{$type}) {
1172 $content=$hooks{htmlize}{$type}{call}->(
1174 content => $content,
1178 error("htmlization of $type not supported");
1181 run_hooks(sanitize => sub {
1184 destpage => $destpage,
1185 content => $content,
1190 # hack to get rid of enclosing junk added by markdown
1191 # and other htmlizers/sanitizers
1192 $content=~s/^<p>//i;
1193 $content=~s/<\/p>\n*$//i;
1204 run_hooks(linkify => sub {
1207 destpage => $destpage,
1208 content => $content,
1216 our $preprocess_preview=0;
1217 sub preprocess ($$$;$$) {
1218 my $page=shift; # the page the data comes from
1219 my $destpage=shift; # the page the data will appear in (different for inline)
1224 # Using local because it needs to be set within any nested calls
1226 local $preprocess_preview=$preview if defined $preview;
1233 $params="" if ! defined $params;
1235 if (length $escape) {
1236 return "[[$prefix$command $params]]";
1238 elsif (exists $hooks{preprocess}{$command}) {
1239 return "" if $scan && ! $hooks{preprocess}{$command}{scan};
1240 # Note: preserve order of params, some plugins may
1241 # consider it significant.
1243 while ($params =~ m{
1244 (?:([-\w]+)=)? # 1: named parameter key?
1246 """(.*?)""" # 2: triple-quoted value
1248 "([^"]*?)" # 3: single-quoted value
1250 (\S+) # 4: unquoted value
1252 (?:\s+|$) # delimiter to next param
1262 elsif (defined $3) {
1265 elsif (defined $4) {
1270 push @params, $key, $val;
1273 push @params, $val, '';
1276 if ($preprocessing{$page}++ > 3) {
1277 # Avoid loops of preprocessed pages preprocessing
1278 # other pages that preprocess them, etc.
1279 return "[[!$command <span class=\"error\">".
1280 sprintf(gettext("preprocessing loop detected on %s at depth %i"),
1281 $page, $preprocessing{$page}).
1287 $hooks{preprocess}{$command}{call}->(
1290 destpage => $destpage,
1291 preview => $preprocess_preview,
1297 $ret="[[!$command <span class=\"error\">".
1298 gettext("Error").": $error"."</span>]]";
1302 # use void context during scan pass
1304 $hooks{preprocess}{$command}{call}->(
1307 destpage => $destpage,
1308 preview => $preprocess_preview,
1313 $preprocessing{$page}--;
1317 return "[[$prefix$command $params]]";
1322 if ($config{prefix_directives}) {
1325 \[\[(!) # directive open; 2: prefix
1326 ([-\w]+) # 3: command
1327 ( # 4: the parameters..
1328 \s+ # Must have space if parameters present
1330 (?:[-\w]+=)? # named parameter key?
1332 """.*?""" # triple-quoted value
1334 "[^"]*?" # single-quoted value
1336 [^"\s\]]+ # unquoted value
1338 \s* # whitespace or end
1341 *)? # 0 or more parameters
1342 \]\] # directive closed
1348 \[\[(!?) # directive open; 2: optional prefix
1349 ([-\w]+) # 3: command
1351 ( # 4: the parameters..
1353 (?:[-\w]+=)? # named parameter key?
1355 """.*?""" # triple-quoted value
1357 "[^"]*?" # single-quoted value
1359 [^"\s\]]+ # unquoted value
1361 \s* # whitespace or end
1364 *) # 0 or more parameters
1365 \]\] # directive closed
1369 $content =~ s{$regex}{$handle->($1, $2, $3, $4)}eg;
1378 run_hooks(filter => sub {
1379 $content=shift->(page => $page, destpage => $destpage,
1380 content => $content);
1387 return "<a href=\"$config{url}\">$config{wikiname}</a>";
1390 sub check_canedit ($$$;$) {
1397 run_hooks(canedit => sub {
1398 return if defined $canedit;
1399 my $ret=shift->($page, $q, $session);
1404 elsif (ref $ret eq 'CODE') {
1405 $ret->() unless $nonfatal;
1408 elsif (defined $ret) {
1409 error($ret) unless $nonfatal;
1414 return defined $canedit ? $canedit : 1;
1417 sub check_content (@) {
1420 return 1 if ! exists $hooks{checkcontent}; # optimisation
1422 if (exists $pagesources{$params{page}}) {
1424 my %old=map { $_ => 1 }
1425 split("\n", readfile(srcfile($pagesources{$params{page}})));
1426 foreach my $line (split("\n", $params{content})) {
1427 push @diff, $line if ! exists $old{$line};
1429 $params{diff}=join("\n", @diff);
1433 run_hooks(checkcontent => sub {
1434 return if defined $ok;
1435 my $ret=shift->(%params);
1440 elsif (ref $ret eq 'CODE') {
1441 $ret->() unless $params{nonfatal};
1444 elsif (defined $ret) {
1445 error($ret) unless $params{nonfatal};
1451 return defined $ok ? $ok : 1;
1457 # Take an exclusive lock on the wiki to prevent multiple concurrent
1458 # run issues. The lock will be dropped on program exit.
1459 if (! -d $config{wikistatedir}) {
1460 mkdir($config{wikistatedir});
1462 open($wikilock, '>', "$config{wikistatedir}/lockfile") ||
1463 error ("cannot write to $config{wikistatedir}/lockfile: $!");
1464 if (! flock($wikilock, 2)) { # LOCK_EX
1465 error("failed to get lock");
1471 POSIX::close($ENV{IKIWIKI_CGILOCK_FD}) if exists $ENV{IKIWIKI_CGILOCK_FD};
1472 return close($wikilock) if $wikilock;
1478 sub commit_hook_enabled () {
1479 open($commitlock, '+>', "$config{wikistatedir}/commitlock") ||
1480 error("cannot write to $config{wikistatedir}/commitlock: $!");
1481 if (! flock($commitlock, 1 | 4)) { # LOCK_SH | LOCK_NB to test
1482 close($commitlock) || error("failed closing commitlock: $!");
1485 close($commitlock) || error("failed closing commitlock: $!");
1489 sub disable_commit_hook () {
1490 open($commitlock, '>', "$config{wikistatedir}/commitlock") ||
1491 error("cannot write to $config{wikistatedir}/commitlock: $!");
1492 if (! flock($commitlock, 2)) { # LOCK_EX
1493 error("failed to get commit lock");
1498 sub enable_commit_hook () {
1499 return close($commitlock) if $commitlock;
1504 %oldrenderedfiles=%pagectime=();
1505 if (! $config{rebuild}) {
1506 %pagesources=%pagemtime=%oldlinks=%links=%depends=
1507 %destsources=%renderedfiles=%pagecase=%pagestate=
1508 %depends_simple=%typedlinks=%oldtypedlinks=();
1511 if (! open ($in, "<", "$config{wikistatedir}/indexdb")) {
1512 if (-e "$config{wikistatedir}/index") {
1513 system("ikiwiki-transition", "indexdb", $config{srcdir});
1514 open ($in, "<", "$config{wikistatedir}/indexdb") || return;
1517 $config{gettime}=1; # first build
1522 my $index=Storable::fd_retrieve($in);
1523 if (! defined $index) {
1528 if (exists $index->{version} && ! ref $index->{version}) {
1529 $pages=$index->{page};
1530 %wikistate=%{$index->{state}};
1537 foreach my $src (keys %$pages) {
1538 my $d=$pages->{$src};
1539 my $page=pagename($src);
1540 $pagectime{$page}=$d->{ctime};
1541 if (! $config{rebuild}) {
1542 $pagesources{$page}=$src;
1543 $pagemtime{$page}=$d->{mtime};
1544 $renderedfiles{$page}=$d->{dest};
1545 if (exists $d->{links} && ref $d->{links}) {
1546 $links{$page}=$d->{links};
1547 $oldlinks{$page}=[@{$d->{links}}];
1549 if (ref $d->{depends_simple} eq 'ARRAY') {
1551 $depends_simple{$page}={
1552 map { $_ => 1 } @{$d->{depends_simple}}
1555 elsif (exists $d->{depends_simple}) {
1556 $depends_simple{$page}=$d->{depends_simple};
1558 if (exists $d->{dependslist}) {
1561 map { $_ => $DEPEND_CONTENT }
1562 @{$d->{dependslist}}
1565 elsif (exists $d->{depends} && ! ref $d->{depends}) {
1567 $depends{$page}={$d->{depends} => $DEPEND_CONTENT };
1569 elsif (exists $d->{depends}) {
1570 $depends{$page}=$d->{depends};
1572 if (exists $d->{state}) {
1573 $pagestate{$page}=$d->{state};
1575 if (exists $d->{typedlinks}) {
1576 $typedlinks{$page}=$d->{typedlinks};
1578 while (my ($type, $links) = each %{$typedlinks{$page}}) {
1579 next unless %$links;
1580 $oldtypedlinks{$page}{$type} = {%$links};
1584 $oldrenderedfiles{$page}=[@{$d->{dest}}];
1586 foreach my $page (keys %pagesources) {
1587 $pagecase{lc $page}=$page;
1589 foreach my $page (keys %renderedfiles) {
1590 $destsources{$_}=$page foreach @{$renderedfiles{$page}};
1596 run_hooks(savestate => sub { shift->() });
1599 foreach my $type (keys %hooks) {
1600 $hookids{$_}=1 foreach keys %{$hooks{$type}};
1602 my @hookids=keys %hookids;
1604 if (! -d $config{wikistatedir}) {
1605 mkdir($config{wikistatedir});
1607 my $newfile="$config{wikistatedir}/indexdb.new";
1608 my $cleanup = sub { unlink($newfile) };
1609 open (my $out, '>', $newfile) || error("cannot write to $newfile: $!", $cleanup);
1612 foreach my $page (keys %pagemtime) {
1613 next unless $pagemtime{$page};
1614 my $src=$pagesources{$page};
1616 $index{page}{$src}={
1617 ctime => $pagectime{$page},
1618 mtime => $pagemtime{$page},
1619 dest => $renderedfiles{$page},
1620 links => $links{$page},
1623 if (exists $depends{$page}) {
1624 $index{page}{$src}{depends} = $depends{$page};
1627 if (exists $depends_simple{$page}) {
1628 $index{page}{$src}{depends_simple} = $depends_simple{$page};
1631 if (exists $typedlinks{$page} && %{$typedlinks{$page}}) {
1632 $index{page}{$src}{typedlinks} = $typedlinks{$page};
1635 if (exists $pagestate{$page}) {
1636 foreach my $id (@hookids) {
1637 foreach my $key (keys %{$pagestate{$page}{$id}}) {
1638 $index{page}{$src}{state}{$id}{$key}=$pagestate{$page}{$id}{$key};
1645 foreach my $id (@hookids) {
1646 foreach my $key (keys %{$wikistate{$id}}) {
1647 $index{state}{$id}{$key}=$wikistate{$id}{$key};
1651 $index{version}="3";
1652 my $ret=Storable::nstore_fd(\%index, $out);
1653 return if ! defined $ret || ! $ret;
1654 close $out || error("failed saving to $newfile: $!", $cleanup);
1655 rename($newfile, "$config{wikistatedir}/indexdb") ||
1656 error("failed renaming $newfile to $config{wikistatedir}/indexdb", $cleanup);
1661 sub template_file ($) {
1664 foreach my $dir ($config{templatedir}, @{$config{templatedirs}},
1665 "$installdir/share/ikiwiki/templates") {
1666 return "$dir/$template" if -e "$dir/$template";
1671 sub template_params (@) {
1672 my $filename=template_file(shift);
1674 if (! defined $filename) {
1675 return if wantarray;
1681 my $text_ref = shift;
1682 ${$text_ref} = decode_utf8(${$text_ref});
1684 filename => $filename,
1685 loop_context_vars => 1,
1686 die_on_bad_params => 0,
1689 return wantarray ? @ret : {@ret};
1692 sub template ($;@) {
1693 require HTML::Template;
1694 return HTML::Template->new(template_params(@_));
1697 sub misctemplate ($$;@) {
1701 my $template=template("misc.tmpl");
1704 indexlink => indexlink(),
1705 wikiname => $config{wikiname},
1706 pagebody => $pagebody,
1707 baseurl => baseurl(),
1710 run_hooks(pagetemplate => sub {
1711 shift->(page => "", destpage => "", template => $template);
1713 return $template->output;
1719 if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
1720 error 'hook requires type, call, and id parameters';
1723 return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
1725 $hooks{$param{type}}{$param{id}}=\%param;
1729 sub run_hooks ($$) {
1730 # Calls the given sub for each hook of the given type,
1731 # passing it the hook function to call.
1735 if (exists $hooks{$type}) {
1736 my (@first, @middle, @last);
1737 foreach my $id (keys %{$hooks{$type}}) {
1738 if ($hooks{$type}{$id}{first}) {
1741 elsif ($hooks{$type}{$id}{last}) {
1748 foreach my $id (@first, @middle, @last) {
1749 $sub->($hooks{$type}{$id}{call});
1757 $hooks{rcs}{rcs_update}{call}->(@_);
1760 sub rcs_prepedit ($) {
1761 $hooks{rcs}{rcs_prepedit}{call}->(@_);
1764 sub rcs_commit ($$$;$$) {
1765 $hooks{rcs}{rcs_commit}{call}->(@_);
1768 sub rcs_commit_staged ($$$) {
1769 $hooks{rcs}{rcs_commit_staged}{call}->(@_);
1773 $hooks{rcs}{rcs_add}{call}->(@_);
1776 sub rcs_remove ($) {
1777 $hooks{rcs}{rcs_remove}{call}->(@_);
1780 sub rcs_rename ($$) {
1781 $hooks{rcs}{rcs_rename}{call}->(@_);
1784 sub rcs_recentchanges ($) {
1785 $hooks{rcs}{rcs_recentchanges}{call}->(@_);
1789 $hooks{rcs}{rcs_diff}{call}->(@_);
1792 sub rcs_getctime ($) {
1793 $hooks{rcs}{rcs_getctime}{call}->(@_);
1796 sub rcs_getmtime ($) {
1797 $hooks{rcs}{rcs_getmtime}{call}->(@_);
1800 sub rcs_receive () {
1801 $hooks{rcs}{rcs_receive}{call}->();
1804 sub add_depends ($$;$) {
1807 my $deptype=shift || $DEPEND_CONTENT;
1809 # Is the pagespec a simple page name?
1810 if ($pagespec =~ /$config{wiki_file_regexp}/ &&
1811 $pagespec !~ /[\s*?()!]/) {
1812 $depends_simple{$page}{lc $pagespec} |= $deptype;
1816 # Add explicit dependencies for influences.
1817 my $sub=pagespec_translate($pagespec);
1818 return unless defined $sub;
1819 foreach my $p (keys %pagesources) {
1820 my $r=$sub->($p, location => $page);
1821 my $i=$r->influences;
1822 my $static=$r->influences_static;
1823 foreach my $k (keys %$i) {
1824 next unless $r || $static || $k eq $page;
1825 $depends_simple{$page}{lc $k} |= $i->{$k};
1830 $depends{$page}{$pagespec} |= $deptype;
1836 foreach my $type (@_) {
1837 if ($type eq 'presence') {
1838 $deptype |= $DEPEND_PRESENCE;
1840 elsif ($type eq 'links') {
1841 $deptype |= $DEPEND_LINKS;
1843 elsif ($type eq 'content') {
1844 $deptype |= $DEPEND_CONTENT;
1850 my $file_prune_regexp;
1851 sub file_pruned ($) {
1854 if (defined $config{include} && length $config{include}) {
1855 return 0 if $file =~ m/$config{include}/;
1858 if (! defined $file_prune_regexp) {
1859 $file_prune_regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
1860 $file_prune_regexp=qr/$file_prune_regexp/;
1862 return $file =~ m/$file_prune_regexp/;
1865 sub define_gettext () {
1866 # If translation is needed, redefine the gettext function to do it.
1867 # Otherwise, it becomes a quick no-op.
1870 if ((exists $ENV{LANG} && length $ENV{LANG}) ||
1871 (exists $ENV{LC_ALL} && length $ENV{LC_ALL}) ||
1872 (exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES})) {
1874 $gettext_obj=eval q{
1875 use Locale::gettext q{textdomain};
1876 Locale::gettext->domain('ikiwiki')
1881 no warnings 'redefine';
1883 $getobj->() if $getobj;
1885 $gettext_obj->get(shift);
1892 $getobj->() if $getobj;
1894 $gettext_obj->nget(@_);
1897 return ($_[2] == 1 ? $_[0] : $_[1])
1915 return (defined $val && (lc($val) eq gettext("yes") || lc($val) eq "yes" || $val eq "1"));
1919 # Injects a new function into the symbol table to replace an
1920 # exported function.
1923 # This is deep ugly perl foo, beware.
1926 if (! defined $params{parent}) {
1927 $params{parent}='::';
1928 $params{old}=\&{$params{name}};
1929 $params{name}=~s/.*:://;
1931 my $parent=$params{parent};
1932 foreach my $ns (grep /^\w+::/, keys %{$parent}) {
1933 $ns = $params{parent} . $ns;
1934 inject(%params, parent => $ns) unless $ns eq '::main::';
1935 *{$ns . $params{name}} = $params{call}
1936 if exists ${$ns}{$params{name}} &&
1937 \&{${$ns}{$params{name}}} == $params{old};
1943 sub add_link ($$;$) {
1948 push @{$links{$page}}, $link
1949 unless grep { $_ eq $link } @{$links{$page}};
1951 if (defined $type) {
1952 $typedlinks{$page}{$type}{$link} = 1;
1956 sub add_autofile ($$$) {
1959 my $generator=shift;
1961 $autofiles{$file}{plugin}=$plugin;
1962 $autofiles{$file}{generator}=$generator;
1965 sub sortspec_translate ($$) {
1967 my $reverse = shift;
1973 (-?) # group 1: perhaps negated
1976 \w+\([^\)]*\) # command(params)
1978 [^\s]+ # or anything else
1986 if ($word =~ m/^(\w+)\((.*)\)$/) {
1987 # command with parameters
1991 elsif ($word !~ m/^\w+$/) {
1992 error(sprintf(gettext("invalid sort type %s"), $word));
2003 if (exists $IkiWiki::SortSpec::{"cmp_$word"}) {
2004 if (defined $params) {
2005 push @data, $params;
2006 $code .= "IkiWiki::SortSpec::cmp_$word(\$data[$#data])";
2009 $code .= "IkiWiki::SortSpec::cmp_$word(undef)";
2013 error(sprintf(gettext("unknown sort type %s"), $word));
2017 if (! length $code) {
2018 # undefined sorting method... sort arbitrarily
2027 return eval 'sub { '.$code.' }';
2030 sub pagespec_translate ($) {
2033 # Convert spec to perl code.
2037 \s* # ignore whitespace
2038 ( # 1: match a single word
2045 \w+\([^\)]*\) # command(params)
2047 [^\s()]+ # any other text
2049 \s* # ignore whitespace
2052 if (lc $word eq 'and') {
2055 elsif (lc $word eq 'or') {
2058 elsif ($word eq "(" || $word eq ")" || $word eq "!") {
2061 elsif ($word =~ /^(\w+)\((.*)\)$/) {
2062 if (exists $IkiWiki::PageSpec::{"match_$1"}) {
2064 $code.="IkiWiki::PageSpec::match_$1(\$page, \$data[$#data], \@_)";
2067 push @data, qq{unknown function in pagespec "$word"};
2068 $code.="IkiWiki::ErrorReason->new(\$data[$#data])";
2073 $code.=" IkiWiki::PageSpec::match_glob(\$page, \$data[$#data], \@_)";
2077 if (! length $code) {
2078 $code="IkiWiki::FailReason->new('empty pagespec')";
2082 return eval 'sub { my $page=shift; '.$code.' }';
2085 sub pagespec_match ($$;@) {
2090 # Backwards compatability with old calling convention.
2092 unshift @params, 'location';
2095 my $sub=pagespec_translate($spec);
2096 return IkiWiki::ErrorReason->new("syntax error in pagespec \"$spec\"")
2098 return $sub->($page, @params);
2101 sub pagespec_match_list ($$;@) {
2106 # Backwards compatability with old calling convention.
2108 print STDERR "warning: a plugin (".caller().") is using pagespec_match_list in an obsolete way, and needs to be updated\n";
2109 $params{list}=$page;
2110 $page=$params{location}; # ugh!
2113 my $sub=pagespec_translate($pagespec);
2114 error "syntax error in pagespec \"$pagespec\""
2116 my $sort=sortspec_translate($params{sort}, $params{reverse})
2117 if defined $params{sort};
2120 if (exists $params{list}) {
2121 @candidates=exists $params{filter}
2122 ? grep { ! $params{filter}->($_) } @{$params{list}}
2126 @candidates=exists $params{filter}
2127 ? grep { ! $params{filter}->($_) } keys %pagesources
2128 : keys %pagesources;
2131 # clear params, remainder is passed to pagespec
2132 $depends{$page}{$pagespec} |= ($params{deptype} || $DEPEND_CONTENT);
2133 my $num=$params{num};
2134 delete @params{qw{num deptype reverse sort filter list}};
2136 # when only the top matches will be returned, it's efficient to
2137 # sort before matching to pagespec,
2138 if (defined $num && defined $sort) {
2139 @candidates=IkiWiki::SortSpec::sort_pages(
2140 $sort, @candidates);
2146 my $accum=IkiWiki::SuccessReason->new();
2147 foreach my $p (@candidates) {
2148 my $r=$sub->($p, %params, location => $page);
2149 error(sprintf(gettext("cannot match pages: %s"), $r))
2150 if $r->isa("IkiWiki::ErrorReason");
2151 unless ($r || $r->influences_static) {
2152 $r->remove_influence($p);
2157 last if defined $num && ++$count == $num;
2161 # Add simple dependencies for accumulated influences.
2162 my $i=$accum->influences;
2163 foreach my $k (keys %$i) {
2164 $depends_simple{$page}{lc $k} |= $i->{$k};
2167 # when all matches will be returned, it's efficient to
2168 # sort after matching
2169 if (! defined $num && defined $sort) {
2170 return IkiWiki::SortSpec::sort_pages(
2178 sub pagespec_valid ($) {
2181 return defined pagespec_translate($spec);
2185 my $re=quotemeta(shift);
2191 package IkiWiki::FailReason;
2194 '""' => sub { $_[0][0] },
2196 '!' => sub { bless $_[0], 'IkiWiki::SuccessReason'},
2197 '&' => sub { $_[0]->merge_influences($_[1], 1); $_[0] },
2198 '|' => sub { $_[1]->merge_influences($_[0]); $_[1] },
2202 our @ISA = 'IkiWiki::SuccessReason';
2204 package IkiWiki::SuccessReason;
2207 '""' => sub { $_[0][0] },
2209 '!' => sub { bless $_[0], 'IkiWiki::FailReason'},
2210 '&' => sub { $_[1]->merge_influences($_[0], 1); $_[1] },
2211 '|' => sub { $_[0]->merge_influences($_[1]); $_[0] },
2218 return bless [$value, {@_}], $class;
2223 $this->[1]={@_} if @_;
2224 my %i=%{$this->[1]};
2229 sub influences_static {
2230 return ! $_[0][1]->{""};
2233 sub merge_influences {
2238 if (! $anded || (($this || %{$this->[1]}) &&
2239 ($other || %{$other->[1]}))) {
2240 foreach my $influence (keys %{$other->[1]}) {
2241 $this->[1]{$influence} |= $other->[1]{$influence};
2250 sub remove_influence {
2254 delete $this->[1]{$torm};
2257 package IkiWiki::ErrorReason;
2259 our @ISA = 'IkiWiki::FailReason';
2261 package IkiWiki::PageSpec;
2267 if ($path =~ m!^\./!) {
2268 $from=~s#/?[^/]+$## if defined $from;
2270 $path="$from/$path" if defined $from && length $from;
2276 sub match_glob ($$;@) {
2281 $glob=derel($glob, $params{location});
2283 my $regexp=IkiWiki::glob2re($glob);
2284 if ($page=~/^$regexp$/i) {
2285 if (! IkiWiki::isinternal($page) || $params{internal}) {
2286 return IkiWiki::SuccessReason->new("$glob matches $page");
2289 return IkiWiki::FailReason->new("$glob matches $page, but the page is an internal page");
2293 return IkiWiki::FailReason->new("$glob does not match $page");
2297 sub match_internal ($$;@) {
2298 return match_glob($_[0], $_[1], @_, internal => 1)
2301 sub match_link ($$;@) {
2306 $link=derel($link, $params{location});
2307 my $from=exists $params{location} ? $params{location} : '';
2308 my $linktype=$params{linktype};
2310 if (defined $linktype) {
2311 $qualifier=" with type $linktype";
2314 my $links = $IkiWiki::links{$page};
2315 return IkiWiki::FailReason->new("$page has no links", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
2316 unless $links && @{$links};
2317 my $bestlink = IkiWiki::bestlink($from, $link);
2318 foreach my $p (@{$links}) {
2319 if (length $bestlink) {
2320 if ((!defined $linktype || exists $IkiWiki::typedlinks{$page}{$linktype}{$p}) && $bestlink eq IkiWiki::bestlink($page, $p)) {
2321 return IkiWiki::SuccessReason->new("$page links to $link$qualifier", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
2325 if ((!defined $linktype || exists $IkiWiki::typedlinks{$page}{$linktype}{$p}) && match_glob($p, $link, %params)) {
2326 return IkiWiki::SuccessReason->new("$page links to page $p$qualifier, matching $link", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
2328 my ($p_rel)=$p=~/^\/?(.*)/;
2330 if ((!defined $linktype || exists $IkiWiki::typedlinks{$page}{$linktype}{$p_rel}) && match_glob($p_rel, $link, %params)) {
2331 return IkiWiki::SuccessReason->new("$page links to page $p_rel$qualifier, matching $link", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
2335 return IkiWiki::FailReason->new("$page does not link to $link$qualifier", $page => $IkiWiki::DEPEND_LINKS, "" => 1);
2338 sub match_backlink ($$;@) {
2339 my $ret=match_link($_[1], $_[0], @_);
2340 $ret->influences($_[1] => $IkiWiki::DEPEND_LINKS);
2344 sub match_created_before ($$;@) {
2349 $testpage=derel($testpage, $params{location});
2351 if (exists $IkiWiki::pagectime{$testpage}) {
2352 if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
2353 return IkiWiki::SuccessReason->new("$page created before $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
2356 return IkiWiki::FailReason->new("$page not created before $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
2360 return IkiWiki::ErrorReason->new("$testpage does not exist", $testpage => $IkiWiki::DEPEND_PRESENCE);
2364 sub match_created_after ($$;@) {
2369 $testpage=derel($testpage, $params{location});
2371 if (exists $IkiWiki::pagectime{$testpage}) {
2372 if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
2373 return IkiWiki::SuccessReason->new("$page created after $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
2376 return IkiWiki::FailReason->new("$page not created after $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
2380 return IkiWiki::ErrorReason->new("$testpage does not exist", $testpage => $IkiWiki::DEPEND_PRESENCE);
2384 sub match_creation_day ($$;@) {
2385 if ((gmtime($IkiWiki::pagectime{shift()}))[3] == shift) {
2386 return IkiWiki::SuccessReason->new('creation_day matched');
2389 return IkiWiki::FailReason->new('creation_day did not match');
2393 sub match_creation_month ($$;@) {
2394 if ((gmtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
2395 return IkiWiki::SuccessReason->new('creation_month matched');
2398 return IkiWiki::FailReason->new('creation_month did not match');
2402 sub match_creation_year ($$;@) {
2403 if ((gmtime($IkiWiki::pagectime{shift()}))[5] + 1900 == shift) {
2404 return IkiWiki::SuccessReason->new('creation_year matched');
2407 return IkiWiki::FailReason->new('creation_year did not match');
2411 sub match_user ($$;@) {
2416 my $regexp=IkiWiki::glob2re($user);
2418 if (! exists $params{user}) {
2419 return IkiWiki::ErrorReason->new("no user specified");
2422 if (defined $params{user} && $params{user}=~/^$regexp$/i) {
2423 return IkiWiki::SuccessReason->new("user is $user");
2425 elsif (! defined $params{user}) {
2426 return IkiWiki::FailReason->new("not logged in");
2429 return IkiWiki::FailReason->new("user is $params{user}, not $user");
2433 sub match_admin ($$;@) {
2438 if (! exists $params{user}) {
2439 return IkiWiki::ErrorReason->new("no user specified");
2442 if (defined $params{user} && IkiWiki::is_admin($params{user})) {
2443 return IkiWiki::SuccessReason->new("user is an admin");
2445 elsif (! defined $params{user}) {
2446 return IkiWiki::FailReason->new("not logged in");
2449 return IkiWiki::FailReason->new("user is not an admin");
2453 sub match_ip ($$;@) {
2458 if (! exists $params{ip}) {
2459 return IkiWiki::ErrorReason->new("no IP specified");
2462 if (defined $params{ip} && lc $params{ip} eq lc $ip) {
2463 return IkiWiki::SuccessReason->new("IP is $ip");
2466 return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");
2470 package IkiWiki::SortSpec;
2472 # This is in the SortSpec namespace so that the $a and $b that sort() uses
2473 # are easily available in this namespace, for cmp functions to use them.
2480 IkiWiki::pagetitle(IkiWiki::basename($a))
2482 IkiWiki::pagetitle(IkiWiki::basename($b))
2485 sub cmp_mtime { $IkiWiki::pagemtime{$b} <=> $IkiWiki::pagemtime{$a} }
2486 sub cmp_age { $IkiWiki::pagectime{$b} <=> $IkiWiki::pagectime{$a} }