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.

wiki.rb 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class Wiki < ApplicationRecord
  19. include Redmine::SafeAttributes
  20. belongs_to :project
  21. has_many :pages, lambda {order(Arel.sql('LOWER(title)').asc)}, :class_name => 'WikiPage', :dependent => :destroy
  22. has_many :redirects, :class_name => 'WikiRedirect'
  23. acts_as_watchable
  24. validates_presence_of :start_page
  25. validates_format_of :start_page, :with => /\A[^,\.\/\?\;\|\:]*\z/
  26. validates_length_of :start_page, maximum: 255
  27. before_destroy :delete_redirects
  28. safe_attributes 'start_page'
  29. def visible?(user=User.current)
  30. !user.nil? && user.allowed_to?(:view_wiki_pages, project)
  31. end
  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. # find the page with the given title
  38. # if page doesn't exist, return a new page
  39. def find_or_new_page(title)
  40. title = start_page if title.blank?
  41. find_page(title) || WikiPage.new(:wiki => self, :title => Wiki.titleize(title))
  42. end
  43. # find the page with the given title
  44. def find_page(title, options = {})
  45. @page_found_with_redirect = false
  46. title = start_page if title.blank?
  47. title = Wiki.titleize(title)
  48. page = pages.find_by("LOWER(title) = LOWER(?)", title)
  49. if page.nil? && options[:with_redirect] != false
  50. # search for a redirect
  51. redirect = redirects.where("LOWER(title) = LOWER(?)", title).first
  52. if redirect
  53. page = redirect.target_page
  54. @page_found_with_redirect = true
  55. end
  56. end
  57. page
  58. end
  59. # Returns true if the last page was found with a redirect
  60. def page_found_with_redirect?
  61. @page_found_with_redirect
  62. end
  63. # Deletes all redirects from/to the wiki
  64. def delete_redirects
  65. WikiRedirect.where(:wiki_id => id).delete_all
  66. WikiRedirect.where(:redirects_to_wiki_id => id).delete_all
  67. end
  68. # Finds a page by title
  69. # The given string can be of one of the forms: "title" or "project:title"
  70. # Examples:
  71. # Wiki.find_page("bar", project => foo)
  72. # Wiki.find_page("foo:bar")
  73. def self.find_page(title, options = {})
  74. project = options[:project]
  75. if title.to_s =~ %r{^([^\:]+)\:(.*)$}
  76. project_identifier, title = $1, $2
  77. project = Project.find_by_identifier(project_identifier) || Project.find_by_name(project_identifier)
  78. end
  79. if project && project.wiki
  80. page = project.wiki.find_page(title)
  81. if page && page.content
  82. page
  83. end
  84. end
  85. end
  86. def self.create_default(project)
  87. create(:project => project, :start_page => 'Wiki')
  88. end
  89. # 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. end
  97. end