To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / models / wiki.rb @ 1533:59e13100ea95
History | View | Annotate | Download (3.28 KB)
| 1 | 0:513646585e45 | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 1494:e248c7af89ec | Chris | # Copyright (C) 2006-2014 Jean-Philippe Lang
|
| 3 | 0:513646585e45 | Chris | #
|
| 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 | 909:cbb26bc654de | Chris | #
|
| 9 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | #
|
| 14 | 0:513646585e45 | Chris | # 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 | 929:5f33065ddc4b | Chris | include Redmine::SafeAttributes |
| 20 | 0:513646585e45 | Chris | belongs_to :project
|
| 21 | has_many :pages, :class_name => 'WikiPage', :dependent => :destroy, :order => 'title' |
||
| 22 | has_many :redirects, :class_name => 'WikiRedirect', :dependent => :delete_all |
||
| 23 | 909:cbb26bc654de | Chris | |
| 24 | 0:513646585e45 | Chris | acts_as_watchable |
| 25 | 909:cbb26bc654de | Chris | |
| 26 | 0:513646585e45 | Chris | validates_presence_of :start_page
|
| 27 | 1464:261b3d9a4903 | Chris | validates_format_of :start_page, :with => /\A[^,\.\/\?\;\|\:]*\z/ |
| 28 | 909:cbb26bc654de | Chris | |
| 29 | 929:5f33065ddc4b | Chris | safe_attributes 'start_page'
|
| 30 | |||
| 31 | 0:513646585e45 | Chris | def visible?(user=User.current) |
| 32 | !user.nil? && user.allowed_to?(:view_wiki_pages, project)
|
||
| 33 | end
|
||
| 34 | 909:cbb26bc654de | Chris | |
| 35 | 0:513646585e45 | Chris | # Returns the wiki page that acts as the sidebar content
|
| 36 | # or nil if no such page exists
|
||
| 37 | def sidebar |
||
| 38 | @sidebar ||= find_page('Sidebar', :with_redirect => false) |
||
| 39 | end
|
||
| 40 | 909:cbb26bc654de | Chris | |
| 41 | 0:513646585e45 | Chris | # find the page with the given title
|
| 42 | # if page doesn't exist, return a new page
|
||
| 43 | def find_or_new_page(title) |
||
| 44 | title = start_page if title.blank?
|
||
| 45 | find_page(title) || WikiPage.new(:wiki => self, :title => Wiki.titleize(title)) |
||
| 46 | end
|
||
| 47 | 909:cbb26bc654de | Chris | |
| 48 | 0:513646585e45 | Chris | # find the page with the given title
|
| 49 | def find_page(title, options = {}) |
||
| 50 | 441:cbce1fd3b1b7 | Chris | @page_found_with_redirect = false |
| 51 | 0:513646585e45 | Chris | title = start_page if title.blank?
|
| 52 | title = Wiki.titleize(title)
|
||
| 53 | 1464:261b3d9a4903 | Chris | page = pages.where("LOWER(title) = LOWER(?)", title).first
|
| 54 | 0:513646585e45 | Chris | if !page && !(options[:with_redirect] == false) |
| 55 | # search for a redirect
|
||
| 56 | 1464:261b3d9a4903 | Chris | redirect = redirects.where("LOWER(title) = LOWER(?)", title).first
|
| 57 | 441:cbce1fd3b1b7 | Chris | if redirect
|
| 58 | page = find_page(redirect.redirects_to, :with_redirect => false) |
||
| 59 | @page_found_with_redirect = true |
||
| 60 | end
|
||
| 61 | 0:513646585e45 | Chris | end
|
| 62 | page |
||
| 63 | end
|
||
| 64 | 909:cbb26bc654de | Chris | |
| 65 | 441:cbce1fd3b1b7 | Chris | # Returns true if the last page was found with a redirect
|
| 66 | def page_found_with_redirect? |
||
| 67 | @page_found_with_redirect
|
||
| 68 | end
|
||
| 69 | |||
| 70 | 0:513646585e45 | Chris | # Finds a page by title
|
| 71 | # The given string can be of one of the forms: "title" or "project:title"
|
||
| 72 | # Examples:
|
||
| 73 | # Wiki.find_page("bar", project => foo)
|
||
| 74 | # Wiki.find_page("foo:bar")
|
||
| 75 | def self.find_page(title, options = {}) |
||
| 76 | project = options[:project]
|
||
| 77 | if title.to_s =~ %r{^([^\:]+)\:(.*)$} |
||
| 78 | project_identifier, title = $1, $2 |
||
| 79 | project = Project.find_by_identifier(project_identifier) || Project.find_by_name(project_identifier) |
||
| 80 | end
|
||
| 81 | if project && project.wiki
|
||
| 82 | page = project.wiki.find_page(title) |
||
| 83 | if page && page.content
|
||
| 84 | page |
||
| 85 | end
|
||
| 86 | end
|
||
| 87 | end
|
||
| 88 | 909:cbb26bc654de | Chris | |
| 89 | 0:513646585e45 | Chris | # turn a string into a valid page title
|
| 90 | def self.titleize(title) |
||
| 91 | # replace spaces with _ and remove unwanted caracters
|
||
| 92 | title = title.gsub(/\s+/, '_').delete(',./?;|:') if title |
||
| 93 | # upcase the first letter
|
||
| 94 | title = (title.slice(0..0).upcase + (title.slice(1..-1) || '')) if title |
||
| 95 | title |
||
| 96 | 909:cbb26bc654de | Chris | end
|
| 97 | 0:513646585e45 | Chris | end |