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