X-Git-Url: http://git.vanrenterghem.biz/git.ikiwiki.info.git/blobdiff_plain/b86276ffed7ee001b35cd610e5d56e5afb4088cf..258a9e1e1bd50968cbd53349fc3bea0e9b1d4e05:/IkiWiki.pm?ds=sidebyside

diff --git a/IkiWiki.pm b/IkiWiki.pm
index a89c14058..1433af99b 100644
--- a/IkiWiki.pm
+++ b/IkiWiki.pm
@@ -5,26 +5,26 @@ package IkiWiki;
 use warnings;
 use strict;
 use Encode;
-use HTML::Entities;
 use URI::Escape q{uri_escape_utf8};
 use POSIX ();
 use Storable;
 use open qw{:utf8 :std};
 
 use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
-	    %pagestate %wikistate %renderedfiles %oldrenderedfiles
-	    %pagesources %destsources %depends %depends_simple %hooks
-	    %forcerebuild %loaded_plugins};
+	%pagestate %wikistate %renderedfiles %oldrenderedfiles
+	%pagesources %delpagesources %destsources %depends %depends_simple
+	@mass_depends %hooks %forcerebuild %loaded_plugins %typedlinks
+	%oldtypedlinks %autofiles};
 
 use Exporter q{import};
-our @EXPORT = qw(hook debug error template htmlpage deptype
-                 add_depends pagespec_match pagespec_match_list bestlink
-		 htmllink readfile writefile pagetype srcfile pagename
-		 displaytime will_render gettext ngettext urlto targetpage
-		 add_underlay pagetitle titlepage linkpage newpagefile
-		 inject add_link
-                 %config %links %pagestate %wikistate %renderedfiles
-                 %pagesources %destsources);
+our @EXPORT = qw(hook debug error htmlpage template template_depends
+	deptype add_depends pagespec_match pagespec_match_list bestlink
+	htmllink readfile writefile pagetype srcfile pagename
+	displaytime strftime_utf8 will_render gettext ngettext urlto targetpage
+	add_underlay pagetitle titlepage linkpage newpagefile
+	inject add_link add_autofile
+	%config %links %pagestate %wikistate %renderedfiles
+	%pagesources %destsources %typedlinks);
 our $VERSION = 3.00; # plugin interface version, next is ikiwiki version
 our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
 our $installdir='/usr'; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
@@ -37,7 +37,7 @@ our $DEPEND_LINKS=4;
 # Optimisation.
 use Memoize;
 memoize("abs2rel");
-memoize("cmpspec_translate");
+memoize("sortspec_translate");
 memoize("pagespec_translate");
 memoize("template_file");
 
