Do a redirect when accessing a renamed wiki page.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@5423 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2011-04-11 19:21:57 +00:00
parent 6db0e8dcef
commit 3cc7353093
4 changed files with 55 additions and 7 deletions

View File

@ -249,12 +249,21 @@ private
# Finds the requested page or a new page if it doesn't exist
def find_existing_or_new_page
@page = @wiki.find_or_new_page(params[:id])
if @wiki.page_found_with_redirect?
redirect_to params.update(:id => @page.title)
end
end
# Finds the requested page and returns a 404 error if it doesn't exist
def find_existing_page
@page = @wiki.find_page(params[:id])
render_404 if @page.nil?
if @page.nil?
render_404
return
end
if @wiki.page_found_with_redirect?
redirect_to params.update(:id => @page.title)
end
end
# Returns true if the current user is allowed to edit the page, otherwise false

View File

@ -44,17 +44,26 @@ class Wiki < ActiveRecord::Base
# find the page with the given title
def find_page(title, options = {})
@page_found_with_redirect = false
title = start_page if title.blank?
title = Wiki.titleize(title)
page = pages.first(:conditions => ["LOWER(title) = LOWER(?)", title])
if !page && !(options[:with_redirect] == false)
# search for a redirect
redirect = redirects.first(:conditions => ["LOWER(title) = LOWER(?)", title])
page = find_page(redirect.redirects_to, :with_redirect => false) if redirect
if redirect
page = find_page(redirect.redirects_to, :with_redirect => false)
@page_found_with_redirect = true
end
end
page
end
# Returns true if the last page was found with a redirect
def page_found_with_redirect?
@page_found_with_redirect
end
# Finds a page by title
# The given string can be of one of the forms: "title" or "project:title"
# Examples:

View File

@ -55,6 +55,13 @@ class WikiControllerTest < ActionController::TestCase
:alt => 'This is a logo' }
end
def test_show_redirected_page
WikiRedirect.create!(:wiki_id => 1, :title => 'Old_title', :redirects_to => 'Another_page')
get :show, :project_id => 'ecookbook', :id => 'Old_title'
assert_redirected_to '/projects/ecookbook/wiki/Another_page'
end
def test_show_with_sidebar
page = Project.find(1).wiki.pages.new(:title => 'Sidebar')
page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar')

View File

@ -1,7 +1,7 @@
# encoding: utf-8
#
# redMine - project management software
# Copyright (C) 2006-2007 Jean-Philippe Lang
# Redmine - project management software
# Copyright (C) 2006-2011 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
@ -39,21 +39,44 @@ class WikiTest < ActiveSupport::TestCase
assert_equal "Another start page", @wiki.start_page
end
def test_find_page
def test_find_page_should_not_be_case_sensitive
wiki = Wiki.find(1)
page = WikiPage.find(2)
assert_equal page, wiki.find_page('Another_page')
assert_equal page, wiki.find_page('Another page')
assert_equal page, wiki.find_page('ANOTHER page')
end
def test_find_page_with_cyrillic_characters
wiki = Wiki.find(1)
page = WikiPage.find(10)
assert_equal page, wiki.find_page('Этикаенеджмента')
end
def test_find_page_with_backslashes
wiki = Wiki.find(1)
page = WikiPage.generate!(:wiki => wiki, :title => '2009\\02\\09')
assert_equal page, wiki.find_page('2009\\02\\09')
end
def test_find_page_without_redirect
wiki = Wiki.find(1)
page = wiki.find_page('Another_page')
assert_not_nil page
assert_equal 'Another_page', page.title
assert_equal false, wiki.page_found_with_redirect?
end
def test_find_page_with_redirect
wiki = Wiki.find(1)
WikiRedirect.create!(:wiki => wiki, :title => 'Old_title', :redirects_to => 'Another_page')
page = wiki.find_page('Old_title')
assert_not_nil page
assert_equal 'Another_page', page.title
assert_equal true, wiki.page_found_with_redirect?
end
def test_titleize
assert_equal 'Page_title_with_CAPITALES', Wiki.titleize('page title with CAPITALES')
assert_equal 'テスト', Wiki.titleize('テスト')