comparison app/models/.svn/text-base/wiki.rb.svn-base @ 0:513646585e45

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