@@ -151,18 +151,11 @@ sub getsetup () {
 	templatedir => {
 		type => "string",
 		default => "$installdir/share/ikiwiki/templates",
-		description => "location of template files",
+		description => "additional directory to search for template files",
 		advanced => 1,
 		safe => 0, # path
 		rebuild => 1,
 	},
-	templatedirs => {
-		type => "internal",
-		default => [],
-		description => "additional directories containing template files",
-		safe => 0,
-		rebuild => 0,
-	},
 	underlaydir => {
 		type => "string",
 		default => "$installdir/share/ikiwiki/basewiki",
@@ -241,6 +234,14 @@ sub getsetup () {
 		safe => 1,
 		rebuild => 1,
 	},
+	html5 => {
+		type => "boolean",
+		default => 0,
+		description => "generate HTML5?",
+		advanced => 0,
+		safe => 1,
+		rebuild => 1,
+	},
 	sslcookie => {
 		type => "boolean",
 		default => 0,
@@ -304,9 +305,9 @@ sub getsetup () {
 		rebuild => 0,
 	},
 	umask => {
-		type => "integer",
-		example => "022",
-		description => "force ikiwiki to use a particular umask",
+		type => "string",
+		example => "public",
+		description => "force ikiwiki to use a particular umask (keywords public, group or private, or a number)",
 		advanced => 1,
 		safe => 0, # paranoia
 		rebuild => 0,
@@ -335,6 +336,14 @@ sub getsetup () {
 		safe => 0, # paranoia
 		rebuild => 0,
 	},
+	timezone => {
+		type => "string", 
+		default => "",
+		example => "US/Eastern",
+		description => "time zone name",
+		safe => 1,
+		rebuild => 1,
+	},
 	include => {
 		type => "string",
 		default => undef,
@@ -355,7 +364,7 @@ sub getsetup () {
 	},
 	wiki_file_prune_regexps => {
 		type => "internal",
-		default => [qr/(^|\/)\.\.(\/|$)/, qr/^\./, qr/\/\./,
+		default => [qr/(^|\/)\.\.(\/|$)/, qr/^\//, qr/^\./, qr/\/\./,
 			qr/\.x?html?$/, qr/\.ikiwiki-new$/,
 			qr/(^|\/).svn\//, qr/.arch-ids\//, qr/{arch}\//,
 			qr/(^|\/)_MTN\//, qr/(^|\/)_darcs\//,
@@ -440,10 +449,16 @@ sub getsetup () {
 		safe => 0,
 		rebuild => 0,
 	},
-	getctime => {
+	wrapper_background_command => {
 		type => "internal",
-		default => 0,
-		description => "running in getctime mode",
+		default => '',
+		description => "background shell command to run",
+		safe => 0,
+		rebuild => 0,
+	},
+	gettime => {
+		type => "internal",
+		description => "running in gettime mode",
 		safe => 0,
 		rebuild => 0,
 	},
@@ -470,7 +485,7 @@ sub getsetup () {
 	},
 	setuptype => {
 		type => "internal",
-		default => "Standard",
+		default => "Yaml",
 		description => "perl class to use to dump setup file",
 		safe => 0,
 		rebuild => 0,
@@ -490,10 +505,15 @@ sub defaultconfig () {
 	foreach my $key (keys %s) {
 		push @ret, $key, $s{$key}->{default};
 	}
-	use Data::Dumper;
 	return @ret;
 }
 
+# URL to top of wiki as a path starting with /, valid from any wiki page or
+# the CGI; if that's not possible, an absolute URL. Either way, it ends with /
+my $local_url;
+# URL to CGI script, similar to $local_url
+my $local_cgiurl;
+
 sub checkconfig () {
 	# locale stuff; avoid LC_ALL since it overrides everything
 	if (defined $ENV{LC_ALL}) {
@@ -516,6 +536,12 @@ sub checkconfig () {
 			$ENV{$val}=$config{ENV}{$val};
 		}
 	}
+	if (defined $config{timezone} && length $config{timezone}) {
+		$ENV{TZ}=$config{timezone};
+	}
+	else {
+		$config{timezone}=$ENV{TZ};
+	}
 
 	if ($config{w3mmode}) {
 		eval q{use Cwd q{abs_path}};
@@ -530,12 +556,54 @@ sub checkconfig () {
 	if ($config{cgi} && ! length $config{url}) {
 		error(gettext("Must specify url to wiki with --url when using --cgi"));
 	}
-	
+
+	if (defined $config{url} && length $config{url}) {
+		eval q{use URI};
+		my $baseurl = URI->new($config{url});
+
+		$local_url = $baseurl->path . "/";
+		$local_cgiurl = undef;
+
+		if (length $config{cgiurl}) {
+			my $cgiurl = URI->new($config{cgiurl});
+
+			$local_cgiurl = $cgiurl->path;
+
+			if ($cgiurl->scheme ne $baseurl->scheme or
+				$cgiurl->authority ne $baseurl->authority) {
+				# too far apart, fall back to absolute URLs
+				$local_url = "$config{url}/";
+				$local_cgiurl = $config{cgiurl};
+			}
+		}
+
+		$local_url =~ s{//$}{/};
+	}
+	else {
+		$local_cgiurl = $config{cgiurl};
+	}
+
 	$config{wikistatedir}="$config{srcdir}/.ikiwiki"
 		unless exists $config{wikistatedir} && defined $config{wikistatedir};
 
 	if (defined $config{umask}) {
-		umask(possibly_foolish_untaint($config{umask}));
+		my $u = possibly_foolish_untaint($config{umask});
+
+		if ($u =~ m/^\d+$/) {
+			umask($u);
+		}
+		elsif ($u eq 'private') {
+			umask(077);
+		}
+		elsif ($u eq 'group') {
+			umask(027);
+		}
+		elsif ($u eq 'public') {
+			umask(022);
+		}
+		else {
+			error(sprintf(gettext("unsupported umask setting %s"), $u));
+		}
 	}
 
 	run_hooks(checkconfig => sub { shift->() });
@@ -592,10 +660,11 @@ sub loadplugins () {
 	return 1;
 }
 
-sub loadplugin ($) {
+sub loadplugin ($;$) {
 	my $plugin=shift;
+	my $force=shift;
 
-	return if grep { $_ eq $plugin} @{$config{disable_plugins}};
+	return if ! $force && grep { $_ eq $plugin} @{$config{disable_plugins}};
 
 	foreach my $dir (defined $config{libdir} ? possibly_foolish_untaint($config{libdir}) : undef,
 	                 "$installdir/lib/ikiwiki") {
@@ -709,7 +778,7 @@ sub pagename ($) {
 
 	my $type=pagetype($file);
 	my $page=$file;
- 	$page=~s/\Q.$type\E*$//
+	$page=~s/\Q.$type\E*$//
 		if defined $type && !$hooks{htmlize}{$type}{keepextension}
 			&& !$hooks{htmlize}{$type}{noextension};
 	if ($config{indexpages} && $page=~/(.*)\/index$/) {
@@ -770,17 +839,23 @@ sub srcfile ($;$) {
 	return (srcfile_stat(@_))[0];
 }
 
-sub add_underlay ($) {
+sub add_literal_underlay ($) {
 	my $dir=shift;
 
-	if ($dir !~ /^\//) {
-		$dir="$config{underlaydirbase}/$dir";
-	}
-
 	if (! grep { $_ eq $dir } @{$config{underlaydirs}}) {
 		unshift @{$config{underlaydirs}}, $dir;
 	}
+}
 
+sub add_underlay ($) {
+	my $dir = shift;
+
+	if ($dir !~ /^\//) {
+		$dir="$config{underlaydirbase}/$dir";
+	}
+
+	add_literal_underlay($dir);
+	# why does it return 1? we just don't know
 	return 1;
 }
 
@@ -815,6 +890,17 @@ sub prep_writefile ($$) {
 		if (-l "$destdir/$test") {
 			error("cannot write to a symlink ($test)");
 		}
+		if (-f _ && $test ne $file) {
+			# Remove conflicting file.
+			foreach my $p (keys %renderedfiles, keys %oldrenderedfiles) {
+				foreach my $f (@{$renderedfiles{$p}}, @{$oldrenderedfiles{$p}}) {
+					if ($f eq $test) {
+						unlink("$destdir/$test");
+						last;
+					}
+				}
+			}
+		}
 		$test=dirname($test);
 	}
 
@@ -868,10 +954,36 @@ sub will_render ($$;$) {
 	my $dest=shift;
 	my $clear=shift;
 
-	# Important security check.
+	# Important security check for independently created files.
 	if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
 	    ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}}, @{$wikistate{editpage}{previews}})) {
-		error("$config{destdir}/$dest independently created, not overwriting with version from $page");
+		my $from_other_page=0;
+	    	# Expensive, but rarely runs.
+		foreach my $p (keys %renderedfiles, keys %oldrenderedfiles) {
+			if (grep {
+				$_ eq $dest ||
+				dirname($_) eq $dest
+			    } @{$renderedfiles{$p}}, @{$oldrenderedfiles{$p}}) {
+				$from_other_page=1;
+				last;
+			}
+		}
+
+		error("$config{destdir}/$dest independently created, not overwriting with version from $page")
+			unless $from_other_page;
+	}
+
+	# If $dest exists as a directory, remove conflicting files in it
+	# rendered from other pages.
+	if (-d _) {
+		foreach my $p (keys %renderedfiles, keys %oldrenderedfiles) {
+			foreach my $f (@{$renderedfiles{$p}}, @{$oldrenderedfiles{$p}}) {
+				if (dirname($f) eq $dest) {
+					unlink("$config{destdir}/$f");
+					rmdir(dirname("$config{destdir}/$f"));
+				}
+			}
+		}
 	}
 
 	if (! $clear || $cleared{$page}) {
@@ -930,7 +1042,7 @@ sub bestlink ($$) {
 sub isinlinableimage ($) {
 	my $file=shift;
 	
-	return $file =~ /\.(png|gif|jpg|jpeg)$/i;
+	return $file =~ /\.(png|gif|jpg|jpeg|svg)$/i;
 }
 
 sub pagetitle ($;$) {
@@ -965,19 +1077,30 @@ sub linkpage ($) {
 sub cgiurl (@) {
 	my %params=@_;
 
-	my $cgiurl=$config{cgiurl};
+	my $cgiurl=$local_cgiurl;
+
 	if (exists $params{cgiurl}) {
 		$cgiurl=$params{cgiurl};
 		delete $params{cgiurl};
 	}
+
+	unless (%params) {
+		return $cgiurl;
+	}
+
 	return $cgiurl."?".
 		join("&", map $_."=".uri_escape_utf8($params{$_}), keys %params);
 }
 
+sub cgiurl_abs (@) {
+	eval q{use URI};
+	URI->new_abs(cgiurl(@_), $config{cgiurl});
+}
+
 sub baseurl (;$) {
 	my $page=shift;
 
-	return "$config{url}/" if ! defined $page;
+	return $local_url if ! defined $page;
 	
 	$page=htmlpage($page);
 	$page=~s/[^\/]+$//;
@@ -985,6 +1108,16 @@ sub baseurl (;$) {
 	return $page;
 }
 
+sub urlabs ($$) {
+	my $url=shift;
+	my $urlbase=shift;
+
+	return $url unless defined $urlbase && length $urlbase;
+
+	eval q{use URI};
+	URI->new_abs($url, $urlbase)->as_string;
+}
+
 sub abs2rel ($$) {
 	# Work around very innefficient behavior in File::Spec if abs2rel
 	# is passed two relative paths. It's much faster if paths are
@@ -998,10 +1131,18 @@ sub abs2rel ($$) {
 	return $ret;
 }
 
-sub displaytime ($;$) {
+sub displaytime ($;$$) {
 	# Plugins can override this function to mark up the time to
 	# display.
-	return '<span class="date">'.formattime(@_).'</span>';
+	my $time=formattime($_[0], $_[1]);
+	if ($config{html5}) {
+		return '<time datetime="'.date_3339($_[0]).'"'.
+			($_[2] ? ' pubdate="pubdate"' : '').
+			'>'.$time.'</time>';
+	}
+	else {
+		return '<span class="date">'.$time.'</span>';
+	}
 }
 
 sub formattime ($;$) {
@@ -1012,9 +1153,29 @@ sub formattime ($;$) {
 		$format=$config{timeformat};
 	}
 
+	return strftime_utf8($format, localtime($time));
+}
+
+my $strftime_encoding;
+sub strftime_utf8 {
 	# strftime doesn't know about encodings, so make sure
-	# its output is properly treated as utf8
-	return decode_utf8(POSIX::strftime($format, localtime($time)));
+	# its output is properly treated as utf8.
+	# Note that this does not handle utf-8 in the format string.
+	($strftime_encoding) = POSIX::setlocale(&POSIX::LC_TIME) =~ m#\.([^@]+)#
+		unless defined $strftime_encoding;
+	$strftime_encoding
+		? Encode::decode($strftime_encoding, POSIX::strftime(@_))
+		: POSIX::strftime(@_);
+}
+
+sub date_3339 ($) {
+	my $time=shift;
+
+	my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
+	POSIX::setlocale(&POSIX::LC_TIME, "C");
+	my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
+	POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
+	return $ret;
 }
 
 sub beautify_urlpath ($) {
@@ -1033,13 +1194,13 @@ sub beautify_urlpath ($) {
 	return $url;
 }
 
-sub urlto ($$;$) {
+sub urlto ($;$$) {
 	my $to=shift;
 	my $from=shift;
 	my $absolute=shift;
 	
 	if (! length $to) {
-		return beautify_urlpath(baseurl($from)."index.$config{htmlext}");
+		$to = 'index';
 	}
 
 	if (! $destsources{$to}) {
@@ -1050,11 +1211,26 @@ sub urlto ($$;$) {
 		return $config{url}.beautify_urlpath("/".$to);
 	}
 
+	if (! defined $from) {
+		my $u = $local_url || '';
+		$u =~ s{/$}{};
+		return $u.beautify_urlpath("/".$to);
+	}
+
 	my $link = abs2rel($to, dirname(htmlpage($from)));
 
 	return beautify_urlpath($link);
 }
 
+sub isselflink ($$) {
+	# Plugins can override this function to support special types
+	# of selflinks.
+	my $page=shift;
+	my $link=shift;
+
+	return $page eq $link;
+}
+
 sub htmllink ($$$;@) {
 	my $lpage=shift; # the page doing the linking
 	my $page=shift; # the page that will contain the link (different for inline)
@@ -1080,21 +1256,23 @@ sub htmllink ($$$;@) {
 	}
 	
 	return "<span class=\"selflink\">$linktext</span>"
-		if length $bestlink && $page eq $bestlink &&
+		if length $bestlink && isselflink($page, $bestlink) &&
 		   ! defined $opts{anchor};
 	
 	if (! $destsources{$bestlink}) {
 		$bestlink=htmlpage($bestlink);
 
 		if (! $destsources{$bestlink}) {
-			return $linktext unless length $config{cgiurl};
-			return "<span class=\"createlink\"><a href=\"".
-				cgiurl(
-					do => "create",
-					page => lc($link),
-					from => $lpage
-				).
-				"\" rel=\"nofollow\">?</a>$linktext</span>"
+			my $cgilink = "";
+			if (length $config{cgiurl}) {
+				$cgilink = "<a href=\"".
+					cgiurl(
+						do => "create",
+						page => $link,
+						from => $lpage
+					)."\" rel=\"nofollow\">?</a>";
+			}
+			return "<span class=\"createlink\">$cgilink$linktext</span>"
 		}
 	}
 	
@@ -1127,7 +1305,7 @@ sub userpage ($) {
 sub openiduser ($) {
 	my $user=shift;
 
-	if ($user =~ m!^https?://! &&
+	if (defined $user && $user =~ m!^https?://! &&
 	    eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
 		my $display;
 
@@ -1165,7 +1343,7 @@ sub htmlize ($$$$) {
 	my $content=shift;
 	
 	my $oneline = $content !~ /\n/;
-
+	
 	if (exists $hooks{htmlize}{$type}) {
 		$content=$hooks{htmlize}{$type}{call}->(
 			page => $page,
@@ -1186,10 +1364,9 @@ sub htmlize ($$$$) {
 	
 	if ($oneline) {
 		# hack to get rid of enclosing junk added by markdown
-		# and other htmlizers
+		# and other htmlizers/sanitizers
 		$content=~s/^<p>//i;
-		$content=~s/<\/p>$//i;
-		chomp $content;
+		$content=~s/<\/p>\n*$//i;
 	}
 
 	return $content;
@@ -1246,10 +1423,15 @@ sub preprocess ($$$;$$) {
 				|
 					"([^"]*?)"	# 3: single-quoted value
 				|
-					(\S+)		# 4: unquoted value
+					'''(.*?)'''     # 4: triple-single-quote
+				|
+					<<([a-zA-Z]+)\n # 5: heredoc start
+					(.*?)\n\5	# 6: heredoc value
+				|
+					(\S+)		# 7: unquoted value
 				)
 				(?:\s+|$)		# delimiter to next param
-			}sgx) {
+			}msgx) {
 				my $key=$1;
 				my $val;
 				if (defined $2) {
@@ -1264,6 +1446,12 @@ sub preprocess ($$$;$$) {
 				elsif (defined $4) {
 					$val=$4;
 				}
+				elsif (defined $7) {
+					$val=$7;
+				}
+				elsif (defined $6) {
+					$val=$6;
+				}
 
 				if (defined $key) {
 					push @params, $key, $val;
@@ -1293,6 +1481,8 @@ sub preprocess ($$$;$$) {
 				if ($@) {
 					my $error=$@;
 					chomp $error;
+					eval q{use HTML::Entities};
+					$error = encode_entities($error);
 				 	$ret="[[!$command <span class=\"error\">".
 						gettext("Error").": $error"."</span>]]";
 				}
@@ -1332,6 +1522,11 @@ sub preprocess ($$$;$$) {
 						|
 						"[^"]*?"	# single-quoted value
 						|
+						'''.*?'''	# triple-single-quote
+						|
+						<<([a-zA-Z]+)\n # 5: heredoc start
+						(?:.*?)\n\5	# heredoc value
+						|
 						[^"\s\]]+	# unquoted value
 					)
 					\s*			# whitespace or end
@@ -1355,6 +1550,11 @@ sub preprocess ($$$;$$) {
 						|
 						"[^"]*?"	# single-quoted value
 						|
+						'''.*?'''       # triple-single-quote
+						|
+						<<([a-zA-Z]+)\n # 5: heredoc start
+						(?:.*?)\n\5	# heredoc value
+						|
 						[^"\s\]]+	# unquoted value
 					)
 					\s*			# whitespace or end
@@ -1382,10 +1582,6 @@ sub filter ($$$) {
 	return $content;
 }
 
-sub indexlink () {
-	return "<a href=\"$config{url}\">$config{wikiname}</a>";
-}
-
 sub check_canedit ($$$;$) {
 	my $page=shift;
 	my $q=shift;
@@ -1450,6 +1646,69 @@ sub check_content (@) {
 	return defined $ok ? $ok : 1;
 }
 
+sub check_canchange (@) {
+	my %params = @_;
+	my $cgi = $params{cgi};
+	my $session = $params{session};
+	my @changes = @{$params{changes}};
+
+	my %newfiles;
+	foreach my $change (@changes) {
+		# This untaint is safe because we check file_pruned and
+		# wiki_file_regexp.
+		my ($file)=$change->{file}=~/$config{wiki_file_regexp}/;
+		$file=possibly_foolish_untaint($file);
+		if (! defined $file || ! length $file ||
+		    file_pruned($file)) {
+			error(gettext("bad file name %s"), $file);
+		}
+
+		my $type=pagetype($file);
+		my $page=pagename($file) if defined $type;
+
+		if ($change->{action} eq 'add') {
+			$newfiles{$file}=1;
+		}
+
+		if ($change->{action} eq 'change' ||
+		    $change->{action} eq 'add') {
+			if (defined $page) {
+				check_canedit($page, $cgi, $session);
+				next;
+			}
+			else {
+				if (IkiWiki::Plugin::attachment->can("check_canattach")) {
+					IkiWiki::Plugin::attachment::check_canattach($session, $file, $change->{path});
+					check_canedit($file, $cgi, $session);
+					next;
+				}
+			}
+		}
+		elsif ($change->{action} eq 'remove') {
+			# check_canremove tests to see if the file is present
+			# on disk. This will fail when a single commit adds a
+			# file and then removes it again. Avoid the problem
+			# by not testing the removal in such pairs of changes.
+			# (The add is still tested, just to make sure that
+			# no data is added to the repo that a web edit
+			# could not add.)
+			next if $newfiles{$file};
+
+			if (IkiWiki::Plugin::remove->can("check_canremove")) {
+				IkiWiki::Plugin::remove::check_canremove(defined $page ? $page : $file, $cgi, $session);
+				check_canedit(defined $page ? $page : $file, $cgi, $session);
+				next;
+			}
+		}
+		else {
+			error "unknown action ".$change->{action};
+		}
+
+		error sprintf(gettext("you are not allowed to change %s"), $file);
+	}
+}
+
+
 my $wikilock;
 
 sub lockwiki () {
@@ -1504,7 +1763,7 @@ sub loadindex () {
 	if (! $config{rebuild}) {
 		%pagesources=%pagemtime=%oldlinks=%links=%depends=
 		%destsources=%renderedfiles=%pagecase=%pagestate=
-		%depends_simple=();
+		%depends_simple=%typedlinks=%oldtypedlinks=();
 	}
 	my $in;
 	if (! open ($in, "<", "$config{wikistatedir}/indexdb")) {
@@ -1513,6 +1772,7 @@ sub loadindex () {
 			open ($in, "<", "$config{wikistatedir}/indexdb") || return;
 		}
 		else {
+			$config{gettime}=1; # first build
 			return;
 		}
 	}
@@ -1526,6 +1786,12 @@ sub loadindex () {
 	if (exists $index->{version} && ! ref $index->{version}) {
 		$pages=$index->{page};
 		%wikistate=%{$index->{state}};
+		# Handle plugins that got disabled by loading a new setup.
+		if (exists $config{setupfile}) {
+			require IkiWiki::Setup;
+			IkiWiki::Setup::disabled_plugins(
+				grep { ! $loaded_plugins{$_} } keys %wikistate);
+		}
 	}
 	else {
 		$pages=$index;
@@ -1536,8 +1802,8 @@ sub loadindex () {
 		my $d=$pages->{$src};
 		my $page=pagename($src);
 		$pagectime{$page}=$d->{ctime};
+		$pagesources{$page}=$src;
 		if (! $config{rebuild}) {
-			$pagesources{$page}=$src;
 			$pagemtime{$page}=$d->{mtime};
 			$renderedfiles{$page}=$d->{dest};
 			if (exists $d->{links} && ref $d->{links}) {
@@ -1570,6 +1836,14 @@ sub loadindex () {
 			if (exists $d->{state}) {
 				$pagestate{$page}=$d->{state};
 			}
+			if (exists $d->{typedlinks}) {
+				$typedlinks{$page}=$d->{typedlinks};
+
+				while (my ($type, $links) = each %{$typedlinks{$page}}) {
+					next unless %$links;
+					$oldtypedlinks{$page}{$type} = {%$links};
+				}
+			}
 		}
 		$oldrenderedfiles{$page}=[@{$d->{dest}}];
 	}
@@ -1585,11 +1859,7 @@ sub loadindex () {
 sub saveindex () {
 	run_hooks(savestate => sub { shift->() });
 
-	my %hookids;
-	foreach my $type (keys %hooks) {
-		$hookids{$_}=1 foreach keys %{$hooks{$type}};
-	}
-	my @hookids=keys %hookids;
+	my @plugins=keys %loaded_plugins;
 
 	if (! -d $config{wikistatedir}) {
 		mkdir($config{wikistatedir});
@@ -1618,8 +1888,12 @@ sub saveindex () {
 			$index{page}{$src}{depends_simple} = $depends_simple{$page};
 		}
 
+		if (exists $typedlinks{$page} && %{$typedlinks{$page}}) {
+			$index{page}{$src}{typedlinks} = $typedlinks{$page};
+		}
+
 		if (exists $pagestate{$page}) {
-			foreach my $id (@hookids) {
+			foreach my $id (@plugins) {
 				foreach my $key (keys %{$pagestate{$page}{$id}}) {
 					$index{page}{$src}{state}{$id}{$key}=$pagestate{$page}{$id}{$key};
 				}
@@ -1628,7 +1902,8 @@ sub saveindex () {
 	}
 
 	$index{state}={};
-	foreach my $id (@hookids) {
+	foreach my $id (@plugins) {
+		$index{state}{$id}={}; # used to detect disabled plugins
 		foreach my $key (keys %{$wikistate{$id}}) {
 			$index{state}{$id}{$key}=$wikistate{$id}{$key};
 		}
@@ -1645,58 +1920,95 @@ sub saveindex () {
 }
 
 sub template_file ($) {
-	my $template=shift;
+	my $name=shift;
+	
+	my $tpage=($name =~ s/^\///) ? $name : "templates/$name";
+	my $template;
+	if ($name !~ /\.tmpl$/ && exists $pagesources{$tpage}) {
+		$template=srcfile($pagesources{$tpage}, 1);
+		$name.=".tmpl";
+	}
+	else {
+		$template=srcfile($tpage, 1);
+	}
 
-	foreach my $dir ($config{templatedir}, @{$config{templatedirs}},
-	                 "$installdir/share/ikiwiki/templates") {
-		return "$dir/$template" if -e "$dir/$template";
+	if (defined $template) {
+		return $template, $tpage, 1 if wantarray;
+		return $template;
 	}
+	else {
+		$name=~s:/::; # avoid path traversal
+		foreach my $dir ($config{templatedir},
+		                 "$installdir/share/ikiwiki/templates") {
+			if (-e "$dir/$name") {
+				$template="$dir/$name";
+				last;
+			}
+		}
+		if (defined $template) {	
+			return $template, $tpage if wantarray;
+			return $template;
+		}
+	}
+
 	return;
 }
 
-sub template_params (@) {
-	my $filename=template_file(shift);
-
+sub template_depends ($$;@) {
+	my $name=shift;
+	my $page=shift;
+	
+	my ($filename, $tpage, $untrusted)=template_file($name);
 	if (! defined $filename) {
-		return if wantarray;
-		return "";
+		error(sprintf(gettext("template %s not found"), $name))
 	}
 
-	my @ret=(
+	if (defined $page && defined $tpage) {
+		add_depends($page, $tpage);
+	}
+	
+	my @opts=(
 		filter => sub {
 			my $text_ref = shift;
 			${$text_ref} = decode_utf8(${$text_ref});
 		},
-		filename => $filename,
 		loop_context_vars => 1,
 		die_on_bad_params => 0,
-		@_
+		parent_global_vars => 1,
+		filename => $filename,
+		@_,
+		($untrusted ? (no_includes => 1) : ()),
 	);
-	return wantarray ? @ret : {@ret};
+	return @opts if wantarray;
+
+	require HTML::Template;
+	return HTML::Template->new(@opts);
 }
 
 sub template ($;@) {
-	require HTML::Template;
-	return HTML::Template->new(template_params(@_));
+	template_depends(shift, undef, @_);
 }
 
-sub misctemplate ($$;@) {
-	my $title=shift;
-	my $pagebody=shift;
-	
-	my $template=template("misc.tmpl");
-	$template->param(
-		title => $title,
-		indexlink => indexlink(),
-		wikiname => $config{wikiname},
-		pagebody => $pagebody,
-		baseurl => baseurl(),
-		@_,
-	);
-	run_hooks(pagetemplate => sub {
-		shift->(page => "", destpage => "", template => $template);
+sub templateactions ($$) {
+	my $template=shift;
+	my $page=shift;
+
+	my $have_actions=0;
+	my @actions;
+	run_hooks(pageactions => sub {
+		push @actions, map { { action => $_ } } 
+			grep { defined } shift->(page => $page);
 	});
-	return $template->output;
+	$template->param(actions => \@actions);
+
+	if ($config{cgiurl} && exists $hooks{auth}) {
+		$template->param(prefsurl => cgiurl(do => "prefs"));
+		$have_actions=1;
+	}
+
+	if ($have_actions || @actions) {
+		$template->param(have_actions => 1);
+	}
 }
 
 sub hook (@) {
@@ -1747,11 +2059,11 @@ sub rcs_prepedit ($) {
 	$hooks{rcs}{rcs_prepedit}{call}->(@_);
 }
 
-sub rcs_commit ($$$;$$) {
+sub rcs_commit (@) {
 	$hooks{rcs}{rcs_commit}{call}->(@_);
 }
 
-sub rcs_commit_staged ($$$) {
+sub rcs_commit_staged (@) {
 	$hooks{rcs}{rcs_commit_staged}{call}->(@_);
 }
 
@@ -1771,7 +2083,7 @@ sub rcs_recentchanges ($) {
 	$hooks{rcs}{rcs_recentchanges}{call}->(@_);
 }
 
-sub rcs_diff ($) {
+sub rcs_diff ($;$) {
 	$hooks{rcs}{rcs_diff}{call}->(@_);
 }
 
@@ -1779,6 +2091,10 @@ sub rcs_getctime ($) {
 	$hooks{rcs}{rcs_getctime}{call}->(@_);
 }
 
+sub rcs_getmtime ($) {
+	$hooks{rcs}{rcs_getmtime}{call}->(@_);
+}
+
 sub rcs_receive () {
 	$hooks{rcs}{rcs_receive}{call}->();
 }
@@ -1797,14 +2113,16 @@ sub add_depends ($$;$) {
 
 	# Add explicit dependencies for influences.
 	my $sub=pagespec_translate($pagespec);
-	return if $@;
+	return unless defined $sub;
 	foreach my $p (keys %pagesources) {
 		my $r=$sub->($p, location => $page);
 		my $i=$r->influences;
+		my $static=$r->influences_static;
 		foreach my $k (keys %$i) {
+			next unless $r || $static || $k eq $page;
 			$depends_simple{$page}{lc $k} |= $i->{$k};
 		}
-		last if $r->influences_static;
+		last if $static;
 	}
 
 	$depends{$page}{$pagespec} |= $deptype;
@@ -1828,15 +2146,8 @@ sub deptype (@) {
 }
 
 my $file_prune_regexp;
-sub file_pruned ($;$) {
+sub file_pruned ($) {
 	my $file=shift;
-	if (@_) {
-		require File::Spec;
-		$file=File::Spec->canonpath($file);
-		my $base=File::Spec->canonpath(shift);
-		return if $file eq $base;
-		$file =~ s#^\Q$base\E/+##;
-	}
 
 	if (defined $config{include} && length $config{include}) {
 		return 0 if $file =~ m/$config{include}/;
@@ -1875,7 +2186,7 @@ sub define_gettext () {
 			return shift;
 		}
 	};
-  	*ngettext=sub {
+	*ngettext=sub {
 		$getobj->() if $getobj;
 		if ($gettext_obj) {
 			$gettext_obj->nget(@_);
@@ -1927,16 +2238,31 @@ sub inject {
 	use warnings;
 }
 
-sub add_link ($$) {
+sub add_link ($$;$) {
 	my $page=shift;
 	my $link=shift;
+	my $type=shift;
 
 	push @{$links{$page}}, $link
 		unless grep { $_ eq $link } @{$links{$page}};
+
+	if (defined $type) {
+		$typedlinks{$page}{$type}{$link} = 1;
+	}
+}
+
+sub add_autofile ($$$) {
+	my $file=shift;
+	my $plugin=shift;
+	my $generator=shift;
+	
+	$autofiles{$file}{plugin}=$plugin;
+	$autofiles{$file}{generator}=$generator;
 }
 
-sub cmpspec_translate ($) {
+sub sortspec_translate ($$) {
 	my $spec = shift;
+	my $reverse = shift;
 
 	my $code = "";
 	my @data;
@@ -1972,17 +2298,13 @@ sub cmpspec_translate ($) {
 			$code .= "-";
 		}
 
-		if (exists $IkiWiki::PageSpec::{"cmp_$word"}) {
-			if (exists $IkiWiki::PageSpec::{"check_cmp_$word"}) {
-				$IkiWiki::PageSpec::{"check_cmp_$word"}->($params);
-			}
-
+		if (exists $IkiWiki::SortSpec::{"cmp_$word"}) {
 			if (defined $params) {
 				push @data, $params;
-				$code .= "IkiWiki::PageSpec::cmp_$word(\@_, \$data[$#data])";
+				$code .= "IkiWiki::SortSpec::cmp_$word(\$data[$#data])";
 			}
 			else {
-				$code .= "IkiWiki::PageSpec::cmp_$word(\@_, undef)";
+				$code .= "IkiWiki::SortSpec::cmp_$word(undef)";
 			}
 		}
 		else {
@@ -1995,6 +2317,10 @@ sub cmpspec_translate ($) {
 		return sub { 0 };
 	}
 
+	if ($reverse) {
+		$code="-($code)";
+	}
+
 	no warnings;
 	return eval 'sub { '.$code.' }';
 }
@@ -2066,7 +2392,7 @@ sub pagespec_match ($$;@) {
 
 	my $sub=pagespec_translate($spec);
 	return IkiWiki::ErrorReason->new("syntax error in pagespec \"$spec\"")
-		if $@ || ! defined $sub;
+		if ! defined $sub;
 	return $sub->($page, @params);
 }
 
@@ -2084,7 +2410,9 @@ sub pagespec_match_list ($$;@) {
 
 	my $sub=pagespec_translate($pagespec);
 	error "syntax error in pagespec \"$pagespec\""
-		if $@ || ! defined $sub;
+		if ! defined $sub;
+	my $sort=sortspec_translate($params{sort}, $params{reverse})
+		if defined $params{sort};
 
 	my @candidates;
 	if (exists $params{list}) {
@@ -2097,21 +2425,19 @@ sub pagespec_match_list ($$;@) {
 			? grep { ! $params{filter}->($_) } keys %pagesources
 			: keys %pagesources;
 	}
-
-	if (defined $params{sort}) {
-		my $f = cmpspec_translate($params{sort});
-
-		@candidates = sort { $f->($a, $b) } @candidates;
-	}
-
-	@candidates=reverse(@candidates) if $params{reverse};
-	
-	$depends{$page}{$pagespec} |= ($params{deptype} || $DEPEND_CONTENT);
 	
 	# clear params, remainder is passed to pagespec
+	$depends{$page}{$pagespec} |= ($params{deptype} || $DEPEND_CONTENT);
 	my $num=$params{num};
 	delete @params{qw{num deptype reverse sort filter list}};
 	
+	# when only the top matches will be returned, it's efficient to
+	# sort before matching to pagespec,
+	if (defined $num && defined $sort) {
+		@candidates=IkiWiki::SortSpec::sort_pages(
+			$sort, @candidates);
+	}
+	
 	my @matches;
 	my $firstfail;
 	my $count=0;
@@ -2120,6 +2446,9 @@ sub pagespec_match_list ($$;@) {
 		my $r=$sub->($p, %params, location => $page);
 		error(sprintf(gettext("cannot match pages: %s"), $r))
 			if $r->isa("IkiWiki::ErrorReason");
+		unless ($r || $r->influences_static) {
+			$r->remove_influence($p);
+		}
 		$accum |= $r;
 		if ($r) {
 			push @matches, $p;
@@ -2133,21 +2462,28 @@ sub pagespec_match_list ($$;@) {
 		$depends_simple{$page}{lc $k} |= $i->{$k};
 	}
 
-	return @matches;
+	# when all matches will be returned, it's efficient to
+	# sort after matching
+	if (! defined $num && defined $sort) {
+		return IkiWiki::SortSpec::sort_pages(
+			$sort, @matches);
+	}
+	else {
+		return @matches;
+	}
 }
 
 sub pagespec_valid ($) {
 	my $spec=shift;
 
-	my $sub=pagespec_translate($spec);
-	return ! $@;
+	return defined pagespec_translate($spec);
 }
 
 sub glob2re ($) {
 	my $re=quotemeta(shift);
 	$re=~s/\\\*/.*/g;
 	$re=~s/\\\?/./g;
-	return $re;
+	return qr/^$re$/i;
 }
 
 package IkiWiki::FailReason;
@@ -2198,7 +2534,7 @@ sub merge_influences {
 	my $anded=shift;
 
 	if (! $anded || (($this || %{$this->[1]}) &&
-	                ($other || %{$other->[1]}))) {
+	                 ($other || %{$other->[1]}))) {
 		foreach my $influence (keys %{$other->[1]}) {
 			$this->[1]{$influence} |= $other->[1]{$influence};
 		}
@@ -2209,6 +2545,13 @@ sub merge_influences {
 	}
 }
 
+sub remove_influence {
+	my $this=shift;
+	my $torm=shift;
+
+	delete $this->[1]{$torm};
+}
+
 package IkiWiki::ErrorReason;
 
 our @ISA = 'IkiWiki::FailReason';
@@ -2219,15 +2562,23 @@ sub derel ($$) {
 	my $path=shift;
 	my $from=shift;
 
-	if ($path =~ m!^\./!) {
-		$from=~s#/?[^/]+$## if defined $from;
-		$path=~s#^\./##;
-		$path="$from/$path" if length $from;
+	if ($path =~ m!^\.(/|$)!) {
+		if ($1) {
+			$from=~s#/?[^/]+$## if defined $from;
+			$path=~s#^\./##;
+			$path="$from/$path" if defined $from && length $from;
+		}
+		else {
+			$path = $from;
+			$path = "" unless defined $path;
+		}
 	}
 
 	return $path;
 }
 
+my %glob_cache;
+
 sub match_glob ($$;@) {
 	my $page=shift;
 	my $glob=shift;
@@ -2235,8 +2586,13 @@ sub match_glob ($$;@) {
 	
 	$glob=derel($glob, $params{location});
 
-	my $regexp=IkiWiki::glob2re($glob);
-	if ($page=~/^$regexp$/i) {
+	# Instead of converting the glob to a regex every time,
+	# cache the compiled regex to save time.
+	my $re=$glob_cache{$glob};
+	unless (defined $re) {
+		$glob_cache{$glob} = $re = IkiWiki::glob2re($glob);
+	}
+	if ($page =~ $re) {
 		if (! IkiWiki::isinternal($page) || $params{internal}) {
 			return IkiWiki::SuccessReason->new("$glob matches $page");
 		}
@@ -2250,7 +2606,22 @@ sub match_glob ($$;@) {
 }
 
 sub match_internal ($$;@) {
-	return match_glob($_[0], $_[1], @_, internal => 1)
+	return match_glob(shift, shift, @_, internal => 1)
+}
+
+sub match_page ($$;@) {
+	my $page=shift;
+	my $match=match_glob($page, shift, @_);
+	if ($match) {
+		my $source=exists $IkiWiki::pagesources{$page} ?
+			$IkiWiki::pagesources{$page} :
+			$IkiWiki::delpagesources{$page};
+		my $type=defined $source ? IkiWiki::pagetype($source) : undef;
+		if (! defined $type) {	
+			return IkiWiki::FailReason->new("$page is not a page");
+		}
+	}
+	return $match;
 }
 
 sub match_link ($$;@) {
@@ -2260,31 +2631,47 @@ sub match_link ($$;@) {
 
 	$link=derel($link, $params{location});
 	my $from=exists $params{location} ? $params{location} : '';
+	my $linktype=$params{linktype};
+	my $qualifier='';
+	if (defined $linktype) {
+		$qualifier=" with type $linktype";
+	}
 
 	my $links = $IkiWiki::links{$page};
-	return IkiWiki::FailReason->new("$page has no links", "" => 1)
+	return IkiWiki::FailReason->new("$page has no links", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
 		unless $links && @{$links};
 	my $bestlink = IkiWiki::bestlink($from, $link);
 	foreach my $p (@{$links}) {
+		next unless (! defined $linktype || exists $IkiWiki::typedlinks{$page}{$linktype}{$p});
+
 		if (length $bestlink) {
-			return IkiWiki::SuccessReason->new("$page links to $link", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
-				if $bestlink eq IkiWiki::bestlink($page, $p);
+			if ($bestlink eq IkiWiki::bestlink($page, $p)) {
+				return IkiWiki::SuccessReason->new("$page links to $link$qualifier", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
+			}
 		}
 		else {
-			return IkiWiki::SuccessReason->new("$page links to page $p matching $link", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
-				if match_glob($p, $link, %params);
+			if (match_glob($p, $link, %params)) {
+				return IkiWiki::SuccessReason->new("$page links to page $p$qualifier, matching $link", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
+			}
 			my ($p_rel)=$p=~/^\/?(.*)/;
 			$link=~s/^\///;
-			return IkiWiki::SuccessReason->new("$page links to page $p_rel matching $link", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
-				if match_glob($p_rel, $link, %params);
+			if (match_glob($p_rel, $link, %params)) {
+				return IkiWiki::SuccessReason->new("$page links to page $p_rel$qualifier, matching $link", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
+			}
 		}
 	}
-	return IkiWiki::FailReason->new("$page does not link to $link", "" => 1);
+	return IkiWiki::FailReason->new("$page does not link to $link$qualifier", $page => $IkiWiki::DEPEND_LINKS, "" => 1);
 }
 
 sub match_backlink ($$;@) {
-	my $ret=match_link($_[1], $_[0], @_);
-	$ret->influences($_[1] => $IkiWiki::DEPEND_LINKS);
+	my $page=shift;
+	my $testpage=shift;
+	my %params=@_;
+	if ($testpage eq '.') {
+		$testpage = $params{'location'}
+	}
+	my $ret=match_link($testpage, $page, @_);
+	$ret->influences($testpage => $IkiWiki::DEPEND_LINKS);
 	return $ret;
 }
 
@@ -2329,7 +2716,12 @@ sub match_created_after ($$;@) {
 }
 
 sub match_creation_day ($$;@) {
-	if ((gmtime($IkiWiki::pagectime{shift()}))[3] == shift) {
+	my $page=shift;
+	my $d=shift;
+	if ($d !~ /^\d+$/) {
+		return IkiWiki::ErrorReason->new("invalid day $d");
+	}
+	if ((localtime($IkiWiki::pagectime{$page}))[3] == $d) {
 		return IkiWiki::SuccessReason->new('creation_day matched');
 	}
 	else {
@@ -2338,7 +2730,12 @@ sub match_creation_day ($$;@) {
 }
 
 sub match_creation_month ($$;@) {
-	if ((gmtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
+	my $page=shift;
+	my $m=shift;
+	if ($m !~ /^\d+$/) {
+		return IkiWiki::ErrorReason->new("invalid month $m");
+	}
+	if ((localtime($IkiWiki::pagectime{$page}))[4] + 1 == $m) {
 		return IkiWiki::SuccessReason->new('creation_month matched');
 	}
 	else {
@@ -2347,7 +2744,12 @@ sub match_creation_month ($$;@) {
 }
 
 sub match_creation_year ($$;@) {
-	if ((gmtime($IkiWiki::pagectime{shift()}))[5] + 1900 == shift) {
+	my $page=shift;
+	my $y=shift;
+	if ($y !~ /^\d+$/) {
+		return IkiWiki::ErrorReason->new("invalid year $y");
+	}
+	if ((localtime($IkiWiki::pagectime{$page}))[5] + 1900 == $y) {
 		return IkiWiki::SuccessReason->new('creation_year matched');
 	}
 	else {
@@ -2366,7 +2768,7 @@ sub match_user ($$;@) {
 		return IkiWiki::ErrorReason->new("no user specified");
 	}
 
-	if (defined $params{user} && $params{user}=~/^$regexp$/i) {
+	if (defined $params{user} && $params{user}=~$regexp) {
 		return IkiWiki::SuccessReason->new("user is $user");
 	}
 	elsif (! defined $params{user}) {
@@ -2414,24 +2816,23 @@ sub match_ip ($$;@) {
 	}
 }
 
+package IkiWiki::SortSpec;
+
+# This is in the SortSpec namespace so that the $a and $b that sort() uses
+# are easily available in this namespace, for cmp functions to use them.
+sub sort_pages {
+	my $f=shift;
+	sort $f @_
+}
+
 sub cmp_title {
-	IkiWiki::pagetitle(IkiWiki::basename($_[0]))
+	IkiWiki::pagetitle(IkiWiki::basename($a))
 	cmp
-	IkiWiki::pagetitle(IkiWiki::basename($_[1]))
+	IkiWiki::pagetitle(IkiWiki::basename($b))
 }
 
-sub cmp_mtime { $IkiWiki::pagemtime{$_[1]} <=> $IkiWiki::pagemtime{$_[0]} }
-sub cmp_age { $IkiWiki::pagectime{$_[1]} <=> $IkiWiki::pagectime{$_[0]} }
-
-sub check_cmp_title_natural {
-	eval q{use Sort::Naturally};
-	if ($@) {
-		error(gettext("Sort::Naturally needed for title_natural sort"));
-	}
-}
-sub cmp_title_natural {
-	Sort::Naturally::ncmp(IkiWiki::pagetitle(IkiWiki::basename($_[0])),
-		IkiWiki::pagetitle(IkiWiki::basename($_[1])))
-}
+sub cmp_path { IkiWiki::pagetitle($a) cmp IkiWiki::pagetitle($b) }
+sub cmp_mtime { $IkiWiki::pagemtime{$b} <=> $IkiWiki::pagemtime{$a} }
+sub cmp_age { $IkiWiki::pagectime{$b} <=> $IkiWiki::pagectime{$a} }
 
 1