Compute local paths to the top of the wiki

"local" here is short for "locally valid" - the idea is that we can use
URLs that are relative in the sense of only having the path part, but
absolute in the sense that they start from '/', such as
'/~smcv/ikiwiki.cgi'. There's no particularly good name that I can find
for these between-relative-and-absolute URLs.

They're useful because in the common case where the pages and the CGI
script have the same scheme and authority component, each page is
identified by the same locally-valid URL when linking from any page or
from the CGI, without hard-coding a choice between HTTP and HTTPS, or
between multiple virtual hostnames with the same path layout. As such,
we can use them in many situations that previously used an absolute URL.

If there's no suitable semi-absolute value for local_url (for instance,
if your pages and your CGI reside on different servers), we can just fall
back to using the absolute URL. I append '/' because $config{url} doesn't
end with '/', but the common case for local_url (on all branchable.com
sites, for instance) is that it's just '/'.
master
Simon McVittie 2010-11-22 23:13:52 +00:00
parent 1968317cac
commit 8f64c69e08
1 changed files with 30 additions and 1 deletions

View File

@ -501,6 +501,12 @@ sub defaultconfig () {
return @ret; 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 () { sub checkconfig () {
# locale stuff; avoid LC_ALL since it overrides everything # locale stuff; avoid LC_ALL since it overrides everything
if (defined $ENV{LC_ALL}) { if (defined $ENV{LC_ALL}) {
@ -538,6 +544,29 @@ sub checkconfig () {
error(gettext("Must specify url to wiki with --url when using --cgi")); error(gettext("Must specify url to wiki with --url when using --cgi"));
} }
if (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{//$}{/};
}
$config{wikistatedir}="$config{srcdir}/.ikiwiki" $config{wikistatedir}="$config{srcdir}/.ikiwiki"
unless exists $config{wikistatedir} && defined $config{wikistatedir}; unless exists $config{wikistatedir} && defined $config{wikistatedir};