annotate .svn/pristine/83/83bb890c49d6486a31b5b46ae3ea92ba18f02e2b.svn-base @ 1296:038ba2d95de8 redmine-2.2

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