@@ -1,94 +1,94 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 |
# Copyright (C) 2006-20 |
|
2 | # Copyright (C) 2006-2011 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 | @page_found_with_redirect = false |
|
47 | @page_found_with_redirect = false | |
48 | title = start_page if title.blank? |
|
48 | title = start_page if title.blank? | |
49 | title = Wiki.titleize(title) |
|
49 | title = Wiki.titleize(title) | |
50 | page = pages.first(:conditions => ["LOWER(title) = LOWER(?)", title]) |
|
50 | page = pages.first(:conditions => ["LOWER(title) = LOWER(?)", title]) | |
51 | if !page && !(options[:with_redirect] == false) |
|
51 | if !page && !(options[:with_redirect] == false) | |
52 | # search for a redirect |
|
52 | # search for a redirect | |
53 | redirect = redirects.first(:conditions => ["LOWER(title) = LOWER(?)", title]) |
|
53 | redirect = redirects.first(:conditions => ["LOWER(title) = LOWER(?)", title]) | |
54 | if redirect |
|
54 | if redirect | |
55 | page = find_page(redirect.redirects_to, :with_redirect => false) |
|
55 | page = find_page(redirect.redirects_to, :with_redirect => false) | |
56 | @page_found_with_redirect = true |
|
56 | @page_found_with_redirect = true | |
57 | end |
|
57 | end | |
58 | end |
|
58 | end | |
59 | page |
|
59 | page | |
60 | end |
|
60 | end | |
61 |
|
61 | |||
62 | # Returns true if the last page was found with a redirect |
|
62 | # Returns true if the last page was found with a redirect | |
63 | def page_found_with_redirect? |
|
63 | def page_found_with_redirect? | |
64 | @page_found_with_redirect |
|
64 | @page_found_with_redirect | |
65 | end |
|
65 | end | |
66 |
|
66 | |||
67 | # Finds a page by title |
|
67 | # Finds a page by title | |
68 | # The given string can be of one of the forms: "title" or "project:title" |
|
68 | # The given string can be of one of the forms: "title" or "project:title" | |
69 | # Examples: |
|
69 | # Examples: | |
70 | # Wiki.find_page("bar", project => foo) |
|
70 | # Wiki.find_page("bar", project => foo) | |
71 | # Wiki.find_page("foo:bar") |
|
71 | # Wiki.find_page("foo:bar") | |
72 | def self.find_page(title, options = {}) |
|
72 | def self.find_page(title, options = {}) | |
73 | project = options[:project] |
|
73 | project = options[:project] | |
74 | if title.to_s =~ %r{^([^\:]+)\:(.*)$} |
|
74 | if title.to_s =~ %r{^([^\:]+)\:(.*)$} | |
75 | project_identifier, title = $1, $2 |
|
75 | project_identifier, title = $1, $2 | |
76 | project = Project.find_by_identifier(project_identifier) || Project.find_by_name(project_identifier) |
|
76 | project = Project.find_by_identifier(project_identifier) || Project.find_by_name(project_identifier) | |
77 | end |
|
77 | end | |
78 | if project && project.wiki |
|
78 | if project && project.wiki | |
79 | page = project.wiki.find_page(title) |
|
79 | page = project.wiki.find_page(title) | |
80 | if page && page.content |
|
80 | if page && page.content | |
81 | page |
|
81 | page | |
82 | end |
|
82 | end | |
83 | end |
|
83 | end | |
84 | end |
|
84 | end | |
85 |
|
85 | |||
86 | # turn a string into a valid page title |
|
86 | # turn a string into a valid page title | |
87 | def self.titleize(title) |
|
87 | def self.titleize(title) | |
88 | # replace spaces with _ and remove unwanted caracters |
|
88 | # replace spaces with _ and remove unwanted caracters | |
89 | title = title.gsub(/\s+/, '_').delete(',./?;|:') if title |
|
89 | title = title.gsub(/\s+/, '_').delete(',./?;|:') if title | |
90 | # upcase the first letter |
|
90 | # upcase the first letter | |
91 | title = (title.slice(0..0).upcase + (title.slice(1..-1) || '')) if title |
|
91 | title = (title.slice(0..0).upcase + (title.slice(1..-1) || '')) if title | |
92 | title |
|
92 | title | |
93 |
end |
|
93 | end | |
94 | end |
|
94 | end |
General Comments 0
You need to be logged in to leave comments.
Login now