From dcfaac392679c3ad031fc9b5806878b76bb4bf09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Javier=20L=C3=B3pez?= Date: Thu, 11 Mar 2021 18:38:59 +0100 Subject: [PATCH] Add redirections file to wiki --- app/controllers/concerns/wiki_actions.rb | 2 ++ app/models/wiki.rb | 31 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/app/controllers/concerns/wiki_actions.rb b/app/controllers/concerns/wiki_actions.rb index 60ff0a12d0ccb6..df1392527ef989 100644 --- a/app/controllers/concerns/wiki_actions.rb +++ b/app/controllers/concerns/wiki_actions.rb @@ -81,6 +81,8 @@ def show render 'shared/wikis/show' elsif file_blob send_blob(wiki.repository, file_blob) + elsif redirected_page = wiki.find_redirection(params[:id]) + redirect_to wiki_page_path(wiki, redirected_page) elsif show_create_form? # Assign a title to the WikiPage unless `id` is a randomly generated slug from #new title = params[:id] unless params[:random_title].present? diff --git a/app/models/wiki.rb b/app/models/wiki.rb index 45747c0b03c484..0bea51d017a003 100644 --- a/app/models/wiki.rb +++ b/app/models/wiki.rb @@ -18,6 +18,8 @@ class Wiki HOMEPAGE = 'home' SIDEBAR = '_sidebar' + REDIRECTIONS = '_redirections' + SPECIAL_FILES = [SIDEBAR, REDIRECTIONS].freeze TITLE_ORDER = 'title' CREATED_AT_ORDER = 'created_at' @@ -159,6 +161,10 @@ def find_sidebar(version = nil) find_page(SIDEBAR, version) end + def find_redirection(path) + redirections[path] + end + def find_file(name, version = nil) wiki.file(name, version) end @@ -281,6 +287,31 @@ def commit_details(action, message = nil, title = nil) def default_message(action, title) "#{user.username} #{action} page: #{title}" end + + def redirections + strong_memoize(:redirections) do + redirection_file = find_redirections_file + + {}.tap do |route_table| + next route_table unless redirection_file + + redirection_file.content.each_line do |line| + route_mapping = line.tr(' ', '').match(/^(.+)->(.+)$/) + + next unless route_mapping + next if route_mapping[1] == route_mapping[2] + # Avoid create redirections for special files + next if (Wiki::SPECIAL_FILES & [route_mapping[1], route_mapping[2]]).present? + + route_table[route_mapping[1]] = route_mapping[2] + end + end + end + end + + def find_redirections_file(version = nil) + find_page(REDIRECTIONS, version) + end end Wiki.prepend_if_ee('EE::Wiki') -- GitLab