From 5be52fa8212b3e8e32c101b4c80f03aaafd68c08 Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Tue, 25 Jul 2017 21:14:46 +0200 Subject: [PATCH] phpBB3 importer: Use permalink normalization for category links --- .../phpbb3/importers/permalink_importer.rb | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/script/import_scripts/phpbb3/importers/permalink_importer.rb b/script/import_scripts/phpbb3/importers/permalink_importer.rb index 54d620e44d0..140a160d8fa 100644 --- a/script/import_scripts/phpbb3/importers/permalink_importer.rb +++ b/script/import_scripts/phpbb3/importers/permalink_importer.rb @@ -1,5 +1,6 @@ module ImportScripts::PhpBB3 class PermalinkImporter + CATEGORY_LINK_NORMALIZATION = '/(viewforum.php\?)(?:.*&)?(f=\d+).*/\1\2' POST_LINK_NORMALIZATION = '/(viewtopic.php\?)(?:.*&)?(p=\d+).*/\1\2' TOPIC_LINK_NORMALIZATION = '/(viewtopic.php\?)(?:.*&)?(t=\d+).*/\1\2' @@ -12,13 +13,9 @@ module ImportScripts::PhpBB3 normalizations = SiteSetting.permalink_normalizations normalizations = normalizations.blank? ? [] : normalizations.split('|') - if @settings.create_post_links && !normalizations.include?(POST_LINK_NORMALIZATION) - normalizations << POST_LINK_NORMALIZATION - end - - if @settings.create_topic_links && !normalizations.include?(TOPIC_LINK_NORMALIZATION) - normalizations << TOPIC_LINK_NORMALIZATION - end + add_normalization(normalizations, CATEGORY_LINK_NORMALIZATION) if @settings.create_category_links + add_normalization(normalizations, POST_LINK_NORMALIZATION) if @settings.create_post_links + add_normalization(normalizations, TOPIC_LINK_NORMALIZATION) if @settings.create_topic_links SiteSetting.permalink_normalizations = normalizations.join('|') end @@ -28,9 +25,7 @@ module ImportScripts::PhpBB3 url = "viewforum.php?f=#{import_id}" - if !Permalink.find_by(url: url) - Permalink.create(url: url, category_id: category.id) - end + Permalink.create(url: url, category_id: category.id) unless permalink_exists(url) end def create_for_topic(topic, import_id) @@ -38,9 +33,7 @@ module ImportScripts::PhpBB3 url = "viewtopic.php?t=#{import_id}" - if !Permalink.find_by(url: url) - Permalink.create(url: url, topic_id: topic.id) - end + Permalink.create(url: url, topic_id: topic.id) unless permalink_exists(url) end def create_for_post(post, import_id) @@ -48,9 +41,17 @@ module ImportScripts::PhpBB3 url = "viewtopic.php?p=#{import_id}" - if !Permalink.find_by(url: url) - Permalink.create(url: url, post_id: post.id) - end + Permalink.create(url: url, post_id: post.id) unless permalink_exists(url) + end + + protected + + def add_normalization(normalizations, normalization) + normalizations << normalization unless normalizations.include?(normalization) + end + + def permalink_exists(url) + Permalink.find_by(url: url) end end end