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 %hooks %forcerebuild
17 $gettext_obj %loaded_plugins};
19 use Exporter q{import};
20 our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
21 bestlink htmllink readfile writefile pagetype srcfile pagename
22 displaytime will_render gettext urlto targetpage
23 add_underlay pagetitle titlepage linkpage newpagefile
25 %config %links %pagestate %wikistate %renderedfiles
26 %pagesources %destsources);
27 our $VERSION = 3.00; # plugin interface version, next is ikiwiki version
28 our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
29 our $installdir=''; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
34 memoize("pagespec_translate");
35 memoize("file_pruned");
41 description => "name of the wiki",
48 example => 'me@example.com',
49 description => "contact email for wiki",
56 description => "users who are wiki admins",
63 description => "users who are banned from the wiki",
70 example => "$ENV{HOME}/wiki",
71 description => "where the source of the wiki is located",
78 example => "/var/www/wiki",
79 description => "where to build the wiki",
86 example => "http://example.com/wiki",
87 description => "base url to the wiki",
94 example => "http://example.com/wiki/ikiwiki.cgi",
95 description => "url to the ikiwiki.cgi",
102 example => "/var/www/wiki/ikiwiki.cgi",
103 description => "filename of cgi wrapper to generate",
110 description => "mode for cgi_wrapper (can safely be made suid)",
117 description => "rcs backend to use",
118 safe => 0, # don't allow overriding
123 default => [qw{mdwn link inline meta htmlscrubber passwordauth
124 openid signinedit lockedit conditional
125 recentchanges parentlinks editpage}],
126 description => "plugins to enable by default",
133 description => "plugins to add to the default configuration",
140 description => "plugins to disable",
146 default => "$installdir/share/ikiwiki/templates",
147 description => "location of template files",
154 default => "$installdir/share/ikiwiki/basewiki",
155 description => "base wiki source location",
163 description => "wrappers to generate",
170 description => "additional underlays to use",
177 description => "display verbose messages?",
184 description => "log to syslog?",
191 description => "create output files named page/index.html?",
192 safe => 0, # changing requires manual transition
195 prefix_directives => {
198 description => "use '!'-prefixed preprocessor directives?",
199 safe => 0, # changing requires manual transition
205 description => "use page/index.mdwn source files",
212 description => "enable Discussion pages?",
219 description => "only send cookies over SSL connections?",
227 description => "extension to use for new pages",
228 safe => 0, # not sanitized
234 description => "extension to use for html files",
235 safe => 0, # not sanitized
241 description => "strftime format string to display date",
249 example => "en_US.UTF-8",
250 description => "UTF-8 locale to use",
259 description => "put user pages below specified page",
266 description => "how many backlinks to show before hiding excess (0 to show all)",
273 description => "attempt to hardlink source files? (optimisation for large files)",
275 safe => 0, # paranoia
281 description => "force ikiwiki to use a particular umask",
283 safe => 0, # paranoia
288 example => "ikiwiki",
289 description => "group for wrappers to run in",
291 safe => 0, # paranoia
297 example => "$ENV{HOME}/.ikiwiki/",
298 description => "extra library and plugin directory",
300 safe => 0, # directory
306 description => "environment variables",
307 safe => 0, # paranoia
314 description => "regexp of source files to ignore",
319 wiki_file_prune_regexps => {
321 default => [qr/(^|\/)\.\.(\/|$)/, qr/^\./, qr/\/\./,
322 qr/\.x?html?$/, qr/\.ikiwiki-new$/,
323 qr/(^|\/).svn\//, qr/.arch-ids\//, qr/{arch}\//,
326 description => "regexps of source files to ignore",
332 description => "specifies the characters that are allowed in source filenames",
333 default => "-[:alnum:]+/.:_",
337 wiki_file_regexp => {
339 description => "regexp of legal source files",
343 web_commit_regexp => {
345 default => qr/^web commit (by (.*?(?=: |$))|from (\d+\.\d+\.\d+\.\d+)):?(.*)/,
346 description => "regexp to parse web commits from logs",
353 description => "run as a cgi",
357 cgi_disable_uploads => {
360 description => "whether CGI should accept file uploads",
367 description => "run as a post-commit hook",
374 description => "running in rebuild mode",
381 description => "running in setup mode",
388 description => "running in refresh mode",
395 description => "running in receive test mode",
402 description => "running in getctime mode",
409 description => "running in w3mmode",
416 description => "path to the .ikiwiki directory holding ikiwiki state",
423 description => "path to setup file",
427 allow_symlinks_before_srcdir => {
430 description => "allow symlinks in the path leading to the srcdir (potentially insecure)",
436 sub defaultconfig () {
439 foreach my $key (keys %s) {
440 push @ret, $key, $s{$key}->{default};
447 # locale stuff; avoid LC_ALL since it overrides everything
448 if (defined $ENV{LC_ALL}) {
449 $ENV{LANG} = $ENV{LC_ALL};
452 if (defined $config{locale}) {
453 if (POSIX::setlocale(&POSIX::LC_ALL, $config{locale})) {
454 $ENV{LANG}=$config{locale};
459 if (! defined $config{wiki_file_regexp}) {
460 $config{wiki_file_regexp}=qr/(^[$config{wiki_file_chars}]+$)/;
463 if (ref $config{ENV} eq 'HASH') {
464 foreach my $val (keys %{$config{ENV}}) {
465 $ENV{$val}=$config{ENV}{$val};
469 if ($config{w3mmode}) {
470 eval q{use Cwd q{abs_path}};
472 $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir}));
473 $config{destdir}=possibly_foolish_untaint(abs_path($config{destdir}));
474 $config{cgiurl}="file:///\$LIB/ikiwiki-w3m.cgi/".$config{cgiurl}
475 unless $config{cgiurl} =~ m!file:///!;
476 $config{url}="file://".$config{destdir};
479 if ($config{cgi} && ! length $config{url}) {
480 error(gettext("Must specify url to wiki with --url when using --cgi"));
483 $config{wikistatedir}="$config{srcdir}/.ikiwiki"
484 unless exists $config{wikistatedir} && defined $config{wikistatedir};
486 if (defined $config{umask}) {
487 umask(possibly_foolish_untaint($config{umask}));
490 run_hooks(checkconfig => sub { shift->() });
498 foreach my $dir (@INC, $config{libdir}) {
499 next unless defined $dir && length $dir;
500 foreach my $file (glob("$dir/IkiWiki/Plugin/*.pm")) {
501 my ($plugin)=$file=~/.*\/(.*)\.pm$/;
505 foreach my $dir ($config{libdir}, "$installdir/lib/ikiwiki") {
506 next unless defined $dir && length $dir;
507 foreach my $file (glob("$dir/plugins/*")) {
508 $ret{basename($file)}=1 if -x $file;
516 if (defined $config{libdir} && length $config{libdir}) {
517 unshift @INC, possibly_foolish_untaint($config{libdir});
520 foreach my $plugin (@{$config{default_plugins}}, @{$config{add_plugins}}) {
525 if (exists $IkiWiki::hooks{rcs}) {
526 error(gettext("cannot use multiple rcs plugins"));
528 loadplugin($config{rcs});
530 if (! exists $IkiWiki::hooks{rcs}) {
534 run_hooks(getopt => sub { shift->() });
535 if (grep /^-/, @ARGV) {
536 print STDERR "Unknown option: $_\n"
537 foreach grep /^-/, @ARGV;
547 return if grep { $_ eq $plugin} @{$config{disable_plugins}};
549 foreach my $dir (defined $config{libdir} ? possibly_foolish_untaint($config{libdir}) : undef,
550 "$installdir/lib/ikiwiki") {
551 if (defined $dir && -x "$dir/plugins/$plugin") {
552 eval { require IkiWiki::Plugin::external };
555 error(sprintf(gettext("failed to load external plugin needed for %s plugin: %s"), $plugin, $reason));
557 import IkiWiki::Plugin::external "$dir/plugins/$plugin";
558 $loaded_plugins{$plugin}=1;
563 my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
566 error("Failed to load plugin $mod: $@");
568 $loaded_plugins{$plugin}=1;
575 log_message('err' => $message) if $config{syslog};
576 if (defined $cleaner) {
583 return unless $config{verbose};
584 return log_message(debug => @_);
588 sub log_message ($$) {
591 if ($config{syslog}) {
594 Sys::Syslog::setlogsock('unix');
595 Sys::Syslog::openlog('ikiwiki', '', 'user');
599 Sys::Syslog::syslog($type, "[$config{wikiname}] %s", join(" ", @_));
602 elsif (! $config{cgi}) {
606 return print STDERR "@_\n";
610 sub possibly_foolish_untaint ($) {
612 my ($untainted)=$tainted=~/(.*)/s;
633 if ($page =~ /\.([^.]+)$/) {
634 return $1 if exists $hooks{htmlize}{$1};
641 return exists $pagesources{$page} &&
642 $pagesources{$page} =~ /\._([^.]+)$/;
648 my $type=pagetype($file);
650 $page=~s/\Q.$type\E*$// if defined $type && !$hooks{htmlize}{$type}{keepextension};
651 if ($config{indexpages} && $page=~/(.*)\/index$/) {
657 sub newpagefile ($$) {
661 if (! $config{indexpages} || $page eq 'index') {
662 return $page.".".$type;
665 return $page."/index.".$type;
669 sub targetpage ($$;$) {
674 if (defined $filename) {
675 return $page."/".$filename.".".$ext;
677 elsif (! $config{usedirs} || $page eq 'index') {
678 return $page.".".$ext;
681 return $page."/index.".$ext;
688 return targetpage($page, $config{htmlext});
695 return "$config{srcdir}/$file", stat(_) if -e "$config{srcdir}/$file";
696 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
697 return "$dir/$file", stat(_) if -e "$dir/$file";
699 error("internal error: $file cannot be found in $config{srcdir} or underlay") unless $nothrow;
704 return (srcfile_stat(@_))[0];
707 sub add_underlay ($) {
711 $dir="$config{underlaydir}/../$dir";
714 if (! grep { $_ eq $dir } @{$config{underlaydirs}}) {
715 unshift @{$config{underlaydirs}}, $dir;
721 sub readfile ($;$$) {
727 error("cannot read a symlink ($file)");
731 open (my $in, "<", $file) || error("failed to read $file: $!");
732 binmode($in) if ($binary);
733 return \*$in if $wantfd;
735 # check for invalid utf-8, and toss it back to avoid crashes
736 if (! utf8::valid($ret)) {
737 $ret=encode_utf8($ret);
739 close $in || error("failed to read $file: $!");
743 sub prep_writefile ($$) {
748 while (length $test) {
749 if (-l "$destdir/$test") {
750 error("cannot write to a symlink ($test)");
752 $test=dirname($test);
755 my $dir=dirname("$destdir/$file");
758 foreach my $s (split(m!/+!, $dir)) {
761 mkdir($d) || error("failed to create directory $d: $!");
769 sub writefile ($$$;$$) {
770 my $file=shift; # can include subdirs
771 my $destdir=shift; # directory to put file in
776 prep_writefile($file, $destdir);
778 my $newfile="$destdir/$file.ikiwiki-new";
780 error("cannot write to a symlink ($newfile)");
783 my $cleanup = sub { unlink($newfile) };
784 open (my $out, '>', $newfile) || error("failed to write $newfile: $!", $cleanup);
785 binmode($out) if ($binary);
787 $writer->(\*$out, $cleanup);
790 print $out $content or error("failed writing to $newfile: $!", $cleanup);
792 close $out || error("failed saving $newfile: $!", $cleanup);
793 rename($newfile, "$destdir/$file") ||
794 error("failed renaming $newfile to $destdir/$file: $!", $cleanup);
800 sub will_render ($$;$) {
805 # Important security check.
806 if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
807 ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}}, @{$wikistate{editpage}{previews}})) {
808 error("$config{destdir}/$dest independently created, not overwriting with version from $page");
811 if (! $clear || $cleared{$page}) {
812 $renderedfiles{$page}=[$dest, grep { $_ ne $dest } @{$renderedfiles{$page}}];
815 foreach my $old (@{$renderedfiles{$page}}) {
816 delete $destsources{$old};
818 $renderedfiles{$page}=[$dest];
821 $destsources{$dest}=$page;
831 if ($link=~s/^\/+//) {
839 $l.="/" if length $l;
842 if (exists $links{$l}) {
845 elsif (exists $pagecase{lc $l}) {
846 return $pagecase{lc $l};
848 } while $cwd=~s{/?[^/]+$}{};
850 if (length $config{userdir}) {
851 my $l = "$config{userdir}/".lc($link);
852 if (exists $links{$l}) {
855 elsif (exists $pagecase{lc $l}) {
856 return $pagecase{lc $l};
860 #print STDERR "warning: page $page, broken link: $link\n";
864 sub isinlinableimage ($) {
867 return $file =~ /\.(png|gif|jpg|jpeg)$/i;
870 sub pagetitle ($;$) {
875 $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : chr($2)/eg;
878 $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : "&#$2;"/eg;
886 # support use w/o %config set
887 my $chars = defined $config{wiki_file_chars} ? $config{wiki_file_chars} : "-[:alnum:]+/.:_";
888 $title=~s/([^$chars]|_)/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
894 my $chars = defined $config{wiki_file_chars} ? $config{wiki_file_chars} : "-[:alnum:]+/.:_";
895 $link=~s/([^$chars])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
902 return $config{cgiurl}."?".
903 join("&", map $_."=".uri_escape_utf8($params{$_}), keys %params);
909 return "$config{url}/" if ! defined $page;
911 $page=htmlpage($page);
913 $page=~s/[^\/]+\//..\//g;
918 # Work around very innefficient behavior in File::Spec if abs2rel
919 # is passed two relative paths. It's much faster if paths are
920 # absolute! (Debian bug #376658; fixed in debian unstable now)
925 my $ret=File::Spec->abs2rel($path, $base);
926 $ret=~s/^// if defined $ret;
930 sub displaytime ($;$) {
931 # Plugins can override this function to mark up the time to
933 return '<span class="date">'.formattime(@_).'</span>';
936 sub formattime ($;$) {
937 # Plugins can override this function to format the time.
940 if (! defined $format) {
941 $format=$config{timeformat};
944 # strftime doesn't know about encodings, so make sure
945 # its output is properly treated as utf8
946 return decode_utf8(POSIX::strftime($format, localtime($time)));
949 sub beautify_urlpath ($) {
952 # Ensure url is not an empty link, and if necessary,
953 # add ./ to avoid colon confusion.
954 if ($url !~ /^\// && $url !~ /^\.\.?\//) {
958 if ($config{usedirs}) {
959 $url =~ s!/index.$config{htmlext}$!/!;
971 return beautify_urlpath(baseurl($from)."index.$config{htmlext}");
974 if (! $destsources{$to}) {
979 return $config{url}.beautify_urlpath("/".$to);
982 my $link = abs2rel($to, dirname(htmlpage($from)));
984 return beautify_urlpath($link);
987 sub htmllink ($$$;@) {
988 my $lpage=shift; # the page doing the linking
989 my $page=shift; # the page that will contain the link (different for inline)
996 if (! $opts{forcesubpage}) {
997 $bestlink=bestlink($lpage, $link);
1000 $bestlink="$lpage/".lc($link);
1004 if (defined $opts{linktext}) {
1005 $linktext=$opts{linktext};
1008 $linktext=pagetitle(basename($link));
1011 return "<span class=\"selflink\">$linktext</span>"
1012 if length $bestlink && $page eq $bestlink &&
1013 ! defined $opts{anchor};
1015 if (! $destsources{$bestlink}) {
1016 $bestlink=htmlpage($bestlink);
1018 if (! $destsources{$bestlink}) {
1019 return $linktext unless length $config{cgiurl};
1020 return "<span class=\"createlink\"><a href=\"".
1026 "\" rel=\"nofollow\">?</a>$linktext</span>"
1030 $bestlink=abs2rel($bestlink, dirname(htmlpage($page)));
1031 $bestlink=beautify_urlpath($bestlink);
1033 if (! $opts{noimageinline} && isinlinableimage($bestlink)) {
1034 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
1037 if (defined $opts{anchor}) {
1038 $bestlink.="#".$opts{anchor};
1042 if (defined $opts{rel}) {
1043 push @attrs, ' rel="'.$opts{rel}.'"';
1045 if (defined $opts{class}) {
1046 push @attrs, ' class="'.$opts{class}.'"';
1049 return "<a href=\"$bestlink\"@attrs>$linktext</a>";
1055 my $oiduser=eval { openiduser($user) };
1056 if (defined $oiduser) {
1057 return "<a href=\"$user\">$oiduser</a>";
1060 eval q{use CGI 'escapeHTML'};
1063 return htmllink("", "", escapeHTML(
1064 length $config{userdir} ? $config{userdir}."/".$user : $user
1065 ), noimageinline => 1);
1069 sub htmlize ($$$$) {
1075 my $oneline = $content !~ /\n/;
1077 if (exists $hooks{htmlize}{$type}) {
1078 $content=$hooks{htmlize}{$type}{call}->(
1080 content => $content,
1084 error("htmlization of $type not supported");
1087 run_hooks(sanitize => sub {
1090 destpage => $destpage,
1091 content => $content,
1096 # hack to get rid of enclosing junk added by markdown
1097 # and other htmlizers
1098 $content=~s/^<p>//i;
1099 $content=~s/<\/p>$//i;
1111 run_hooks(linkify => sub {
1114 destpage => $destpage,
1115 content => $content,
1123 our $preprocess_preview=0;
1124 sub preprocess ($$$;$$) {
1125 my $page=shift; # the page the data comes from
1126 my $destpage=shift; # the page the data will appear in (different for inline)
1131 # Using local because it needs to be set within any nested calls
1133 local $preprocess_preview=$preview if defined $preview;
1140 $params="" if ! defined $params;
1142 if (length $escape) {
1143 return "[[$prefix$command $params]]";
1145 elsif (exists $hooks{preprocess}{$command}) {
1146 return "" if $scan && ! $hooks{preprocess}{$command}{scan};
1147 # Note: preserve order of params, some plugins may
1148 # consider it significant.
1150 while ($params =~ m{
1151 (?:([-\w]+)=)? # 1: named parameter key?
1153 """(.*?)""" # 2: triple-quoted value
1155 "([^"]+)" # 3: single-quoted value
1157 (\S+) # 4: unquoted value
1159 (?:\s+|$) # delimiter to next param
1169 elsif (defined $3) {
1172 elsif (defined $4) {
1177 push @params, $key, $val;
1180 push @params, $val, '';
1183 if ($preprocessing{$page}++ > 3) {
1184 # Avoid loops of preprocessed pages preprocessing
1185 # other pages that preprocess them, etc.
1186 return "[[!$command <span class=\"error\">".
1187 sprintf(gettext("preprocessing loop detected on %s at depth %i"),
1188 $page, $preprocessing{$page}).
1194 $hooks{preprocess}{$command}{call}->(
1197 destpage => $destpage,
1198 preview => $preprocess_preview,
1203 $ret="[[!$command <span class=\"error\">".
1204 gettext("Error").": $@"."</span>]]";
1208 # use void context during scan pass
1210 $hooks{preprocess}{$command}{call}->(
1213 destpage => $destpage,
1214 preview => $preprocess_preview,
1219 $preprocessing{$page}--;
1223 return "[[$prefix$command $params]]";
1228 if ($config{prefix_directives}) {
1231 \[\[(!) # directive open; 2: prefix
1232 ([-\w]+) # 3: command
1233 ( # 4: the parameters..
1234 \s+ # Must have space if parameters present
1236 (?:[-\w]+=)? # named parameter key?
1238 """.*?""" # triple-quoted value
1240 "[^"]+" # single-quoted value
1242 [^\s\]]+ # unquoted value
1244 \s* # whitespace or end
1247 *)? # 0 or more parameters
1248 \]\] # directive closed
1254 \[\[(!?) # directive open; 2: optional prefix
1255 ([-\w]+) # 3: command
1257 ( # 4: the parameters..
1259 (?:[-\w]+=)? # named parameter key?
1261 """.*?""" # triple-quoted value
1263 "[^"]+" # single-quoted value
1265 [^\s\]]+ # unquoted value
1267 \s* # whitespace or end
1270 *) # 0 or more parameters
1271 \]\] # directive closed
1275 $content =~ s{$regex}{$handle->($1, $2, $3, $4)}eg;
1284 run_hooks(filter => sub {
1285 $content=shift->(page => $page, destpage => $destpage,
1286 content => $content);
1293 return "<a href=\"$config{url}\">$config{wikiname}</a>";
1299 # Take an exclusive lock on the wiki to prevent multiple concurrent
1300 # run issues. The lock will be dropped on program exit.
1301 if (! -d $config{wikistatedir}) {
1302 mkdir($config{wikistatedir});
1304 open($wikilock, '>', "$config{wikistatedir}/lockfile") ||
1305 error ("cannot write to $config{wikistatedir}/lockfile: $!");
1306 if (! flock($wikilock, 2)) { # LOCK_EX
1307 error("failed to get lock");
1313 POSIX::close($ENV{IKIWIKI_CGILOCK_FD}) if exists $ENV{IKIWIKI_CGILOCK_FD};
1314 return close($wikilock) if $wikilock;
1320 sub commit_hook_enabled () {
1321 open($commitlock, '+>', "$config{wikistatedir}/commitlock") ||
1322 error("cannot write to $config{wikistatedir}/commitlock: $!");
1323 if (! flock($commitlock, 1 | 4)) { # LOCK_SH | LOCK_NB to test
1324 close($commitlock) || error("failed closing commitlock: $!");
1327 close($commitlock) || error("failed closing commitlock: $!");
1331 sub disable_commit_hook () {
1332 open($commitlock, '>', "$config{wikistatedir}/commitlock") ||
1333 error("cannot write to $config{wikistatedir}/commitlock: $!");
1334 if (! flock($commitlock, 2)) { # LOCK_EX
1335 error("failed to get commit lock");
1340 sub enable_commit_hook () {
1341 return close($commitlock) if $commitlock;
1346 %oldrenderedfiles=%pagectime=();
1347 if (! $config{rebuild}) {
1348 %pagesources=%pagemtime=%oldlinks=%links=%depends=
1349 %destsources=%renderedfiles=%pagecase=%pagestate=();
1352 if (! open ($in, "<", "$config{wikistatedir}/indexdb")) {
1353 if (-e "$config{wikistatedir}/index") {
1354 system("ikiwiki-transition", "indexdb", $config{srcdir});
1355 open ($in, "<", "$config{wikistatedir}/indexdb") || return;
1362 my $index=Storable::fd_retrieve($in);
1363 if (! defined $index) {
1368 if (exists $index->{version} && ! ref $index->{version}) {
1369 $pages=$index->{page};
1370 %wikistate=%{$index->{state}};
1377 foreach my $src (keys %$pages) {
1378 my $d=$pages->{$src};
1379 my $page=pagename($src);
1380 $pagectime{$page}=$d->{ctime};
1381 if (! $config{rebuild}) {
1382 $pagesources{$page}=$src;
1383 $pagemtime{$page}=$d->{mtime};
1384 $renderedfiles{$page}=$d->{dest};
1385 if (exists $d->{links} && ref $d->{links}) {
1386 $links{$page}=$d->{links};
1387 $oldlinks{$page}=[@{$d->{links}}];
1389 if (exists $d->{depends}) {
1390 $depends{$page}=$d->{depends};
1392 if (exists $d->{state}) {
1393 $pagestate{$page}=$d->{state};
1396 $oldrenderedfiles{$page}=[@{$d->{dest}}];
1398 foreach my $page (keys %pagesources) {
1399 $pagecase{lc $page}=$page;
1401 foreach my $page (keys %renderedfiles) {
1402 $destsources{$_}=$page foreach @{$renderedfiles{$page}};
1408 run_hooks(savestate => sub { shift->() });
1411 foreach my $type (keys %hooks) {
1412 $hookids{$_}=1 foreach keys %{$hooks{$type}};
1414 my @hookids=keys %hookids;
1416 if (! -d $config{wikistatedir}) {
1417 mkdir($config{wikistatedir});
1419 my $newfile="$config{wikistatedir}/indexdb.new";
1420 my $cleanup = sub { unlink($newfile) };
1421 open (my $out, '>', $newfile) || error("cannot write to $newfile: $!", $cleanup);
1424 foreach my $page (keys %pagemtime) {
1425 next unless $pagemtime{$page};
1426 my $src=$pagesources{$page};
1428 $index{page}{$src}={
1429 ctime => $pagectime{$page},
1430 mtime => $pagemtime{$page},
1431 dest => $renderedfiles{$page},
1432 links => $links{$page},
1435 if (exists $depends{$page}) {
1436 $index{page}{$src}{depends} = $depends{$page};
1439 if (exists $pagestate{$page}) {
1440 foreach my $id (@hookids) {
1441 foreach my $key (keys %{$pagestate{$page}{$id}}) {
1442 $index{page}{$src}{state}{$id}{$key}=$pagestate{$page}{$id}{$key};
1449 foreach my $id (@hookids) {
1450 foreach my $key (keys %{$wikistate{$id}}) {
1451 $index{state}{$id}{$key}=$wikistate{$id}{$key};
1455 $index{version}="3";
1456 my $ret=Storable::nstore_fd(\%index, $out);
1457 return if ! defined $ret || ! $ret;
1458 close $out || error("failed saving to $newfile: $!", $cleanup);
1459 rename($newfile, "$config{wikistatedir}/indexdb") ||
1460 error("failed renaming $newfile to $config{wikistatedir}/indexdb", $cleanup);
1465 sub template_file ($) {
1468 foreach my $dir ($config{templatedir}, "$installdir/share/ikiwiki/templates") {
1469 return "$dir/$template" if -e "$dir/$template";
1474 sub template_params (@) {
1475 my $filename=template_file(shift);
1477 if (! defined $filename) {
1478 return if wantarray;
1484 my $text_ref = shift;
1485 ${$text_ref} = decode_utf8(${$text_ref});
1487 filename => $filename,
1488 loop_context_vars => 1,
1489 die_on_bad_params => 0,
1492 return wantarray ? @ret : {@ret};
1495 sub template ($;@) {
1496 require HTML::Template;
1497 return HTML::Template->new(template_params(@_));
1500 sub misctemplate ($$;@) {
1504 my $template=template("misc.tmpl");
1507 indexlink => indexlink(),
1508 wikiname => $config{wikiname},
1509 pagebody => $pagebody,
1510 baseurl => baseurl(),
1513 run_hooks(pagetemplate => sub {
1514 shift->(page => "", destpage => "", template => $template);
1516 return $template->output;
1522 if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
1523 error 'hook requires type, call, and id parameters';
1526 return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
1528 $hooks{$param{type}}{$param{id}}=\%param;
1532 sub run_hooks ($$) {
1533 # Calls the given sub for each hook of the given type,
1534 # passing it the hook function to call.
1538 if (exists $hooks{$type}) {
1539 my (@first, @middle, @last);
1540 foreach my $id (keys %{$hooks{$type}}) {
1541 if ($hooks{$type}{$id}{first}) {
1544 elsif ($hooks{$type}{$id}{last}) {
1551 foreach my $id (@first, @middle, @last) {
1552 $sub->($hooks{$type}{$id}{call});
1560 $hooks{rcs}{rcs_update}{call}->(@_);
1563 sub rcs_prepedit ($) {
1564 $hooks{rcs}{rcs_prepedit}{call}->(@_);
1567 sub rcs_commit ($$$;$$) {
1568 $hooks{rcs}{rcs_commit}{call}->(@_);
1571 sub rcs_commit_staged ($$$) {
1572 $hooks{rcs}{rcs_commit_staged}{call}->(@_);
1576 $hooks{rcs}{rcs_add}{call}->(@_);
1579 sub rcs_remove ($) {
1580 $hooks{rcs}{rcs_remove}{call}->(@_);
1583 sub rcs_rename ($$) {
1584 $hooks{rcs}{rcs_rename}{call}->(@_);
1587 sub rcs_recentchanges ($) {
1588 $hooks{rcs}{rcs_recentchanges}{call}->(@_);
1592 $hooks{rcs}{rcs_diff}{call}->(@_);
1595 sub rcs_getctime ($) {
1596 $hooks{rcs}{rcs_getctime}{call}->(@_);
1599 sub rcs_receive () {
1600 $hooks{rcs}{rcs_receive}{call}->();
1609 sub add_depends ($$) {
1613 return unless pagespec_valid($pagespec);
1615 if (! exists $depends{$page}) {
1616 $depends{$page}=$pagespec;
1619 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
1625 sub file_pruned ($$) {
1627 my $file=File::Spec->canonpath(shift);
1628 my $base=File::Spec->canonpath(shift);
1629 $file =~ s#^\Q$base\E/+##;
1631 my $regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
1632 return $file =~ m/$regexp/ && $file ne $base;
1636 # Only use gettext in the rare cases it's needed.
1637 if ((exists $ENV{LANG} && length $ENV{LANG}) ||
1638 (exists $ENV{LC_ALL} && length $ENV{LC_ALL}) ||
1639 (exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES})) {
1640 if (! $gettext_obj) {
1641 $gettext_obj=eval q{
1642 use Locale::gettext q{textdomain};
1643 Locale::gettext->domain('ikiwiki')
1651 return $gettext_obj->get(shift);
1661 return (defined $val && (lc($val) eq gettext("yes") || lc($val) eq "yes" || $val eq "1"));
1665 # Injects a new function into the symbol table to replace an
1666 # exported function.
1669 # This is deep ugly perl foo, beware.
1672 if (! defined $params{parent}) {
1673 $params{parent}='::';
1674 $params{old}=\&{$params{name}};
1675 $params{name}=~s/.*:://;
1677 my $parent=$params{parent};
1678 foreach my $ns (grep /^\w+::/, keys %{$parent}) {
1679 $ns = $params{parent} . $ns;
1680 inject(%params, parent => $ns) unless $ns eq '::main::';
1681 *{$ns . $params{name}} = $params{call}
1682 if exists ${$ns}{$params{name}} &&
1683 \&{${$ns}{$params{name}}} == $params{old};
1689 sub pagespec_merge ($$) {
1693 return $a if $a eq $b;
1694 return "($a) or ($b)";
1697 sub pagespec_translate ($) {
1700 # Convert spec to perl code.
1703 \s* # ignore whitespace
1704 ( # 1: match a single word
1711 \w+\([^\)]*\) # command(params)
1713 [^\s()]+ # any other text
1715 \s* # ignore whitespace
1718 if (lc $word eq 'and') {
1721 elsif (lc $word eq 'or') {
1724 elsif ($word eq "(" || $word eq ")" || $word eq "!") {
1727 elsif ($word =~ /^(\w+)\((.*)\)$/) {
1728 if (exists $IkiWiki::PageSpec::{"match_$1"}) {
1729 $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \@_)";
1732 $code.="IkiWiki::FailReason->new(".safequote(qq{unknown function in pagespec "$word"}).")";
1736 $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \@_)";
1740 if (! length $code) {
1741 $code="IkiWiki::FailReason->new('empty pagespec')";
1745 return eval 'sub { my $page=shift; '.$code.' }';
1748 sub pagespec_match ($$;@) {
1753 # Backwards compatability with old calling convention.
1755 unshift @params, 'location';
1758 my $sub=pagespec_translate($spec);
1759 return IkiWiki::FailReason->new("syntax error in pagespec \"$spec\"")
1760 if $@ || ! defined $sub;
1761 return $sub->($page, @params);
1764 sub pagespec_valid ($) {
1767 my $sub=pagespec_translate($spec);
1772 my $re=quotemeta(shift);
1778 package IkiWiki::FailReason;
1781 '""' => sub { ${$_[0]} },
1783 '!' => sub { bless $_[0], 'IkiWiki::SuccessReason'},
1790 return bless \$value, $class;
1793 package IkiWiki::SuccessReason;
1796 '""' => sub { ${$_[0]} },
1798 '!' => sub { bless $_[0], 'IkiWiki::FailReason'},
1805 return bless \$value, $class;
1808 package IkiWiki::PageSpec;
1814 if ($path =~ m!^\./!) {
1815 $from=~s#/?[^/]+$## if defined $from;
1817 $path="$from/$path" if length $from;
1823 sub match_glob ($$;@) {
1828 $glob=derel($glob, $params{location});
1830 my $regexp=IkiWiki::glob2re($glob);
1831 if ($page=~/^$regexp$/i) {
1832 if (! IkiWiki::isinternal($page) || $params{internal}) {
1833 return IkiWiki::SuccessReason->new("$glob matches $page");
1836 return IkiWiki::FailReason->new("$glob matches $page, but the page is an internal page");
1840 return IkiWiki::FailReason->new("$glob does not match $page");
1844 sub match_internal ($$;@) {
1845 return match_glob($_[0], $_[1], @_, internal => 1)
1848 sub match_link ($$;@) {
1853 $link=derel($link, $params{location});
1854 my $from=exists $params{location} ? $params{location} : '';
1856 my $links = $IkiWiki::links{$page};
1857 return IkiWiki::FailReason->new("$page has no links") unless $links && @{$links};
1858 my $bestlink = IkiWiki::bestlink($from, $link);
1859 foreach my $p (@{$links}) {
1860 if (length $bestlink) {
1861 return IkiWiki::SuccessReason->new("$page links to $link")
1862 if $bestlink eq IkiWiki::bestlink($page, $p);
1865 return IkiWiki::SuccessReason->new("$page links to page $p matching $link")
1866 if match_glob($p, $link, %params);
1869 return IkiWiki::SuccessReason->new("$page links to page $p matching $link")
1870 if match_glob($p, $link, %params);
1873 return IkiWiki::FailReason->new("$page does not link to $link");
1876 sub match_backlink ($$;@) {
1877 return match_link($_[1], $_[0], @_);
1880 sub match_created_before ($$;@) {
1885 $testpage=derel($testpage, $params{location});
1887 if (exists $IkiWiki::pagectime{$testpage}) {
1888 if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
1889 return IkiWiki::SuccessReason->new("$page created before $testpage");
1892 return IkiWiki::FailReason->new("$page not created before $testpage");
1896 return IkiWiki::FailReason->new("$testpage has no ctime");
1900 sub match_created_after ($$;@) {
1905 $testpage=derel($testpage, $params{location});
1907 if (exists $IkiWiki::pagectime{$testpage}) {
1908 if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
1909 return IkiWiki::SuccessReason->new("$page created after $testpage");
1912 return IkiWiki::FailReason->new("$page not created after $testpage");
1916 return IkiWiki::FailReason->new("$testpage has no ctime");
1920 sub match_creation_day ($$;@) {
1921 if ((gmtime($IkiWiki::pagectime{shift()}))[3] == shift) {
1922 return IkiWiki::SuccessReason->new('creation_day matched');
1925 return IkiWiki::FailReason->new('creation_day did not match');
1929 sub match_creation_month ($$;@) {
1930 if ((gmtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
1931 return IkiWiki::SuccessReason->new('creation_month matched');
1934 return IkiWiki::FailReason->new('creation_month did not match');
1938 sub match_creation_year ($$;@) {
1939 if ((gmtime($IkiWiki::pagectime{shift()}))[5] + 1900 == shift) {
1940 return IkiWiki::SuccessReason->new('creation_year matched');
1943 return IkiWiki::FailReason->new('creation_year did not match');
1947 sub match_user ($$;@) {
1952 if (! exists $params{user}) {
1953 return IkiWiki::FailReason->new("no user specified");
1956 if (defined $params{user} && lc $params{user} eq lc $user) {
1957 return IkiWiki::SuccessReason->new("user is $user");
1959 elsif (! defined $params{user}) {
1960 return IkiWiki::FailReason->new("not logged in");
1963 return IkiWiki::FailReason->new("user is $params{user}, not $user");
1967 sub match_admin ($$;@) {
1972 if (! exists $params{user}) {
1973 return IkiWiki::FailReason->new("no user specified");
1976 if (defined $params{user} && IkiWiki::is_admin($params{user})) {
1977 return IkiWiki::SuccessReason->new("user is an admin");
1979 elsif (! defined $params{user}) {
1980 return IkiWiki::FailReason->new("not logged in");
1983 return IkiWiki::FailReason->new("user is not an admin");
1987 sub match_ip ($$;@) {
1992 if (! exists $params{ip}) {
1993 return IkiWiki::FailReason->new("no IP specified");
1996 if (defined $params{ip} && lc $params{ip} eq lc $ip) {
1997 return IkiWiki::SuccessReason->new("IP is $ip");
2000 return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");