Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

422 lines
12 KiB
Ruby
Raw Permalink Normal View History

2025-07-18 17:36:37 +01:00
# frozen_string_literal: true
#-- copyright
2020-01-15 11:31:26 +01:00
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
2011-05-30 20:52:25 +02:00
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
2011-05-30 20:52:25 +02:00
#
2013-09-16 17:59:31 +02:00
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
2021-01-13 17:47:45 +01:00
# Copyright (C) 2006-2013 Jean-Philippe Lang
2013-09-16 17:59:31 +02:00
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++
2012-10-01 16:52:41 +02:00
require "htmldiff"
2007-07-14 11:25:03 +00:00
# The WikiController follows the Rails REST controller pattern but with
# a few differences
#
# * index - shows a list of WikiPages grouped by page or date
# * new - not used
# * create - not used
# * show - will also show the form for creating a new wiki page
# * edit - used to edit an existing or new page
# * update - used to save a wiki page update to the database, including new pages
# * destroy - normal
#
# Other member and collection methods are also used
2007-03-10 15:09:49 +00:00
class WikiController < ApplicationController
2009-10-21 17:07:18 +00:00
default_search_scope :wiki_pages
2016-09-06 15:40:49 +02:00
before_action :find_wiki, :authorize
before_action :find_existing_page, only: %i[edit_parent_page
update_parent_page
rename
protect
history
diff
annotate
destroy]
before_action :find_wiki_page, only: %i[show]
before_action :handle_new_wiki_page, only: %i[show]
2023-03-22 16:08:40 +01:00
before_action :build_wiki_page, only: %i[new]
2011-05-30 20:52:25 +02:00
include AttachableServiceCall
2011-05-30 20:52:25 +02:00
include AttachmentsHelper
2013-06-19 16:15:06 +02:00
include PaginationHelper
include Redmine::MenuManager::WikiMenuHelper
attr_reader :page, :related_page
current_menu_item :index do |controller|
2016-08-22 13:06:09 +02:00
controller.current_menu_item_sym :related_page
end
2018-06-26 16:32:42 +02:00
current_menu_item :new_child do |controller|
controller.current_menu_item_sym :parent_page
end
current_menu_item do |controller|
controller.current_menu_item_sym :page
end
# List of pages, sorted alphabetically and by parent (hierarchy)
def index
slug = wiki_page_title.nil? ? "wiki" : WikiPage.slug(wiki_page_title)
2016-08-29 08:24:11 +02:00
@related_page = WikiPage.find_by(wiki_id: @wiki.id, slug:)
2012-09-19 23:02:10 +02:00
@pages = @wiki.pages.order(Arel.sql("title")).includes(wiki: :project)
@pages_by_parent_id = @pages.group_by(&:parent_id)
end
2011-05-30 20:52:25 +02:00
2007-03-10 15:09:49 +00:00
# display a page (in editing mode if it doesn't exist)
2026-04-22 15:09:38 +02:00
def show # rubocop:disable Metrics/AbcSize
# Set the related page ID to make it the parent of new links
flash[:_related_wiki_page_id] = @page.id
version = params[:version] if User.current.allowed_in_project?(:view_wiki_edits, @project)
2023-03-22 16:08:40 +01:00
@page = ::WikiPages::AtVersion.new(@page, version)
2026-04-22 15:09:38 +02:00
if params[:format] == "markdown" && User.current.allowed_in_project?(:view_wiki_pages, @project)
2023-03-22 16:08:40 +01:00
send_data(@page.text, type: "text/plain", filename: "#{@page.title}.md")
return
2007-03-10 15:09:49 +00:00
end
2023-03-22 16:08:40 +01:00
2010-02-06 09:30:53 +00:00
@editable = editable?
@show_create = show_create?
2007-03-10 15:09:49 +00:00
end
2011-05-30 20:52:25 +02:00
2023-03-09 10:25:57 +01:00
def new; end
def new_child
find_existing_page
return if performed?
old_page = @page
2023-03-22 16:08:40 +01:00
build_wiki_page
2023-03-09 10:25:57 +01:00
@page.parent = old_page
render action: "new"
end
def menu
@page = @wiki.pages.find_by(id: params[:id])
render layout: nil
end
2007-03-10 15:09:49 +00:00
# edit an existing page or a new one
def edit
2023-03-22 16:08:40 +01:00
page = @wiki.find_or_new_page(wiki_page_title)
return render_403 unless editable?(page)
2023-03-22 16:08:40 +01:00
if page.new_record? && flash[:_related_wiki_page_id]
page.parent_id = flash[:_related_wiki_page_id]
end
2011-05-30 20:52:25 +02:00
version = params[:version] if User.current.allowed_in_project?(:view_wiki_edits, @project)
2023-03-22 16:08:40 +01:00
@page = ::WikiPages::AtVersion.new(page, version)
end
2023-03-09 10:25:57 +01:00
def create
call = attachable_create_call ::WikiPages::CreateService,
2023-05-02 10:17:57 +02:00
args: permitted_params.wiki_page.to_h.merge(wiki: @wiki)
2023-03-09 10:25:57 +01:00
@page = call.result
if call.success?
call_hook(:controller_wiki_edit_after_save, params:, page: @page)
flash[:notice] = I18n.t(:notice_successful_create)
redirect_to_show
else
2024-08-28 12:43:09 +02:00
render action: :new, status: :unprocessable_entity
2023-03-09 10:25:57 +01:00
end
end
# Creates a new page or updates an existing one
2024-11-05 06:46:17 +01:00
def update # rubocop:disable Metrics/AbcSize
2018-08-15 07:29:39 +02:00
@old_title = params[:id]
@page = @wiki.find_or_new_page(@old_title)
2018-12-06 13:37:57 +01:00
if @page.nil?
render_404
return
end
2018-07-18 08:11:51 +02:00
return if locked?
2016-02-09 14:57:16 +01:00
call = attachable_update_call ::WikiPages::UpdateService,
model: @page,
2023-05-02 10:17:57 +02:00
args: permitted_params.wiki_page.to_h
@page = call.result
2018-07-18 08:11:51 +02:00
if call.success?
call_hook(:controller_wiki_edit_after_save, params:, page: @page)
2020-09-16 11:26:15 +02:00
flash[:notice] = I18n.t(:notice_successful_update)
redirect_to_show
else
2024-08-28 12:43:09 +02:00
render action: :edit, status: :unprocessable_entity
2007-03-10 15:09:49 +00:00
end
2007-05-26 17:22:27 +00:00
rescue ActiveRecord::StaleObjectError
# Optimistic locking exception
2020-09-16 11:26:15 +02:00
flash.now[:error] = I18n.t(:notice_locking_conflict)
2024-08-28 12:43:09 +02:00
render action: :edit, status: :unprocessable_entity
2007-03-10 15:09:49 +00:00
end
# rename a page
2026-04-22 15:09:38 +02:00
def rename # rubocop:disable Metrics/AbcSize
return render_403 unless editable?
2021-02-11 16:02:18 +01:00
@page.redirect_existing_links = true
# used to display the *original* title if some AR validation errors occur
@original_title = @page.title
if request.patch?
attributes = permitted_params.wiki_page_rename
if (item = conflicting_menu_item(attributes["title"]))
flash[:error] = I18n.t(
:error_wiki_root_menu_item_conflict,
old_name: @page.title,
new_name: attributes["title"],
existing_caption: item.caption,
2021-02-11 16:02:18 +01:00
existing_identifier: item.name
)
redirect_to_show
2019-09-20 08:34:51 +02:00
elsif @page.update(attributes)
flash[:notice] = t(:notice_successful_update)
redirect_to_show
end
end
end
def conflicting_menu_item(title)
page.menu_item &&
page.menu_item.parent_id.nil? &&
project_menu_items.find { |item| item.name.to_s == WikiPage.slug(title) }
end
def project_menu_items
Redmine::MenuManager.items("project_menu").children + wiki_root_menu_items
end
def wiki_root_menu_items
MenuItems::WikiMenuItem
.main_items(@wiki.id)
2026-05-06 13:32:01 +02:00
.map { OpenStruct.new name: it.name, caption: it.title, item: it }
end
def edit_parent_page
return render_403 unless editable?
@parent_pages = @wiki.pages.includes(:parent) - @page.self_and_descendants
end
def update_parent_page
return render_403 unless editable?
@page.parent_id = params[:wiki_page][:parent_id]
if @page.save
2020-09-16 11:26:15 +02:00
flash[:notice] = I18n.t(:notice_successful_update)
redirect_to_show
else
@parent_pages = @wiki.pages.includes(:parent) - @page.self_and_descendants
render "edit_parent_page"
end
end
2011-05-30 20:52:25 +02:00
def protect
@page.update_attribute :protected, params[:protected]
redirect_to_show
end
2007-03-10 15:09:49 +00:00
# show page history
def history
2011-05-30 20:52:25 +02:00
# don't load text
2026-05-04 14:52:52 +02:00
@versions = @page.journals
.select(:id, :user_id, :notes, :created_at, :version)
2026-05-06 13:32:01 +02:00
.order(version: :desc)
2026-05-04 14:52:52 +02:00
.page(page_param)
.per_page(per_page_param)
2007-06-23 18:53:45 +00:00
2014-11-03 21:10:20 +01:00
render layout: !request.xhr?
2007-03-10 15:09:49 +00:00
end
2011-05-30 20:52:25 +02:00
2007-07-14 11:25:03 +00:00
def diff
if (@diff = @page.diff(params[:version_to], params[:version_from]))
2024-08-12 16:39:57 +02:00
@html_diff = OpenProject::HtmlDiff.from_markdown(
@diff.content_from.data.text,
@diff.content_to.data.text
)
2012-08-30 15:38:45 +02:00
else
render_404
end
2007-07-14 11:25:03 +00:00
end
2011-05-30 20:52:25 +02:00
def annotate
@annotate = @page.annotate(params[:version])
render_404 unless @annotate
end
# Removes a wiki page and its history
# Children can be either set as root pages, removed or reassigned to another parent page
def destroy # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
2016-02-09 14:57:16 +01:00
unless editable?
flash.now[:error] = I18n.t(:error_unable_delete_wiki)
2016-02-09 14:57:16 +01:00
return render_403
end
2011-05-30 20:52:25 +02:00
@descendants_count = @page.descendants.size
if @descendants_count > 0
case params[:todo]
when "nullify"
# Nothing to do
when "destroy"
# Removes all its descendants
@page.descendants.each(&:destroy)
when "reassign"
# Reassign children to another parent page
2018-08-03 08:08:11 +02:00
reassign_to = @wiki.pages.find_by(id: params[:reassign_to_id].presence)
return unless reassign_to
2021-02-11 16:02:18 +01:00
@page.children.each do |child|
child.update_attribute(:parent, reassign_to)
end
else
@reassignable_to = @wiki.pages - @page.self_and_descendants
return
end
end
2026-04-22 15:09:38 +02:00
@page.destroy!
flash[:notice] = I18n.t(:notice_successful_delete)
2013-11-07 13:06:56 +01:00
if page = @wiki.find_page(@wiki.start_page) || @wiki.pages.first
redirect_to action: "index", project_id: @project, id: page, status: :see_other
else
redirect_to project_path(@project), status: :see_other
end
end
2007-03-10 15:09:49 +00:00
# Export wiki to a single html file
def export
2026-04-22 15:09:38 +02:00
if User.current.allowed_in_project?(:view_wiki_pages, @project)
2018-11-20 16:23:33 +01:00
@pages = @wiki.pages.order(Arel.sql("title"))
2014-11-03 21:10:20 +01:00
export = render_to_string action: "export_multiple", layout: false
send_data(export, type: "text/html", filename: "wiki.html")
else
2014-11-03 21:10:20 +01:00
redirect_to action: "show", project_id: @project, id: nil
end
end
2011-05-30 20:52:25 +02:00
def current_menu_item_sym(page)
2018-06-27 18:26:01 +02:00
page = page_for_menu_item(page)
2018-06-26 16:32:42 +02:00
menu_item = page.try(:menu_item)
return menu_item.menu_identifier if menu_item.present?
return unless page
default_item = default_menu_item(page)
return unless default_item
2024-01-04 17:01:17 +01:00
:"no-menu-item-#{default_item.menu_identifier}"
end
def show_create?
@editable && @page && User.current.allowed_in_project?(:edit_wiki_pages, @project)
end
2013-11-06 18:01:00 +01:00
private
2011-05-30 20:52:25 +02:00
2018-07-18 08:11:51 +02:00
def locked?
return false if editable?
2020-09-16 11:26:15 +02:00
flash[:error] = I18n.t(:error_unable_update_wiki)
2018-07-18 08:11:51 +02:00
render_403
true
end
2018-06-27 18:26:01 +02:00
def page_for_menu_item(page)
if page == :parent_page
page = send(:page)
2026-04-22 15:09:38 +02:00
page = page.parent if page&.parent
2018-06-27 18:26:01 +02:00
else
page = send(page)
end
page
end
2016-01-08 14:10:34 +01:00
def wiki_page_title
params[:title] || (action_name == "new_child" ? "" : params[:id].to_s.capitalize.tr("-", " "))
2016-01-08 14:10:34 +01:00
end
2007-03-10 15:09:49 +00:00
def find_wiki
2026-02-12 16:24:59 +01:00
@project = Project.visible.find(params[:project_id])
2007-03-10 15:09:49 +00:00
@wiki = @project.wiki
render_404 unless @wiki
2007-03-10 15:09:49 +00:00
end
2011-05-30 20:52:25 +02:00
# Finds or created the wiki page associated
# to the wiki
def find_wiki_page
@page = @wiki.find_or_new_page(wiki_page_title)
end
# Handles new pages for non-editable permissions
def handle_new_wiki_page
return unless @page.new_record?
if User.current.allowed_in_project?(:edit_wiki_pages, @project) && editable?
edit
render action: :new
elsif params[:id] == "wiki"
flash[:info] = I18n.t("wiki.page_not_editable_index")
redirect_to action: :index
else
render_404
end
end
# Finds the requested page and returns a 404 error if it doesn't exist
def find_existing_page
2018-06-26 16:32:42 +02:00
@page = @wiki.find_page(wiki_page_title.presence || params[:id])
render_404 if @page.nil?
end
2011-05-30 20:52:25 +02:00
2023-03-22 16:08:40 +01:00
def build_wiki_page
2023-10-23 16:47:28 +02:00
# Using the empty contract here as we use the method to instantiate the model, not to save it (new and new_child action).
# Errors are expected here as the user has not yet entered any data.
2023-03-22 16:08:40 +01:00
@page = WikiPages::SetAttributesService
2026-05-04 14:52:52 +02:00
.new(model: WikiPage.new, user: current_user, contract_class: EmptyContract)
.call(wiki: @wiki, title: wiki_page_title.presence, parent_id: flash[:_related_wiki_page_id])
.result
2013-11-06 18:01:00 +01:00
end
# Returns true if the current user is allowed to edit the page, otherwise false
def editable?(page = @page)
page.editable_by?(User.current)
end
2008-10-27 11:08:29 +00:00
def redirect_to_show
redirect_to action: :show, project_id: @project, id: @page
end
2007-03-10 15:09:49 +00:00
end