@@ -1,107 +1,107 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2016 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 | include Redmine::SafeAttributes |
|
19 | include Redmine::SafeAttributes | |
20 | belongs_to :project |
|
20 | belongs_to :project | |
21 | has_many :pages, lambda {order('title')}, :class_name => 'WikiPage', :dependent => :destroy |
|
21 | has_many :pages, lambda {order('title')}, :class_name => 'WikiPage', :dependent => :destroy | |
22 | has_many :redirects, :class_name => 'WikiRedirect' |
|
22 | has_many :redirects, :class_name => 'WikiRedirect' | |
23 |
|
23 | |||
24 | acts_as_watchable |
|
24 | acts_as_watchable | |
25 |
|
25 | |||
26 | validates_presence_of :start_page |
|
26 | validates_presence_of :start_page | |
27 | validates_format_of :start_page, :with => /\A[^,\.\/\?\;\|\:]*\z/ |
|
27 | validates_format_of :start_page, :with => /\A[^,\.\/\?\;\|\:]*\z/ | |
28 |
validates_length_of : |
|
28 | validates_length_of :start_page, maximum: 255 | |
29 | attr_protected :id |
|
29 | attr_protected :id | |
30 |
|
30 | |||
31 | before_destroy :delete_redirects |
|
31 | before_destroy :delete_redirects | |
32 |
|
32 | |||
33 | safe_attributes 'start_page' |
|
33 | safe_attributes 'start_page' | |
34 |
|
34 | |||
35 | def visible?(user=User.current) |
|
35 | def visible?(user=User.current) | |
36 | !user.nil? && user.allowed_to?(:view_wiki_pages, project) |
|
36 | !user.nil? && user.allowed_to?(:view_wiki_pages, project) | |
37 | end |
|
37 | end | |
38 |
|
38 | |||
39 | # Returns the wiki page that acts as the sidebar content |
|
39 | # Returns the wiki page that acts as the sidebar content | |
40 | # or nil if no such page exists |
|
40 | # or nil if no such page exists | |
41 | def sidebar |
|
41 | def sidebar | |
42 | @sidebar ||= find_page('Sidebar', :with_redirect => false) |
|
42 | @sidebar ||= find_page('Sidebar', :with_redirect => false) | |
43 | end |
|
43 | end | |
44 |
|
44 | |||
45 | # find the page with the given title |
|
45 | # find the page with the given title | |
46 | # if page doesn't exist, return a new page |
|
46 | # if page doesn't exist, return a new page | |
47 | def find_or_new_page(title) |
|
47 | def find_or_new_page(title) | |
48 | title = start_page if title.blank? |
|
48 | title = start_page if title.blank? | |
49 | find_page(title) || WikiPage.new(:wiki => self, :title => Wiki.titleize(title)) |
|
49 | find_page(title) || WikiPage.new(:wiki => self, :title => Wiki.titleize(title)) | |
50 | end |
|
50 | end | |
51 |
|
51 | |||
52 | # find the page with the given title |
|
52 | # find the page with the given title | |
53 | def find_page(title, options = {}) |
|
53 | def find_page(title, options = {}) | |
54 | @page_found_with_redirect = false |
|
54 | @page_found_with_redirect = false | |
55 | title = start_page if title.blank? |
|
55 | title = start_page if title.blank? | |
56 | title = Wiki.titleize(title) |
|
56 | title = Wiki.titleize(title) | |
57 | page = pages.where("LOWER(title) = LOWER(?)", title).first |
|
57 | page = pages.where("LOWER(title) = LOWER(?)", title).first | |
58 | if page.nil? && options[:with_redirect] != false |
|
58 | if page.nil? && options[:with_redirect] != false | |
59 | # search for a redirect |
|
59 | # search for a redirect | |
60 | redirect = redirects.where("LOWER(title) = LOWER(?)", title).first |
|
60 | redirect = redirects.where("LOWER(title) = LOWER(?)", title).first | |
61 | if redirect |
|
61 | if redirect | |
62 | page = redirect.target_page |
|
62 | page = redirect.target_page | |
63 | @page_found_with_redirect = true |
|
63 | @page_found_with_redirect = true | |
64 | end |
|
64 | end | |
65 | end |
|
65 | end | |
66 | page |
|
66 | page | |
67 | end |
|
67 | end | |
68 |
|
68 | |||
69 | # Returns true if the last page was found with a redirect |
|
69 | # Returns true if the last page was found with a redirect | |
70 | def page_found_with_redirect? |
|
70 | def page_found_with_redirect? | |
71 | @page_found_with_redirect |
|
71 | @page_found_with_redirect | |
72 | end |
|
72 | end | |
73 |
|
73 | |||
74 | # Deletes all redirects from/to the wiki |
|
74 | # Deletes all redirects from/to the wiki | |
75 | def delete_redirects |
|
75 | def delete_redirects | |
76 | WikiRedirect.where(:wiki_id => id).delete_all |
|
76 | WikiRedirect.where(:wiki_id => id).delete_all | |
77 | WikiRedirect.where(:redirects_to_wiki_id => id).delete_all |
|
77 | WikiRedirect.where(:redirects_to_wiki_id => id).delete_all | |
78 | end |
|
78 | end | |
79 |
|
79 | |||
80 | # Finds a page by title |
|
80 | # Finds a page by title | |
81 | # The given string can be of one of the forms: "title" or "project:title" |
|
81 | # The given string can be of one of the forms: "title" or "project:title" | |
82 | # Examples: |
|
82 | # Examples: | |
83 | # Wiki.find_page("bar", project => foo) |
|
83 | # Wiki.find_page("bar", project => foo) | |
84 | # Wiki.find_page("foo:bar") |
|
84 | # Wiki.find_page("foo:bar") | |
85 | def self.find_page(title, options = {}) |
|
85 | def self.find_page(title, options = {}) | |
86 | project = options[:project] |
|
86 | project = options[:project] | |
87 | if title.to_s =~ %r{^([^\:]+)\:(.*)$} |
|
87 | if title.to_s =~ %r{^([^\:]+)\:(.*)$} | |
88 | project_identifier, title = $1, $2 |
|
88 | project_identifier, title = $1, $2 | |
89 | project = Project.find_by_identifier(project_identifier) || Project.find_by_name(project_identifier) |
|
89 | project = Project.find_by_identifier(project_identifier) || Project.find_by_name(project_identifier) | |
90 | end |
|
90 | end | |
91 | if project && project.wiki |
|
91 | if project && project.wiki | |
92 | page = project.wiki.find_page(title) |
|
92 | page = project.wiki.find_page(title) | |
93 | if page && page.content |
|
93 | if page && page.content | |
94 | page |
|
94 | page | |
95 | end |
|
95 | end | |
96 | end |
|
96 | end | |
97 | end |
|
97 | end | |
98 |
|
98 | |||
99 | # turn a string into a valid page title |
|
99 | # turn a string into a valid page title | |
100 | def self.titleize(title) |
|
100 | def self.titleize(title) | |
101 | # replace spaces with _ and remove unwanted caracters |
|
101 | # replace spaces with _ and remove unwanted caracters | |
102 | title = title.gsub(/\s+/, '_').delete(',./?;|:') if title |
|
102 | title = title.gsub(/\s+/, '_').delete(',./?;|:') if title | |
103 | # upcase the first letter |
|
103 | # upcase the first letter | |
104 | title = (title.slice(0..0).upcase + (title.slice(1..-1) || '')) if title |
|
104 | title = (title.slice(0..0).upcase + (title.slice(1..-1) || '')) if title | |
105 | title |
|
105 | title | |
106 | end |
|
106 | end | |
107 | end |
|
107 | end |
General Comments 0
You need to be logged in to leave comments.
Login now