##// END OF EJS Templates
Fixed: wiki page with backslash in title can not be found (#7589)....
Jean-Philippe Lang -
r4977:f7127e946686
parent child
Show More
@@ -1,85 +1,85
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2009 Jean-Philippe Lang
2 # Copyright (C) 2006-2009 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class Wiki < ActiveRecord::Base
18 class Wiki < ActiveRecord::Base
19 belongs_to :project
19 belongs_to :project
20 has_many :pages, :class_name => 'WikiPage', :dependent => :destroy, :order => 'title'
20 has_many :pages, :class_name => 'WikiPage', :dependent => :destroy, :order => 'title'
21 has_many :redirects, :class_name => 'WikiRedirect', :dependent => :delete_all
21 has_many :redirects, :class_name => 'WikiRedirect', :dependent => :delete_all
22
22
23 acts_as_watchable
23 acts_as_watchable
24
24
25 validates_presence_of :start_page
25 validates_presence_of :start_page
26 validates_format_of :start_page, :with => /^[^,\.\/\?\;\|\:]*$/
26 validates_format_of :start_page, :with => /^[^,\.\/\?\;\|\:]*$/
27
27
28 def visible?(user=User.current)
28 def visible?(user=User.current)
29 !user.nil? && user.allowed_to?(:view_wiki_pages, project)
29 !user.nil? && user.allowed_to?(:view_wiki_pages, project)
30 end
30 end
31
31
32 # Returns the wiki page that acts as the sidebar content
32 # Returns the wiki page that acts as the sidebar content
33 # or nil if no such page exists
33 # or nil if no such page exists
34 def sidebar
34 def sidebar
35 @sidebar ||= find_page('Sidebar', :with_redirect => false)
35 @sidebar ||= find_page('Sidebar', :with_redirect => false)
36 end
36 end
37
37
38 # find the page with the given title
38 # find the page with the given title
39 # if page doesn't exist, return a new page
39 # if page doesn't exist, return a new page
40 def find_or_new_page(title)
40 def find_or_new_page(title)
41 title = start_page if title.blank?
41 title = start_page if title.blank?
42 find_page(title) || WikiPage.new(:wiki => self, :title => Wiki.titleize(title))
42 find_page(title) || WikiPage.new(:wiki => self, :title => Wiki.titleize(title))
43 end
43 end
44
44
45 # find the page with the given title
45 # find the page with the given title
46 def find_page(title, options = {})
46 def find_page(title, options = {})
47 title = start_page if title.blank?
47 title = start_page if title.blank?
48 title = Wiki.titleize(title)
48 title = Wiki.titleize(title)
49 page = pages.first(:conditions => ["LOWER(title) LIKE LOWER(?)", title])
49 page = pages.first(:conditions => ["LOWER(title) = LOWER(?)", title])
50 if !page && !(options[:with_redirect] == false)
50 if !page && !(options[:with_redirect] == false)
51 # search for a redirect
51 # search for a redirect
52 redirect = redirects.first(:conditions => ["LOWER(title) LIKE LOWER(?)", title])
52 redirect = redirects.first(:conditions => ["LOWER(title) = LOWER(?)", title])
53 page = find_page(redirect.redirects_to, :with_redirect => false) if redirect
53 page = find_page(redirect.redirects_to, :with_redirect => false) if redirect
54 end
54 end
55 page
55 page
56 end
56 end
57
57
58 # Finds a page by title
58 # Finds a page by title
59 # The given string can be of one of the forms: "title" or "project:title"
59 # The given string can be of one of the forms: "title" or "project:title"
60 # Examples:
60 # Examples:
61 # Wiki.find_page("bar", project => foo)
61 # Wiki.find_page("bar", project => foo)
62 # Wiki.find_page("foo:bar")
62 # Wiki.find_page("foo:bar")
63 def self.find_page(title, options = {})
63 def self.find_page(title, options = {})
64 project = options[:project]
64 project = options[:project]
65 if title.to_s =~ %r{^([^\:]+)\:(.*)$}
65 if title.to_s =~ %r{^([^\:]+)\:(.*)$}
66 project_identifier, title = $1, $2
66 project_identifier, title = $1, $2
67 project = Project.find_by_identifier(project_identifier) || Project.find_by_name(project_identifier)
67 project = Project.find_by_identifier(project_identifier) || Project.find_by_name(project_identifier)
68 end
68 end
69 if project && project.wiki
69 if project && project.wiki
70 page = project.wiki.find_page(title)
70 page = project.wiki.find_page(title)
71 if page && page.content
71 if page && page.content
72 page
72 page
73 end
73 end
74 end
74 end
75 end
75 end
76
76
77 # turn a string into a valid page title
77 # turn a string into a valid page title
78 def self.titleize(title)
78 def self.titleize(title)
79 # replace spaces with _ and remove unwanted caracters
79 # replace spaces with _ and remove unwanted caracters
80 title = title.gsub(/\s+/, '_').delete(',./?;|:') if title
80 title = title.gsub(/\s+/, '_').delete(',./?;|:') if title
81 # upcase the first letter
81 # upcase the first letter
82 title = (title.slice(0..0).upcase + (title.slice(1..-1) || '')) if title
82 title = (title.slice(0..0).upcase + (title.slice(1..-1) || '')) if title
83 title
83 title
84 end
84 end
85 end
85 end
@@ -1,77 +1,80
1 # encoding: utf-8
1 # encoding: utf-8
2 #
2 #
3 # redMine - project management software
3 # redMine - project management software
4 # Copyright (C) 2006-2007 Jean-Philippe Lang
4 # Copyright (C) 2006-2007 Jean-Philippe Lang
5 #
5 #
6 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
9 # of the License, or (at your option) any later version.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
20 require File.expand_path('../../test_helper', __FILE__)
20 require File.expand_path('../../test_helper', __FILE__)
21
21
22 class WikiTest < ActiveSupport::TestCase
22 class WikiTest < ActiveSupport::TestCase
23 fixtures :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions
23 fixtures :projects, :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions
24
24
25 def test_create
25 def test_create
26 wiki = Wiki.new(:project => Project.find(2))
26 wiki = Wiki.new(:project => Project.find(2))
27 assert !wiki.save
27 assert !wiki.save
28 assert_equal 1, wiki.errors.count
28 assert_equal 1, wiki.errors.count
29
29
30 wiki.start_page = "Start page"
30 wiki.start_page = "Start page"
31 assert wiki.save
31 assert wiki.save
32 end
32 end
33
33
34 def test_update
34 def test_update
35 @wiki = Wiki.find(1)
35 @wiki = Wiki.find(1)
36 @wiki.start_page = "Another start page"
36 @wiki.start_page = "Another start page"
37 assert @wiki.save
37 assert @wiki.save
38 @wiki.reload
38 @wiki.reload
39 assert_equal "Another start page", @wiki.start_page
39 assert_equal "Another start page", @wiki.start_page
40 end
40 end
41
41
42 def test_find_page
42 def test_find_page
43 wiki = Wiki.find(1)
43 wiki = Wiki.find(1)
44 page = WikiPage.find(2)
44 page = WikiPage.find(2)
45
45
46 assert_equal page, wiki.find_page('Another_page')
46 assert_equal page, wiki.find_page('Another_page')
47 assert_equal page, wiki.find_page('Another page')
47 assert_equal page, wiki.find_page('Another page')
48 assert_equal page, wiki.find_page('ANOTHER page')
48 assert_equal page, wiki.find_page('ANOTHER page')
49
49
50 page = WikiPage.find(10)
50 page = WikiPage.find(10)
51 assert_equal page, wiki.find_page('Этика_менеджмента')
51 assert_equal page, wiki.find_page('Этика_менеджмента')
52
53 page = WikiPage.generate!(:wiki => wiki, :title => '2009\\02\\09')
54 assert_equal page, wiki.find_page('2009\\02\\09')
52 end
55 end
53
56
54 def test_titleize
57 def test_titleize
55 assert_equal 'Page_title_with_CAPITALES', Wiki.titleize('page title with CAPITALES')
58 assert_equal 'Page_title_with_CAPITALES', Wiki.titleize('page title with CAPITALES')
56 assert_equal 'テスト', Wiki.titleize('テスト')
59 assert_equal 'テスト', Wiki.titleize('テスト')
57 end
60 end
58
61
59 context "#sidebar" do
62 context "#sidebar" do
60 setup do
63 setup do
61 @wiki = Wiki.find(1)
64 @wiki = Wiki.find(1)
62 end
65 end
63
66
64 should "return nil if undefined" do
67 should "return nil if undefined" do
65 assert_nil @wiki.sidebar
68 assert_nil @wiki.sidebar
66 end
69 end
67
70
68 should "return a WikiPage if defined" do
71 should "return a WikiPage if defined" do
69 page = @wiki.pages.new(:title => 'Sidebar')
72 page = @wiki.pages.new(:title => 'Sidebar')
70 page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar')
73 page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar')
71 page.save!
74 page.save!
72
75
73 assert_kind_of WikiPage, @wiki.sidebar
76 assert_kind_of WikiPage, @wiki.sidebar
74 assert_equal 'Sidebar', @wiki.sidebar.title
77 assert_equal 'Sidebar', @wiki.sidebar.title
75 end
78 end
76 end
79 end
77 end
80 end
General Comments 0
You need to be logged in to leave comments. Login now