You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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. class Wiki < ActiveRecord::Base
  18. include Redmine::SafeAttributes
  19. belongs_to :project
  20. has_many :pages, lambda {order('title')}, :class_name => 'WikiPage', :dependent => :destroy
  21. has_many :redirects, :class_name => 'WikiRedirect'
  22. acts_as_watchable
  23. validates_presence_of :start_page
  24. validates_format_of :start_page, :with => /\A[^,\.\/\?\;\|\:]*\z/
  25. validates_length_of :start_page, maximum: 255
  26. before_destroy :delete_redirects
  27. safe_attributes 'start_page'
  28. def visible?(user=User.current)
  29. !user.nil? && user.allowed_to?(:view_wiki_pages, project)
  30. end
  31. # Returns the wiki page that acts as the sidebar content
  32. # or nil if no such page exists
  33. def sidebar
  34. @sidebar ||= find_page('Sidebar', :with_redirect => false)
  35. end
  36. # find the page with the given title
  37. # if page doesn't exist, return a new page
  38. def find_or_new_page(title)
  39. title = start_page if title.blank?
  40. find_page(title) || WikiPage.new(:wiki => self, :title => Wiki.titleize(title))
  41. end
  42. # find the page with the given title
  43. def find_page(title, options = {})
  44. @page_found_with_redirect = false
  45. title = start_page if title.blank?
  46. title = Wiki.titleize(title)
  47. page = pages.find_by("LOWER(title) = LOWER(?)", title)
  48. if page.nil? && options[:with_redirect] != false
  49. # search for a redirect
  50. redirect = redirects.where("LOWER(title) = LOWER(?)", title).first
  51. if redirect
  52. page = redirect.target_page
  53. @page_found_with_redirect = true
  54. end
  55. end
  56. page
  57. end
  58. # Returns true if the last page was found with a redirect
  59. def page_found_with_redirect?
  60. @page_found_with_redirect
  61. end
  62. # Deletes all redirects from/to the wiki
  63. def delete_redirects
  64. WikiRedirect.where(:wiki_id => id).delete_all
  65. WikiRedirect.where(:redirects_to_wiki_id => id).delete_all
  66. end
  67. # Finds a page by title
  68. # The given string can be of one of the forms: "title" or "project:title"
  69. # Examples:
  70. # Wiki.find_page("bar", project => foo)
  71. # Wiki.find_page("foo:bar")
  72. def self.find_page(title, options = {})
  73. project = options[:project]
  74. if title.to_s =~ %r{^([^\:]+)\:(.*)$}
  75. project_identifier, title = $1, $2
  76. project = Project.find_by_identifier(project_identifier) || Project.find_by_name(project_identifier)
  77. end
  78. if project && project.wiki
  79. page = project.wiki.find_page(title)
  80. if page && page.content
  81. page
  82. end
  83. end
  84. end
  85. # turn a string into a valid page title
  86. def self.titleize(title)
  87. # replace spaces with _ and remove unwanted caracters
  88. title = title.gsub(/\s+/, '_').delete(',./?;|:') if title
  89. # upcase the first letter
  90. title = (title.slice(0..0).upcase + (title.slice(1..-1) || '')) if title
  91. title
  92. end
  93. end