From 8f64c69e084c698a389565203bd18bccb33f5d69 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 22 Nov 2010 23:13:52 +0000 Subject: 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 '/'. --- IkiWiki.pm | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'IkiWiki.pm') diff --git a/IkiWiki.pm b/IkiWiki.pm index f57ef8c6c..1d37e7f8e 100644 --- a/IkiWiki.pm +++ b/IkiWiki.pm @@ -501,6 +501,12 @@ sub defaultconfig () { 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}) { @@ -537,7 +543,30 @@ sub checkconfig () { if ($config{cgi} && ! length $config{url}) { 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" unless exists $config{wikistatedir} && defined $config{wikistatedir}; -- cgit v1.2.3 From 200c599dcaeb4dde77564b84022b67bf71cd6bfa Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 22 Nov 2010 23:16:59 +0000 Subject: cgiurl: don't append "?" if there are no parameters This means we can use cgiurl() instead of $config{cgiurl} if an absolute URL isn't desired. --- IkiWiki.pm | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'IkiWiki.pm') diff --git a/IkiWiki.pm b/IkiWiki.pm index 1d37e7f8e..989f3bee7 100644 --- a/IkiWiki.pm +++ b/IkiWiki.pm @@ -1044,6 +1044,11 @@ sub cgiurl (@) { $cgiurl=$params{cgiurl}; delete $params{cgiurl}; } + + unless (%params) { + return $cgiurl; + } + return $cgiurl."?". join("&", map $_."=".uri_escape_utf8($params{$_}), keys %params); } -- cgit v1.2.3 From 6be4e6d1a501fa10d7894ff46b4a812cddb26d49 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 22 Nov 2010 23:20:32 +0000 Subject: cgiurl(): return a locally-valid path by default To get an absolute version you can use cgiurl(cgiurl => $config{cgiurl}). The only place in IkiWiki that seems to actually need an absolute URL is the openid plugin, and that already uses the named parameter. --- IkiWiki.pm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'IkiWiki.pm') diff --git a/IkiWiki.pm b/IkiWiki.pm index 989f3bee7..41e9e3f82 100644 --- a/IkiWiki.pm +++ b/IkiWiki.pm @@ -1039,7 +1039,8 @@ sub linkpage ($) { sub cgiurl (@) { my %params=@_; - my $cgiurl=$config{cgiurl}; + my $cgiurl=$local_cgiurl; + if (exists $params{cgiurl}) { $cgiurl=$params{cgiurl}; delete $params{cgiurl}; -- cgit v1.2.3 From f032bce79158005032c09e4db75093abaca45751 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 22 Nov 2010 23:25:45 +0000 Subject: baseurl(): return local path, not absolute URL, if the argument is undef --- IkiWiki.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'IkiWiki.pm') diff --git a/IkiWiki.pm b/IkiWiki.pm index 41e9e3f82..a4afef8e0 100644 --- a/IkiWiki.pm +++ b/IkiWiki.pm @@ -1057,7 +1057,7 @@ sub cgiurl (@) { sub baseurl (;$) { my $page=shift; - return "$config{url}/" if ! defined $page; + return $local_url if ! defined $page; $page=htmlpage($page); $page=~s/[^\/]+$//; -- cgit v1.2.3 From 4c224ae143ebc4b13f824fe1782561a5cd165864 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 22 Nov 2010 23:33:13 +0000 Subject: urlto(): if $from is undef, return a local path, not an absolute URL --- IkiWiki.pm | 6 ++++++ doc/plugins/write.mdwn | 4 ++++ 2 files changed, 10 insertions(+) (limited to 'IkiWiki.pm') diff --git a/IkiWiki.pm b/IkiWiki.pm index a4afef8e0..ee0b1f1ea 100644 --- a/IkiWiki.pm +++ b/IkiWiki.pm @@ -1148,6 +1148,12 @@ 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); diff --git a/doc/plugins/write.mdwn b/doc/plugins/write.mdwn index 6b751f0cd..33db3e707 100644 --- a/doc/plugins/write.mdwn +++ b/doc/plugins/write.mdwn @@ -988,6 +988,10 @@ Construct a relative url to the first parameter from the page named by the second. The first parameter can be either a page name, or some other destination file, as registered by `will_render`. +If the second parameter is `undef`, the URL will be valid from any page on the +wiki, or from the CGI; if possible it'll be a path starting with `/`, but an +absolute URL will be used if the wiki and the CGI are on different servers. + If the third parameter is passed and is true, an absolute url will be constructed instead of the default relative url. -- cgit v1.2.3 From d01334b8e9c229895ecb6d0b0a7acda86515df71 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 23 Nov 2010 22:21:31 +0000 Subject: If cgiurl is set, but url isn't, use an absolute $local_cgiurl --- IkiWiki.pm | 3 +++ 1 file changed, 3 insertions(+) (limited to 'IkiWiki.pm') diff --git a/IkiWiki.pm b/IkiWiki.pm index ee0b1f1ea..140f7f740 100644 --- a/IkiWiki.pm +++ b/IkiWiki.pm @@ -566,6 +566,9 @@ sub checkconfig () { $local_url =~ s{//$}{/}; } + else { + $local_cgiurl = $config{cgiurl}; + } $config{wikistatedir}="$config{srcdir}/.ikiwiki" unless exists $config{wikistatedir} && defined $config{wikistatedir}; -- cgit v1.2.3