Chris@0: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@909: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@909: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: class Wiki < ActiveRecord::Base Chris@929: include Redmine::SafeAttributes Chris@0: belongs_to :project Chris@0: has_many :pages, :class_name => 'WikiPage', :dependent => :destroy, :order => 'title' Chris@0: has_many :redirects, :class_name => 'WikiRedirect', :dependent => :delete_all Chris@909: Chris@0: acts_as_watchable Chris@909: Chris@0: validates_presence_of :start_page Chris@1464: validates_format_of :start_page, :with => /\A[^,\.\/\?\;\|\:]*\z/ Chris@909: Chris@929: safe_attributes 'start_page' Chris@929: Chris@0: def visible?(user=User.current) Chris@0: !user.nil? && user.allowed_to?(:view_wiki_pages, project) Chris@0: end Chris@909: Chris@0: # Returns the wiki page that acts as the sidebar content Chris@0: # or nil if no such page exists Chris@0: def sidebar Chris@0: @sidebar ||= find_page('Sidebar', :with_redirect => false) Chris@0: end Chris@909: Chris@0: # find the page with the given title Chris@0: # if page doesn't exist, return a new page Chris@0: def find_or_new_page(title) Chris@0: title = start_page if title.blank? Chris@0: find_page(title) || WikiPage.new(:wiki => self, :title => Wiki.titleize(title)) Chris@0: end Chris@909: Chris@0: # find the page with the given title Chris@0: def find_page(title, options = {}) Chris@441: @page_found_with_redirect = false Chris@0: title = start_page if title.blank? Chris@0: title = Wiki.titleize(title) Chris@1464: page = pages.where("LOWER(title) = LOWER(?)", title).first Chris@0: if !page && !(options[:with_redirect] == false) Chris@0: # search for a redirect Chris@1464: redirect = redirects.where("LOWER(title) = LOWER(?)", title).first Chris@441: if redirect Chris@441: page = find_page(redirect.redirects_to, :with_redirect => false) Chris@441: @page_found_with_redirect = true Chris@441: end Chris@0: end Chris@0: page Chris@0: end Chris@909: Chris@441: # Returns true if the last page was found with a redirect Chris@441: def page_found_with_redirect? Chris@441: @page_found_with_redirect Chris@441: end Chris@441: Chris@0: # Finds a page by title Chris@0: # The given string can be of one of the forms: "title" or "project:title" Chris@0: # Examples: Chris@0: # Wiki.find_page("bar", project => foo) Chris@0: # Wiki.find_page("foo:bar") Chris@0: def self.find_page(title, options = {}) Chris@0: project = options[:project] Chris@0: if title.to_s =~ %r{^([^\:]+)\:(.*)$} Chris@0: project_identifier, title = $1, $2 Chris@0: project = Project.find_by_identifier(project_identifier) || Project.find_by_name(project_identifier) Chris@0: end Chris@0: if project && project.wiki Chris@0: page = project.wiki.find_page(title) Chris@0: if page && page.content Chris@0: page Chris@0: end Chris@0: end Chris@0: end Chris@909: Chris@0: # turn a string into a valid page title Chris@0: def self.titleize(title) Chris@0: # replace spaces with _ and remove unwanted caracters Chris@0: title = title.gsub(/\s+/, '_').delete(',./?;|:') if title Chris@0: # upcase the first letter Chris@0: title = (title.slice(0..0).upcase + (title.slice(1..-1) || '')) if title Chris@0: title Chris@909: end Chris@0: end