Merge pull request #1433 from gollum/redirects
Add redirect support (e.g., after renames). Fixes #1023.
This commit is contained in:
+16
-5
@@ -89,6 +89,7 @@ module Precious
|
|||||||
settings.wiki_options[:allow_editing] = settings.wiki_options.fetch(:allow_editing, true)
|
settings.wiki_options[:allow_editing] = settings.wiki_options.fetch(:allow_editing, true)
|
||||||
@allow_editing = settings.wiki_options[:allow_editing]
|
@allow_editing = settings.wiki_options[:allow_editing]
|
||||||
@critic_markup = settings.wiki_options[:critic_markup]
|
@critic_markup = settings.wiki_options[:critic_markup]
|
||||||
|
@redirects_enabled = settings.wiki_options.fetch(:redirects_enabled, true)
|
||||||
@per_page_uploads = settings.wiki_options[:per_page_uploads]
|
@per_page_uploads = settings.wiki_options[:per_page_uploads]
|
||||||
|
|
||||||
forbid unless @allow_editing || request.request_method == "GET"
|
forbid unless @allow_editing || request.request_method == "GET"
|
||||||
@@ -158,15 +159,15 @@ module Precious
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
get %r{/(edit|create)/(custom|mathjax\.config)\.(js|css)} do
|
get %r{/(edit|create)/(\.redirects.gollum|(custom|mathjax\.config)\.(js|css))} do
|
||||||
forbid('Changing this resource is not allowed.')
|
forbid('Changing this resource is not allowed.')
|
||||||
end
|
end
|
||||||
|
|
||||||
post %r{/(delete|rename|edit|create)/(custom|mathjax\.config)\.(js|css)} do
|
post %r{/(delete|rename|edit|create)/(\.redirects.gollum|(custom|mathjax\.config)\.(js|css))} do
|
||||||
forbid('Changing this resource is not allowed.')
|
forbid('Changing this resource is not allowed.')
|
||||||
end
|
end
|
||||||
|
|
||||||
post %r{/revert/(custom|mathjax\.config\.)\.(js|css)/.*/.*} do
|
post %r{/revert/(\.redirects.gollum|(custom|mathjax\.config\.)\.(js|css)/.*/.*)} do
|
||||||
forbid('Changing this resource is not allowed.')
|
forbid('Changing this resource is not allowed.')
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -276,7 +277,11 @@ module Precious
|
|||||||
committer.commit
|
committer.commit
|
||||||
|
|
||||||
# Renaming preserves format, so add the page's format to the renamed path to retrieve the renamed page
|
# Renaming preserves format, so add the page's format to the renamed path to retrieve the renamed page
|
||||||
page = wiki_page("#{rename}.#{Gollum::Page.format_to_ext(page.format)}").page
|
new_path = "#{rename}.#{Gollum::Page.format_to_ext(page.format)}"
|
||||||
|
# Add a redirect from the old page to the new
|
||||||
|
wiki.add_redirect(page.url_path, clean_url(new_path)) if @redirects_enabled
|
||||||
|
|
||||||
|
page = wiki_page(new_path).page
|
||||||
return if page.nil?
|
return if page.nil?
|
||||||
redirect to("/#{page.escaped_url_path}")
|
redirect to("/#{page.escaped_url_path}")
|
||||||
end
|
end
|
||||||
@@ -496,7 +501,7 @@ module Precious
|
|||||||
@newable = true
|
@newable = true
|
||||||
mustache :overview
|
mustache :overview
|
||||||
end
|
end
|
||||||
end
|
end # gollum namespace
|
||||||
|
|
||||||
get %r{/(.+?)/([0-9a-f]{40})} do
|
get %r{/(.+?)/([0-9a-f]{40})} do
|
||||||
file_path = params[:captures][0]
|
file_path = params[:captures][0]
|
||||||
@@ -520,6 +525,10 @@ module Precious
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
get '/\.redirects\.gollum' do
|
||||||
|
forbid('Accessing this resource is not allowed.')
|
||||||
|
end
|
||||||
|
|
||||||
get '/*' do
|
get '/*' do
|
||||||
show_page_or_file(params[:splat].first)
|
show_page_or_file(params[:splat].first)
|
||||||
end
|
end
|
||||||
@@ -545,6 +554,8 @@ module Precious
|
|||||||
mustache :page
|
mustache :page
|
||||||
elsif file = wiki.file(fullpath, wiki.ref, true)
|
elsif file = wiki.file(fullpath, wiki.ref, true)
|
||||||
show_file(file)
|
show_file(file)
|
||||||
|
elsif @redirects_enabled && redirect_path = wiki.redirects[fullpath]
|
||||||
|
redirect to("#{encodeURIComponent(redirect_path)}?redirected_from=#{encodeURIComponent(fullpath)}")
|
||||||
else
|
else
|
||||||
if @allow_editing
|
if @allow_editing
|
||||||
path = fullpath[-1] == '/' ? "#{fullpath}#{wiki.index_page}" : fullpath # Append default index page if no page name is supplied
|
path = fullpath[-1] == '/' ? "#{fullpath}#{wiki.index_page}" : fullpath # Append default index page if no page name is supplied
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ module Precious
|
|||||||
|
|
||||||
# Remove all slashes from the start of string.
|
# Remove all slashes from the start of string.
|
||||||
# Remove all double slashes
|
# Remove all double slashes
|
||||||
def clean_url url
|
def clean_url(url)
|
||||||
return url if url.nil?
|
return url if url.nil?
|
||||||
url.gsub('%2F', '/').gsub(/^\/+/, '').gsub('//', '/')
|
url.gsub('%2F', '/').gsub(/^\/+/, '').gsub('//', '/')
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -102,6 +102,12 @@ function preparePage () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function flashNotice(type, notice) {
|
||||||
|
// accepted types: info, success, warn, error
|
||||||
|
html = '<p><div class="flash flash-' + type +'"><button class="flash-close js-flash-close" type="button" onclick="parentNode.remove()"><%=rocticon('x')%></button>' + notice + '</div></p>';
|
||||||
|
$('#wiki-content h1').before(html);
|
||||||
|
}
|
||||||
|
|
||||||
// ua
|
// ua
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
// for deleting the current page
|
// for deleting the current page
|
||||||
@@ -148,6 +154,7 @@ $(document).ready(function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if ($('#minibutton-upload-page').length) {
|
if ($('#minibutton-upload-page').length) {
|
||||||
new ClipboardJS('#ClipboardJSlink');
|
new ClipboardJS('#ClipboardJSlink');
|
||||||
$('#minibutton-upload-page').parent().removeClass('jaws');
|
$('#minibutton-upload-page').parent().removeClass('jaws');
|
||||||
@@ -600,7 +607,11 @@ $(document).ready(function() {
|
|||||||
if($('.markdown-body').length ){
|
if($('.markdown-body').length ){
|
||||||
// Set text direction (LTR or RTL)
|
// Set text direction (LTR or RTL)
|
||||||
preparePage();
|
preparePage();
|
||||||
|
// Check if there was a redirect here
|
||||||
|
if (match = new RegExp(/[?&]redirected\_from=([^?]*)/).exec(window.location.href)) {
|
||||||
|
notice = "The page you requested was renamed or moved. You've been successfully redirected to its new location.";
|
||||||
|
flashNotice('success', notice);
|
||||||
|
}
|
||||||
// Set the 'e' hotkey for editing pages.
|
// Set the 'e' hotkey for editing pages.
|
||||||
Mousetrap.bind(['e'], function( e ) {
|
Mousetrap.bind(['e'], function( e ) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|||||||
@@ -25,6 +25,17 @@ context "Precious::Views::Editing" do
|
|||||||
assert page.nil?
|
assert page.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test ".redirects.gollum file should not be accessible" do
|
||||||
|
Precious::App.set(:wiki_options, { allow_editing: true, allow_uploads: true })
|
||||||
|
get '/.redirects.gollum'
|
||||||
|
assert_match /Accessing this resource is not allowed/, last_response.body
|
||||||
|
end
|
||||||
|
|
||||||
|
test ".redirects.gollum file should not be editable" do
|
||||||
|
Precious::App.set(:wiki_options, { allow_editing: true, allow_uploads: true })
|
||||||
|
get '/gollum/edit/.redirects.gollum'
|
||||||
|
assert_match /Changing this resource is not allowed/, last_response.body
|
||||||
|
end
|
||||||
|
|
||||||
test "frontend links for editing are not blocked" do
|
test "frontend links for editing are not blocked" do
|
||||||
Precious::App.set(:wiki_options, { allow_editing: true, allow_uploads: true })
|
Precious::App.set(:wiki_options, { allow_editing: true, allow_uploads: true })
|
||||||
|
|||||||
+3
-3
@@ -169,7 +169,7 @@ context "Frontend" do
|
|||||||
assert_nil @wiki.page("B")
|
assert_nil @wiki.page("B")
|
||||||
page_2 = @wiki.page('C')
|
page_2 = @wiki.page('C')
|
||||||
assert_equal "INITIAL\n\nSPAM2\n", page_2.raw_data
|
assert_equal "INITIAL\n\nSPAM2\n", page_2.raw_data
|
||||||
assert_equal 'def', page_2.version.message
|
assert_equal 'def', page_2.last_version.message
|
||||||
assert_not_equal page_1.version.sha, page_2.version.sha
|
assert_not_equal page_1.version.sha, page_2.version.sha
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -216,7 +216,7 @@ context "Frontend" do
|
|||||||
assert_nil @wiki.page("G/H")
|
assert_nil @wiki.page("G/H")
|
||||||
page_2 = @wiki.page('I/C')
|
page_2 = @wiki.page('I/C')
|
||||||
assert_equal "INITIAL\n\nSPAM2\n", page_2.raw_data
|
assert_equal "INITIAL\n\nSPAM2\n", page_2.raw_data
|
||||||
assert_equal 'def', page_2.version.message
|
assert_equal 'def', page_2.last_version.message
|
||||||
assert_not_equal page_1.version.sha, page_2.version.sha
|
assert_not_equal page_1.version.sha, page_2.version.sha
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -233,7 +233,7 @@ context "Frontend" do
|
|||||||
assert_nil @wiki.page("G/H")
|
assert_nil @wiki.page("G/H")
|
||||||
page_2 = @wiki.page('G/K/C')
|
page_2 = @wiki.page('G/K/C')
|
||||||
assert_equal "INITIAL\n\nSPAM2\n", page_2.raw_data
|
assert_equal "INITIAL\n\nSPAM2\n", page_2.raw_data
|
||||||
assert_equal 'def', page_2.version.message
|
assert_equal 'def', page_2.last_version.message
|
||||||
assert_not_equal page_1.version.sha, page_2.version.sha
|
assert_not_equal page_1.version.sha, page_2.version.sha
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user