@@ -1,107 +1,107 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class Wiki < ActiveRecord::Base |
|
19 | 19 | include Redmine::SafeAttributes |
|
20 | 20 | belongs_to :project |
|
21 | 21 | has_many :pages, lambda {order('title')}, :class_name => 'WikiPage', :dependent => :destroy |
|
22 | 22 | has_many :redirects, :class_name => 'WikiRedirect' |
|
23 | 23 | |
|
24 | 24 | acts_as_watchable |
|
25 | 25 | |
|
26 | 26 | validates_presence_of :start_page |
|
27 | 27 | validates_format_of :start_page, :with => /\A[^,\.\/\?\;\|\:]*\z/ |
|
28 |
validates_length_of : |
|
|
28 | validates_length_of :start_page, maximum: 255 | |
|
29 | 29 | attr_protected :id |
|
30 | 30 | |
|
31 | 31 | before_destroy :delete_redirects |
|
32 | 32 | |
|
33 | 33 | safe_attributes 'start_page' |
|
34 | 34 | |
|
35 | 35 | def visible?(user=User.current) |
|
36 | 36 | !user.nil? && user.allowed_to?(:view_wiki_pages, project) |
|
37 | 37 | end |
|
38 | 38 | |
|
39 | 39 | # Returns the wiki page that acts as the sidebar content |
|
40 | 40 | # or nil if no such page exists |
|
41 | 41 | def sidebar |
|
42 | 42 | @sidebar ||= find_page('Sidebar', :with_redirect => false) |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | # find the page with the given title |
|
46 | 46 | # if page doesn't exist, return a new page |
|
47 | 47 | def find_or_new_page(title) |
|
48 | 48 | title = start_page if title.blank? |
|
49 | 49 | find_page(title) || WikiPage.new(:wiki => self, :title => Wiki.titleize(title)) |
|
50 | 50 | end |
|
51 | 51 | |
|
52 | 52 | # find the page with the given title |
|
53 | 53 | def find_page(title, options = {}) |
|
54 | 54 | @page_found_with_redirect = false |
|
55 | 55 | title = start_page if title.blank? |
|
56 | 56 | title = Wiki.titleize(title) |
|
57 | 57 | page = pages.where("LOWER(title) = LOWER(?)", title).first |
|
58 | 58 | if page.nil? && options[:with_redirect] != false |
|
59 | 59 | # search for a redirect |
|
60 | 60 | redirect = redirects.where("LOWER(title) = LOWER(?)", title).first |
|
61 | 61 | if redirect |
|
62 | 62 | page = redirect.target_page |
|
63 | 63 | @page_found_with_redirect = true |
|
64 | 64 | end |
|
65 | 65 | end |
|
66 | 66 | page |
|
67 | 67 | end |
|
68 | 68 | |
|
69 | 69 | # Returns true if the last page was found with a redirect |
|
70 | 70 | def page_found_with_redirect? |
|
71 | 71 | @page_found_with_redirect |
|
72 | 72 | end |
|
73 | 73 | |
|
74 | 74 | # Deletes all redirects from/to the wiki |
|
75 | 75 | def delete_redirects |
|
76 | 76 | WikiRedirect.where(:wiki_id => id).delete_all |
|
77 | 77 | WikiRedirect.where(:redirects_to_wiki_id => id).delete_all |
|
78 | 78 | end |
|
79 | 79 | |
|
80 | 80 | # Finds a page by title |
|
81 | 81 | # The given string can be of one of the forms: "title" or "project:title" |
|
82 | 82 | # Examples: |
|
83 | 83 | # Wiki.find_page("bar", project => foo) |
|
84 | 84 | # Wiki.find_page("foo:bar") |
|
85 | 85 | def self.find_page(title, options = {}) |
|
86 | 86 | project = options[:project] |
|
87 | 87 | if title.to_s =~ %r{^([^\:]+)\:(.*)$} |
|
88 | 88 | project_identifier, title = $1, $2 |
|
89 | 89 | project = Project.find_by_identifier(project_identifier) || Project.find_by_name(project_identifier) |
|
90 | 90 | end |
|
91 | 91 | if project && project.wiki |
|
92 | 92 | page = project.wiki.find_page(title) |
|
93 | 93 | if page && page.content |
|
94 | 94 | page |
|
95 | 95 | end |
|
96 | 96 | end |
|
97 | 97 | end |
|
98 | 98 | |
|
99 | 99 | # turn a string into a valid page title |
|
100 | 100 | def self.titleize(title) |
|
101 | 101 | # replace spaces with _ and remove unwanted caracters |
|
102 | 102 | title = title.gsub(/\s+/, '_').delete(',./?;|:') if title |
|
103 | 103 | # upcase the first letter |
|
104 | 104 | title = (title.slice(0..0).upcase + (title.slice(1..-1) || '')) if title |
|
105 | 105 | title |
|
106 | 106 | end |
|
107 | 107 | end |
General Comments 0
You need to be logged in to leave comments.
Login now