From 51950f7fbf080923c083f0b3eeb666bdd2fdcb3a Mon Sep 17 00:00:00 2001 From: Alex Schroeder Date: Thu, 11 Aug 2016 17:14:14 +0200 Subject: [PATCH] namespaces.pl: more changes to get it working Encoding of namespace and page name under Mojolicious is not OK and I don't know why. I added some tests that try to at least prove that the workaround in the config file is OK. --- modules/namespaces.pl | 27 +++++++++++-------- t/mojolicious-namespaces.t | 54 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 11 deletions(-) diff --git a/modules/namespaces.pl b/modules/namespaces.pl index 12db6e02..8c3fee3b 100644 --- a/modules/namespaces.pl +++ b/modules/namespaces.pl @@ -80,6 +80,21 @@ $NamespaceSlashing = 0; # affects : decoding NamespaceRcLines # variables (eg. localnames.pl) unshift(@MyInitVariables, \&NamespacesInitVariables); +sub GetNamespace { + my $ns = GetParam('ns', ''); + if (not $ns and $UsePathInfo) { + my $path_info = decode_utf8($q->path_info()); + # make sure ordinary page names are not matched! + if ($path_info =~ m|^/($InterSitePattern)(/.*)?| + and ($2 or $q->keywords or NamespaceRequiredByParameter())) { + $ns = $1; + } + } + ReportError(Ts('%s is not a legal name for a namespace', $ns)) + if $ns and $ns !~ m/^($InterSitePattern)$/; + return $ns; +} + sub NamespacesInitVariables { %Namespaces = (); # Do this before changing the $DataDir and $ScriptName @@ -96,17 +111,7 @@ sub NamespacesInitVariables { } $NamespaceRoot = $ScriptName; # $ScriptName may be changed below $NamespaceCurrent = ''; - my $ns = GetParam('ns', ''); - if (not $ns and $UsePathInfo) { - my $path_info = decode_utf8($q->path_info()); - # make sure ordinary page names are not matched! - if ($path_info =~ m|^/($InterSitePattern)(/.*)?| - and ($2 or $q->keywords or NamespaceRequiredByParameter())) { - $ns = $1; - } - } - ReportError(Ts('%s is not a legal name for a namespace', $ns)) - if $ns and $ns !~ m/^($InterSitePattern)$/; + my $ns = GetNamespace(); if ($ns and $ns ne $NamespacesMain and $ns ne $NamespacesSelf) { diff --git a/t/mojolicious-namespaces.t b/t/mojolicious-namespaces.t index 79f55512..bc900e3e 100644 --- a/t/mojolicious-namespaces.t +++ b/t/mojolicious-namespaces.t @@ -16,11 +16,55 @@ package OddMuse; use Test::More; use Test::Mojo; +use utf8; # tests contain UTF-8 characters and it matters require 't/test.pl'; add_module('namespaces.pl'); +# Sadly, this appears to be required because path_info is already decoded! +AppendStringToFile($ConfigFile, <<'EOF'); +sub GetNamespace { + my $ns = GetParam('ns', ''); + if (not $ns and $UsePathInfo) { + my $path_info = $q->path_info(); + # make sure ordinary page names are not matched! + if ($path_info =~ m|^/($InterSitePattern)(/.*)?| + and ($2 or $q->keywords or NamespaceRequiredByParameter())) { + $ns = $1; + } + } + ReportError(Ts('%s is not a legal name for a namespace', $ns)) + if $ns and $ns !~ m/^($InterSitePattern)$/; + return $ns; +} + +*GetId = \&NamespacesNewGetId; + +sub NamespacesNewGetId { + my $id = UnquoteHtml(GetParam('id', GetParam('title', ''))); # id=x or title=x -> x + if (not $id and $q->keywords) { + $id = decode_utf8(join('_', $q->keywords)); # script?p+q -> p_q + } + if ($UsePathInfo and $q->path_info) { + my @path = split(/\//, $q->path_info); + $id ||= pop(@path); # script/p/q -> q + foreach my $p (@path) { + # https://campaignwiki.org/wiki/F%c3%bcnfWinde/G%c3%b6tter means that + # FünfWinde and Götter are both treated correctly. + SetParam($p, 1); # script/p/q -> p=1 + } + } + # http://example.org/cgi-bin/wiki.pl?action=browse;ns=Test;id=Test means NamespaceCurrent=Test and id=Test + # http://example.org/cgi-bin/wiki.pl/Test/Test means NamespaceCurrent=Test and id=Test + # In this case GetId() will have set the parameter Test to 1. + # http://example.org/cgi-bin/wiki.pl/Test?rollback-1234=foo + # This doesn't set the Test parameter. + return if $id and $UsePathInfo and $id eq $NamespaceCurrent and not GetParam($id) and not GetParam('ns'); + return $id; +} +EOF + start_mojolicious_server(); sleep(1); @@ -60,5 +104,15 @@ $t->post_ok("$ScriptName/F%C3%BCnfWinde" $t->get_ok("$ScriptName/F%C3%BCnfWinde/Some_Page") ->status_is(200) ->content_like(qr/Wir sind im Namensraum Fünf Winde/); +ok(IsDir("$DataDir/FünfWinde"), '$DataDir FünfWinde exists'); + +# Double trouble with Umlautes +$t->post_ok("$ScriptName/F%C3%BCnfWinde" + => form => {title => 'Zürich', + text => 'Wir sind immer noch im Namensraum Fünf Winde.'}) + ->status_is(302); +$t->get_ok("$ScriptName/F%C3%BCnfWinde/Z%c3%bcrich") + ->status_is(200) + ->content_like(qr/Wir sind immer noch im Namensraum Fünf Winde/); done_testing();